void-mklive/mkimage.sh.in

198 lines
6.1 KiB
Bash

#!/bin/sh
#-
# Copyright (c) 2013-2015 Juan Romero Pardines.
# 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
cleanup() {
umount -f ${ROOTFSDIR}/boot 2>/dev/null
umount -f ${ROOTFSDIR} 2>/dev/null
if [ -e "$LOOPDEV" ]; then
partx -d $LOOPDEV 2>/dev/null
losetup -d $LOOPDEV 2>/dev/null
fi
[ -d "$ROOTFSDIR" ] && rmdir $ROOTFSDIR
}
info_msg() {
printf "\033[1m[${PLATFORM}] $@\n\033[m"
}
die() {
echo "FATAL: $@"
exit 1
}
usage() {
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.
Accepted sizes suffixes: K, M, G, T, E.
OPTIONS
-b <fstype> Set /boot filesystem type (defaults to FAT)
-B <bsize> Set /boot filesystem size (defaults to 64MB)
-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
exit 0
}
#
# main()
#
while getopts "b:B:o:r:s:hV" opt; do
case $opt in
b) BOOT_FSTYPE="$OPTARG";;
B) BOOT_FSSIZE="$OPTARG";;
o) FILENAME="$OPTARG";;
r) ROOT_FSTYPE="$OPTARG";;
s) IMGSIZE="$OPTARG";;
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
PLATFORM="${ROOTFS_TARBALL#void-}"
PLATFORM="${PLATFORM%-rootfs*}"
if [ "$(id -u)" -ne 0 ]; then
die "need root perms to continue, exiting."
fi
: ${IMGSIZE:=2G}
: ${BOOT_FSTYPE:=vfat}
: ${BOOT_FSSIZE:=64M}
: ${ROOT_FSTYPE:=ext4}
if [ -z "$FILENAME" ]; then
FILENAME="void-${PLATFORM}-$(date +%Y%m%d).img"
fi
# double check PLATFORM is supported...
case "$PLATFORM" in
bananapi|beaglebone|cubieboard2|odroid-u2|rpi|rpi2|usbarmory);;
*) die "The $PLATFORM is not supported, exiting..."
esac
for f in parted partx losetup mount mkfs.${BOOT_FSTYPE} mkfs.${ROOT_FSTYPE}; do
if ! which ${f} >/dev/null; then
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) ..."
dd if=/dev/zero of=$FILENAME bs=$IMGSIZE count=1 ${DD_SPARSE} >/dev/null 2>&1
info_msg "Creating disk image partitions/filesystems ..."
parted $FILENAME mktable msdos
if [ "$BOOT_FSTYPE" = "vfat" ]; then
_btype="fat32"
_args="-I -F16"
fi
parted $FILENAME mkpart primary ${_btype} 4096s ${BOOT_FSSIZE}
parted $FILENAME mkpart primary ext2 ${BOOT_FSSIZE} 100%
parted $FILENAME toggle 1 boot
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
ROOTFSDIR=$(mktemp -d)
mount ${LOOPDEV}p2 $ROOTFSDIR
mkdir -p ${ROOTFSDIR}/boot
mount ${LOOPDEV}p1 ${ROOTFSDIR}/boot
info_msg "Unpacking rootfs tarball ..."
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
tar xfp $ROOTFS_TARBALL -C $ROOTFSDIR
BOOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p1)
ROOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p2)
fspassno="1"
if [ "$ROOT_FSTYPE" = "f2fs" ]; then
fspassno="0"
fi
echo "UUID=$ROOT_UUID / $ROOT_FSTYPE defaults 0 ${fspassno}" >> ${ROOTFSDIR}/etc/fstab
echo "UUID=$BOOT_UUID /boot $BOOT_FSTYPE defaults${fstab_args} 0 2" >> ${ROOTFSDIR}/etc/fstab
if [ "$PLATFORM" = "cubieboard2" -o "$PLATFORM" = "bananapi" ]; then
dd if=${ROOTFSDIR}/boot/u-boot-sunxi-with-spl.bin of=${LOOPDEV} bs=1024 seek=8 >/dev/null 2>&1
elif [ "$PLATFORM" = "odroid-u2" ]; then
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
elif [ "$PLATFORM" = "usbarmory" ]; then
dd if=${ROOTFSDIR}/boot/u-boot.imx of=${LOOPDEV} bs=512 seek=2 conv=fsync
fi
mountpoint -q ${ROOTFSDIR}/boot && umount ${ROOTFSDIR}/boot
umount $ROOTFSDIR
losetup -d $LOOPDEV
rmdir $ROOTFSDIR
chmod 644 $FILENAME
info_msg "Successfully created $FILENAME image."
# vim: set ts=4 sw=4 et: