-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-flash-image
executable file
·64 lines (58 loc) · 2.14 KB
/
create-flash-image
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
#!/bin/bash
set -e
set -x
export LC_ALL=C
image_size=480 # MB
part_begin=1 # MB
part_size=477 # MB
if [ "$build_dir" = "" ]; then
build_dir="."
fi
if [ -e $build_dir/img ]; then rm -f $build_dir/img; fi
if [ -d $build_dir/mnt ]; then rmdir $build_dir/mnt; fi
mkdir $build_dir/mnt
dd if=/dev/zero of=$build_dir/img bs=1M count=0 seek=$image_size
set +e
if [ "$(/sbin/fdisk -v | grep -i "^gnu fdisk")" != "" ]; then
echo "Sorry, gnu-fdisk is not supported due to bug #611011"
exit 1
fi
set -e
/sbin/fdisk -C 61 -H 255 -S 63 -c -u $build_dir/img <<EOF
n
p
1
$(expr $part_begin \* 1024 \* 1024 / 512)
$(expr \( $part_begin + $part_size \) \* 1024 \* 1024 / 512)
a
w
q
EOF
if [ ! -e /dev/loop0 ]; then
sudo modprobe loop
fi
loopdev="$(sudo losetup -f)"
sudo losetup -o $(expr $part_begin \* 1024 \* 1024) --sizelimit $(expr $part_size \* 1024 \* 1024) "${loopdev}" $build_dir/img
sudo mkfs -t ext4 -b 4096 "${loopdev}"
ramroot_uuid="$(uuidgen | sed 's@^........-....-....@962d307f-8f1f-4301@')"
sudo tune2fs -U $ramroot_uuid "${loopdev}"
sync
sudo losetup -d "${loopdev}"
img_loop=$(sudo losetup -f --sizelimit $(expr $part_begin \* 1024 \* 1024) --show $build_dir/img)
sudo mount $build_dir/img $build_dir/mnt -oloop,offset=$(expr $part_begin \* 1024 \* 1024)
sudo cp -a $build_dir/fs/snapshot $build_dir/mnt
sudo mkdir -p $build_dir/mnt/boot/grub
sudo touch $build_dir/mnt/boot/grub/device.map
sudo mount /dev $build_dir/mnt/snapshot/0/dev -obind
sudo chroot $build_dir/mnt/snapshot/0 grub-install --modules 'part_msdos biosdisk ext2 search_fs_uuid' --root-directory / --no-floppy $img_loop
sudo mv $build_dir/mnt/snapshot/0/boot/grub/i386-pc $build_dir/mnt/boot/grub
sudo umount $build_dir/mnt/snapshot/0/dev
sudo umount $build_dir/mnt
sudo losetup -d $img_loop
sudo mount $build_dir/img $build_dir/mnt -oloop,offset=$(expr $part_begin \* 1024 \* 1024)
sudo sh -c "ramroot_build=1 ramroot=$build_dir/mnt boot_uuid=$ramroot_uuid $build_dir/mnt/snapshot/0/usr/local/bin/ramroot snapshot enable 0"
sudo chroot $build_dir/mnt/snapshot/0 sh -c "du --one-file-system --summarize --bytes / | cut -f1 > /etc/ramroot/size"
sync
sleep 2
sudo umount $build_dir/mnt
rmdir $build_dir/mnt