summary refs log tree commit diff
path: root/mkimage.sh.in
blob: 26007dd4ea504080b24612b1d326b9eca74628ed (plain) (blame)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/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|cubietruck|odroid-u2|rpi|rpi2|usbarmory|*-musl);;
    *) 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) ..."
truncate -s "${IMGSIZE}" $FILENAME >/dev/null 2>&1

ROOTFSDIR=$(mktemp -d)

info_msg "Creating disk image partitions/filesystems ..."
parted $FILENAME mktable msdos
if [ "$BOOT_FSTYPE" = "vfat" ]; then
    _btype="fat32"
    _args="-I -F16"
fi
case "$PLATFORM" in
cubieboard2|cubietruck)
    parted $FILENAME mkpart primary ext2 2048s ${ROOT_FSSIZE} 100%
    parted $FILENAME toggle 1 boot
    LOOPDEV=$(losetup --show --find --partscan $FILENAME)
    mkfs.${ROOT_FSTYPE} $disable_journal ${LOOPDEV}p1 >/dev/null 2>&1
    mount ${LOOPDEV}p1 $ROOTFSDIR
    ROOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p1)
    ;;
*)
    parted $FILENAME mkpart primary ${_btype} 2048s ${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
    mount ${LOOPDEV}p2 $ROOTFSDIR
    mkdir -p ${ROOTFSDIR}/boot
    mount ${LOOPDEV}p1 ${ROOTFSDIR}/boot
    BOOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p1)
    ROOT_UUID=$(blkid -o value -s UUID ${LOOPDEV}p2)
    ;;
esac

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

fspassno="1"
if [ "$ROOT_FSTYPE" = "f2fs" ]; then
    fspassno="0"
fi
echo "UUID=$ROOT_UUID / $ROOT_FSTYPE defaults 0 ${fspassno}" >> ${ROOTFSDIR}/etc/fstab
if [ -n "$BOOT_UUID" ]; then
    echo "UUID=$BOOT_UUID /boot $BOOT_FSTYPE defaults${fstab_args} 0 2" >> ${ROOTFSDIR}/etc/fstab
fi

case "$PLATFORM" in
bananapi|cubieboard2|cubietruck)
    dd if=${ROOTFSDIR}/boot/u-boot-sunxi-with-spl.bin of=${LOOPDEV} bs=1024 seek=8 >/dev/null 2>&1
    ;;
odroid-u2)
    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
    ;;
usbarmory)
    dd if=${ROOTFSDIR}/boot/u-boot.imx of=${LOOPDEV} bs=512 seek=2 conv=fsync >/dev/null 2>&1
    ;;
esac

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: