-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathimage.sh
executable file
·72 lines (50 loc) · 1.3 KB
/
image.sh
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
#!/bin/bash
set -e
set -o pipefail
set -u
script=$(readlink -f "$0")
top=$(dirname "$script")
cd ${top}
. ${top}/settings.inc.sh
if [ $# -ne 2 ]; then
echo usage: $0 BOOT_TARBALL IMAGE_NAME
exit 1
fi
boot_dev=
boot_tarball=$1
image_file=$2
if ! [ -f $boot_tarball ]; then
echo $0: No such file: $boot_tarball
exit 1
fi
cleanup () {
echo Cleaning up
umount ${boot_mnt} || true
[ x"${boot_dev}" != x ] && losetup -d ${boot_dev}
sync
}
trap 'cleanup; exit 1' EXIT
echo Creating image file
mkdir -p ${top}/out
rm -f ${image_file} ${image_file}.gz
truncate -s ${image_size}M ${image_file}
echo Creating partition table
sfdisk ${image_file} < ${top}/sfdisk-script.txt
echo Setting up loopback for boot
boot_dev=$(losetup -o ${boot_offset} --sizelimit ${boot_size} -f --show ${image_file})
echo Creating boot filesystem
mkfs.vfat -n BOOT ${boot_dev}
echo Mounting bootfs
mount ${boot_dev} ${boot_mnt}
echo Unpacking boot tarball
tar xf ${boot_tarball} -C ${boot_mnt} --no-same-owner --no-same-permissions --strip-components=2
umount ${boot_mnt}
[ x"${boot_dev}" != x ] && losetup -d ${boot_dev}
echo Copying rootfs image
gunzip -c ${root_image}.gz | dd of=${image_file} seek=$[${root_offset}/512] bs=512
trap - EXIT
echo Compressing image
gzip ${image_file}
echo Wrote image to ${image_file}.gz
sync
echo Done