-
Notifications
You must be signed in to change notification settings - Fork 13
/
mkdisk.bash
executable file
·344 lines (286 loc) · 9.08 KB
/
mkdisk.bash
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#! /bin/bash
###########################################################################
# GLUED: GNU/Linux Uniform Environment Distribution #
# Copyright (C) 2007-2021 Universidade do Porto - Faculdade de Engenharia #
# Laboratório de Sistemas e Tecnologia Subaquática (LSTS) #
###########################################################################
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or (at #
# your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA #
# 02110-1301 USA. #
###########################################################################
# Author: Ricardo Martins #
###########################################################################
cmd_parted()
{
bdev="$1"; shift
"$cmd_parted" "$bdev" -a cylinder -s -- $@
}
cmd_mount()
{
fs="$1"
bdev="$2"
mkdir -p mount &&
mount -t "$fs" "$bdev" mount
}
cmd_unmount()
{
while [ 1 ]; do
(umount mount && rmdir mount) > /dev/null 2>&1
[ $? -eq 0 ] && break
sleep 1
done
}
die()
{
umount -v mount > /dev/null 2>&1
rmdir mount > /dev/null 2>&1
[ -n "$dev_loop" ] && losetup -d "$dev_loop"
exit 1
}
create_part_xboot()
{
nfo1 "X-Boot partition ($part_label)"
nfo2 "Creating partition"
cmd_parted "$dev_loop" \
mkpart primary fat32 "$part_start" "$part_end" \
set "$part_nr" boot on \
align-check minimal "$part_nr" \
|| die
nfo2 "Creating filesystem"
$cmd_mkdosfs -n "$part_label" "$part_dev" > /dev/null || die
nfo2 "Populating filesystem"
cmd_mount vfat "$part_dev" || die
for f in MLO u-boot.img uEnv.txt; do
if [ -f "$cfg_sys_family/rootfs/boot/$f" ]; then
nfo2 installing $f to boot partition
cp "$cfg_sys_family/rootfs/boot/$f" mount || die
fi
done
cmd_parted "$dev_loop" \
set "$part_nr" lba on \
|| die
cmd_parted "$dev_loop" print > /dev/null
}
create_part_rpiboot()
{
nfo1 "RPi-Boot partition ($part_label)"
nfo2 "Creating partition"
cmd_parted "$dev_loop" \
mkpart primary fat32 "$part_start" "$part_end" \
set "$part_nr" boot on \
align-check minimal "$part_nr" \
|| die
nfo2 "Creating filesystem"
$cmd_mkdosfs -n "$part_label" "$part_dev" > /dev/null || die
nfo2 "Populating filesystem"
cmd_mount vfat "$part_dev" || die
for f in "$cfg_sys_family/rootfs/boot/"*; do
nfo2 installing $f to boot partition
cp -r "$f" mount || die
done
nfo2 renaming kernel to kernel7.img
mv mount/kernel mount/kernel7.img
if [ -f mount/board.dtb ]; then
dtb=$(basename "$cfg_target_linux_dtb")
nfo2 renaming board.dtb to "$dtb"
mv "mount/board.dtb" "mount/$dtb"
fi
cmd_parted "$dev_loop" \
set "$part_nr" lba on \
|| die
cmd_parted "$dev_loop" print > /dev/null
}
create_part_root()
{
nfo1 "Root partition ($part_label)"
nfo2 "Creating partition"
cmd_parted "$dev_loop" \
mkpart primary ext2 "$part_start" "$part_end" \
align-check minimal "$part_nr" \
|| die
nfo2 "Creating filesystem"
"$cmd_mkfs_ext2" -q -F -L "$part_label" "$part_dev" > /dev/null 2>&1 || die
nfo2 "Populating filesystem"
cmd_mount ext2 "$part_dev" || die
tar -C mount -pxf "$cfg_rootfs_tar" || die
# Install bootloader.
if [ -f "$cfg_sys_family/toolchain/bin/extlinux" ]; then
nfo2 "Installing extlinux to root partition"
"$cfg_sys_family/toolchain/bin/extlinux" -S 63 -H 255 -i mount/boot/extlinux || die
fi
cmd_unmount || die
# Mark partition bootable if needed.
if [ -f "$cfg_sys_family/toolchain/bin/extlinux" ]; then
cmd_parted "$dev_loop" \
set "$part_nr" boot on \
|| die
fi
}
create_part_data()
{
nfo1 "Data partition ($part_label)"
nfo2 "Creating partition"
cmd_parted "$dev_loop" \
mkpart primary ext4 "$part_start" "$part_end" \
align-check minimal "$part_nr" \
|| die
nfo2 "Creating filesystem"
"$cmd_mkfs_ext4" -q -F -L "$part_label" -O dir_index "$part_dev" > /dev/null 2>&1 || die
nfo2 "Populating filesystem"
cmd_mount ext4 "$part_dev" || die
mkdir mount/lsts || die
cmd_unmount || die
}
export LC_ALL=C
if [ -z "$BASH_VERSION" ]; then
echo "ERROR: you must use bash to run this script."
exit 1
fi
if [ "$(whoami)" != 'root' ]; then
echo "ERROR: you must be root to execute this target."
exit 1
fi
if [ $# -lt 2 ]; then
echo "Usage: $0 <config> <disk> [disk_size]"
exit 1
fi
source "$1"
if ! [ -f "$cfg_rootfs_tar" ]; then
echo "ERROR: you must run pkrootfs.bash first."
exit 1
fi
if [ -x "$cfg_dir_toolchain/sbin/parted" ]; then
cmd_parted="$cfg_dir_toolchain/sbin/parted"
else
echo "Warning: using system parted, this might lead to errors"
cmd_parted="parted"
fi
if [ -x "$cfg_dir_toolchain/sbin/fdisk" ]; then
cmd_fdisk="$cfg_dir_toolchain/sbin/fdisk"
else
echo "Warning: using system fdisk, this might lead to errors"
cmd_fdisk="fdisk"
fi
if [ -x "$cfg_dir_toolchain/sbin/mkdosfs" ]; then
cmd_mkdosfs="$cfg_dir_toolchain/sbin/mkdosfs"
else
echo "Warning: using system mkdosfs, this might lead to errors"
cmd_mkdosfs="mkdosfs"
fi
if [ -x "$cfg_dir_toolchain/sbin/mkfs.ext2" ]; then
cmd_mkfs_ext2="$cfg_dir_toolchain/sbin/mkfs.ext2"
else
echo "Warning: using system mkfs.ext2, this might lead to errors"
cmd_mkfs_ext2="mkfs.ext2"
fi
if [ -x "$cfg_dir_toolchain/sbin/mkfs.ext4" ]; then
cmd_mkfs_ext4="$cfg_dir_toolchain/sbin/mkfs.ext4"
else
echo "Warning: using system mkfs.ext4, this might lead to errors"
cmd_mkfs_ext4="mkfs.ext4"
fi
source "functions.bash"
dev="$2"
# unmounting partitions on target device
mounted_partition=$(mount | grep $dev | cut -f1 -d " ");
if [ ${#mounted_partition} -gt 0 ]; then
echo "Warning: some partitions of $hd_dev are mounted:"
for p in ${mounted_partition};
do
echo " unmounting $p";
umount $p;
done
fi
if [ -z "$cfg_partitions" ]; then
cfg_partitions=( \
'root' 'root0' '512B' '532MiB'
'data' 'data0' '532MiB' '-1'
)
fi
cmd_loop_attach()
{
export dev="$1"
export dev_loop="$(losetup -f)"
nfo1 "Attaching $dev to $dev_loop"
losetup -v "$dev_loop" "$dev"
if [ $? -ne 0 ]; then
unset dev_loop
die
fi
}
cmd_loop_detach()
{
nfo1 "Detaching $dev_loop"
sync && sync && sync && sync
blockdev --flushbufs "$dev_loop"
losetup -v -d "$dev_loop"
sync && sync && sync && sync
unset dev_loop
}
nfo1 "Obliterating partition table"
cmd_loop_attach "$dev"
dd if=/dev/zero of="$dev_loop" bs=1 count=1M count=32 > /dev/null 2>&1 || die
losetup -a
cmd_loop_detach
losetup -a
nfo1 "Creating empty partition table"
cmd_loop_attach "$dev"
cmd_parted "$dev_loop" mklabel msdos || die
cmd_loop_detach
part_nr=1
xboot_system_id=0
for ((i = 0; i < ${#cfg_partitions[@]}; i += 4)); do
cmd_loop_attach "$dev"
part_type="${cfg_partitions[$i+0]}"
part_label="${cfg_partitions[$i+1]}"
part_start="${cfg_partitions[$i+2]}"
part_end="${cfg_partitions[$i+3]}"
part_dev="${dev_loop}p${part_nr}"
case $part_type in
'x-boot')
create_part_xboot
xboot_system_id=1
;;
'rpi-boot')
create_part_rpiboot
xboot_system_id=1
;;
'root')
create_part_root
;;
'data')
create_part_data
;;
*)
echo "ERROR: unknown partition type '$part_type'"
die
;;
esac
cmd_loop_detach
let part_nr++
done
if [ -f "$cfg_sys_family/rootfs/boot/extlinux/mbr.bin" ]; then
nfo1 "Installing bootloader in MBR..."
cmd_loop_attach "$dev"
dd if="$cfg_sys_family/rootfs/boot/extlinux/mbr.bin" of="$dev_loop" > /dev/null 2>&1
cmd_loop_detach
fi
nfo1 "Synchronizing caches"
sync && sync && sync && sync && sync && sync
# Make sure the partition has 0x0e type.
if [ $xboot_system_id -eq 1 ]; then
cmd_loop_attach "$dev"
echo -en "t\n1\ne\nw\n" | $cmd_fdisk "$dev_loop" > /dev/null 2>&1
cmd_loop_detach
fi