2016-04-11 02:21:07 -04:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
2017-07-02 20:52:41 -04:00
|
|
|
export IMG_NAME='BarnacleOS'
|
|
|
|
export USERNAME='user'
|
|
|
|
export PASSWORD='password'
|
|
|
|
|
2017-07-02 07:07:37 -04:00
|
|
|
export BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
2017-07-02 07:08:24 -04:00
|
|
|
export DEPLOY_DIR="$BASE_DIR/deploy"
|
2017-07-02 20:17:20 -04:00
|
|
|
export ROOTFS_DIR="$BASE_DIR/rootfs"
|
2017-07-02 20:24:50 -04:00
|
|
|
export MOUNT_DIR="$BASE_DIR/mnt"
|
2017-07-06 19:49:19 -04:00
|
|
|
export KEYS_DIR="$BASE_DIR/keys"
|
2017-07-06 16:57:44 -04:00
|
|
|
export FILES_DIR="$BASE_DIR/files"
|
2017-07-02 07:07:37 -04:00
|
|
|
|
2017-07-02 20:55:48 -04:00
|
|
|
export IMG_DATE="$(date +%Y-%m-%d)"
|
2017-07-02 09:18:10 -04:00
|
|
|
|
2017-07-02 20:55:48 -04:00
|
|
|
export IMG_FILE="$DEPLOY_DIR/$IMG_DATE-${IMG_NAME}.img"
|
|
|
|
export ZIP_FILE="$DEPLOY_DIR/$IMG_DATE-${IMG_NAME}.zip"
|
2017-07-02 14:01:01 -04:00
|
|
|
|
2017-07-05 20:52:20 -04:00
|
|
|
export QUILT_PATCHES="$BASE_DIR/patches"
|
2017-07-02 07:07:37 -04:00
|
|
|
export QUILT_NO_DIFF_INDEX=1
|
|
|
|
export QUILT_NO_DIFF_TIMESTAMPS=1
|
2017-07-02 07:10:54 -04:00
|
|
|
export QUILT_REFRESH_ARGS='-p ab'
|
2017-07-02 07:07:37 -04:00
|
|
|
|
2017-07-03 03:03:21 -04:00
|
|
|
# dependencies_check
|
|
|
|
# $@ Dependnecy files to check
|
|
|
|
#
|
|
|
|
# Each dependency is in the form of a tool to test for, optionally followed by
|
|
|
|
# a : and the name of a package if the package on a Debian-ish system is not
|
|
|
|
# named for the tool (i.e., qemu-user-static).
|
|
|
|
dependencies_check() {
|
|
|
|
local missing
|
|
|
|
|
|
|
|
if [[ -f "$1" ]]; then
|
|
|
|
for dep in $(cat "$1"); do
|
|
|
|
if ! hash ${dep%:*} 2>/dev/null; then
|
|
|
|
missing="${missing:+$missing }${dep#*:}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$missing" ]]; then
|
|
|
|
tput setaf 1 # Red color
|
|
|
|
echo 'Reqired dependencies not installed.'
|
|
|
|
echo 'This can be resolved on Debian/Raspbian systems by installing the following packages:'
|
|
|
|
for package_name in $missing; do
|
|
|
|
echo " * $package_name"
|
|
|
|
done
|
|
|
|
tput sgr0 # No color
|
|
|
|
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
}
|
2017-07-02 21:04:47 -04:00
|
|
|
|
2017-07-04 09:39:31 -04:00
|
|
|
on_chroot() {
|
2017-07-03 02:50:15 -04:00
|
|
|
capsh --drop=cap_setfcap "--chroot=$ROOTFS_DIR/" -- "$@"
|
|
|
|
}
|
|
|
|
|
2017-07-05 20:52:50 -04:00
|
|
|
apply_patch() {
|
2017-07-03 02:48:36 -04:00
|
|
|
pushd "$ROOTFS_DIR" > /dev/null
|
|
|
|
|
|
|
|
quilt upgrade
|
|
|
|
RC=0
|
2017-07-04 15:44:58 -04:00
|
|
|
quilt push "$1" || RC=$?
|
2017-07-03 02:48:36 -04:00
|
|
|
|
|
|
|
case "$RC" in
|
|
|
|
0|2)
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
false
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
popd > /dev/null
|
|
|
|
}
|
2017-07-02 21:04:47 -04:00
|
|
|
|
2017-07-06 16:59:06 -04:00
|
|
|
apply_dir() {
|
|
|
|
install -d "$ROOTFS_DIR/$1"
|
|
|
|
}
|
|
|
|
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file() {
|
|
|
|
local MODE="$1"
|
|
|
|
local FILE="$2"
|
|
|
|
|
|
|
|
local SRC="$FILES_DIR/$FILE"
|
|
|
|
local DST="$ROOTFS_DIR/$FILE"
|
|
|
|
|
|
|
|
if [ ! -f "$SRC" ]; then
|
|
|
|
tput setaf 1 # Red color
|
|
|
|
echo "Source file $FILE does not exist"
|
|
|
|
tput sgr0 # No color
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
install -m "$MODE" "$SRC" "$DST"
|
|
|
|
}
|
|
|
|
|
2017-07-03 02:53:17 -04:00
|
|
|
unmount() {
|
|
|
|
if [ -z "$1" ]; then
|
2017-07-03 07:17:27 -04:00
|
|
|
local DIR=$PWD
|
2017-07-03 02:53:17 -04:00
|
|
|
else
|
2017-07-03 07:17:27 -04:00
|
|
|
local DIR=$1
|
2017-07-03 02:53:17 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
while mount | grep -q "$DIR"; do
|
2017-07-03 07:17:27 -04:00
|
|
|
local LOCS=$(mount | grep "$DIR" | cut -f 3 -d ' ' | sort -r)
|
|
|
|
|
2017-07-03 02:53:17 -04:00
|
|
|
for loc in $LOCS; do
|
|
|
|
umount "$loc"
|
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
unmount_image() {
|
|
|
|
sync
|
|
|
|
sleep 1
|
2017-07-03 07:17:27 -04:00
|
|
|
local LOOP_DEVICES=$(losetup -j "$1" | cut -f1 -d ':')
|
|
|
|
|
|
|
|
for LOOP_DEV in $LOOP_DEVICES; do
|
|
|
|
if [ -n "$LOOP_DEV" ]; then
|
|
|
|
local MOUNTED_DIR=$(mount | grep "$(basename "$LOOP_DEV")" | head -n 1 | cut -f 3 -d ' ')
|
|
|
|
|
|
|
|
if [ -n "$MOUNTED_DIR" ] && [ "$MOUNTED_DIR" != "/" ]; then
|
|
|
|
unmount "$(dirname "$MOUNTED_DIR")"
|
2017-07-03 02:53:17 -04:00
|
|
|
fi
|
2017-07-03 07:17:27 -04:00
|
|
|
|
2017-07-03 02:53:17 -04:00
|
|
|
sleep 1
|
2017-07-03 07:17:27 -04:00
|
|
|
losetup -d "$LOOP_DEV"
|
2017-07-03 02:53:17 -04:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-07-03 07:51:22 -04:00
|
|
|
##
|
|
|
|
# Prepare environment.
|
|
|
|
#
|
2017-07-03 02:28:10 -04:00
|
|
|
if [ "$(id -u)" != '0' ]; then
|
|
|
|
echo 'Please run as root' 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2017-07-02 20:56:30 -04:00
|
|
|
|
2017-07-03 02:28:10 -04:00
|
|
|
dependencies_check "$BASE_DIR/depends"
|
2017-07-02 20:58:19 -04:00
|
|
|
|
2017-07-03 02:28:10 -04:00
|
|
|
mkdir -p "$DEPLOY_DIR"
|
|
|
|
mkdir -p "$MOUNT_DIR"
|
2017-07-02 07:07:37 -04:00
|
|
|
|
2017-07-03 02:40:57 -04:00
|
|
|
cd "$BASE_DIR"
|
2017-07-02 07:30:26 -04:00
|
|
|
|
2017-07-03 07:51:22 -04:00
|
|
|
##
|
|
|
|
# Bootstrap a basic Debian system.
|
|
|
|
#
|
2017-07-03 02:42:46 -04:00
|
|
|
if [ ! -d "$ROOTFS_DIR" ]; then
|
|
|
|
ARCH="$(dpkg --print-architecture)"
|
|
|
|
|
|
|
|
if [ "$ARCH" != 'armhf' ]; then
|
|
|
|
BOOTSTRAP_CMD='qemu-debootstrap'
|
|
|
|
else
|
|
|
|
BOOTSTRAP_CMD='debootstrap'
|
|
|
|
fi
|
|
|
|
|
|
|
|
capsh --drop=cap_setfcap -- -c "$BOOTSTRAP_CMD \
|
|
|
|
--components=main,contrib,non-free \
|
|
|
|
--arch armhf \
|
2017-07-06 19:49:19 -04:00
|
|
|
--keyring $KEYS_DIR/raspberrypi.gpg \
|
2017-07-03 02:42:46 -04:00
|
|
|
jessie \
|
|
|
|
$ROOTFS_DIR \
|
2017-07-06 12:01:25 -04:00
|
|
|
http://mirrordirector.raspbian.org/raspbian/" || rmdir "$ROOTFS_DIR/debootstrap/"
|
2017-07-03 02:42:46 -04:00
|
|
|
fi
|
|
|
|
|
2017-07-04 15:44:58 -04:00
|
|
|
##
|
|
|
|
# Prepare for Quilt patching.
|
|
|
|
#
|
2017-07-06 12:01:25 -04:00
|
|
|
rm -rf "$ROOTFS_DIR/.pc/"
|
|
|
|
mkdir "$ROOTFS_DIR/.pc/"
|
2017-07-04 15:44:58 -04:00
|
|
|
|
2017-07-05 03:19:20 -04:00
|
|
|
##
|
|
|
|
# Prevent services to start after package installation in chroot environment.
|
|
|
|
#
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file 744 '/usr/sbin/policy-rc.d'
|
2017-07-05 03:19:20 -04:00
|
|
|
|
2017-07-03 07:51:22 -04:00
|
|
|
##
|
|
|
|
# Mount virtual file systems.
|
|
|
|
#
|
2017-07-03 05:53:52 -04:00
|
|
|
mount --bind /dev "$ROOTFS_DIR/dev"
|
|
|
|
mount --bind /dev/pts "$ROOTFS_DIR/dev/pts"
|
|
|
|
mount -t proc /proc "$ROOTFS_DIR/proc"
|
|
|
|
mount --bind /sys "$ROOTFS_DIR/sys"
|
2017-07-03 05:48:00 -04:00
|
|
|
|
2017-07-05 03:09:58 -04:00
|
|
|
##
|
2017-07-05 03:33:27 -04:00
|
|
|
# Add /etc/fstab and /etc/mtab
|
2017-07-05 03:09:58 -04:00
|
|
|
#
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file 644 '/etc/fstab'
|
|
|
|
ln -nsf /proc/mounts "$ROOTFS_DIR/etc/mtab"
|
2017-07-05 03:09:58 -04:00
|
|
|
|
2017-07-03 07:51:22 -04:00
|
|
|
##
|
|
|
|
# Prepare package manager.
|
|
|
|
#
|
2017-07-06 17:33:48 -04:00
|
|
|
apply_file 644 '/etc/apt/sources.list'
|
2017-07-03 02:42:46 -04:00
|
|
|
|
2017-07-06 19:49:19 -04:00
|
|
|
on_chroot apt-key add - < "$KEYS_DIR/raspberrypi.gpg.asc"
|
2017-07-03 02:42:46 -04:00
|
|
|
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file 644 '/etc/apt/apt.conf.d/50raspi'
|
2017-07-05 04:02:03 -04:00
|
|
|
|
2017-07-04 09:39:31 -04:00
|
|
|
on_chroot << EOF
|
2017-07-03 02:42:46 -04:00
|
|
|
apt-get update
|
|
|
|
apt-get dist-upgrade -y
|
|
|
|
EOF
|
|
|
|
|
2017-07-05 03:26:31 -04:00
|
|
|
##
|
2017-07-05 20:29:17 -04:00
|
|
|
# Install kernel and bootloader.
|
2017-07-05 03:26:31 -04:00
|
|
|
#
|
2017-07-05 20:29:17 -04:00
|
|
|
on_chroot << EOF
|
|
|
|
apt-get install -y raspberrypi-kernel raspberrypi-bootloader
|
|
|
|
EOF
|
2017-07-05 03:26:31 -04:00
|
|
|
|
2017-07-05 20:29:17 -04:00
|
|
|
##
|
|
|
|
# Prepare Raspberry Pi boot partition.
|
|
|
|
#
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file 644 '/boot/cmdline.txt'
|
|
|
|
apply_file 644 '/boot/config.txt'
|
2017-07-05 03:26:31 -04:00
|
|
|
|
2017-07-05 03:22:54 -04:00
|
|
|
##
|
|
|
|
# This script is executed at the end of each multiuser runlevel.
|
|
|
|
#
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file 755 '/etc/rc.local'
|
2017-07-05 03:22:54 -04:00
|
|
|
|
2017-07-05 03:31:01 -04:00
|
|
|
##
|
|
|
|
# Install SSH server
|
|
|
|
#
|
|
|
|
on_chroot << EOF
|
|
|
|
apt-get install -y ssh
|
|
|
|
EOF
|
|
|
|
|
2017-07-05 20:52:50 -04:00
|
|
|
apply_patch '01-no-root-login.diff'
|
2017-07-05 03:31:01 -04:00
|
|
|
|
2017-07-05 23:37:44 -04:00
|
|
|
rm -fv '/etc/ssh/ssh_host_key'
|
|
|
|
rm -fv '/etc/ssh/ssh_host_key.pub'
|
|
|
|
|
|
|
|
rm -fv '/etc/ssh/ssh_host_dsa_key'
|
|
|
|
rm -fv '/etc/ssh/ssh_host_dsa_key.pub'
|
|
|
|
|
|
|
|
rm -fv '/etc/ssh/ssh_host_ecdsa_key'
|
|
|
|
rm -fv '/etc/ssh/ssh_host_ecdsa_key.pub'
|
|
|
|
|
|
|
|
rm -fv '/etc/ssh/ssh_host_ed25519_key'
|
|
|
|
rm -fv '/etc/ssh/ssh_host_ed25519_key.pub'
|
|
|
|
|
|
|
|
rm -fv '/etc/ssh/ssh_host_rsa_key'
|
|
|
|
rm -fv '/etc/ssh/ssh_host_rsa_key.pub'
|
|
|
|
|
2017-07-03 07:51:22 -04:00
|
|
|
##
|
2017-07-05 20:29:17 -04:00
|
|
|
# ?????
|
2017-07-03 07:51:22 -04:00
|
|
|
#
|
2017-07-04 09:39:31 -04:00
|
|
|
on_chroot << EOF
|
2017-07-05 20:29:17 -04:00
|
|
|
apt-get install -y raspberrypi-sys-mods
|
2017-07-03 02:42:46 -04:00
|
|
|
EOF
|
|
|
|
|
2017-07-05 03:44:31 -04:00
|
|
|
##
|
|
|
|
# Configure network.
|
|
|
|
#
|
2017-07-06 18:56:08 -04:00
|
|
|
apply_file 644 '/etc/hostname'
|
2017-07-03 02:42:46 -04:00
|
|
|
|
2017-07-06 18:56:08 -04:00
|
|
|
apply_patch '02-hosts.diff'
|
2017-07-03 02:42:46 -04:00
|
|
|
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file 644 '/etc/network/interfaces'
|
2017-07-06 17:01:16 -04:00
|
|
|
apply_file 644 '/etc/network/interfaces.d/eth0'
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file 644 '/etc/network/interfaces.d/eth1'
|
2017-07-06 02:51:52 -04:00
|
|
|
|
2017-07-06 18:05:04 -04:00
|
|
|
apply_file 644 '/etc/resolv.conf'
|
|
|
|
|
2017-07-05 03:44:31 -04:00
|
|
|
##
|
|
|
|
# Add user.
|
|
|
|
#
|
2017-07-05 04:12:25 -04:00
|
|
|
on_chroot << EOF
|
|
|
|
apt-get install -y sudo
|
|
|
|
EOF
|
|
|
|
|
2017-07-06 19:00:01 -04:00
|
|
|
apply_patch '03-bashrc.diff'
|
|
|
|
apply_patch '04-useradd.diff'
|
2017-07-05 03:38:56 -04:00
|
|
|
|
2017-07-04 09:39:31 -04:00
|
|
|
on_chroot << EOF
|
2017-07-03 02:42:46 -04:00
|
|
|
if ! id -u $USERNAME >/dev/null 2>&1; then
|
|
|
|
adduser --disabled-password --gecos "" $USERNAME
|
|
|
|
fi
|
|
|
|
echo "$USERNAME:$PASSWORD" | chpasswd
|
|
|
|
passwd -d root
|
2017-07-05 03:48:04 -04:00
|
|
|
adduser $USERNAME sudo
|
2017-07-03 02:42:46 -04:00
|
|
|
EOF
|
|
|
|
|
2017-07-05 03:44:31 -04:00
|
|
|
##
|
|
|
|
# Configure time zone.
|
|
|
|
#
|
2017-07-04 09:39:31 -04:00
|
|
|
on_chroot << EOF
|
2017-07-03 02:42:46 -04:00
|
|
|
debconf-set-selections <<SELEOF
|
|
|
|
tzdata tzdata/Areas select Etc
|
|
|
|
tzdata tzdata/Zones/Etc select UTC
|
|
|
|
SELEOF
|
2017-07-05 03:50:44 -04:00
|
|
|
|
|
|
|
apt-get install -y tzdata
|
2017-07-03 02:42:46 -04:00
|
|
|
EOF
|
|
|
|
|
2017-07-05 03:44:31 -04:00
|
|
|
##
|
2017-07-05 20:48:28 -04:00
|
|
|
# The certificate authorities shipped with Mozilla's browser
|
|
|
|
# to allow SSL-based applications to check for the authenticity of SSL connections.
|
2017-07-05 03:44:31 -04:00
|
|
|
#
|
2017-07-04 09:39:31 -04:00
|
|
|
on_chroot << EOF
|
2017-07-05 20:47:09 -04:00
|
|
|
apt-get install -y ca-certificates
|
2017-07-03 02:42:46 -04:00
|
|
|
EOF
|
|
|
|
|
2017-07-05 03:45:40 -04:00
|
|
|
##
|
|
|
|
# Enable swap.
|
|
|
|
#
|
|
|
|
on_chroot << EOF
|
|
|
|
apt-get install -y dphys-swapfile
|
|
|
|
EOF
|
|
|
|
|
2017-07-06 19:00:01 -04:00
|
|
|
apply_patch '05-swap.diff'
|
2017-07-05 03:45:40 -04:00
|
|
|
|
2017-07-05 03:47:28 -04:00
|
|
|
##
|
|
|
|
# Configure environment.
|
|
|
|
#
|
2017-07-06 19:00:01 -04:00
|
|
|
apply_patch '06-path.diff'
|
2017-07-03 02:42:46 -04:00
|
|
|
|
2017-07-05 23:52:17 -04:00
|
|
|
##
|
|
|
|
# Make user-friendly environment.
|
|
|
|
#
|
|
|
|
on_chroot << EOF
|
|
|
|
apt-get install -y \
|
|
|
|
bash-completion \
|
|
|
|
less \
|
|
|
|
vim
|
|
|
|
|
|
|
|
update-alternatives --set editor /usr/bin/vim.basic
|
|
|
|
EOF
|
|
|
|
|
2017-07-04 09:37:52 -04:00
|
|
|
##
|
|
|
|
# Save fake hardware clock time for more realistic time after startup.
|
|
|
|
#
|
2017-07-05 03:53:08 -04:00
|
|
|
on_chroot << EOF
|
2017-07-05 04:14:11 -04:00
|
|
|
apt-get install -y fake-hwclock ntp
|
2017-07-05 03:53:08 -04:00
|
|
|
systemctl disable hwclock.sh
|
|
|
|
fake-hwclock save
|
|
|
|
EOF
|
2017-07-04 09:37:52 -04:00
|
|
|
|
2017-07-06 02:51:52 -04:00
|
|
|
##
|
|
|
|
# ISC DHCP server
|
|
|
|
#
|
|
|
|
on_chroot << EOF
|
|
|
|
apt-get install -y isc-dhcp-server
|
|
|
|
EOF
|
|
|
|
|
2017-07-06 19:00:01 -04:00
|
|
|
apply_patch '07-dhcp-server.diff'
|
2017-07-06 02:51:52 -04:00
|
|
|
|
2017-07-06 16:59:06 -04:00
|
|
|
apply_dir '/etc/dhcp/dhcpd.conf.d/'
|
2017-07-06 16:57:44 -04:00
|
|
|
apply_file 644 '/etc/dhcp/dhcpd.conf.d/192.168.82.0.conf'
|
2017-07-06 02:51:52 -04:00
|
|
|
|
2017-07-06 03:25:39 -04:00
|
|
|
##
|
|
|
|
# IP forwarding.
|
|
|
|
#
|
2017-07-06 19:00:01 -04:00
|
|
|
apply_patch '08-ipv4-forwarding.diff'
|
2017-07-06 03:25:39 -04:00
|
|
|
|
2017-07-03 07:38:42 -04:00
|
|
|
##
|
|
|
|
# Unmount virtual file systems.
|
|
|
|
#
|
2017-07-04 12:34:06 -04:00
|
|
|
umount "$ROOTFS_DIR/sys"
|
|
|
|
umount "$ROOTFS_DIR/proc"
|
|
|
|
umount "$ROOTFS_DIR/dev/pts"
|
|
|
|
umount "$ROOTFS_DIR/dev"
|
2017-07-03 07:38:42 -04:00
|
|
|
|
2017-07-04 15:44:58 -04:00
|
|
|
##
|
|
|
|
# Cleanup after Quilt patching.
|
|
|
|
#
|
2017-07-06 12:01:25 -04:00
|
|
|
rm -rf "$ROOTFS_DIR/.pc/"
|
2017-07-04 15:44:58 -04:00
|
|
|
|
2017-07-05 03:19:20 -04:00
|
|
|
##
|
|
|
|
# Allow services to start.
|
|
|
|
#
|
|
|
|
rm -f "$ROOTFS_DIR/usr/sbin/policy-rc.d"
|
|
|
|
|
2017-07-03 07:38:42 -04:00
|
|
|
##
|
|
|
|
# Prepare image file systems.
|
|
|
|
#
|
2017-07-03 02:42:46 -04:00
|
|
|
rm -f "$IMG_FILE"
|
|
|
|
|
|
|
|
BOOT_SIZE=$(du --apparent-size -s "$ROOTFS_DIR/boot" --block-size=1 | cut -f 1)
|
|
|
|
TOTAL_SIZE=$(du --apparent-size -s "$ROOTFS_DIR" --exclude var/cache/apt/archives --block-size=1 | cut -f 1)
|
|
|
|
|
|
|
|
IMG_SIZE=$((BOOT_SIZE + TOTAL_SIZE + (800 * 1024 * 1024)))
|
|
|
|
|
|
|
|
truncate -s $IMG_SIZE "$IMG_FILE"
|
|
|
|
|
|
|
|
fdisk -H 255 -S 63 "$IMG_FILE" <<EOF
|
|
|
|
o
|
|
|
|
n
|
|
|
|
|
|
|
|
|
|
|
|
8192
|
|
|
|
+$((BOOT_SIZE * 2 / 512))
|
|
|
|
p
|
|
|
|
t
|
|
|
|
c
|
|
|
|
n
|
|
|
|
|
|
|
|
|
|
|
|
8192
|
|
|
|
|
|
|
|
|
|
|
|
p
|
|
|
|
w
|
|
|
|
EOF
|
|
|
|
|
|
|
|
PARTED_OUT=$(parted -s "$IMG_FILE" unit b print)
|
|
|
|
|
|
|
|
BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^ 1' | xargs echo -n \
|
|
|
|
| cut -d" " -f 2 | tr -d B)
|
|
|
|
BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^ 1' | xargs echo -n \
|
|
|
|
| cut -d" " -f 4 | tr -d B)
|
|
|
|
ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^ 2' | xargs echo -n \
|
|
|
|
| cut -d" " -f 2 | tr -d B)
|
|
|
|
ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^ 2' | xargs echo -n \
|
|
|
|
| cut -d" " -f 4 | tr -d B)
|
|
|
|
|
|
|
|
BOOT_DEV=$(losetup --show -f -o $BOOT_OFFSET --sizelimit $BOOT_LENGTH "$IMG_FILE")
|
|
|
|
ROOT_DEV=$(losetup --show -f -o $ROOT_OFFSET --sizelimit $ROOT_LENGTH "$IMG_FILE")
|
|
|
|
|
|
|
|
mkdosfs -n boot -F 32 -v $BOOT_DEV > /dev/null
|
2017-07-04 18:15:25 -04:00
|
|
|
mkfs.ext4 -O ^huge_file $ROOT_DEV > /dev/null
|
2017-07-03 02:42:46 -04:00
|
|
|
|
2017-07-03 07:38:42 -04:00
|
|
|
##
|
|
|
|
# Mount image file systems.
|
|
|
|
#
|
2017-07-03 05:54:24 -04:00
|
|
|
mkdir -p "$MOUNT_DIR"
|
2017-07-03 02:42:46 -04:00
|
|
|
mount -v $ROOT_DEV "$MOUNT_DIR" -t ext4
|
|
|
|
|
2017-07-06 12:01:25 -04:00
|
|
|
mkdir -p "$MOUNT_DIR/boot/"
|
|
|
|
mount -v $BOOT_DEV "$MOUNT_DIR/boot/" -t vfat
|
2017-07-03 02:42:46 -04:00
|
|
|
|
2017-07-03 07:38:42 -04:00
|
|
|
##
|
|
|
|
# Copy root file system to image file systems.
|
|
|
|
#
|
2017-07-03 02:42:46 -04:00
|
|
|
rsync -aHAXx --exclude var/cache/apt/archives "$ROOTFS_DIR/" "$MOUNT_DIR/"
|
|
|
|
|
2017-07-03 07:31:42 -04:00
|
|
|
##
|
|
|
|
# Store file system UUIDs to configuration files.
|
|
|
|
#
|
2017-07-03 02:42:46 -04:00
|
|
|
IMGID="$(fdisk -l "$IMG_FILE" | sed -n 's/Disk identifier: 0x\([^ ]*\)/\1/p')"
|
|
|
|
|
|
|
|
BOOT_PARTUUID="$IMGID-01"
|
|
|
|
ROOT_PARTUUID="$IMGID-02"
|
|
|
|
|
|
|
|
sed -i "s/BOOTDEV/PARTUUID=$BOOT_PARTUUID/" "$MOUNT_DIR/etc/fstab"
|
|
|
|
sed -i "s/ROOTDEV/PARTUUID=$ROOT_PARTUUID/" "$MOUNT_DIR/etc/fstab"
|
|
|
|
sed -i "s/ROOTDEV/PARTUUID=$ROOT_PARTUUID/" "$MOUNT_DIR/boot/cmdline.txt"
|
|
|
|
|
2017-07-03 07:31:42 -04:00
|
|
|
##
|
2017-07-03 07:24:44 -04:00
|
|
|
# Unmount all file systems and minimize image file for distribution.
|
2017-07-03 07:31:42 -04:00
|
|
|
#
|
2017-07-04 18:57:29 -04:00
|
|
|
ROOT_DEV=$(mount | grep "$MOUNT_DIR " | cut -f1 -d ' ')
|
2017-07-06 12:01:25 -04:00
|
|
|
umount "$MOUNT_DIR/boot/"
|
2017-07-04 18:50:09 -04:00
|
|
|
umount "$MOUNT_DIR"
|
2017-07-03 02:42:46 -04:00
|
|
|
zerofree -v "$ROOT_DEV"
|
|
|
|
unmount_image "$IMG_FILE"
|
|
|
|
|
2017-07-03 07:31:42 -04:00
|
|
|
##
|
2017-07-03 07:24:44 -04:00
|
|
|
# Create zip archive with image file for distribution.
|
2017-07-03 07:31:42 -04:00
|
|
|
#
|
2017-07-03 02:42:46 -04:00
|
|
|
rm -f "$ZIP_FILE"
|
|
|
|
pushd $(dirname "$IMG_FILE") > /dev/null
|
|
|
|
zip "$ZIP_FILE" $(basename "$IMG_FILE")
|
|
|
|
popd > /dev/null
|