-
Notifications
You must be signed in to change notification settings - Fork 10
/
mkusb.sh
executable file
·390 lines (309 loc) · 7.2 KB
/
mkusb.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/sh -e
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
cat >&2 <<EOF
$0 [distros] [device] [efi partition] [live partition]
See mkusb(1) manual page for more info
EOF
exit
fi
if [ "$(id -u)" -ne 0 ]; then
echo "error: script must be run as root"
exit 1
fi
# whether to partition with fat or ext2
fat=1
# get system configuration
if [ -n "$MKUSB_GRUB" ]; then
sysgrub="$MKUSB_GRUB"
elif which grub2-install >/dev/null 2>/dev/null; then
sysgrub=grub2
else
sysgrub=grub
fi
if [ -n "$MKUSB_GRUB_EFI" ]; then
sysgrubefi="$MKUSB_GRUB_EFI"
else
sysgrubefi="$sysgrub"-install
fi
if [ -n "$MKUSB_GRUB_PC" ]; then
sysgrubpc="$MKUSB_GRUB_PC"
else
sysgrubpc="$sysgrub"-install
fi
if [ -n "$MKUSB_MEMDISK" ]; then
sysmemdisk="$MKUSB_MEMDISK"
elif [ -e /usr/lib/syslinux/bios/memdisk ]; then
sysmemdisk="/usr/lib/syslinux/bios/memdisk"
else
sysmemdisk="/usr/share/syslinux/memdisk"
fi
# get distro configuration
distros="$1"
if [ -z "$distros" ]; then
distros="./distros"
fi
# get device
dev="$2"
if [ -z "$dev" ]; then
echo "getting device..."
devs="$(find /dev/disk/by-path | grep -- '-usb-' | grep -v -- '-part[0-9]*$' || true)"
if [ -z "$devs" ]; then
echo "error: no usb device found"
exit 2
fi
devs="$(readlink -f $devs)"
dialogdevs=""
dialogmodel=""
for dialogdev in $devs; do
dialogmodel="$(lsblk -ndo model "$dialogdev")"
dialogdevs="$dialogdevs $dialogdev '$dialogmodel' off"
done
unset dialogdev
unset dialogmodel
while [ -z "$dev" ]; do
dev="$(eval "dialog --stdout --radiolist 'select usb device' 12 40 5 $dialogdevs")"
if [ "$?" -ne "0" ]; then
exit
fi
done
unset dialogdevs
unset devs
fi
# store label
label="LIVE"
# get partition
efipart="$3"
livepart="$4"
if [ -z "$efipart" ] || [ -z "$livepart" ]; then
# sector sizes
secstart=2048
secefi=1048576
# partition device
echo "partitioning..."
dd if=/dev/zero of="$dev" count=$secstart >/dev/null 2>/dev/null
sfdisk "$dev" >/dev/null <<EOF
label: dos
device: $dev
unit: sectors
${dev}1 : start=$secstart, size=$(expr "$(blockdev --getsz "$dev")" - $secstart - $secefi), type=b
${dev}2 : size=$secefi, type=ef
EOF
blockdev --rereadpt "$dev"
if [ -b "$dev"p1 ]; then
livepart="$dev"p1
efipart="$dev"p2
else
livepart="$dev"1
efipart="$dev"2
fi
unset secstart
unset secefi
# format device
echo "formatting..."
if [ $fat -ne 0 ]; then
mkfs.fat -F 32 -n "$label" "$livepart" >/dev/null
else
mkfs.ext2 -L "$label" "$livepart" >/dev/null
fi
mkfs.fat -F 32 -n ESP "$efipart" >/dev/null
formatted=1
else
label="$(lsblk -ndo label "$livepart")"
fi
# mount device
echo "mounting..."
livemnt="$(mktemp -d)"
efimnt="$(mktemp -d)"
mount "$livepart" "$livemnt"
mount "$efipart" "$efimnt"
trap "(set +e; umount '$livemnt'; rmdir '$livemnt'; umount '$efimnt'; rmdir '$efimnt'; true) >/dev/null 2>/dev/null" EXIT
unset efipart
# install grub
echo "installing grub..."
"$sysgrubefi" --target=x86_64-efi --boot-directory="$efimnt" --efi-directory="$efimnt" --removable >/dev/null
"$sysgrubpc" --target=i386-pc --boot-directory="$efimnt" "$dev" >/dev/null
unset dev
# copy config and distros
echo "copying distros..."
# create empty grub configuration
printf "" >"$livemnt"/grub.cfg
label() {
if [ -n "$formatted" ]; then
label="$1"
umount "$livepart"
if [ $fat -ne 0 ]; then
fatlabel "$livepart" "$label" >/dev/null
else
e2label "$livepart" "$label" >/dev/null
fi
mount "$livepart" "$livemnt"
fi
}
iso() {
printf " %s\n" "$1"
case "$2" in
/*)
src="$2"
;;
*)
if [ -f "$(dirname "$(readlink -f "$distros")")/$2" ]; then
src="$(dirname "$(readlink -f "$distros")")/$2"
else
src="$2"
fi
;;
esac
cat >>"$livemnt"/grub.cfg <<EOF
menuentry '$1' {
set filename=/$(basename "$src")
set label=$label
loopback iso \$filename
EOF
IFS='#' read -r kernel64 kernel32 <<EOF
$(printf "%s" "$3" | sed -e 's/[[:space:]]*--[[:space:]]*/#/g')
EOF
IFS='#' read -r initrd64 initrd32 <<EOF
$(printf "%s" "$4" | sed -e 's/[[:space:]]*--[[:space:]]*/#/g')
EOF
IFS='#' read -r param64 param32 <<EOF
$(printf "%s" "$5" | sed -e 's/%\([^%]\+\)%/$\1/g' | sed -e 's/[[:space:]]*--[[:space:]]*/#/g')
EOF
if [ -n "$kernel32" ]; then
if [ -z "$initrd32" ]; then
initrd32="$initrd64"
fi
if [ -z "$param32" ]; then
param32="$param64"
fi
cat >>"$livemnt"/grub.cfg <<EOF
if cpuid -l; then
linux (iso)$kernel64 $param64
initrd$(for initrd in $initrd64; do printf " (iso)%s" "$initrd"; done)
else
linux (iso)$kernel32 $param32
initrd$(for initrd in $initrd32; do printf " (iso)%s" "$initrd"; done)
fi
EOF
else
cat >>"$livemnt"/grub.cfg <<EOF
linux (iso)$kernel64 $param64
initrd$(for initrd in $initrd64; do printf " (iso)%s" "$initrd"; done)
EOF
fi
unset param64
unset param32
unset initrd64
unset initrd32
unset kernel64
unset kernel32
cat >>"$livemnt"/grub.cfg <<EOF
}
EOF
iso="$livemnt/$(basename "$src")"
{ [ ! -e "$iso" ] || [ -n "$(find -L "$src" -prune -newer "$iso" -exec echo . ';')" ]; } && cp "$src" "$iso"
unset iso
}
freedos() {
printf " %s\n" "$1"
case "$2" in
/*)
src="$2"
;;
*)
if [ -f "$(dirname "$(readlink -f "$distros")")/$2" ]; then
src="$(dirname "$(readlink -f "$distros")")/$2"
else
src="$2"
fi
;;
esac
cat >>"$livemnt"/grub.cfg <<EOF
if [ \${grub_platform} == "pc" ]; then
menuentry '$1' {
insmod progress
set filename=/$(basename "$src")
set label=$label
linux16 /memdisk
initrd16 /\$filename
}
fi
EOF
memdisk="$livemnt/memdisk"
{ [ ! -e "$memdisk" ] || [ -n "$(find -L "$sysmemdisk" -prune -newer "$memdisk" -exec echo . ';')" ]; } && cp "$sysmemdisk" "$memdisk"
img="$livemnt/$(basename "$src")"
{ [ ! -e "$img" ] || [ -n "$(find -L "$src" -prune -newer "$img" -exec echo . ';')" ]; } && cp "$src" "$img"
unset memdisk
unset img
}
refind() {
printf " %s\n" "$1"
case "$2" in
/*)
src="$2"
;;
*)
if [ -f "$(dirname "$(readlink -f "$distros")")/$2" ]; then
src="$(dirname "$(readlink -f "$distros")")/$2"
else
src="$2"
fi
;;
esac
refindmnt="$(mktemp -d)"
mount -o ro "$src" "$refindmnt"
cat >>"$livemnt"/grub.cfg <<EOF
if [ \${grub_platform} == "efi" ]; then
menuentry '$1' {
search --no-floppy --file --set=root /EFI/refind/bootx64.efi
if cpuid -l; then
chainloader /EFI/refind/bootx64.efi
else
chainloader /EFI/refind/bootia32.efi
fi
}
fi
EOF
cp -r "$refindmnt"/EFI/boot "$efimnt"/EFI/refind
cp -r "$refindmnt"/EFI/tools "$efimnt"/EFI/tools
rm -rf "$livemnt"/refind
cp -r "$refindmnt" "$livemnt"/refind
rm -rf "$livemnt"/refind/EFI
umount "$refindmnt"
unset refindmnt
}
. "$(readlink -f "$distros")"
# configure grub
echo "configuring grub..."
cat >"$efimnt"/"$sysgrub"/grub.cfg <<EOF
if loadfont /$sysgrub/fonts/unicode.pf2; then
set gfxmode=auto
if [ \${grub_platform} == "efi" ]; then
insmod efi_gop
insmod efi_uga
else
insmod all_video
fi
insmod gfxterm
terminal_output gfxterm
fi
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
set gfxpayload=keep
search --no-floppy --label --set=root $label
source /grub.cfg
EOF
unset livepart
unset label
unset distros
# sync
echo "syncing..."
sync
# unmount
echo "unmounting..."
umount "$efimnt"
umount "$livemnt"
rmdir "$efimnt"
rmdir "$livemnt"
unset efimnt
unset livemnt
echo "done"