mkimage.sh.in: lib.sh and a generous serving of quotes

This commit is contained in:
Michael Aldridge 2017-08-12 20:55:31 -07:00
parent 119a0ad1b5
commit 7a7766cfcb

View File

@ -25,42 +25,27 @@
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#- #-
readonly PROGNAME=$(basename $0) readonly PROGNAME=$(basename "$0")
readonly ARCH=$(uname -m) readonly ARCH=$(uname -m)
trap 'printf "\nInterrupted! exiting...\n"; cleanup; exit 0' INT TERM HUP trap 'printf "\nInterrupted! exiting...\n"; cleanup; exit 0' INT TERM HUP
mount_pseudofs() { # This source pulls in all the functions from lib.sh. This set of
for f in sys dev proc; do # functions makes it much easier to work with chroots and abstracts
mkdir -p $ROOTFS/$f # away all the problems with running binaries with QEMU.
mount --bind /$f $ROOTFS/$f # shellcheck source=./lib.sh
done . ./lib.sh
}
umount_pseudofs() {
umount -f $ROOTFS/sys >/dev/null 2>&1
umount -f $ROOTFS/dev >/dev/null 2>&1
umount -f $ROOTFS/proc >/dev/null 2>&1
}
cleanup() { cleanup() {
unmount_pseudofs unmount_pseudofs
umount -f ${ROOTFS}/boot 2>/dev/null umount -f "${ROOTFS}/boot" 2>/dev/null
umount -f ${ROOTFS} 2>/dev/null umount -f "${ROOTFS}" 2>/dev/null
if [ -e "$LOOPDEV" ]; then if [ -e "$LOOPDEV" ]; then
partx -d $LOOPDEV 2>/dev/null partx -d "$LOOPDEV" 2>/dev/null
losetup -d $LOOPDEV 2>/dev/null losetup -d "$LOOPDEV" 2>/dev/null
fi fi
[ -d "$ROOTFS" ] && rmdir $ROOTFS [ -d "$ROOTFS" ] && rmdir "$ROOTFS"
}
info_msg() {
printf "\033[1m[${PLATFORM}] $@\n\033[m"
}
die() {
echo "FATAL: $@"
exit 1
} }
usage() { usage() {
@ -86,9 +71,10 @@ _EOF
exit 0 exit 0
} }
# # ########################################
# main() # SCRIPT EXECUTION STARTS HERE
# # ########################################
while getopts "b:B:o:r:s:hV" opt; do while getopts "b:B:o:r:s:hV" opt; do
case $opt in case $opt in
b) BOOT_FSTYPE="$OPTARG";; b) BOOT_FSTYPE="$OPTARG";;
@ -100,186 +86,197 @@ while getopts "b:B:o:r:s:hV" opt; do
h) usage;; h) usage;;
esac esac
done done
shift $(($OPTIND - 1)) shift $((OPTIND - 1))
ROOTFS_TARBALL="$1" ROOTFS_TARBALL="$1"
if [ -z "$ROOTFS_TARBALL" ]; then if [ -z "$ROOTFS_TARBALL" ]; then
usage usage
elif [ ! -r "$ROOTFS_TARBALL" ]; then elif [ ! -r "$ROOTFS_TARBALL" ]; then
# In rare cases the tarball can wind up owned by the wrong user.
# This leads to confusing failures if execution is allowed to
# proceed.
die "Cannot read rootfs tarball: $ROOTFS_TARBALL" die "Cannot read rootfs tarball: $ROOTFS_TARBALL"
fi fi
PLATFORM="${ROOTFS_TARBALL#void-}" # By default we build all platform images with a 64MiB boot partition
PLATFORM="${PLATFORM%-rootfs*}" # formated FAT16, and an approxomately 1.9GiB root partition formated
# ext4. More exotic combinations are of course possible, but this
# combination works on all known platforms.
: "${IMGSIZE:=2G}"
: "${BOOT_FSTYPE:=vfat}"
: "${BOOT_FSSIZE:=64MiB}"
: "${ROOT_FSTYPE:=ext4}"
# Verify that the required tooling is available
readonly REQTOOLS="sfdisk partx losetup mount truncate mkfs.${BOOT_FSTYPE} mkfs.${ROOT_FSTYPE}"
check_tools
# Setup the platform variable. Here we want just the name and
# optionally -musl if this is the musl variant.
PLATFORM="${ROOTFS_TARBALL#void-}"
PLATFORM="${PLATFORM%-ROOTFS*}"
# This is an aweful hack since the script isn't using privesc
# mechanisms selectively. This is a TODO item.
if [ "$(id -u)" -ne 0 ]; then if [ "$(id -u)" -ne 0 ]; then
die "need root perms to continue, exiting." die "need root perms to continue, exiting."
fi fi
: ${IMGSIZE:=2G} # Set the default filename if none was provided above. The default
: ${BOOT_FSTYPE:=vfat} # will include the platform the image is being built for and the date
: ${BOOT_FSSIZE:=64MiB} # on which it was built.
: ${ROOT_FSTYPE:=ext4}
if [ -z "$FILENAME" ]; then if [ -z "$FILENAME" ]; then
FILENAME="void-${PLATFORM}-$(date +%Y%m%d).img" FILENAME="void-${PLATFORM}-$(date +%Y%m%d).img"
fi fi
# double check PLATFORM is supported... # Be absolutely certain the platform is supported before continuing
case "$PLATFORM" in case "$PLATFORM" in
bananapi|beaglebone|cubieboard2|cubietruck|odroid-c2|odroid-u2|rpi|rpi2|rpi3|usbarmory|GCP|*-musl);; bananapi|beaglebone|cubieboard2|cubietruck|odroid-c2|odroid-u2|rpi|rpi2|rpi3|usbarmory|GCP|*-musl);;
*) die "The $PLATFORM is not supported, exiting..." *) die "The $PLATFORM is not supported, exiting..."
esac esac
for f in sfdisk partx losetup mount mkfs.${BOOT_FSTYPE} mkfs.${ROOT_FSTYPE}; do # Create the base image. This was previously accomplished with dd,
if ! which ${f} >/dev/null; then # but truncate is markedly faster.
die "Cannot find ${f}, exiting."
fi
done
# dd conv=sparse support first appeared in coreutils-8.16, disable it in
# older versions.
DD_VERSION=$(dd --version|head -n1|awk '{print $3}')
case "$DD_VERSION" in
[8-9].1[6-9]*|[8-9].[2-9]*) DD_SPARSE="conv=sparse";;
esac
info_msg "Creating disk image ($IMGSIZE) ..." info_msg "Creating disk image ($IMGSIZE) ..."
truncate -s "${IMGSIZE}" $FILENAME >/dev/null 2>&1 truncate -s "${IMGSIZE}" "$FILENAME" >/dev/null 2>&1
# Grab a tmpdir for the rootfs. If this fails we need to halt now
# because otherwise things will go very badly for the host system.
ROOTFS=$(mktemp -d) || die "Could not create tmpdir for ROOTFS"
ROOTFS=$(mktemp -d)
info_msg "Creating disk image partitions/filesystems ..." info_msg "Creating disk image partitions/filesystems ..."
if [ "$BOOT_FSTYPE" = "vfat" ]; then if [ "$BOOT_FSTYPE" = "vfat" ]; then
_btype="fat32"
_args="-I -F16" _args="-I -F16"
fi fi
case "$PLATFORM" in case "$PLATFORM" in
cubieboard2|cubietruck|ci20*|odroid-c2*) cubieboard2|cubietruck|ci20*|odroid-c2*)
sfdisk ${FILENAME} <<_EOF sfdisk "${FILENAME}" <<_EOF
label: dos label: dos
2048,,L 2048,,L
_EOF _EOF
LOOPDEV=$(losetup --show --find --partscan $FILENAME) LOOPDEV=$(losetup --show --find --partscan "$FILENAME")
mkfs.${ROOT_FSTYPE} -O '^64bit,^extra_isize,^has_journal' ${LOOPDEV}p1 >/dev/null 2>&1 mkfs.${ROOT_FSTYPE} -O '^64bit,^extra_isize,^has_journal' "${LOOPDEV}p1" >/dev/null 2>&1
mount ${LOOPDEV}p1 $ROOTFS mount "${LOOPDEV}p1" "$ROOTFS"
ROOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p1) ROOT_UUID=$(blkid -o value -s UUID "${LOOPDEV}p1")
;; ;;
*) *)
sfdisk ${FILENAME} <<_EOF sfdisk "${FILENAME}" <<_EOF
label: dos label: dos
2048,${BOOT_FSSIZE},b,* 2048,${BOOT_FSSIZE},b,*
,+,L ,+,L
_EOF _EOF
LOOPDEV=$(losetup --show --find --partscan $FILENAME) LOOPDEV=$(losetup --show --find --partscan "$FILENAME")
mkfs.${BOOT_FSTYPE} $_args ${LOOPDEV}p1 >/dev/null # Normally we need to quote to prevent argument splitting, but
case "$ROOT_FSTYPE" in # we explicitly want argument splitting here.
ext[34]) disable_journal="-O ^has_journal";; # shellcheck disable=SC2086
esac mkfs.${BOOT_FSTYPE} $_args "${LOOPDEV}p1" >/dev/null
mkfs.${ROOT_FSTYPE} $disable_journal ${LOOPDEV}p2 >/dev/null 2>&1 case "$ROOT_FSTYPE" in
mount ${LOOPDEV}p2 $ROOTFS ext[34]) disable_journal="-O ^has_journal";;
mkdir -p ${ROOTFS}/boot esac
mount ${LOOPDEV}p1 ${ROOTFS}/boot mkfs.${ROOT_FSTYPE} "$disable_journal" "${LOOPDEV}p2" >/dev/null 2>&1
BOOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p1) mount "${LOOPDEV}p2" "$ROOTFS"
ROOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p2) mkdir -p "${ROOTFS}/boot"
;; mount "${LOOPDEV}p1" "${ROOTFS}/boot"
BOOT_UUID=$(blkid -o value -s UUID "${LOOPDEV}p1")
ROOT_UUID=$(blkid -o value -s UUID "${LOOPDEV}p2")
;;
esac esac
info_msg "Unpacking rootfs tarball ..." info_msg "Unpacking rootfs tarball ..."
if [ "$PLATFORM" = "beaglebone" ]; then if [ "$PLATFORM" = "beaglebone" ]; then
fstab_args=",noauto" fstab_args=",noauto"
tar xfp $ROOTFS_TARBALL -C $ROOTFS ./boot/MLO tar xfp "$ROOTFS_TARBALL" -C "$ROOTFS" ./boot/MLO
tar xfp $ROOTFS_TARBALL -C $ROOTFS ./boot/u-boot.img tar xfp "$ROOTFS_TARBALL" -C "$ROOTFS" ./boot/u-boot.img
touch $ROOTFS/boot/uEnv.txt touch "$ROOTFS/boot/uEnv.txt"
umount $ROOTFS/boot umount "$ROOTFS/boot"
fi fi
tar xfp $ROOTFS_TARBALL --xattrs --xattrs-include='*' -C $ROOTFS tar xfp "$ROOTFS_TARBALL" --xattrs --xattrs-include='*' -C "$ROOTFS"
fspassno="1" fspassno="1"
if [ "$ROOT_FSTYPE" = "f2fs" ]; then if [ "$ROOT_FSTYPE" = "f2fs" ]; then
fspassno="0" fspassno="0"
fi fi
echo "UUID=$ROOT_UUID / $ROOT_FSTYPE defaults 0 ${fspassno}" >> ${ROOTFS}/etc/fstab echo "UUID=$ROOT_UUID / $ROOT_FSTYPE defaults 0 ${fspassno}" >> "${ROOTFS}/etc/fstab"
if [ -n "$BOOT_UUID" ]; then if [ -n "$BOOT_UUID" ]; then
echo "UUID=$BOOT_UUID /boot $BOOT_FSTYPE defaults${fstab_args} 0 2" >> ${ROOTFS}/etc/fstab echo "UUID=$BOOT_UUID /boot $BOOT_FSTYPE defaults${fstab_args} 0 2" >> "${ROOTFS}/etc/fstab"
fi fi
info_msg "Configuring image for platform $PLATFORM"
case "$PLATFORM" in case "$PLATFORM" in
bananapi*|cubieboard2*|cubietruck*) bananapi*|cubieboard2*|cubietruck*)
dd if=${ROOTFS}/boot/u-boot-sunxi-with-spl.bin of=${LOOPDEV} bs=1024 seek=8 >/dev/null 2>&1 dd if="${ROOTFS}/boot/u-boot-sunxi-with-spl.bin" of="${LOOPDEV}" bs=1024 seek=8 >/dev/null 2>&1
;; ;;
odroid-c2*) odroid-c2*)
dd if=${ROOTFS}/boot/bl1.bin.hardkernel of=${LOOPDEV} bs=1 count=442 >/dev/null 2>&1 dd if="${ROOTFS}/boot/bl1.bin.hardkernel" of="${LOOPDEV}" bs=1 count=442 >/dev/null 2>&1
dd if=${ROOTFS}/boot/bl1.bin.hardkernel of=${LOOPDEV} bs=512 skip=1 seek=1 >/dev/null 2>&1 dd if="${ROOTFS}/boot/bl1.bin.hardkernel" of="${LOOPDEV}" bs=512 skip=1 seek=1 >/dev/null 2>&1
dd if=${ROOTFS}/boot/u-boot.bin of=${LOOPDEV} bs=512 seek=97 >/dev/null 2>&1 dd if="${ROOTFS}/boot/u-boot.bin" of="${LOOPDEV}" bs=512 seek=97 >/dev/null 2>&1
;; ;;
odroid-u2*) odroid-u2*)
dd if=${ROOTFS}/boot/E4412_S.bl1.HardKernel.bin of=${LOOPDEV} seek=1 >/dev/null 2>&1 dd if="${ROOTFS}/boot/E4412_S.bl1.HardKernel.bin" of="${LOOPDEV}" seek=1 >/dev/null 2>&1
dd if=${ROOTFS}/boot/bl2.signed.bin of=${LOOPDEV} seek=31 >/dev/null 2>&1 dd if="${ROOTFS}/boot/bl2.signed.bin" of="${LOOPDEV}" seek=31 >/dev/null 2>&1
dd if=${ROOTFS}/boot/u-boot.bin of=${LOOPDEV} seek=63 >/dev/null 2>&1 dd if="${ROOTFS}/boot/u-boot.bin" of="${LOOPDEV}" seek=63 >/dev/null 2>&1
dd if=${ROOTFS}/boot/E4412_S.tzsw.signed.bin of=${LOOPDEV} seek=2111 >/dev/null 2>&1 dd if="${ROOTFS}/boot/E4412_S.tzsw.signed.bin" of="${LOOPDEV}" seek=2111 >/dev/null 2>&1
;; ;;
usbarmory*) usbarmory*)
dd if=${ROOTFS}/boot/u-boot.imx of=${LOOPDEV} bs=512 seek=2 conv=fsync >/dev/null 2>&1 dd if="${ROOTFS}/boot/u-boot.imx" of="${LOOPDEV}" bs=512 seek=2 conv=fsync >/dev/null 2>&1
;; ;;
ci20*) ci20*)
dd if=${ROOTFS}/boot/u-boot-spl.bin of=${LOOPDEV} obs=512 seek=1 >/dev/null 2>&1 dd if="${ROOTFS}/boot/u-boot-spl.bin" of="${LOOPDEV}" obs=512 seek=1 >/dev/null 2>&1
dd if=${ROOTFS}/boot/u-boot.img of=${LOOPDEV} obs=1K seek=14 >/dev/null 2>&1 dd if="${ROOTFS}/boot/u-boot.img" of="${LOOPDEV}" obs=1K seek=14 >/dev/null 2>&1
;; ;;
GCP*) GCP*)
# Setup GRUB # Setup GRUB
mount_pseudofs mount_pseudofs
chroot ${ROOTFS} grub-install ${LOOPDEV} chroot "${ROOTFS}" grub-install "${LOOPDEV}"
sed -i "s:page_poison=1:page_poison=1 console=ttyS0,38400n8d:" ${ROOTFS}/etc/default/grub sed -i "s:page_poison=1:page_poison=1 console=ttyS0,38400n8d:" "${ROOTFS}/etc/default/grub"
chroot ${ROOTFS} update-grub chroot "${ROOTFS}" update-grub
umount_pseudofs umount_pseudofs
# Setup the GCP Guest services # Setup the GCP Guest services
for _service in dhcpcd sshd agetty-console nanoklogd socklog-unix GCP-Guest-Initialization GCP-accounts GCP-clock-skew GCP-ip-forwarding ; do for _service in dhcpcd sshd agetty-console nanoklogd socklog-unix GCP-Guest-Initialization GCP-accounts GCP-clock-skew GCP-ip-forwarding ; do
chroot ${ROOTFS} ln -sv /etc/sv/$_service /etc/runit/runsvdir/default/$_service chroot "${ROOTFS}" ln -sv /etc/sv/$_service /etc/runit/runsvdir/default/$_service
done done
# Turn off the agetty's since we can't use them anyway # Turn off the agetty's since we can't use them anyway
rm -v ${ROOTFS}/etc/runit/runsvdir/default/agetty-tty* rm -v "${ROOTFS}/etc/runit/runsvdir/default/agetty-tty*"
# Disable root login over ssh and lock account # Disable root login over ssh and lock account
sed -i "s:PermitRootLogin yes:PermitRootLogin no:" ${ROOTFS}/etc/ssh/sshd_config sed -i "s:PermitRootLogin yes:PermitRootLogin no:" "${ROOTFS}/etc/ssh/sshd_config"
chroot ${ROOTFS} passwd -l root chroot "${ROOTFS}" passwd -l root
# Set the Timezone # Set the Timezone
chroot ${ROOTFS} ln -svf /usr/share/zoneinfo/UTC /etc/localtime chroot "${ROOTFS}" ln -svf /usr/share/zoneinfo/UTC /etc/localtime
# Generate glibc-locales if necessary (this is a noop on musl) # Generate glibc-locales if necessary (this is a noop on musl)
if [ "$PLATFORM" = GCP ] ; then if [ "$PLATFORM" = GCP ] ; then
chroot ${ROOTFS} xbps-reconfigure -f glibc-locales chroot "${ROOTFS}" xbps-reconfigure -f glibc-locales
fi fi
# Remove SSH host keys (these will get rebuilt on first boot) # Remove SSH host keys (these will get rebuilt on first boot)
rm -v ${ROOTFS}/etc/ssh/*key* rm -f "${ROOTFS}/etc/ssh/*key*"
rm -v ${ROOTFS}/etc/ssh/moduli rm -f "${ROOTFS}/etc/ssh/moduli"
# Force hte hostname since this isn't read from DHCP # Force hte hostname since this isn't read from DHCP
echo "void-GCE" > ${ROOTFS}/etc/hostname echo void-GCE > "${ROOTFS}/etc/hostname"
;; ;;
esac esac
mountpoint -q ${ROOTFS}/boot && umount ${ROOTFS}/boot umount -R "$ROOTFS"
umount $ROOTFS losetup -d "$LOOPDEV"
losetup -d $LOOPDEV rmdir "$ROOTFS" || die "$ROOTFS not empty!"
rmdir $ROOTFS
chmod 644 $FILENAME chmod 644 "$FILENAME"
case "$PLATFORM" in case "$PLATFORM" in
GCP*) GCP*)
mv void-GCP*.img disk.raw mv void-GCP*.img disk.raw
info_msg "Compressing disk.raw" info_msg "Compressing disk.raw"
tar Sczf "${FILENAME/.img/.tar.gz}" disk.raw tar Sczf "${FILENAME%.img}.tar.gz" disk.raw
rm disk.raw rm disk.raw
info_msg "Sucessfully created ${FILENAME/.img/.tar.gz/} image." info_msg "Sucessfully created ${FILENAME%.img}.tar.gz image."
;; ;;
*) *)
info_msg "Successfully created $FILENAME image." info_msg "Successfully created $FILENAME image."
;; ;;
esac esac
# vim: set ts=4 sw=4 et: