193 lines
5.8 KiB
Bash
193 lines
5.8 KiB
Bash
#!/bin/sh
|
|
#-
|
|
# Copyright (c) 2013 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$@\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="$2"
|
|
if [ -z "$IMGSIZE" ]; then
|
|
IMGSIZE="2G"
|
|
else
|
|
IMGSIZE="${2}G"
|
|
fi
|
|
|
|
: ${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
|
|
cubieboard2|odroid-u2|rpi);;
|
|
*) 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"
|
|
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 $FILENAME)
|
|
partx -a $LOOPDEV
|
|
mkfs.${BOOT_FSTYPE} $_args ${LOOPDEV}p1 >/dev/null
|
|
mkfs.${ROOT_FSTYPE} ${LOOPDEV}p2 >/dev/null 2>&1
|
|
|
|
info_msg "Unpacking rootfs tarball ..."
|
|
ROOTFSDIR=$(mktemp -d)
|
|
mount ${LOOPDEV}p2 $ROOTFSDIR
|
|
mkdir -p ${ROOTFSDIR}/boot
|
|
mount ${LOOPDEV}p1 ${ROOTFSDIR}/boot
|
|
|
|
tar xfp $ROOTFS_TARBALL -C $ROOTFSDIR
|
|
|
|
BOOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p1)
|
|
ROOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p2)
|
|
echo "UUID=$BOOT_UUID /boot $BOOT_FSTYPE defaults 0 0" >> ${ROOTFSDIR}/etc/fstab
|
|
echo "UUID=$ROOT_UUID / $ROOT_FSTYPE defaults 0 1" >> ${ROOTFSDIR}/etc/fstab
|
|
|
|
if [ -s ${ROOTFSDIR}/boot/cmdline.txt ]; then
|
|
sed -e "s,rootfstype=ext4,rootfstype=${ROOT_FSTYPE}," -i ${ROOTFSDIR}/boot/cmdline.txt
|
|
fi
|
|
|
|
# For cubieboard we need to flash u-boot to the image.
|
|
if [ "$PLATFORM" = "cubieboard2" ]; 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
|
|
fi
|
|
|
|
umount ${ROOTFSDIR}/boot
|
|
umount $ROOTFSDIR
|
|
partx -d $LOOPDEV
|
|
losetup -d $LOOPDEV
|
|
rmdir $ROOTFSDIR
|
|
|
|
chmod 644 $FILENAME
|
|
info_msg "Successfully created $FILENAME ($PLATFORM) image."
|
|
|
|
# vim: set ts=4 sw=4 et:
|