-
Notifications
You must be signed in to change notification settings - Fork 35
/
macos_usb.sh
executable file
·171 lines (147 loc) · 4.07 KB
/
macos_usb.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
#!/bin/bash
# Create a bootable macOS flash drive on Linux and macOS
# (c) notthebee, corpnewt
# url: https://github.com/notthebee/macos_usb
# The script will terminate if any subsequent command fails
set -e
function checkdep {
# check if running on Linux or macOS
case "$OSTYPE" in
"linux-gnu"|"darwin"*)
;;
*)
echo "This script will only run on Linux or macOS!"
exit 1
;;
esac
# check the Python version
if ! $python3 --version &> /dev/null; then
PYTHON=python3
elif [ -n $(python --version | grep 3.) ]; then
PYTHON=python
else
echo "Please install python3"
exit 1
fi
# check for dependencies
for n in git 7z; do
if ! $n --help &> /dev/null; then
echo "Please install $n"
exit 1
fi
done
}
function version {
printf '
Press a key to select the macOS version
Press Enter to download latest release (default)
[H]igh Sierra (10.13)
[M]ojave (10.14)
[C]atalina (10.15)
[B]ig Sur (11)
M[o]nterey (12)
'
read -n 1 -p "[H/M/C/B/O] " macOS_release_name 2>/dev/tty
echo ""
}
function gibmacos {
echo "Fetching latest gibMacOS by corpnewt (Modified to work)"
rm -rf gibMacOS && git clone "https://github.com/corpnewt/gibMacOS" &> /dev/null
case $macOS_release_name in
[Hh])
$PYTHON gibMacOS/gibMacOS.py -r -v 10.13
;;
[Mm])
$PYTHON gibMacOS/gibMacOS.py -r -v 10.14
;;
[Cc])
$PYTHON gibMacOS/gibMacOS.py -r -v 10.15
;;
[Bb])
$PYTHON gibMacOS/gibMacOS.py -r -v 11
;;
[Oo])
$PYTHON gibMacOS/gibMacOS.py -r -v 11
;;
*)
$PYTHON gibMacOS/gibMacOS.py -r -l
;;
esac
}
function unpackhfs {
# Store the name of the file in a variable
hfsfile="$(find . -type f -name '4.hfs' -o -name '5.hfs')"
if [ -z "$hfsfile" ]; then
echo "Unpacking the installation files"
mv gibMacOS/macOS\ Downloads/publicrelease/*/*.pkg .
7z e -txar *.pkg *.dmg; 7z e *.dmg */Base*; 7z e -tdmg Base*.dmg *.hfs
else
echo "Already unpacked"
fi
}
function partition {
# if we don't have the HFS file at this point,
# let's not bother partitioning the drive
hfsfile="$(find . -type f -name '4.hfs' -o -name '5.hfs')"
# assigning the variable twice is the only way i was able to make it work
# if you know a better way, please let me know
if [ -z "$hfsfile" ]; then
echo "ERROR! HFS image not found"
exit 1
fi
# Execute diskutil list if running on macOS
lsblk 2>/dev/null || diskutil list
printf "\nEnter the path to your flash drive (e.g. /dev/sdb or /dev/disk3)"
printf "\nDOUBLE CHECK THE EXACT PATH WITH lsblk or diskutil\n"
read flashdrive 2>/dev/tty
# Check if user specified a USB device
if [[ "$OSTYPE" == "linux-gnu" ]]; then
usb="$(readlink /sys/block/$(echo ${flashdrive} | sed 's/^\/dev\///') | grep -o usb)"
elif [[ "$OSTYPE" == "darwin"* ]]; then
usb=$(diskutil list | grep -e ".*$flashdrive.*external")
fi
if [ -z "$usb" ]; then
echo "WARNING! ${flashdrive} is NOT a USB device"
echo "Are you sure you know what you're doing?"
read -p " [Y/N] " answer 2>/dev/tty
if [[ ! $answer =~ ^[Yy]$ ]]; then
echo "Abort"
exit 0
fi
fi
# Allow "Not mounted" errors etc.
set +e
# Destroy the GPT and partition the drive anew
if [[ "$OSTYPE" == "darwin"* ]]; then
# Using raw disk
flashdrive=$(echo ${flashdrive} | sed 's/disk/rdisk/')
sudo diskutil eraseDisk JHFS+ INSTALL ${flashdrive}
sudo diskutil umountDisk ${flashdrive}
else
sudo umount ${flashdrive}*
sudo sgdisk --zap-all ${flashdrive}
sudo sgdisk -n 0:0:+200MiB -t 0:0700 ${flashdrive}
sudo sgdisk -n 0:0:0 -t 0:af00 ${flashdrive}
sudo mkfs.vfat -F 32 -n "CLOVER" ${flashdrive}1
fi
}
function burn {
echo "Flashing the image"
case "$OSTYPE" in
"linux-gnu"*)
sudo dd if=${hfsfile} of=${flashdrive}2 bs=1M status=progress oflag=sync 2>/dev/null
;;
*)
# Catalina introduces a new version of dd that accepts capital M as a block size
sudo dd if=${hfsfile} of=${flashdrive}s2 bs=1m || sudo dd if=${hfsfile} of=${flashdrive}s2 bs=1M
;;
esac
}
checkdep
version
gibmacos
unpackhfs
partition
burn
echo "Success!"
echo "Don't forget to install the Clover bootloader in case you plan to use the media on a non-Apple computer!"