New void-mkimage script to generate working images with a provided rootfs tarball.
This commit is contained in:
parent
112a6a7775
commit
065b000f4f
140
mkimage.sh.in
Normal file
140
mkimage.sh.in
Normal file
@ -0,0 +1,140 @@
|
||||
#!/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
|
||||
|
||||
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 ${ROOT_FSTYPE} 256M 100%
|
||||
parted $FILENAME toggle 1 boot
|
||||
LOOPDEV=$(losetup --show --find -P $FILENAME)
|
||||
mkfs.${BOOT_FSTYPE} -F32 ${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
|
||||
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:
|
Loading…
x
Reference in New Issue
Block a user