151 lines
4.5 KiB
Bash
151 lines
4.5 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
|
|
[ -e "$LOOPDEV" ] && losetup -d $LOOPDEV 2>/dev/null
|
|
[ -d "$ROOTFSDIR" ] && rmdir $ROOTFSDIR
|
|
}
|
|
|
|
info_msg() {
|
|
printf "\033[1m$@\n\033[m"
|
|
}
|
|
|
|
die() {
|
|
echo "FATAL: $@"
|
|
exit 1
|
|
}
|
|
|
|
usage() {
|
|
echo "Usage: $PROGNAME [-b fstype] [-o filename] [-r fstype] [-hV] <rootfs-tarball> [size]"
|
|
echo
|
|
echo " The <rootfs-tarball> argument expects a file generated by void-mkrootfs."
|
|
echo " The <size> argument expects a number (GB). If <size> is not set, defaults to 2."
|
|
echo
|
|
echo "OPTIONS"
|
|
echo " -b <fstype> Set /boot <fstype> (defaults to fat32, 256MB)"
|
|
echo " -o <filename> Set output <filename>."
|
|
echo " -r <fstype> Set / <fstype> (defaults to ext4, 256MB - <size>)"
|
|
echo
|
|
echo " Resulting image will have 2 partitions, /boot and / of total <size>."
|
|
exit 0
|
|
}
|
|
|
|
#
|
|
# main()
|
|
#
|
|
while getopts "b:o:r:hV" opt; do
|
|
case $opt in
|
|
b) BOOT_FSTYPE="$OPTARG";;
|
|
o) FILENAME="$OPTARG";;
|
|
r) ROOT_FSTYPE="$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
|
|
|
|
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
|
|
|
|
if [ -z "$BOOT_FSTYPE" ]; then
|
|
BOOT_FSTYPE="vfat"
|
|
fi
|
|
if [ -z "$ROOT_FSTYPE" ]; then
|
|
ROOT_FSTYPE="ext4"
|
|
fi
|
|
if [ -z "$FILENAME" ]; then
|
|
FILENAME="void-image-$(date +%Y%m%d).img"
|
|
fi
|
|
|
|
for f in parted losetup mount mkfs.${BOOT_FSTYPE} mkfs.${ROOT_FSTYPE}; do
|
|
if ! which ${f} >/dev/null; then
|
|
die "Cannot find ${f}, exiting."
|
|
fi
|
|
done
|
|
|
|
info_msg "Creating disk image ($IMGSIZE) ..."
|
|
dd if=/dev/zero of=$FILENAME bs=$IMGSIZE count=1 conv=sparse >/dev/null 2>&1
|
|
|
|
info_msg "Creating disk image partitions/filesystems ..."
|
|
parted $FILENAME mktable msdos
|
|
if [ "$BOOT_FSTYPE" = "vfat" ]; then
|
|
_btype="fat32"
|
|
fi
|
|
parted $FILENAME mkpart primary ${_btype} 2048s 256M
|
|
parted $FILENAME mkpart primary ext2 256M 100%
|
|
parted $FILENAME toggle 1 boot
|
|
LOOPDEV=$(losetup --show --find -P $FILENAME)
|
|
mkfs.${BOOT_FSTYPE} ${LOOPDEV}p1 >/dev/null 2>&1
|
|
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
|
|
|
|
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:
|