-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_disk_image
executable file
·175 lines (151 loc) · 6.26 KB
/
create_disk_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
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
#!/bin/bash -
#===============================================================================
#
# FILE: create_disk_image
#
# USAGE: ./create_disk_image [ output_directory [ disk_size [ image_size ]]]
#
# DESCRIPTION: does the image creation stuff that requires sudo
#
#===============================================================================
set -o nounset # Treat unset variables as an error
for arg in "$@" ; do
case "$arg" in
-*)
printf 'Usage: %s [ output_directory [ disk_size [ image_size ]]]\n' "$0"
printf 'This script also needs root priviledges\n' "$0"
printf 'required for the mount syscall\n' "$0"
exit
;;
esac
done
if [ $UID -ne 0 ]; then
printf 'This script needs root priviledges.\n' >&2
exit 1
fi
DIR="$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd )"
output_dir="$DIR/buildroot/output"
br_config_file="$output_dir/.config"
if [ -d "$PWD/output" ];then
output_dir="$PWD/output"
br_config_file="$output_dir/.config"
fi
if [ -f "$PWD/.output_dir" ] && [ -d "$(cat "$PWD/.output_dir")" ] ;then
output_dir="$(cat "$PWD/.output_dir")"
br_config_file="$output_dir/.config"
fi
disk_size=$(grep BR2_BRBOX_DISKSIZE "$br_config_file" | sed 's;BR2_BRBOX_DISKSIZE="\(.*\)";\1;')
if [ $# -gt 0 ]; then
disk_size="$1"
shift
fi
partition_size=$(grep BR2_BRBOX_PARTITIONSIZE "$br_config_file" | sed 's;BR2_BRBOX_PARTITIONSIZE="\(.*\)";\1;')
if [ $# -gt 0 ]; then
partition_size="$1"
shift
if [ $# -gt 0 ]; then
printf 'Too many arguments' >&2
exit 1
fi
fi
image_filename="$output_dir/images/bootable-usb-disk.img"
fallocate -l "$disk_size" "$image_filename"
test $? -eq 0 || { printf 'Could not create %s\n' "$image_filename" >&2 ; exit 1; }
BOOTSIZE=1M
STTNGSIZE=16M
ROOT1_LABEL=ROOT1 #linux-1
ROOT2_LABEL=ROOT2 #linux-2
STTNG_LABEL=STTNG #settings
USRDAT_LABEL=USRDAT #userdata
# IMAGE_VERSION=01.00.12345
GRUB_MENU_ENTRY1='brbox1'
GRUB_MENU_ENTRY2='brbox2'
GRUB2_TIMEOUT=1
ROOTDELAY=5
# Formatting disk
sgdisk -Z "$image_filename" >/dev/null
sgdisk -n 1::+"$BOOTSIZE" -t 1:ef02 -c 1:boot "$image_filename" >/dev/null
sgdisk -n 2::+"$partition_size" -c 2:$ROOT1_LABEL "$image_filename" >/dev/null
sgdisk -n 3::+"$partition_size" -c 3:$ROOT2_LABEL "$image_filename" >/dev/null
sgdisk -n 4::+"$STTNGSIZE" -c 4:$STTNG_LABEL "$image_filename" >/dev/null
sgdisk -n 5:: -c 5:$USRDAT_LABEL "$image_filename" >/dev/null
test $? -eq 0 || { printf 'Could not partition %s\n' "$image_filename" >&2 ; exit 1; }
# needs to write this file to disk, cannot find partitions otherwise
sync
echo "Creating disk image"
loopdevice=$(losetup -f --show "$image_filename")
test $? -eq 0 || { printf 'Could not setup loop device %s\n' "$image_filename" >&2 ; exit 1; }
partx -a "$loopdevice"
test $? -eq 0 || { printf 'Could not find partitions\n' >&2 ; exit 1; }
echo "Formating disk image"
mkfs.ext3 -L $ROOT1_LABEL "${loopdevice}p2" 1>/dev/null 2>/dev/null
test $? -eq 0 || { printf 'Formatting %s failed\n' $ROOT1_LABEL >&2 ; exit 1; }
mkfs.ext3 -L $ROOT2_LABEL "${loopdevice}p3" 1>/dev/null 2>/dev/null
test $? -eq 0 || { printf 'Formatting %s failed\n' $ROOT2_LABEL >&2 ; exit 1; }
mkfs.ext3 -L $STTNG_LABEL "${loopdevice}p4" 1>/dev/null 2>/dev/null
test $? -eq 0 || { printf 'Formatting %s failed\n' $STTNG_LABEL >&2 ; exit 1; }
mkfs.ext3 -L $USRDAT_LABEL "${loopdevice}p4" 1>/dev/null 2>/dev/null
test $? -eq 0 || { printf 'Formatting %s failed\n' $USRDAT_LABEL >&2 ; exit 1; }
mountdir=$(mktemp -d)
mkdir "$mountdir/root1" "$mountdir/root2"
mount "${loopdevice}p2" "$mountdir/root1"
test $? -eq 0 || { printf 'Could not mount root1\n' >&2 ; exit 1; }
mount "${loopdevice}p3" "$mountdir/root2"
test $? -eq 0 || { printf 'Could not mount root2\n' >&2 ; exit 1; }
echo "Copying files"
rootfs="$output_dir/images/rootfs.tar.xz"
tar -C "$mountdir/root1" -Jxf "$rootfs"
test $? -eq 0 || { printf 'Could not extract files onto root1\n' >&2; exit 1; }
tar -C "$mountdir/root2" -Jxf "$rootfs"
test $? -eq 0 || { printf 'Could not extract files onto root2\n' >&2; exit 1; }
echo "Generating grub configs"
cat <<ENDOFGRUBCFG >"$output_dir/grub.cfg.in"
insmod part_gpt
insmod part_msdos
insmod ext2
search -l $ROOT1_LABEL -s root
if [ -e /boot/grub/grub.cfg ]; then
configfile /boot/grub/grub.cfg
else
search -l $ROOT2_LABEL -s root
configfile /boot/grub/grub.cfg
fi
ENDOFGRUBCFG
test $? -eq 0 || { printf 'Could not create grub.cfg.in\n' >&2 ; exit 1; }
part1uuid=$(sgdisk -i=2 "$loopdevice" |grep 'Partition unique GUID' |cut -d\ -f4)
part2uuid=$(sgdisk -i=3 "$loopdevice" |grep 'Partition unique GUID' |cut -d\ -f4)
cat <<ENDOFGRUBCFG >"$output_dir/grub.cfg"
set timeout=$GRUB2_TIMEOUT
set default=0
insmod ext2
menuentry $GRUB_MENU_ENTRY1 {
search -l $ROOT1_LABEL -s root
linux /boot/bzImage rootdelay=$ROOTDELAY consoleblank=0 ro enable_mtrr_cleanup mtrr_spare_reg_nr=1 brboxsystem=$GRUB_MENU_ENTRY1 net.ifnames=0 root=PARTUUID=$part1uuid
}
menuentry $GRUB_MENU_ENTRY2 {
search -l $ROOT2_LABEL -s root
linux /boot/bzImage rootdelay=$ROOTDELAY consoleblank=0 ro enable_mtrr_cleanup mtrr_spare_reg_nr=1 brboxsystem=$GRUB_MENU_ENTRY2 net.ifnames=0 root=PARTUUID=$part2uuid
}
ENDOFGRUBCFG
test $? -eq 0 || { printf 'Could not create grub.cfg\n' >&2 ; exit 1; }
echo "Installing grub"
"$output_dir/host/usr/bin/grub-mkimage" -d "$output_dir/host/usr/lib/grub/i386-pc" \
-O i386-pc -o "$output_dir/images/grub.img" -c "$output_dir/grub.cfg.in" \
acpi cat boot linux ext2 fat part_msdos part_gpt normal biosdisk \
search echo search_fs_uuid normal ls ata configfile halt help \
hello read png vga lspci echo minicmd vga_text terminal
test $? -eq 0 || { printf 'Could not generate grub.img\n' >&2 ; exit 1; }
"$output_dir/host/usr/sbin/grub-bios-setup" \
-b "$output_dir/host/usr/lib/grub/i386-pc/boot.img" \
-c "$output_dir/images/grub.img" -d / "$loopdevice"
test $? -eq 0 || { printf 'Could setup grub\n' >&2 ; exit 1; }
cp "$output_dir/grub.cfg" "$mountdir/root1/boot/grub/grub.cfg"
cp "$output_dir/grub.cfg" "$mountdir/root2/boot/grub/grub.cfg"
printf "Cleaning up\n"
umount "$mountdir/root1"
umount "$mountdir/root2"
partx -d "$loopdevice"
losetup -d "$loopdevice"
rm -rf "$mountdir"
sync
chown "$SUDO_UID:$SUDO_GID" "$image_filename"