1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#!/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
for f in parted mkfs.${BOOT_FSTYPE} mkfs.${ROOT_FSTYPE}; do
if ! which ${f} >/dev/null; then
die "Cannot find ${f}, exiting."
fi
done
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 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:
|