refs #1224 add ogConfigureFstab()

pull/1/head
Natalia Serrano 2024-12-04 10:49:02 +01:00
parent 57ab927976
commit 66e3150b1a
1 changed files with 55 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import os
import re
import tempfile
import subprocess
import shutil
import ogGlobals
import SystemLib
@ -414,6 +415,60 @@ part_letter={registered_vol}
#@exception OG_ERR_NOTFOUND No se encuentra el fichero fstab a procesar.
#@warning Puede haber un error si hay más de 1 partición swap.
#*/ ##
def ogConfigureFstab (disk, par):
fstab = FileLib.ogGetPath (src=f'{disk} {par}', file='/etc/fstab')
if not fstab:
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, f'{disk},{par},/etc/fstab')
return
shutil.copy2 (fstab, fstab+'.backup')
with open ('/etc/fstab', 'r') as fd:
while True:
l = fd.readline()
if not l: break
cosas = l.split()
if 6 != len (cosas): continue
fstab_dev, fstab_mntpt, fstab_fstype, fstab_opts, fstab_dump, fstab_pass = cosas
if '#' in fstab_dev: continue
if '/' == fstab_mntpt:
defroot = fstab_dev
elif 'swap' == fstab_fstype:
defswap = fstab_dev
elif '/boot/efi' == fstab_mntpt:
efiopt = '\t'.join ([fstab_fstype, fstab_opts, fstab_dump, fstab_pass])
partroot = DiskLib.ogDiskToDev (disk, par)
partswap = subprocess.run (['blkid', '-', ' TYPE=swap'], capture_output=True, text=True).stdout
if partswap:
partswap = partswap.splitlines()[0]
partswap = partswap.split (':')[0]
if defswap:
print ("Hay definicion de SWAP en el FSTAB $DEFSWAP -> modificamos fichero con nuevo valor $DEFSWAP->$PARTSWAP") # Mensaje temporal.
subprocess.run (f'sed "s|{defswap}|{partswap}|g; s|{defroot}|{partroot}|g" {fstab}.backup > {fstab}', shell=True)
else:
print ("No hay definicion de SWAP y si hay partición SWAP -> moficamos fichero") # Mensaje temporal.
subprocess.run (f'sed "s|{defroot}|{partroot}|g" {fstab}.backup > {fstab}', shell=True)
with open ('/etc/fstab', 'a') as fd:
fd.write (f'{partswap} none swap sw 0 0\n')
else:
print ("No hay partición SWAP -> configuramos FSTAB") # Mensaje temporal.
subprocess.run (f'sed "/swap/d" {fstab}.backup > {fstab}', shell=True)
# Si es un sistema EFI incluimos partición ESP (Si existe la modificamos)
if InventoryLib.ogIsEfiActive():
esp = DiskLib.ogGetEsp()
efidisk, efipart = esp.split()
efidev = DiskLib.ogDiskToDev (efidisk, efipart)
## Opciones de la partición ESP: si no existe ponemos un valor por defecto
if not efiopt:
efiopt = '\t'.join (['vfat', 'umask=0077', '0', '1'])
subprocess.run (f'sed -i /"boot\\/efi"/d {fstab}', shell=True)
with open ('/etc/fstab', 'a') as fd:
fd.write ('{efidev}\t/boot/efi\t{efiopt}\n')
#/**
# ogSetLinuxName int_ndisk int_nfilesys [str_name]