-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathiso-to-usb
executable file
·154 lines (120 loc) · 2.98 KB
/
iso-to-usb
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
#!/bin/bash
set -euo pipefail
# See also https://github.com/jsamr/bootiso
run() {
echo >&2 "+ $*"
"$@"
}
cleanup() {
if [ -d "${loop_path-}" ]; then
run umount "$loop_path"
run rmdir "$loop_path"
fi
if [ -d "${tmpdir-}" ]; then
run rmdir "$tmpdir"
fi
}
usage() {
cat >&2 <<EOM
usage: $(basename "$0") [options] ISO_FILE MOUNTED_USB_PATH
Burn ISO_FILE to a USB drive mounted at MOUNTED_USB_PATH.
Options:
-h, --help Print this help message
--delete Delete unknown files from MOUNTED_USB_PATH
--delete-exclude DIR Delete all files from MOUNTED_USB_PATH except DIR
EOM
}
check_dependencies() {
for dep in findmnt install-mbr mount mv rmdir rsync sync syslinux umount
do
if ! which "$dep" >/dev/null; then
echo >&2 "Failed to find dependency on PATH: $dep"
return 1
fi
done
}
mount_iso() {
local iso_file mount_path
iso_file="$1"
mount_path="$2"
mkdir -v "$mount_path"
run mount -o loop "$iso_file" "$mount_path"
}
device_for_mount_point() {
run findmnt -n --output=source "$1"
}
copy_files() {
local rsync_opts
rsync_opts=("-v")
if [ -n "$delete" ]; then
rsync_opts+=("--del")
fi
if [ -n "$delete_exclude" ]; then
rsync_opts+=("--exclude" "$delete_exclude")
fi
run rsync "${rsync_opts[@]}" -c -rpt "$loop_path/" "$mounted_usb_path/"
}
setup_boot() {
run mv -vT "$mounted_usb_path/isolinux" "$mounted_usb_path/syslinux"
run mv -vT "$mounted_usb_path/syslinux/"{iso,sys}linux.cfg
run syslinux -s "$usb_partition"
}
check_partition_bootable_and_install_mbr() {
local disk partition
partition="$1"
disk="/dev/$(run lsblk -no pkname "$partition")"
run install-mbr "$disk"
if run parted -m -s "$disk" print | grep boot; then
echo "Looks like disk is bootable"
else
echo "Doesn't look like partition $partition is bootable!"
echo "Please make sure that it has the boot partition flag"
fi
}
delete=
delete_exclude=
while [ $# -gt 0 ] && [[ $1 == -* ]]; do
case "$1" in
--delete)
delete=1
;;
--delete-exclude)
delete=1
delete_exclude="$2"
shift
;;
-h|--help)
usage
exit
;;
*)
usage
echo >&2 "Unknown option $1"
exit 1
;;
esac
shift
done
if [ $# -ne 2 ]; then
usage
exit 1
fi
iso_file="$1"
mounted_usb_path="$2"
if [ "$(id -u)" -ne 0 ]; then
echo >&2 "error: This script must be run as root"
exit 1
fi
check_dependencies
usb_partition="$(device_for_mount_point "$mounted_usb_path")"
trap cleanup EXIT
tmpdir="$(mktemp -d)"
loop_path="$tmpdir/iso"
mount_iso "$iso_file" "$loop_path"
copy_files
setup_boot
check_partition_bootable_and_install_mbr "$usb_partition"
trap - EXIT
cleanup
run sync -f "$mounted_usb_path"
echo "All done!"