2013-08-11 12:17:18 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#-
|
2016-04-26 15:00:55 +02:00
|
|
|
# Copyright (c) 2013-2016 Juan Romero Pardines.
|
2017-07-23 17:55:41 -07:00
|
|
|
# Copyright (c) 2017 Google
|
2013-08-11 12:17:18 +02:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#-
|
|
|
|
|
|
|
|
readonly PROGNAME=$(basename $0)
|
|
|
|
readonly ARCH=$(uname -m)
|
|
|
|
|
|
|
|
trap 'printf "\nInterrupted! exiting...\n"; cleanup; exit 0' INT TERM HUP
|
|
|
|
|
2017-07-23 17:55:41 -07:00
|
|
|
mount_pseudofs() {
|
|
|
|
for f in sys dev proc; do
|
|
|
|
mkdir -p $ROOTFSDIR/$f
|
|
|
|
mount --bind /$f $ROOTFSDIR/$f
|
|
|
|
done
|
|
|
|
}
|
|
|
|
umount_pseudofs() {
|
|
|
|
umount -f $ROOTFSDIR/sys >/dev/null 2>&1
|
|
|
|
umount -f $ROOTFSDIR/dev >/dev/null 2>&1
|
|
|
|
umount -f $ROOTFSDIR/proc >/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
2013-08-11 12:17:18 +02:00
|
|
|
cleanup() {
|
2017-07-23 17:55:41 -07:00
|
|
|
unmount_pseudofs
|
2013-08-11 12:17:18 +02:00
|
|
|
umount -f ${ROOTFSDIR}/boot 2>/dev/null
|
|
|
|
umount -f ${ROOTFSDIR} 2>/dev/null
|
2014-01-22 18:15:57 +01:00
|
|
|
if [ -e "$LOOPDEV" ]; then
|
|
|
|
partx -d $LOOPDEV 2>/dev/null
|
|
|
|
losetup -d $LOOPDEV 2>/dev/null
|
|
|
|
fi
|
|
|
|
|
2013-08-11 12:17:18 +02:00
|
|
|
[ -d "$ROOTFSDIR" ] && rmdir $ROOTFSDIR
|
|
|
|
}
|
|
|
|
|
|
|
|
info_msg() {
|
2015-02-22 09:39:47 +01:00
|
|
|
printf "\033[1m[${PLATFORM}] $@\n\033[m"
|
2013-08-11 12:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
die() {
|
|
|
|
echo "FATAL: $@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
usage() {
|
2014-01-28 17:04:15 +01:00
|
|
|
cat <<_EOF
|
|
|
|
Usage: $PROGNAME [options] <rootfs-tarball>
|
|
|
|
|
|
|
|
The <rootfs-tarball> argument expects a tarball generated by void-mkrootfs.
|
|
|
|
The platform is guessed automatically by its name.
|
|
|
|
|
2016-04-26 15:00:55 +02:00
|
|
|
Accepted sizes suffixes: KiB, MiB, GiB, TiB, EiB.
|
2014-01-28 17:04:15 +01:00
|
|
|
|
|
|
|
OPTIONS
|
|
|
|
-b <fstype> Set /boot filesystem type (defaults to FAT)
|
2016-04-26 15:00:55 +02:00
|
|
|
-B <bsize> Set /boot filesystem size (defaults to 64MiB)
|
2014-01-28 17:04:15 +01:00
|
|
|
-r <fstype> Set / filesystem type (defaults to EXT4)
|
|
|
|
-s <totalsize> Set total image size (defaults to 2GB)
|
|
|
|
-o <output> Set image filename (guessed automatically)
|
|
|
|
-h Show this help
|
|
|
|
-V Show version
|
|
|
|
|
|
|
|
Resulting image will have 2 partitions, /boot and /.
|
|
|
|
_EOF
|
2013-08-11 12:17:18 +02:00
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# main()
|
|
|
|
#
|
2014-01-28 17:04:15 +01:00
|
|
|
while getopts "b:B:o:r:s:hV" opt; do
|
2013-08-11 12:17:18 +02:00
|
|
|
case $opt in
|
|
|
|
b) BOOT_FSTYPE="$OPTARG";;
|
2014-01-28 17:04:15 +01:00
|
|
|
B) BOOT_FSSIZE="$OPTARG";;
|
2013-08-11 12:17:18 +02:00
|
|
|
o) FILENAME="$OPTARG";;
|
|
|
|
r) ROOT_FSTYPE="$OPTARG";;
|
2014-01-28 17:04:15 +01:00
|
|
|
s) IMGSIZE="$OPTARG";;
|
2013-08-11 12:17:18 +02:00
|
|
|
V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
|
|
|
|
h) usage;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
|
|
|
|
ROOTFS_TARBALL="$1"
|
|
|
|
if [ -z "$ROOTFS_TARBALL" ]; then
|
|
|
|
usage
|
|
|
|
elif [ ! -r "$ROOTFS_TARBALL" ]; then
|
|
|
|
die "Cannot read rootfs tarball: $ROOTFS_TARBALL"
|
|
|
|
fi
|
|
|
|
|
2014-01-28 17:04:15 +01:00
|
|
|
PLATFORM="${ROOTFS_TARBALL#void-}"
|
|
|
|
PLATFORM="${PLATFORM%-rootfs*}"
|
|
|
|
|
2013-08-11 12:17:18 +02:00
|
|
|
if [ "$(id -u)" -ne 0 ]; then
|
|
|
|
die "need root perms to continue, exiting."
|
|
|
|
fi
|
|
|
|
|
2014-12-05 11:06:39 +01:00
|
|
|
: ${IMGSIZE:=2G}
|
2014-01-28 17:04:15 +01:00
|
|
|
: ${BOOT_FSTYPE:=vfat}
|
2016-04-26 15:00:55 +02:00
|
|
|
: ${BOOT_FSSIZE:=64MiB}
|
2014-01-28 17:04:15 +01:00
|
|
|
: ${ROOT_FSTYPE:=ext4}
|
|
|
|
|
2013-08-11 12:17:18 +02:00
|
|
|
if [ -z "$FILENAME" ]; then
|
2014-01-28 17:04:15 +01:00
|
|
|
FILENAME="void-${PLATFORM}-$(date +%Y%m%d).img"
|
2013-08-11 12:17:18 +02:00
|
|
|
fi
|
|
|
|
|
2014-01-28 17:04:15 +01:00
|
|
|
# double check PLATFORM is supported...
|
2014-01-26 20:32:54 +01:00
|
|
|
case "$PLATFORM" in
|
2017-07-23 17:55:41 -07:00
|
|
|
bananapi|beaglebone|cubieboard2|cubietruck|odroid-c2|odroid-u2|rpi|rpi2|rpi3|usbarmory|GCP|*-musl);;
|
2014-01-28 17:04:15 +01:00
|
|
|
*) die "The $PLATFORM is not supported, exiting..."
|
2014-01-26 20:32:54 +01:00
|
|
|
esac
|
|
|
|
|
2016-04-26 15:00:55 +02:00
|
|
|
for f in sfdisk partx losetup mount mkfs.${BOOT_FSTYPE} mkfs.${ROOT_FSTYPE}; do
|
2013-11-11 10:09:00 +01:00
|
|
|
if ! which ${f} >/dev/null; then
|
|
|
|
die "Cannot find ${f}, exiting."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2014-01-26 20:32:54 +01:00
|
|
|
# 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
|
|
|
|
|
2013-08-11 12:17:18 +02:00
|
|
|
info_msg "Creating disk image ($IMGSIZE) ..."
|
2016-02-24 14:35:13 +01:00
|
|
|
truncate -s "${IMGSIZE}" $FILENAME >/dev/null 2>&1
|
2013-08-11 12:17:18 +02:00
|
|
|
|
2015-05-21 12:12:17 +02:00
|
|
|
ROOTFSDIR=$(mktemp -d)
|
|
|
|
|
2013-08-11 12:17:18 +02:00
|
|
|
info_msg "Creating disk image partitions/filesystems ..."
|
|
|
|
if [ "$BOOT_FSTYPE" = "vfat" ]; then
|
|
|
|
_btype="fat32"
|
2014-12-05 11:06:39 +01:00
|
|
|
_args="-I -F16"
|
2013-08-11 12:17:18 +02:00
|
|
|
fi
|
2015-05-21 12:12:17 +02:00
|
|
|
case "$PLATFORM" in
|
2016-05-06 14:41:04 +02:00
|
|
|
cubieboard2|cubietruck|ci20*|odroid-c2*)
|
2016-04-26 15:00:55 +02:00
|
|
|
sfdisk ${FILENAME} <<_EOF
|
2016-04-29 14:17:49 +02:00
|
|
|
label: dos
|
2016-04-26 15:00:55 +02:00
|
|
|
2048,,L
|
|
|
|
_EOF
|
2015-05-21 12:12:17 +02:00
|
|
|
LOOPDEV=$(losetup --show --find --partscan $FILENAME)
|
2016-09-24 17:23:31 +02:00
|
|
|
mkfs.${ROOT_FSTYPE} -O '^64bit,^extra_isize,^has_journal' ${LOOPDEV}p1 >/dev/null 2>&1
|
2015-05-21 12:12:17 +02:00
|
|
|
mount ${LOOPDEV}p1 $ROOTFSDIR
|
|
|
|
ROOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p1)
|
|
|
|
;;
|
|
|
|
*)
|
2016-04-26 15:00:55 +02:00
|
|
|
sfdisk ${FILENAME} <<_EOF
|
2016-04-29 14:17:49 +02:00
|
|
|
label: dos
|
2016-08-10 07:40:35 +02:00
|
|
|
2048,${BOOT_FSSIZE},b,*
|
2016-11-29 13:57:25 -08:00
|
|
|
,+,L
|
2016-04-26 15:00:55 +02:00
|
|
|
_EOF
|
2015-05-21 12:12:17 +02:00
|
|
|
LOOPDEV=$(losetup --show --find --partscan $FILENAME)
|
|
|
|
mkfs.${BOOT_FSTYPE} $_args ${LOOPDEV}p1 >/dev/null
|
|
|
|
case "$ROOT_FSTYPE" in
|
|
|
|
ext[34]) disable_journal="-O ^has_journal";;
|
|
|
|
esac
|
|
|
|
mkfs.${ROOT_FSTYPE} $disable_journal ${LOOPDEV}p2 >/dev/null 2>&1
|
|
|
|
mount ${LOOPDEV}p2 $ROOTFSDIR
|
|
|
|
mkdir -p ${ROOTFSDIR}/boot
|
|
|
|
mount ${LOOPDEV}p1 ${ROOTFSDIR}/boot
|
|
|
|
BOOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p1)
|
|
|
|
ROOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p2)
|
|
|
|
;;
|
2014-11-29 18:16:50 +01:00
|
|
|
esac
|
2013-08-11 12:17:18 +02:00
|
|
|
|
2014-12-05 05:46:05 +01:00
|
|
|
info_msg "Unpacking rootfs tarball ..."
|
2014-12-05 11:06:39 +01:00
|
|
|
if [ "$PLATFORM" = "beaglebone" ]; then
|
|
|
|
fstab_args=",noauto"
|
|
|
|
tar xfp $ROOTFS_TARBALL -C $ROOTFSDIR ./boot/MLO
|
|
|
|
tar xfp $ROOTFS_TARBALL -C $ROOTFSDIR ./boot/u-boot.img
|
|
|
|
touch $ROOTFSDIR/boot/uEnv.txt
|
|
|
|
umount $ROOTFSDIR/boot
|
|
|
|
fi
|
2016-12-10 03:30:13 -08:00
|
|
|
tar xfp $ROOTFS_TARBALL --xattrs --xattrs-include='*' -C $ROOTFSDIR
|
2013-08-11 12:17:18 +02:00
|
|
|
|
2015-01-17 11:31:10 +01:00
|
|
|
fspassno="1"
|
|
|
|
if [ "$ROOT_FSTYPE" = "f2fs" ]; then
|
|
|
|
fspassno="0"
|
|
|
|
fi
|
|
|
|
echo "UUID=$ROOT_UUID / $ROOT_FSTYPE defaults 0 ${fspassno}" >> ${ROOTFSDIR}/etc/fstab
|
2015-05-21 12:12:17 +02:00
|
|
|
if [ -n "$BOOT_UUID" ]; then
|
|
|
|
echo "UUID=$BOOT_UUID /boot $BOOT_FSTYPE defaults${fstab_args} 0 2" >> ${ROOTFSDIR}/etc/fstab
|
|
|
|
fi
|
2014-01-26 20:32:54 +01:00
|
|
|
|
2015-05-16 20:16:32 +02:00
|
|
|
case "$PLATFORM" in
|
2016-04-26 15:47:56 +02:00
|
|
|
bananapi*|cubieboard2*|cubietruck*)
|
2014-01-26 20:32:54 +01:00
|
|
|
dd if=${ROOTFSDIR}/boot/u-boot-sunxi-with-spl.bin of=${LOOPDEV} bs=1024 seek=8 >/dev/null 2>&1
|
2015-05-16 20:16:32 +02:00
|
|
|
;;
|
2016-05-06 14:41:04 +02:00
|
|
|
odroid-c2*)
|
|
|
|
dd if=${ROOTFSDIR}/boot/bl1.bin.hardkernel of=${LOOPDEV} bs=1 count=442 >/dev/null 2>&1
|
|
|
|
dd if=${ROOTFSDIR}/boot/bl1.bin.hardkernel of=${LOOPDEV} bs=512 skip=1 seek=1 >/dev/null 2>&1
|
|
|
|
dd if=${ROOTFSDIR}/boot/u-boot.bin of=${LOOPDEV} bs=512 seek=97 >/dev/null 2>&1
|
|
|
|
;;
|
2016-04-26 15:47:56 +02:00
|
|
|
odroid-u2*)
|
2014-01-28 14:31:08 +01:00
|
|
|
dd if=${ROOTFSDIR}/boot/E4412_S.bl1.HardKernel.bin of=${LOOPDEV} seek=1 >/dev/null 2>&1
|
|
|
|
dd if=${ROOTFSDIR}/boot/bl2.signed.bin of=${LOOPDEV} seek=31 >/dev/null 2>&1
|
|
|
|
dd if=${ROOTFSDIR}/boot/u-boot.bin of=${LOOPDEV} seek=63 >/dev/null 2>&1
|
|
|
|
dd if=${ROOTFSDIR}/boot/E4412_S.tzsw.signed.bin of=${LOOPDEV} seek=2111 >/dev/null 2>&1
|
2015-05-16 20:16:32 +02:00
|
|
|
;;
|
2016-04-26 15:47:56 +02:00
|
|
|
usbarmory*)
|
2015-06-12 10:59:23 +02:00
|
|
|
dd if=${ROOTFSDIR}/boot/u-boot.imx of=${LOOPDEV} bs=512 seek=2 conv=fsync >/dev/null 2>&1
|
2015-05-16 20:16:32 +02:00
|
|
|
;;
|
2016-04-26 15:47:56 +02:00
|
|
|
ci20*)
|
2016-04-26 11:20:14 +02:00
|
|
|
dd if=${ROOTFSDIR}/boot/u-boot-spl.bin of=${LOOPDEV} obs=512 seek=1 >/dev/null 2>&1
|
|
|
|
dd if=${ROOTFSDIR}/boot/u-boot.img of=${LOOPDEV} obs=1K seek=14 >/dev/null 2>&1
|
|
|
|
;;
|
2017-07-23 17:55:41 -07:00
|
|
|
GCP*)
|
|
|
|
# Setup GRUB
|
|
|
|
mount_pseudofs
|
|
|
|
chroot ${ROOTFSDIR} grub-install ${LOOPDEV}
|
|
|
|
sed -i "s:page_poison=1:page_poison=1 console=ttyS0,38400n8d:" ${ROOTFSDIR}/etc/default/grub
|
|
|
|
chroot ${ROOTFSDIR} update-grub
|
|
|
|
umount_pseudofs
|
|
|
|
|
|
|
|
# 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
|
|
|
|
chroot ${ROOTFSDIR} ln -sv /etc/sv/$_service /etc/runit/runsvdir/default/$_service
|
|
|
|
done
|
|
|
|
|
|
|
|
# Turn off the agetty's since we can't use them anyway
|
|
|
|
rm -v ${ROOTFSDIR}/etc/runit/runsvdir/default/agetty-tty*
|
|
|
|
|
|
|
|
# Disable root login over ssh and lock account
|
|
|
|
sed -i "s:PermitRootLogin yes:PermitRootLogin no:" ${ROOTFSDIR}/etc/ssh/sshd_config
|
|
|
|
chroot ${ROOTFSDIR} passwd -l root
|
|
|
|
|
|
|
|
# Set the Timezone
|
|
|
|
chroot ${ROOTFSDIR} ln -svf /usr/share/zoneinfo/UTC /etc/localtime
|
|
|
|
|
|
|
|
# Generate glibc-locales if necessary (this is a noop on musl)
|
|
|
|
if [ "$PLATFORM" = GCP ] ; then
|
|
|
|
chroot ${ROOTFSDIR} xbps-reconfigure -f glibc-locales
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove SSH host keys (these will get rebuilt on first boot)
|
|
|
|
rm -v ${ROOTFSDIR}/etc/ssh/*key*
|
|
|
|
rm -v ${ROOTFSDIR}/etc/ssh/moduli
|
|
|
|
|
|
|
|
# Force hte hostname since this isn't read from DHCP
|
|
|
|
echo "void-GCE" > ${ROOTFSDIR}/etc/hostname
|
|
|
|
;;
|
2015-05-16 20:16:32 +02:00
|
|
|
esac
|
2014-01-26 20:32:54 +01:00
|
|
|
|
2015-02-22 09:39:47 +01:00
|
|
|
mountpoint -q ${ROOTFSDIR}/boot && umount ${ROOTFSDIR}/boot
|
2013-08-11 12:17:18 +02:00
|
|
|
umount $ROOTFSDIR
|
|
|
|
losetup -d $LOOPDEV
|
|
|
|
rmdir $ROOTFSDIR
|
|
|
|
|
|
|
|
chmod 644 $FILENAME
|
2017-07-23 17:55:41 -07:00
|
|
|
case "$PLATFORM" in
|
|
|
|
GCP*)
|
|
|
|
mv void-GCP*.img disk.raw
|
|
|
|
info_msg "Compressing disk.raw"
|
|
|
|
tar Sczf "${FILENAME/.img/.tar.gz}" disk.raw
|
|
|
|
rm disk.raw
|
|
|
|
info_msg "Sucessfully created ${FILENAME/.img/.tar.gz/} image."
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
info_msg "Successfully created $FILENAME image."
|
|
|
|
;;
|
|
|
|
esac
|
2013-08-11 12:17:18 +02:00
|
|
|
|
|
|
|
# vim: set ts=4 sw=4 et:
|