-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlinux-usb
executable file
·73 lines (55 loc) · 1.87 KB
/
linux-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
#!/bin/bash
IFS=$'\n'
# Helpers
function error {
echo "${1}" >&2
exit 1
}
function usage {
echo "
Create bootable Linux USB sticks from ISOs on macOS.
Usage:
${program} [options] <iso>
Options:
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
# Checks
if [[ "${1}" =~ ^(-h|--help)$ ]]
then
usage
exit 0
fi
readonly linux_iso="${1}"
if [[ -z "${linux_iso}" || "$(file --mime-type --brief "${linux_iso}")" != 'application/x-iso9660-image' ]]
then
usage
exit 1
fi
# Get disk to save to
readonly external_disks=($(diskutil list | grep '(external, physical)' | awk '{ print $1 }'))
[[ "${#external_disks[@]}" -lt 1 ]] && error 'Found no external disks. Connect one and run the script again.'
disks_with_info=()
disk_order='0'
for disk in "${external_disks[@]}"; do
disk_order="$(bc <<< "${disk_order} + 1")"
disk_size="$(diskutil info "${disk}" | grep 'Disk Size' | awk '{ print $3" "$4 }')"
disks_with_info+=("[${disk_order}] ${disk} ${disk_size}")
done
while [[ -z "${disk_to_write_number}" ]] || [[ "${disk_to_write_number}" -lt 1 ]] || [[ "${disk_to_write_number}" -gt "${#disks_with_info[@]}" ]]; do
echo 'Pick a disk do write to:'
printf '%s\n' "${disks_with_info[@]}"
echo
read -r -p '> ' disk_to_write_number
done
readonly disk_to_write="$(sed "${disk_to_write_number}q;d" <<< "${disks_with_info[@]}" | awk '{ print $2 }')"
# Convert the Linux iso
readonly linux_dmg="$(mktemp).dmg"
hdiutil convert -quiet "${linux_iso}" -format UDRW -o "${linux_dmg}"
# Write to the disk
diskutil unmountDisk "${disk_to_write}"
echo 'Linux will now be saved to your USB flash drive. This should take a while. You may need to enter your password (for the write permissions).'
sudo dd if="${linux_dmg}" of="${disk_to_write}" status=progress
# eject disk
diskutil eject "${disk_to_write}"
echo 'Done. You now have a bootable Linux USB flash drive.'