forked from vitorgalvao/tiny-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux-usb
More file actions
executable file
·51 lines (37 loc) · 1.57 KB
/
Copy pathlinux-usb
File metadata and controls
executable file
·51 lines (37 loc) · 1.57 KB
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
#!/bin/bash
IFS=$'\n'
function error {
echo "${1}" >&2
exit 1
}
# Get Linux disk image
read -rp 'Set the full path to your Linux iso.
> ' linux_iso
[[ -f "${linux_iso}" ]] || error 'There is no file at that path.'
# 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}" bs=1m
# eject disk
diskutil eject "${disk_to_write}"
echo 'Done. You now have a bootable Linux USB flash drive.'