1 | |
---|
2 | |
---|
3 | from engine.SystemLib import * |
---|
4 | import subprocess |
---|
5 | import os |
---|
6 | import shutil |
---|
7 | |
---|
8 | |
---|
9 | def ogNvramActiveEntry(*args): |
---|
10 | FUNCNAME = ogNvramActiveEntry.__name__ |
---|
11 | NUMENTRY = None |
---|
12 | |
---|
13 | # Si se solicita, mostrar ayuda. |
---|
14 | if "help" in args: |
---|
15 | ogHelp(FUNCNAME, f"{FUNCNAME} [ Num_order_entry | Label_entry ]", f"{FUNCNAME} 2", f'{FUNCNAME} "Windows Boot Manager"') |
---|
16 | return |
---|
17 | |
---|
18 | # Error si no se recibe 1 parámetro. |
---|
19 | if len(args) != 1: |
---|
20 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} [ Num_order_entry | Label_entry ]") |
---|
21 | return |
---|
22 | |
---|
23 | # Si no es equipo UEFI salir con error |
---|
24 | if not ogIsEfiActive(): |
---|
25 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
26 | return |
---|
27 | |
---|
28 | # Distingo si es número de orden o etiqueta |
---|
29 | if args[0].isdigit(): |
---|
30 | NUMENTRY = subprocess.check_output(['efibootmgr', '-v']).decode().split()[0][4:8] |
---|
31 | else: |
---|
32 | NUMENTRY = subprocess.check_output(['efibootmgr', '-v']).decode().split()[0][4:8] |
---|
33 | |
---|
34 | if NUMENTRY == "": |
---|
35 | ogRaiseError(OG_ERR_NOTFOUND, f"NVRAM entry '{args[0]}'") |
---|
36 | return |
---|
37 | |
---|
38 | subprocess.run(['efibootmgr', '-a', '-b', NUMENTRY], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|
39 | |
---|
40 | def ogNvramAddEntry(*args): |
---|
41 | FUNCNAME = ogNvramAddEntry.__name__ |
---|
42 | EFIDISK = None |
---|
43 | EFIPART = None |
---|
44 | BOOTLABEL = None |
---|
45 | BOOTLOADER = None |
---|
46 | ADDORDER = None |
---|
47 | |
---|
48 | # Si se solicita, mostrar ayuda. |
---|
49 | if "help" in args: |
---|
50 | ogHelp(FUNCNAME, f"{FUNCNAME} Str_label_entry Str_boot_loader [ Bool_add_bootorder ]", f"{FUNCNAME} 1 2 TRUE", f'{FUNCNAME} grub /EFI/grub/grubx64.efi TRUE', f'{FUNCNAME} Windows /EFI/Microsoft/Boot/bootmgfw.efi') |
---|
51 | return |
---|
52 | |
---|
53 | # Error si no se recibe 2 o 3 parámetros. |
---|
54 | if len(args) < 2 or len(args) > 3: |
---|
55 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} Str_label_entry Str_boot_loader") |
---|
56 | return |
---|
57 | |
---|
58 | # Si no es equipo UEFI salir con error |
---|
59 | if not ogIsEfiActive(): |
---|
60 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
61 | return |
---|
62 | |
---|
63 | EFIDISK, EFIPART = ogGetEsp().split() |
---|
64 | if EFIPART == "": |
---|
65 | ogRaiseError(OG_ERR_NOTFOUND, "ESP") |
---|
66 | return |
---|
67 | |
---|
68 | # Recogemos parámetros |
---|
69 | # Distinguimos si es disco/partición o etiqueta/cargador |
---|
70 | if args[0].isdigit() and args[1].isdigit(): |
---|
71 | BOOTLABEL = f"Part-{int(args[0]):02d}-{int(args[1]):02d}" |
---|
72 | BOOTLOADER = f"/EFI/{BOOTLABEL}/Boot/ogloader.efi" |
---|
73 | else: |
---|
74 | BOOTLABEL = args[0] |
---|
75 | BOOTLOADER = args[1] |
---|
76 | |
---|
77 | # Si existe entrada con la misma etiqueta la borramos |
---|
78 | ogNvramDeleteEntry(BOOTLABEL) |
---|
79 | |
---|
80 | subprocess.run(['efibootmgr', '-C', '-d', ogDiskToDev(EFIDISK), '-p', EFIPART, '-L', BOOTLABEL, '-l', BOOTLOADER], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|
81 | |
---|
82 | # Incluimos la entrada en el orden de arranque (opcional) |
---|
83 | if len(args) == 3 and args[2].upper() == "TRUE": |
---|
84 | NUMENTRY = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
85 | for entry in NUMENTRY: |
---|
86 | if entry.startswith(BOOTLABEL): |
---|
87 | NUMENTRY = entry[4:8] |
---|
88 | break |
---|
89 | ogNvramSetOrder(*ogNvramGetOrder().split(','), NUMENTRY) |
---|
90 | |
---|
91 | def ogCopyEfiBootLoader(): |
---|
92 | FUNCNAME = ogCopyEfiBootLoader.__name__ |
---|
93 | # Si se solicita, mostrar ayuda. |
---|
94 | if "help" in args: |
---|
95 | ogHelp(FUNCNAME, f"{FUNCNAME} int_ndisk int_part", f"{FUNCNAME} 1 2") |
---|
96 | return |
---|
97 | |
---|
98 | # Error si no se reciben 2 parámetros. |
---|
99 | if len(args) != 2: |
---|
100 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} int_ndisk int_part") |
---|
101 | return |
---|
102 | |
---|
103 | # Comprobamos que exista partición de sistema y la ESP |
---|
104 | MNTDIR = ogMount(args[0], args[1]) |
---|
105 | if MNTDIR is None: |
---|
106 | ogRaiseError(OG_ERR_PARTITION, f"{DISK} {PART}") |
---|
107 | return |
---|
108 | |
---|
109 | EFIDIR = ogMount(ogGetEsp()) |
---|
110 | if EFIDIR is None: |
---|
111 | ogRaiseError(OG_ERR_PARTITION, "ESP") |
---|
112 | return |
---|
113 | |
---|
114 | # Comprobamos que exista el cargador |
---|
115 | BOOTLABEL = f"Part-{int(args[0]):02d}-{int(args[1]):02d}" |
---|
116 | OSVERSION = ogGetOsVersion(args[0], args[1]) |
---|
117 | LOADER = None |
---|
118 | for f in [f"{EFIDIR}/EFI/Microsoft/Boot/bootmgfw.efi", f"{EFIDIR}/EFI/{BOOTLABEL}/Boot/bootmgfw.efi"]: |
---|
119 | if os.path.isfile(f): |
---|
120 | LOADER = f |
---|
121 | break |
---|
122 | |
---|
123 | if LOADER is None: |
---|
124 | ogRaiseError(OG_ERR_NOTOS, f"{args[0]} {args[1]} ({OSVERSION}, EFI)") |
---|
125 | return |
---|
126 | |
---|
127 | # Si existe el directorio Boot lo borramos |
---|
128 | if os.path.isdir(f"{MNTDIR}/ogBoot"): |
---|
129 | shutil.rmtree(f"{MNTDIR}/ogBoot") |
---|
130 | |
---|
131 | DIRLOADER = os.path.realpath(os.path.join(os.path.dirname(LOADER), "..")) |
---|
132 | shutil.copytree(f"{DIRLOADER}/Boot", f"{MNTDIR}/ogBoot") |
---|
133 | |
---|
134 | def ogNvramDeleteEntry(*args): |
---|
135 | FUNCNAME = ogNvramDeleteEntry.__name__ |
---|
136 | NUMENTRY = None |
---|
137 | |
---|
138 | # Si se solicita, mostrar ayuda. |
---|
139 | if "help" in args: |
---|
140 | ogHelp(FUNCNAME, f"{FUNCNAME} [ Num_order_entry | Label_entry ]", f"{FUNCNAME} 2", f'{FUNCNAME} "Windows Boot Manager"') |
---|
141 | return |
---|
142 | |
---|
143 | # Error si no se recibe 1 parámetro. |
---|
144 | if len(args) != 1: |
---|
145 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} [ Num_order_entry | Label_entry ]") |
---|
146 | return |
---|
147 | |
---|
148 | # Si no es equipo UEFI salir con error |
---|
149 | if not ogIsEfiActive(): |
---|
150 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
151 | return |
---|
152 | |
---|
153 | # Distingo si es número de orden o etiqueta |
---|
154 | if args[0].isdigit(): |
---|
155 | NUMENTRY = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
156 | for entry in NUMENTRY: |
---|
157 | if entry.startswith(args[0]): |
---|
158 | NUMENTRY = entry[4:8] |
---|
159 | break |
---|
160 | else: |
---|
161 | NUMENTRY = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
162 | for entry in NUMENTRY: |
---|
163 | if entry.startswith(args[0]): |
---|
164 | NUMENTRY = entry[4:8] |
---|
165 | break |
---|
166 | |
---|
167 | if NUMENTRY == "": |
---|
168 | ogRaiseError(OG_ERR_NOTFOUND, f"NVRAM entry '{args[0]}'") |
---|
169 | return |
---|
170 | |
---|
171 | for n in NUMENTRY: |
---|
172 | subprocess.run(['efibootmgr', '-B', '-b', n], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|
173 | |
---|
174 | def ogNvramGetCurrent(): |
---|
175 | FUNCNAME = ogNvramGetCurrent.__name__ |
---|
176 | # Si se solicita, mostrar ayuda. |
---|
177 | if "help" in args: |
---|
178 | ogHelp(FUNCNAME, FUNCNAME, FUNCNAME) |
---|
179 | return |
---|
180 | |
---|
181 | # Si no es equipo UEFI salir con error |
---|
182 | if not ogIsEfiActive(): |
---|
183 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
184 | return |
---|
185 | |
---|
186 | output = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
187 | bootentry = 99999 |
---|
188 | for line in output: |
---|
189 | if "BootCurrent" in line: |
---|
190 | bootentry = line.split()[1] |
---|
191 | if bootentry in line: |
---|
192 | num_entry = line[4:8] |
---|
193 | label_entry = line[line.index(bootentry):] |
---|
194 | print(f"{num_entry} {label_entry}") |
---|
195 | |
---|
196 | def ogNvramGetNext(): |
---|
197 | # Si se solicita, mostrar ayuda. |
---|
198 | if "help" in args: |
---|
199 | ogHelp(FUNCNAME, FUNCNAME, FUNCNAME) |
---|
200 | return |
---|
201 | |
---|
202 | # Si no es equipo UEFI salir con error |
---|
203 | if not ogIsEfiActive(): |
---|
204 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
205 | return |
---|
206 | |
---|
207 | output = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
208 | for line in output: |
---|
209 | if "BootNext" in line: |
---|
210 | bootnext = line.split()[1] |
---|
211 | return bootnext |
---|
212 | return None |
---|
213 | |
---|
214 | def ogNvramGetOrder(): |
---|
215 | FUNCNAME = ogNvramGetOrder.__name__ |
---|
216 | # Si se solicita, mostrar ayuda. |
---|
217 | if "help" in args: |
---|
218 | ogHelp(FUNCNAME, FUNCNAME, FUNCNAME) |
---|
219 | return |
---|
220 | |
---|
221 | # Si no es equipo UEFI salir con error |
---|
222 | if not ogIsEfiActive(): |
---|
223 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
224 | return |
---|
225 | |
---|
226 | output = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
227 | for line in output: |
---|
228 | if "BootOrder" in line: |
---|
229 | bootorder = line.split(':')[1].strip() |
---|
230 | return bootorder |
---|
231 | return None |
---|
232 | |
---|
233 | def ogNvramGetTimeout(): |
---|
234 | FUNCNAME = ogNvramGetTimeout.__name__ |
---|
235 | # Si se solicita, mostrar ayuda. |
---|
236 | if "help" in args: |
---|
237 | ogHelp(FUNCNAME, FUNCNAME, FUNCNAME) |
---|
238 | return |
---|
239 | |
---|
240 | # Si no es equipo UEFI salir con error |
---|
241 | if not ogIsEfiActive(): |
---|
242 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
243 | return |
---|
244 | |
---|
245 | output = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
246 | for line in output: |
---|
247 | if "Timeout" in line: |
---|
248 | timeout = line.split(':')[1].strip() |
---|
249 | return timeout |
---|
250 | return None |
---|
251 | |
---|
252 | def ogGrubUefiConf(*args): |
---|
253 | FUNCNAME = ogGrubUefiConf.__name__ |
---|
254 | EFIDIR = None |
---|
255 | BOOTLABEL = None |
---|
256 | GRUBEFI = None |
---|
257 | UUID = None |
---|
258 | DEVICE = None |
---|
259 | PREFIXSECONDSTAGE = None |
---|
260 | EFIGRUBDIR = None |
---|
261 | |
---|
262 | # Si se solicita, mostrar ayuda. |
---|
263 | if "help" in args: |
---|
264 | ogHelp(FUNCNAME, f"{FUNCNAME} int_ndisk int_part [ str_dir_grub ]", f"{FUNCNAME} 1 2", f"{FUNCNAME} 1 3 /boot/grubPARTITION") |
---|
265 | return |
---|
266 | |
---|
267 | # Error si no se reciben al menos 2 parámetros. |
---|
268 | if len(args) < 2: |
---|
269 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} int_ndisk int_part [ str_dir_grub ]") |
---|
270 | return |
---|
271 | |
---|
272 | EFIDIR = ogMount(ogGetEsp()) |
---|
273 | if EFIDIR is None: |
---|
274 | ogRaiseError(OG_ERR_PARTITION, "ESP") |
---|
275 | return |
---|
276 | |
---|
277 | BOOTLABEL = f"Part-{int(args[0]):02d}-{int(args[1]):02d}" |
---|
278 | EFIGRUBDIR = f"{EFIDIR}/EFI/{BOOTLABEL}/boot/grub" |
---|
279 | # Comprobamos que existe directorio |
---|
280 | if not os.path.isdir(EFIGRUBDIR): |
---|
281 | os.makedirs(EFIGRUBDIR) |
---|
282 | |
---|
283 | # Parcheamos uuid y particion en grub.cfg |
---|
284 | UUID = subprocess.check_output(['blkid', '-o', 'value', '-s', 'UUID', ogDiskToDev(args[0], args[1])]).decode().strip() |
---|
285 | DEVICE = f"hd{int(args[0]) - 1},gpt{int(args[1])}" |
---|
286 | |
---|
287 | with open(f"{EFIGRUBDIR}/grub.cfg", "w") as f: |
---|
288 | f.write(f"set root='{DEVICE}'\n") |
---|
289 | f.write(f"set prefix=($root)'{args[2]}/boot/grub'\n") |
---|
290 | f.write(f"configfile $prefix/grub.cfg\n") |
---|
291 | |
---|
292 | # Provisional: confirmar si el segundo archivo se utiliza |
---|
293 | shutil.copy(f"{EFIGRUBDIR}/grub.cfg", f"{EFIDIR}/EFI/{BOOTLABEL}/grub.cfg") |
---|
294 | |
---|
295 | def ogNvramInactiveEntry(*args): |
---|
296 | FUNCNAME = ogNvramInactiveEntry.__name__ |
---|
297 | NUMENTRY = None |
---|
298 | |
---|
299 | # Si se solicita, mostrar ayuda. |
---|
300 | if "help" in args: |
---|
301 | ogHelp(FUNCNAME, f"{FUNCNAME} [ Num_order_entry | Label_entry ]", f"{FUNCNAME} 2", f'{FUNCNAME} "Windows Boot Manager"') |
---|
302 | return |
---|
303 | |
---|
304 | # Error si no se recibe 1 parámetro. |
---|
305 | if len(args) != 1: |
---|
306 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} [ Num_order_entry | Label_entry ]") |
---|
307 | return |
---|
308 | |
---|
309 | # Si no es equipo UEFI salir con error |
---|
310 | if not ogIsEfiActive(): |
---|
311 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
312 | return |
---|
313 | |
---|
314 | # Distingo si es número de orden o etiqueta |
---|
315 | if args[0].isdigit(): |
---|
316 | NUMENTRY = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
317 | for entry in NUMENTRY: |
---|
318 | if entry.startswith(args[0]): |
---|
319 | NUMENTRY = entry[4:8] |
---|
320 | break |
---|
321 | else: |
---|
322 | NUMENTRY = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
323 | for entry in NUMENTRY: |
---|
324 | if entry.startswith(args[0]): |
---|
325 | NUMENTRY = entry[4:8] |
---|
326 | break |
---|
327 | |
---|
328 | if NUMENTRY == "": |
---|
329 | ogRaiseError(OG_ERR_NOTFOUND, f"NVRAM entry '{args[0]}'") |
---|
330 | return |
---|
331 | |
---|
332 | subprocess.run(['efibootmgr', '-A', '-b', NUMENTRY], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|
333 | |
---|
334 | def ogNvramList(): |
---|
335 | FUNCNAME = ogNvramList.__name__ |
---|
336 | # Si se solicita, mostrar ayuda. |
---|
337 | if "help" in args: |
---|
338 | ogHelp(FUNCNAME, FUNCNAME, FUNCNAME) |
---|
339 | return |
---|
340 | |
---|
341 | # Si no es equipo UEFI salir con error |
---|
342 | if not ogIsEfiActive(): |
---|
343 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
344 | return |
---|
345 | |
---|
346 | output = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
347 | for line in output: |
---|
348 | if line.startswith("Boot"): |
---|
349 | num_entry = line[4:8] |
---|
350 | label_entry = line[line.index(num_entry):] |
---|
351 | active = "*" if line.startswith("*") else "" |
---|
352 | print(f"{num_entry} {label_entry} {active}") |
---|
353 | |
---|
354 | def ogNvramPxeFirstEntry(): |
---|
355 | FUNCNAME = ogNvramPxeFirstEntry.__name__ |
---|
356 | NUMENTRY = None |
---|
357 | ORDER = None |
---|
358 | |
---|
359 | # Si se solicita, mostrar ayuda. |
---|
360 | if "help" in args: |
---|
361 | ogHelp(FUNCNAME, FUNCNAME, FUNCNAME) |
---|
362 | return |
---|
363 | |
---|
364 | # Si no es equipo UEFI salir con error |
---|
365 | if not ogIsEfiActive(): |
---|
366 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
367 | return |
---|
368 | |
---|
369 | NUMENTRY = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
370 | for entry in NUMENTRY: |
---|
371 | if entry.startswith("IPv4"): |
---|
372 | NUMENTRY = entry[4:8] |
---|
373 | break |
---|
374 | |
---|
375 | # Si la entrada es la primera nos salimos. |
---|
376 | if NUMENTRY in ogNvramGetOrder().split(','): |
---|
377 | return |
---|
378 | |
---|
379 | # Si la entrada ya existe la borramos. |
---|
380 | ORDER = ogNvramGetOrder().split(',') |
---|
381 | if NUMENTRY in ORDER: |
---|
382 | ORDER.remove(NUMENTRY) |
---|
383 | |
---|
384 | ogNvramSetOrder(','.join([NUMENTRY] + ORDER)) |
---|
385 | |
---|
386 | def ogRestoreEfiBootLoader(*args): |
---|
387 | FUNCNAME = ogRestoreEfiBootLoader.__name__ |
---|
388 | MNTDIR = None |
---|
389 | EFIDIR = None |
---|
390 | BOOTLABEL = None |
---|
391 | OSVERSION = None |
---|
392 | LOADER = None |
---|
393 | UUID = None |
---|
394 | DEVICE = None |
---|
395 | |
---|
396 | # Si se solicita, mostrar ayuda. |
---|
397 | if "help" in args: |
---|
398 | ogHelp(FUNCNAME, f"{FUNCNAME} int_ndisk int_part", f"{FUNCNAME} 1 2") |
---|
399 | return |
---|
400 | |
---|
401 | # Error si no se reciben 2 parámetros. |
---|
402 | if len(args) != 2: |
---|
403 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} int_ndisk int_part") |
---|
404 | return |
---|
405 | |
---|
406 | # Comprobamos que exista partición de sistema y la ESP |
---|
407 | MNTDIR = ogMount(args[0], args[1]) |
---|
408 | if MNTDIR is None: |
---|
409 | ogRaiseError(OG_ERR_PARTITION, f"{DISK} {PART}") |
---|
410 | return |
---|
411 | |
---|
412 | EFIDIR = ogMount(ogGetEsp()) |
---|
413 | if EFIDIR is None: |
---|
414 | ogFormat(ogGetEsp(), "FAT32") |
---|
415 | EFIDIR = ogMount(ogGetEsp()) |
---|
416 | if EFIDIR is None: |
---|
417 | ogRaiseError(OG_ERR_PARTITION, "ESP") |
---|
418 | return |
---|
419 | |
---|
420 | # Comprobamos que exista el cargador |
---|
421 | BOOTLABEL = f"Part-{int(args[0]):02d}-{int(args[1]):02d}" |
---|
422 | OSVERSION = ogGetOsVersion(args[0], args[1]) |
---|
423 | if "Windows" in OSVERSION: |
---|
424 | LOADER = ogGetPath(f"{MNTDIR}/ogBoot/bootmgfw.efi") |
---|
425 | if LOADER is None: |
---|
426 | ogRaiseError(OG_ERR_NOTOS, f"{args[0]} {args[1]} ({OSVERSION}, EFI)") |
---|
427 | return |
---|
428 | if os.path.isdir(f"{EFIDIR}/EFI/{BOOTLABEL}"): |
---|
429 | shutil.rmtree(f"{EFIDIR}/EFI/{BOOTLABEL}") |
---|
430 | os.makedirs(f"{EFIDIR}/EFI/{BOOTLABEL}") |
---|
431 | shutil.copytree(f"{LOADER.rstrip('/bootmgfw.efi')}", f"{EFIDIR}/EFI/{BOOTLABEL}/Boot") |
---|
432 | shutil.copy(LOADER, f"{EFIDIR}/EFI/{BOOTLABEL}/Boot/ogloader.efi") |
---|
433 | if os.path.isdir(f"{EFIDIR}/EFI/Microsoft"): |
---|
434 | os.rename(f"{EFIDIR}/EFI/Microsoft", f"{EFIDIR}/EFI/Microsoft.backup.og") |
---|
435 | |
---|
436 | def ogRestoreUuidPartitions(): |
---|
437 | FUNCNAME = ogRestoreUuidPartitions.__name__ |
---|
438 | DISK = None |
---|
439 | PART = None |
---|
440 | IMGNAME = None |
---|
441 | INFOFILE = None |
---|
442 | DEVICE = None |
---|
443 | DATA = None |
---|
444 | GUID = None |
---|
445 | UUID = None |
---|
446 | IMGGUID = None |
---|
447 | EFIDEVICE = None |
---|
448 | EFIDATA = None |
---|
449 | EFIGUID = None |
---|
450 | EFIUUID = None |
---|
451 | IMGEFIGUID = None |
---|
452 | |
---|
453 | # Si se solicita, mostrar ayuda. |
---|
454 | if "help" in args: |
---|
455 | ogHelp(FUNCNAME, f"{FUNCNAME} REPO|CACHE str_imgname int_ndisk int_npart", f"{FUNCNAME} REPO Windows 1 2") |
---|
456 | return |
---|
457 | |
---|
458 | # Error si no se reciben 4 parámetros. |
---|
459 | if len(args) != 4: |
---|
460 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} REPO|CACHE str_imgname int_ndisk int_npart") |
---|
461 | return |
---|
462 | |
---|
463 | # Sólo se ejecuta si es UEFI |
---|
464 | if not ogIsEfiActive(): |
---|
465 | return |
---|
466 | |
---|
467 | # Parámetros de entrada |
---|
468 | IMGNAME = args[1] |
---|
469 | INFOFILE = f"{OGIMG}/.{IMGNAME}.img.json" |
---|
470 | if args[0].upper() == "CACHE": |
---|
471 | INFOFILE = f"{OGCAC}{INFOFILE}" |
---|
472 | # TODO: que la función getPath soporte archivos ocultos |
---|
473 | if not os.path.exists(INFOFILE): |
---|
474 | ogRaiseError(OG_ERR_NOTFOUND, INFOFILE) |
---|
475 | return |
---|
476 | |
---|
477 | DISK = args[2] |
---|
478 | PART = args[3] |
---|
479 | |
---|
480 | DEVICE = ogDiskToDev(DISK) |
---|
481 | EFIDISK, EFIPART = ogGetEsp().split() |
---|
482 | |
---|
483 | # Datos de la imagen |
---|
484 | IMGGUID = subprocess.check_output(['jq', '.guid', INFOFILE]).decode().strip().strip('"') |
---|
485 | IMGEFIGUID = subprocess.check_output(['jq', '.espguid', INFOFILE]).decode().strip().strip('"') |
---|
486 | |
---|
487 | # Datos actuales |
---|
488 | DATA = subprocess.check_output(['sfdisk', '-J', DEVICE]).decode() |
---|
489 | GUID = subprocess.check_output(['echo', DATA, '|', 'jq', '".partitiontable|.id"', '|', 'tr', '-d', '"']).decode().strip() |
---|
490 | |
---|
491 | if IMGGUID != GUID: |
---|
492 | subprocess.run(['sgdisk', '-U', IMGGUID, DEVICE]) |
---|
493 | subprocess.run(['partprobe']) |
---|
494 | |
---|
495 | if DISK == EFIDISK: |
---|
496 | EFIDATA = DATA |
---|
497 | EFIDEVICE = DEVICE |
---|
498 | else: |
---|
499 | EFIDEVICE = ogDiskToDev(EFIDISK) |
---|
500 | EFIDATA = subprocess.check_output(['sfdisk', '-J', EFIDEVICE]).decode() |
---|
501 | EFIGUID = subprocess.check_output(['echo', EFIDATA, '|', 'jq', '".partitiontable|.id"', '|', 'tr', '-d', '"']).decode().strip() |
---|
502 | |
---|
503 | if IMGEFIGUID != EFIGUID: |
---|
504 | subprocess.run(['sgdisk', '-U', IMGEFIGUID, EFIDEVICE]) |
---|
505 | subprocess.run(['partprobe']) |
---|
506 | |
---|
507 | def ogNvramSetNext(*args): |
---|
508 | FUNCNAME = ogNvramSetNext.__name__ |
---|
509 | NUMENTRY = None |
---|
510 | |
---|
511 | # Si se solicita, mostrar ayuda. |
---|
512 | if "help" in args: |
---|
513 | ogHelp(FUNCNAME, f"{FUNCNAME} [ Num_order_entry | Label_entry ]", f"{FUNCNAME} 2", f'{FUNCNAME} "Windows Boot Manager"') |
---|
514 | return |
---|
515 | |
---|
516 | # Error si no se recibe 1 parámetro. |
---|
517 | if len(args) != 1: |
---|
518 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} [ Num_order_entry | Label_entry ]") |
---|
519 | return |
---|
520 | |
---|
521 | # Si no es equipo UEFI salir con error |
---|
522 | if not ogIsEfiActive(): |
---|
523 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
524 | return |
---|
525 | |
---|
526 | # Distingo si es número de orden o etiqueta |
---|
527 | if args[0].isdigit(): |
---|
528 | NUMENTRY = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
529 | for entry in NUMENTRY: |
---|
530 | if entry.startswith(args[0]): |
---|
531 | NUMENTRY = entry[4:8] |
---|
532 | break |
---|
533 | else: |
---|
534 | NUMENTRY = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
535 | for entry in NUMENTRY: |
---|
536 | if entry.startswith(args[0]): |
---|
537 | NUMENTRY = entry[4:8] |
---|
538 | break |
---|
539 | |
---|
540 | if NUMENTRY == "": |
---|
541 | ogRaiseError(OG_ERR_NOTFOUND, f"NVRAM entry '{args[0]}'") |
---|
542 | return |
---|
543 | |
---|
544 | subprocess.run(['efibootmgr', '-n', NUMENTRY], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|
545 | |
---|
546 | def ogNvramSetOrder(*args): |
---|
547 | FUNCNAME = ogNvramSetOrder.__name |
---|
548 | # Si se solicita, mostrar ayuda. |
---|
549 | if "help" in args: |
---|
550 | ogHelp(FUNCNAME, f"{FUNCNAME} Num_order1 [ Num_order2 ] ...", f"{FUNCNAME} 1 3") |
---|
551 | return |
---|
552 | |
---|
553 | # Error si no se recibe al menos 1 parámetro. |
---|
554 | if len(args) < 1: |
---|
555 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} Num_order1 [ Num_order2 ] ...") |
---|
556 | return |
---|
557 | |
---|
558 | # Si no es equipo UEFI salir con error |
---|
559 | if not ogIsEfiActive(): |
---|
560 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
561 | return |
---|
562 | |
---|
563 | # Comprobamos que sean números |
---|
564 | for arg in args: |
---|
565 | if not arg.isdigit(): |
---|
566 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} Num_order1 [ Num_order2 ] ...") |
---|
567 | return |
---|
568 | |
---|
569 | # Entradas de la NVRAM actuales |
---|
570 | NUMENTRYS = subprocess.check_output(['efibootmgr']).decode().split('\n') |
---|
571 | NUMENTRYS = [entry[4:8] for entry in NUMENTRYS if entry.startswith("Boot")] |
---|
572 | |
---|
573 | ORDER = "" |
---|
574 | for arg in args: |
---|
575 | # Si no existe la entrada me salgo |
---|
576 | if arg not in NUMENTRYS: |
---|
577 | ogRaiseError(OG_ERR_NOTFOUND, f"NVRAM entry order '{arg}'") |
---|
578 | return |
---|
579 | ORDER += f",{arg}" |
---|
580 | |
---|
581 | # Cambiamos el orden |
---|
582 | subprocess.run(['efibootmgr', '-o', ORDER[1:]], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|
583 | |
---|
584 | def ogNvramSetTimeout(*args): |
---|
585 | FUNCNAME = ogNvramSetTimeout.__name__ |
---|
586 | |
---|
587 | # Si se solicita, mostrar ayuda. |
---|
588 | if "help" in args: |
---|
589 | ogHelp(FUNCNAME, f"{FUNCNAME} int_Timeout (seg)", f"{FUNCNAME} 2") |
---|
590 | return |
---|
591 | |
---|
592 | # Si no es equipo UEFI salir con error |
---|
593 | if not ogIsEfiActive(): |
---|
594 | ogRaiseError(OG_ERR_NOTUEFI) |
---|
595 | return |
---|
596 | |
---|
597 | # Error si no se recibe 1 parámetro. |
---|
598 | if len(args) != 1: |
---|
599 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} int_Timeout (seg)") |
---|
600 | return |
---|
601 | |
---|
602 | # Comprobamos que sea un número |
---|
603 | if not args[0].isdigit(): |
---|
604 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} int_Timeout (seg)") |
---|
605 | return |
---|
606 | |
---|
607 | # Cambiamos el timeout |
---|
608 | subprocess.run(['efibootmgr', '-t', args[0]], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|
609 | |
---|
610 | def ogUuidChange(*args): |
---|
611 | FUNCNAME = ogUuidChange.__name__ |
---|
612 | MNTDIR = None |
---|
613 | DEVICE = None |
---|
614 | UUID = None |
---|
615 | NEWUUID = None |
---|
616 | |
---|
617 | # Si se solicita, mostrar ayuda. |
---|
618 | if "help" in args: |
---|
619 | ogHelp(FUNCNAME, f"{FUNCNAME} int_ndisk int_part", f"{FUNCNAME} 1 2") |
---|
620 | return |
---|
621 | |
---|
622 | # Error si no se reciben al menos 2 parámetros. |
---|
623 | if len(args) < 2: |
---|
624 | ogRaiseError(OG_ERR_FORMAT, f"{FUNCNAME} int_ndisk int_part") |
---|
625 | return |
---|
626 | |
---|
627 | # Comprobamos que exista la partición |
---|
628 | MNTDIR = ogMount(args[0], args[1]) |
---|
629 | if MNTDIR is None: |
---|
630 | ogRaiseError(OG_ERR_NOTFOUND, f"Device {args[0]} {args[1]}") |
---|
631 | return |
---|
632 | |
---|
633 | DEVICE = ogDiskToDev(args[0], args[1]) |
---|
634 | UUID = subprocess.check_output(['blkid', '-o', 'value', '-s', 'UUID', DEVICE]).decode().strip() |
---|
635 | NEWUUID = subprocess.check_output(['cat', '/proc/sys/kernel/random/uuid']).decode().strip() |
---|
636 | |
---|
637 | # Cambiamos UUID a la partición |
---|
638 | ogUnmount(args[0], args[1]) |
---|
639 | subprocess.run(['tune2fs', DEVICE, '-U', NEWUUID], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|
640 | |
---|
641 | # Cambiamos UUID en la configuración (fstab y grub) |
---|
642 | ogMount(args[0], args[1]) |
---|
643 | for f in [f"{MNTDIR}/etc/fstab", f"{MNTDIR}/{{,boot/}}{{grubMBR,grubPARTITION}}/boot/{{,grub{,2},{,efi/}EFI/*/}}/{{menu.lst,grub.cfg}}"]: |
---|
644 | if os.path.isfile(f): |
---|
645 | subprocess.run(['sed', '-i', f's/{UUID}/{NEWUUID}/g', f], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
---|