forked from corpnewt/MountEFI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MountEFI.command
executable file
·345 lines (326 loc) · 11.9 KB
/
MountEFI.command
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python
# 0.0.0
from Scripts import *
import os, tempfile, datetime, shutil, time, plistlib, json, sys, argparse
class MountEFI:
def __init__(self, **kwargs):
self.r = run.Run()
self.d = disk.Disk()
self.dl = downloader.Downloader()
self.u = utils.Utils("MountEFI")
self.re = reveal.Reveal()
# Get the tools we need
self.script_folder = "Scripts"
self.update_url = "https://raw.githubusercontent.com/corpnewt/MountEFIv2/master/MountEFI.command"
self.settings_file = kwargs.get("settings", None)
cwd = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__)))
if self.settings_file and os.path.exists(self.settings_file):
self.settings = json.load(open(self.settings_file))
else:
self.settings = {
# Default settings here
"default_disk" : None,
"after_mount" : None,
"full_layout" : False,
"skip_countdown" : False,
}
os.chdir(cwd)
self.full = self.settings.get("full_layout", False)
def check_update(self):
# Checks against https://raw.githubusercontent.com/corpnewt/MountEFIv2/master/MountEFI.command to see if we need to update
self.u.head("Checking for Updates")
print(" ")
with open(os.path.realpath(__file__), "r") as f:
# Our version should always be the second line
version = get_version(f.read())
print(version)
try:
new_text = _get_string(url)
new_version = get_version(new_text)
except:
# Not valid json data
print("Error checking for updates (network issue)")
return
if version == new_version:
# The same - return
print("v{} is already current.".format(version))
return
# Split the version number
try:
v = version.split(".")
cv = new_version.split(".")
except:
# not formatted right - bail
print("Error checking for updates (version string malformed)")
return
if not need_update(cv, v):
print("v{} is already current.".format(version))
return
# Update
with open(os.path.realpath(__file__), "w") as f:
f.write(new_text)
# chmod +x, then restart
run_command(["chmod", "+x", __file__])
os.execv(__file__, sys.argv)
def flush_settings(self):
if self.settings_file:
cwd = os.getcwd()
os.chdir(os.path.dirname(os.path.realpath(__file__)))
json.dump(self.settings, open(self.settings_file, "w"))
os.chdir(cwd)
def after_mount(self):
if self.settings.get("resize_window"): self.u.resize(80, 24)
self.u.head("After Mount Action")
print(" ")
print("1. Return to Menu")
print("2. Quit")
print("3. Open EFI and Return to Menu")
print("4. Open EFI and Quit")
if not self.settings.get("skip_countdown", False):
print("5. Skip After-Mount Countdown")
else:
print("5. Use After-Mount Countdown")
print(" ")
print("M. Main Menu")
print("Q. Quit")
print(" ")
menu = self.u.grab("Please pick an option: ")
if not len(menu):
self.after_mount()
return
menu = menu.lower()
if menu in ["1","2","3","4"]:
self.settings["after_mount"] = [
"Return to Menu",
"Quit",
"Reveal and Return to Menu",
"Reveal and Quit"
][int(menu)-1]
self.flush_settings()
return
elif menu == "5":
cd = self.settings.get("skip_countdown", False)
cd ^= True
self.settings["skip_countdown"] = cd
self.flush_settings()
self.after_mount()
return
elif menu == "m":
return
elif menu == "q":
self.u.custom_quit()
self.after_mount()
def default_disk(self):
self.d.update()
clover = bdmesg.get_bootloader_uuid()
if self.settings.get("resize_window"): self.u.resize(80, 24)
self.u.head("Select Default Disk")
print(" ")
print("1. None")
print("2. Boot Disk")
if clover:
print("3. Booted EFI (Clover/OC)")
print(" ")
print("M. Main Menu")
print("Q. Quit")
print(" ")
menu = self.u.grab("Please pick a default disk: ")
if not len(menu):
self.default_disk()
menu = menu.lower()
if menu in ["1","2"]:
self.settings["default_disk"] = [None, "boot"][int(menu)-1]
self.flush_settings()
return
elif menu == "3" and clover:
self.settings["default_disk"] = "clover"
self.flush_settings()
return
elif menu == "m":
return
elif menu == "q":
self.u.custom_quit()
self.default_disk()
def get_efi(self):
self.d.update()
clover = bdmesg.get_bootloader_uuid()
i = 0
disk_string = ""
if not self.full:
clover_disk = self.d.get_parent(clover)
mounts = self.d.get_mounted_volume_dicts()
for d in mounts:
i += 1
disk_string += "{}. {} ({})".format(i, d["name"], d["identifier"])
if self.d.get_parent(d["identifier"]) == clover_disk:
# if d["disk_uuid"] == clover:
disk_string += " *"
disk_string += "\n"
else:
mounts = self.d.get_disks_and_partitions_dict()
disks = list(mounts)
for d in disks:
i += 1
disk_string+= "{}. {}:\n".format(i, d)
parts = mounts[d]["partitions"]
part_list = []
for p in parts:
p_text = " - {} ({})".format(p["name"], p["identifier"])
if p["disk_uuid"] == clover:
# Got Clover
p_text += " *"
part_list.append(p_text)
if len(part_list):
disk_string += "\n".join(part_list) + "\n"
height = len(disk_string.split("\n"))+16
if height < 24:
height = 24
if self.settings.get("resize_window"): self.u.resize(80, height)
self.u.head()
print(" ")
print(disk_string)
if not self.full:
print("S. Switch to Full Output")
else:
print("S. Switch to Slim Output")
lay = self.settings.get("full_layout", False)
l_str = "Slim"
if lay:
l_str = "Full"
print("L. Set As Default Layout (Current: {})".format(l_str))
print("B. Mount the Boot Drive's EFI")
if clover:
print("C. Mount the Booted EFI (Clover/OC)")
print("")
dd = self.settings.get("default_disk", None)
if dd == "clover":
dd = clover
elif dd == "boot":
dd = "/"
di = self.d.get_identifier(dd)
if di:
print("D. Pick Default Disk ({} - {})".format(self.d.get_volume_name(di), di))
else:
print("D. Pick Default Disk (None Set)")
am = self.settings.get("after_mount", None)
if not am:
am = "Return to Menu"
print("M. After Mounting: "+am)
print("R. Toggle Window Resizing (Currently {})".format("Enabled" if self.settings.get("resize_window",True) else "Disabled"))
print("Q. Quit")
print(" ")
print("(* denotes the booted EFI (Clover/OC))")
menu = self.u.grab("Pick the drive containing your EFI: ")
if not len(menu):
if not di:
return self.get_efi()
return self.d.get_efi(di)
menu = menu.lower()
if menu == "q":
if self.settings.get("resize_window"): self.u.resize(80,24)
self.u.custom_quit()
elif menu == "s":
self.full ^= True
return self.get_efi()
elif menu == "b":
return self.d.get_efi("/")
elif menu == "c" and clover:
return self.d.get_efi(clover)
elif menu == "m":
self.after_mount()
return
elif menu == "d":
self.default_disk()
return
elif menu == "l":
self.settings["full_layout"] = self.full
self.flush_settings()
return
elif menu == "r":
self.settings["resize_window"] = not self.settings.get("resize_window",True)
self.flush_settings()
return
try: disk = mounts[int(menu)-1]["identifier"] if isinstance(mounts, list) else list(mounts)[int(menu)-1]
except: disk = menu
iden = self.d.get_identifier(disk)
name = self.d.get_volume_name(disk)
if not iden:
self.u.grab("Invalid disk!", timeout=3)
return self.get_efi()
# Valid disk!
efi = self.d.get_efi(iden)
if not efi:
self.u.head("No EFI Partition")
print("")
print("There is no EFI partition associated with {}!".format(iden))
print("")
self.u.grab("Press returning in 3 seconds...", timeout=3)
return self.get_efi()
return efi
def main(self):
while True:
efi = self.get_efi()
if not efi:
# Got nothing back
continue
# Resize and then mount the EFI partition
if self.settings.get("resize_window"): self.u.resize(80, 24)
self.u.head("Mounting {}".format(efi))
print(" ")
out = self.d.mount_partition(efi)
if out[2] == 0:
print(out[0])
else:
print(out[1])
# Check our settings
am = self.settings.get("after_mount", None)
if not am:
continue
if "reveal" in am.lower():
# Reveal
mp = self.d.get_mount_point(efi)
if mp:
self.r.run({"args":["open", mp]})
# Hang out for a couple seconds
if not self.settings.get("skip_countdown", False):
self.u.grab("", timeout=3)
if "quit" in am.lower():
# Quit
self.u.custom_quit()
def quiet_mount(self, disk_list, unmount=False):
ret = 0
for disk in disk_list:
ident = self.d.get_identifier(disk)
if not ident:
continue
efi = self.d.get_efi(ident)
if not efi:
continue
if unmount:
out = self.d.unmount_partition(efi)
else:
out = self.d.mount_partition(efi)
if not out[2] == 0:
ret = out[2]
exit(ret)
if __name__ == '__main__':
# Setup the cli args
parser = argparse.ArgumentParser(prog="MountEFI.command", description="MountEFI - an EFI Mounting Utility by CorpNewt")
parser.add_argument("-u", "--unmount", help="unmount instead of mount the passed EFIs", action="store_true")
parser.add_argument("-p", "--print-efi", help="prints the disk#s# of the EFI attached to the passed var")
parser.add_argument("disks",nargs="*")
args = parser.parse_args()
m = MountEFI(settings="./Scripts/settings.json")
# Gather defaults
unmount = False
if args.unmount:
unmount = True
if args.print_efi:
print("{}".format(m.d.get_efi(args.print_efi)))
# Check for args
if len(args.disks):
# We got command line args!
m.quiet_mount(args.disks, unmount)
elif not args.print_efi:
m.main()