-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_grub2_head_image
executable file
·183 lines (149 loc) · 5.02 KB
/
create_grub2_head_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
175
176
177
178
179
180
181
182
183
#!/bin/bash
#
# \brief Create hard-disk image bootable via GRUB2
# \author Christian Helmuth
# \author Alexander Boettcher
# \date 2014-07-11
#
# We generate a head-image file only containing a BIOS legacy bootable
# image with GPT partition. The head-image contains a bios_grub partition
# (required by GRUB2), a FAT partition to store the additional GRUB2 boot
# modules (*.mod) and the initial configuration (grub.cfg). The configuration
# points to the 3. partition to load from the Genode scenario.
# The 3. partition is supposed to be resized by the Genode tool/run tools to
# fit the size requirements of the Genode scenario subject to boot.
#
# The script requires a PC GRUB2 Bios legacy installation, which may be created
# by "create_grub2_bootloader" script.
#
# Some parts of this script must be executed with superuser privileges and
# utilize 'sudo' for this purpose.
set -u # treat unset variables as error
set -e # exit immediately on error
USE_SUDO=sudo
# hints in case you haven't enough /dev/loops
#
# modprobe loop max_loop=64
# mknod -m 660 /dev/loop24 b 7 24
# mknod -m 660 /dev/loop25 b 7 25
# mknod -m 660 /dev/loop26 b 7 26
mnt_grub2=$(mktemp -d)
mnt_genode=$(mktemp -d)
function cleanup {
set +u # ignore unset variables
set +e # don't exit on error
# show numbers about allocated/free space
df -h $loop_part2 $loop_part3
if [ -d $mnt_grub2 ]; then
$USE_SUDO umount $mnt_grub2
rm -rf $mnt_grub2
fi
if [ -d $mnt_genode ]; then
$USE_SUDO umount $mnt_genode
rm -rf $mnt_genode
fi
if [ $loop_part3 ]; then
$USE_SUDO losetup -d $loop_part3
fi
if [ $loop_part2 ]; then
$USE_SUDO losetup -d $loop_part2
fi
if [ $loop_image ]; then
$USE_SUDO losetup -d $loop_image
fi
}
trap cleanup EXIT
#
# config
#
GRUB2_BASE=/tmp/tmp.genode_grub2
GRUB2_DIR=$GRUB2_BASE/32_pc/binary
GRUB2_INSTALL=$GRUB2_DIR/sbin/grub-install
if [ ! -d $GRUB2_DIR ]; then
echo "GRUB2 installation '$GRUB2_DIR' missing. Exiting..."
exit 1
fi
#head_size_kib=4276
#head_img="grub2-head-big.img"
head_size_kib=2276
head_img="grub2-head.img"
genode_size_kib=$(expr $head_size_kib - 128)
# generate image file
if [ -f $head_img ]; then
echo "$head_img exists. Exiting..."
exit 1
fi
fallocate -l ${head_size_kib}kiB $head_img
# prepare label and partition table
parted="parted -a none -s $head_img -- "
let "bios_boot_fs = 40 * 512 / 512" # fs == firstsector
let "bios_boot_ls = $bios_boot_fs + 100" # ls == lastsector
let "grub2_fs = $bios_boot_ls + 1"
let "grub2_ls = $genode_size_kib * 1024 / 512 - $bios_boot_fs - 1"
let "genode_fs = $grub2_ls + 1"
let "genode_ls = $head_size_kib * 1024 / 512 - $bios_boot_fs - 1"
$parted "mklabel gpt"
$parted "mkpart BIOSBOOT fat32 ${bios_boot_fs}s ${bios_boot_ls}s set 1 bios_grub on"
$parted "mkpart GRUB2 fat32 ${grub2_fs}s ${grub2_ls}s set 2 boot on"
$parted "mkpart GENODE* fat32 ${genode_fs}s ${genode_ls}s"
#
# mount loop images
#
let "grub2_offset = $grub2_fs * 512"
let "grub2_size = ($grub2_ls - $grub2_fs) * 512"
let "genode_offset = $genode_fs * 512"
let "genode_size = ($genode_ls - $genode_fs) * 512"
loop_image=$($USE_SUDO losetup -f)
$USE_SUDO losetup $loop_image $head_img
loop_part2=$($USE_SUDO losetup -f)
$USE_SUDO losetup $loop_part2 $head_img -o $grub2_offset --sizelimit $grub2_size
loop_part3=$($USE_SUDO losetup -f)
$USE_SUDO losetup $loop_part3 $head_img -o $genode_offset --sizelimit $genode_size
echo "using loop file $loop_image for $head_img"
echo "using loop file $loop_part2 for second partition"
echo "using loop file $loop_part3 for third partition"
# initialize partitions
$USE_SUDO mkfs.vfat $loop_part2 -n GRUB2
$USE_SUDO mkfs.vfat $loop_part3 -n GENODE
# install GRUB2
$USE_SUDO mount $loop_part2 $mnt_grub2
$USE_SUDO mount $loop_part3 $mnt_genode
$USE_SUDO $GRUB2_INSTALL --root-directory=$mnt_grub2 --no-floppy \
--target i386-pc \
--install-modules="configfile multiboot multiboot2 gzio ext2 gfxterm_background png regexp" \
--locales="" --fonts="" \
--modules="biosdisk part_gpt vbe" $loop_image
$USE_SUDO rm $mnt_grub2/boot/grub/grubenv
$USE_SUDO rm $mnt_grub2/boot/grub/i386-pc/*.img
$USE_SUDO rm $mnt_grub2/boot/grub/i386-pc/*.sh
$USE_SUDO rm $mnt_grub2/boot/grub/i386-pc/*.o
$USE_SUDO rm $mnt_grub2/boot/grub/i386-pc/*.cfg
$USE_SUDO rmdir $mnt_grub2/boot/grub/fonts
$USE_SUDO rmdir $mnt_grub2/boot/grub/locale
# generate GRUB2 configuration
cfg=$(mktemp)
cat > $cfg <<EOF
regexp --set 1:disc "([a-z]+[0-9]+)" "\$root"
set root=(\$disc,3)
set gfxpayload="0x0x32"
#set menu_color_normal=black/black
#set color_normal=black/black
insmod gfxterm
terminal_output gfxterm
insmod gfxterm_background
insmod png
loadfont /boot/font.pf2
background_image -m center /boot/boot.png
configfile /boot/grub/grub.cfg
EOF
$USE_SUDO cp $cfg $mnt_grub2/boot/grub/grub.cfg
$USE_SUDO mkdir -p $mnt_grub2/efi/boot
cat > $cfg <<EOF
menuentry 'Genode on ???' {
multiboot2 /boot/bender
}
EOF
$USE_SUDO mkdir -p $mnt_genode/boot/grub
$USE_SUDO cp $cfg $mnt_genode/boot/grub/grub.cfg
# cleanup
rm $cfg