-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacboot
More file actions
executable file
·158 lines (133 loc) · 4.33 KB
/
Copy pathmacboot
File metadata and controls
executable file
·158 lines (133 loc) · 4.33 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
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
#!/bin/bash
LOG_FILE="macos-creator-$(date +%Y%m%d-%H%M%S).log"
exec > >(tee -a "$LOG_FILE") 2>&1
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
msg_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
msg_note() {
echo -e "${YELLOW}[NOTE]${NC} $1"
}
msg_fail() {
echo -e "${RED}[FAIL]${NC} $1"
}
cleanup() {
echo ""
msg_note "Running cleanup..."
if [ -n "$TEMP_IMG_FILE" ] && [ -f "$TEMP_IMG_FILE" ]; then
rm -f "$TEMP_IMG_FILE"
msg_note "Removed temporary image file: $TEMP_IMG_FILE"
fi
msg_note "Script execution finished at $(date)."
msg_note "Full log available at: ${LOG_FILE}"
}
usage() {
echo "Usage: $0 [source_image] [target_device]"
echo " Example: sudo $0 /path/to/ventura.dmg /dev/sdb"
echo "If arguments are not provided, the script will run in interactive mode."
exit 1
}
trap cleanup EXIT
echo "--- Script execution started at $(date) ---"
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
usage
fi
if [ "$EUID" -ne 0 ]; then
msg_fail "This script must be run as root. Please use sudo."
exit 1
fi
msg_note "Checking for required dependencies..."
dependencies=("dmg2img" "util-linux")
missing_deps=()
for dep in "${dependencies[@]}"; do
if ! pacman -Q "$dep" &>/dev/null; then
missing_deps+=("$dep")
fi
done
if [ ${#missing_deps[@]} -gt 0 ]; then
msg_note "Missing dependencies: ${missing_deps[*]}"
read -p "Do you want to install them now? (y/n): " confirm
if [[ "$confirm" == [yY] || "$confirm" == [yY][eE][sS] ]]; then
pacman -Syu --noconfirm "${missing_deps[@]}"
if [ $? -ne 0 ]; then
msg_fail "Failed to install dependencies. Please install them manually and try again."
exit 1
fi
msg_success "Dependencies installed successfully."
else
msg_fail "Cannot proceed without dependencies. Exiting."
exit 1
fi
fi
if [ -n "$1" ]; then
SOURCE_IMAGE="$1"
msg_note "Source image provided via argument: $SOURCE_IMAGE"
else
read -p "Enter the full path to the macOS DMG or ISO file: " SOURCE_IMAGE
fi
if [ ! -f "$SOURCE_IMAGE" ]; then
msg_fail "Source file not found: $SOURCE_IMAGE"
exit 1
fi
file_ext="${SOURCE_IMAGE##*.}"
if [[ "$file_ext" != "dmg" && "$file_ext" != "iso" ]]; then
msg_fail "Invalid file type selected: .$file_ext. Please use a .dmg or .iso file."
exit 1
fi
if [ -n "$2" ]; then
USB_DEVICE="$2"
msg_note "Target device provided via argument: $USB_DEVICE"
else
msg_note "Available block devices:"
lsblk -d -n -p -o NAME,SIZE,MODEL
echo ""
read -p "Enter the target USB device (e.g., /dev/sdb): " USB_DEVICE
fi
if [ ! -b "$USB_DEVICE" ]; then
msg_fail "The specified device does not exist or is not a block device: $USB_DEVICE"
exit 1
fi
echo ""
msg_note "WARNING: All data on ${USB_DEVICE} will be permanently erased."
read -p "Are you absolutely sure you want to continue? (y/n): " final_confirm
if [[ "$final_confirm" != [yY] && "$final_confirm" != [yY][eE][sS] ]]; then
msg_fail "Operation cancelled by the user."
exit 1
fi
echo ""
msg_note "Unmounting all partitions on ${USB_DEVICE}..."
umount ${USB_DEVICE}* &>/dev/null
sleep 1
SOURCE_FOR_DD="$SOURCE_IMAGE"
if [[ "$file_ext" == "dmg" ]]; then
msg_note "Detected DMG file. Converting to IMG format (this may take a while)..."
TEMP_IMG_FILE=$(mktemp --suffix=.img)
dmg2img -p -i "$SOURCE_IMAGE" -o "$TEMP_IMG_FILE"
if [ $? -ne 0 ]; then
msg_fail "Failed to convert DMG file. Make sure it's a valid, uncorrupted image."
exit 1
fi
SOURCE_FOR_DD="$TEMP_IMG_FILE"
else
msg_note "Detected ISO file. Conversion will be skipped."
fi
msg_note "Writing image to ${USB_DEVICE}. This is the longest step. Please be patient..."
dd if="$SOURCE_FOR_DD" of="$USB_DEVICE" bs=4M status=progress
if [ $? -ne 0 ]; then
msg_fail "Failed to write image to the USB device. Check for I/O errors or if the device was disconnected."
exit 1
fi
msg_note "Syncing filesystem to ensure all data is written to the device..."
sync
msg_note "Ejecting device: ${USB_DEVICE}..."
eject "$USB_DEVICE"
if [ $? -ne 0 ]; then
msg_fail "Could not eject the device. Please do not remove it until all activity lights are off."
exit 1
fi
echo ""
msg_success "Process complete. The device has been ejected and is now safe to remove."
exit 0