forked from ulidtko/multibootusb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeUSB.sh
executable file
·316 lines (270 loc) · 7.55 KB
/
makeUSB.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
#!/bin/sh
# Description: Script to prepare multiboot USB drive
# show line number when execute by bash -x makeUSB.sh
[ "$BASH" ] && \
export PS4=' +\t $BASH_SOURCE:$LINENO: ${FUNCNAME[0]:+${FUNCNAME[0]}():}'
# Exit if there is an unbound variable or an error
set -o nounset
set -o errexit
# Defaults
scriptname=$(basename "$0")
hybrid=0
clone=0
eficonfig=0
interactive=0
data_part=2
data_fmt="vfat"
data_size=""
efi_mnt=""
data_mnt=""
data_subdir="boot"
repo_dir=""
tmp_dir="${TMPDIR-/tmp}"
# Show usage
showUsage() {
cat <<- EOF
Script to prepare multiboot USB drive
Usage: $scriptname [options] device [fs-type] [data-size]
device Device to modify (e.g. /dev/sdb)
fs-type Filesystem type for the data partition [ext3|ext4|vfat|ntfs]
data-size Data partition size (e.g. 5G)
-b, --hybrid Create a hybrid MBR
-c, --clone Clone Git repository on the device
-e, --efi Enable EFI compatibility
-i, --interactive Launch gdisk to create a hybrid MBR
-h, --help Display this message
-s, --subdirectory <NAME> Specify a data subdirectory (default: "boot")
EOF
}
# Clean up when exiting
cleanUp() {
# Change ownership of files
{ [ "$data_mnt" ] && \
chown -R "$normal_user" "${data_mnt}"/* 2>/dev/null; } \
|| true
# Unmount everything
umount -f "$efi_mnt" 2>/dev/null || true
umount -f "$data_mnt" 2>/dev/null || true
# Delete mountpoints
[ -d "$efi_mnt" ] && rmdir "$efi_mnt"
[ -d "$data_mnt" ] && rmdir "$data_mnt"
[ -d "$repo_dir" ] && rmdir "$repo_dir"
# Exit
exit "${1-0}"
}
# Make sure USB drive is not mounted
unmountUSB() {
umount -f "${1}"* 2>/dev/null || true
}
# Trap kill signals (SIGHUP, SIGINT, SIGTERM) to do some cleanup and exit
trap 'cleanUp' 1 2 15
# Show help before checking for root
[ "$#" -eq 0 ] && showUsage && exit 0
case "$1" in
-h|--help)
showUsage
exit 0
;;
esac
# Check for root
if [ "$(id -u)" -ne 0 ]; then
printf 'This script must be run as root. Using sudo...\n' "$scriptname" >&2
exec sudo -k -- /bin/sh "$0" "$@" || cleanUp 2
fi
# Get original user
normal_user="${SUDO_USER-$(who -m | awk '{print $1}')}"
# Check arguments
while [ "$#" -gt 0 ]; do
case "$1" in
-b|--hybrid)
hybrid=1
;;
-c|--clone)
clone=1
;;
-e|--efi)
eficonfig=1
data_part=3
;;
-i|--interactive)
interactive=1
;;
-s|--subdirectory)
shift && data_subdir="$1"
;;
/dev/*)
if [ -b "$1" ]; then
usb_dev="$1"
else
printf '%s: %s is not a valid device.\n' "$scriptname" "$1" >&2
cleanUp 1
fi
;;
[a-z]*)
data_fmt="$1"
;;
[0-9]*)
data_size="$1"
;;
*)
printf '%s: %s is not a valid argument.\n' "$scriptname" "$1" >&2
cleanUp 1
;;
esac
shift
done
# Check for required arguments
if [ ! "$usb_dev" ]; then
printf '%s: No device was provided.\n' "$scriptname" >&2
showUsage
cleanUp 1
fi
# Check for GRUB installation binary
grub_cmd=$(command -v grub2-install) \
|| grub_cmd=$(command -v grub-install) \
|| cleanUp 3
# Unmount device
unmountUSB "$usb_dev"
# Confirm the device
printf 'Are you sure you want to use %s? [y/N] ' "$usb_dev"
read -r answer1
case "$answer1" in
[yY][eE][sS]|[yY])
printf 'THIS WILL DELETE ALL DATA ON THE DEVICE. Are you sure? [y/N] '
read -r answer2
case $answer2 in
[yY][eE][sS]|[yY])
true
;;
*)
cleanUp 3
;;
esac
;;
*)
cleanUp 3
;;
esac
# Print all steps
set -o verbose
# Remove partitions
sgdisk --zap-all "$usb_dev"
# Create GUID Partition Table
sgdisk --mbrtogpt "$usb_dev" || cleanUp 10
# Create BIOS boot partition (1M)
sgdisk --new 1::+1M --typecode 1:ef02 \
--change-name 1:"BIOS boot partition" "$usb_dev" || cleanUp 10
# Create EFI System partition (50M)
[ "$eficonfig" -eq 1 ] && \
{ sgdisk --new 2::+50M --typecode 2:ef00 \
--change-name 2:"EFI System" "$usb_dev" || cleanUp 10; }
# Set data partition size
[ -z "$data_size" ] || \
data_size="+$data_size"
# Set data partition information
case "$data_fmt" in
ext2|ext3|ext4)
type_code="8300"
part_name="Linux filesystem"
;;
msdos|fat|vfat|ntfs)
type_code="0700"
part_name="Microsoft basic data"
;;
*)
printf '%s: %s is an invalid filesystem type.\n' "$scriptname" "$data_fmt" >&2
showUsage
cleanUp 1
;;
esac
# Create data partition
sgdisk --new ${data_part}::"${data_size}": --typecode ${data_part}:"$type_code" \
--change-name ${data_part}:"$part_name" "$usb_dev" || cleanUp 10
# Unmount device
unmountUSB "$usb_dev"
# Interactive configuration?
if [ "$interactive" -eq 1 ]; then
# Create hybrid MBR manually
# https://bit.ly/2z7HBrP
gdisk "$usb_dev"
elif [ "$hybrid" -eq 1 ]; then
# Create hybrid MBR
if [ "$eficonfig" -eq 1 ]; then
sgdisk --hybrid 1:2:3 "$usb_dev" || cleanUp 10
else
sgdisk --hybrid 1:2 "$usb_dev" || cleanUp 10
fi
fi
# Set bootable flag for data partion
sgdisk --attributes ${data_part}:set:2 "$usb_dev" || cleanUp 10
# Unmount device
unmountUSB "$usb_dev"
# Wipe BIOS boot partition
wipefs -af "${usb_dev}1" || true
# Format EFI System partition
if [ "$eficonfig" -eq 1 ]; then
wipefs -af "${usb_dev}2" || true
mkfs.vfat -v -F 32 "${usb_dev}2" || cleanUp 10
fi
# Wipe data partition
wipefs -af "${usb_dev}${data_part}" || true
# Format data partition
if [ "$data_fmt" = "ntfs" ]; then
# Use mkntfs quick format
mkfs -t "$data_fmt" -f "${usb_dev}${data_part}" || cleanUp 10
else
mkfs -t "$data_fmt" "${usb_dev}${data_part}" || cleanUp 10
fi
# Unmount device
unmountUSB "$usb_dev"
# Create temporary directories
efi_mnt=$(mktemp -p "$tmp_dir" -d efi.XXXX) || cleanUp 10
data_mnt=$(mktemp -p "$tmp_dir" -d data.XXXX) || cleanUp 10
repo_dir=$(mktemp -p "$tmp_dir" -d repo.XXXX) || cleanUp 10
# Mount EFI System partition
[ "$eficonfig" -eq 1 ] && \
{ mount "${usb_dev}2" "$efi_mnt" || cleanUp 10; }
# Mount data partition
mount "${usb_dev}${data_part}" "$data_mnt" || cleanUp 10
# Install GRUB for EFI
[ "$eficonfig" -eq 1 ] && \
{ $grub_cmd --target=x86_64-efi --efi-directory="$efi_mnt" \
--boot-directory="${data_mnt}/${data_subdir}" --removable --recheck \
|| cleanUp 10; }
# Install GRUB for BIOS
$grub_cmd --force --target=i386-pc \
--boot-directory="${data_mnt}/${data_subdir}" --recheck "$usb_dev" \
|| cleanUp 10
# Install fallback GRUB
$grub_cmd --force --target=i386-pc \
--boot-directory="${data_mnt}/${data_subdir}" --recheck "${usb_dev}${data_part}" \
|| true
# Create necessary directories
mkdir -p "${data_mnt}/${data_subdir}/isos" || cleanUp 10
if [ "$clone" -eq 1 ]; then
# Clone Git repository
(cd "$repo_dir" && \
git clone https://github.com/hackerncoder/multibootusb . && \
# Move all visible and hidden files and folders except '.' and '..'
for x in * .[!.]* ..?*; do if [ -e "$x" ]; then mv -- "$x" \
"${data_mnt}/${data_subdir}"/grub*/; fi; done) \
|| cleanUp 10
else
# Copy files
cp -R ./mbusb.* "${data_mnt}/${data_subdir}"/grub*/ \
|| cleanUp 10
# Copy example configuration for GRUB
cp ./grub.cfg.example "${data_mnt}/${data_subdir}"/grub*/ \
|| cleanUp 10
fi
# Rename example configuration
( cd "${data_mnt}/${data_subdir}"/grub*/ && cp grub.cfg.example grub.cfg ) \
|| cleanUp 10
# Download memdisk
syslinux_url='https://mirrors.edge.kernel.org/pub/linux/utils/boot/syslinux/syslinux-6.03.tar.gz'
{ wget -qO - "$syslinux_url" 2>/dev/null || curl -sL "$syslinux_url" 2>/dev/null; } \
| tar -xz -C "${data_mnt}/${data_subdir}"/grub*/ --no-same-owner --strip-components 3 \
'syslinux-6.03/bios/memdisk/memdisk' \
|| cleanUp 10
# Clean up and exit
cleanUp