refs #2412 check partition sizes before touching the disk

particionador
Natalia Serrano 2025-07-09 14:01:11 +02:00
parent a7f4d767f5
commit 9cad17e4f4
2 changed files with 72 additions and 2 deletions

View File

@ -78,6 +78,8 @@ tbf = {} # Tabla de formateo
sparams = sparam.split('%')
maxp=0
sum_tam = 0
do_sum_tam = True
for item in sparams:
if not item: continue ## por si nos pasan un '%' al final de todo
# Leer datos de la partición, separados por "*".
@ -94,7 +96,7 @@ for item in sparams:
if 'par' == k: par = int (v)
elif 'cpt' == k: cpt = v
elif 'sfi' == k: sfi = v
elif 'tam' == k: tam = v
elif 'tam' == k: tam = int (v)
elif 'ope' == k: ope = int (v)
missing_params = []
@ -111,6 +113,11 @@ for item in sparams:
tch = tam
else:
tbp.append (f'{cpt}:{tam}')
if do_sum_tam:
sum_tam += tam
if 'EXTENDED' == cpt:
do_sum_tam = False ## don't sum sizes anymore
if ope:
# Si se activa operación de formatear, componer datos de formateo.
if cpt not in ['EMPTY', 'EXTENDED', 'LINUX-LVM', 'LVM', 'ZPOOL']:
@ -119,7 +126,10 @@ for item in sparams:
if par > maxp: maxp = par
if tch is None:
tch = '0'
tch = 0
if is_there_cache and 0 == tch:
print (f'nati ===================== is_there_cache ({is_there_cache}) tch ({tch}), this should not happen')
@ -133,6 +143,29 @@ if cur_ptt and ptt != cur_ptt:
SystemLib.ogEcho (['session', 'log'], None, f'Current partition table type "{cur_ptt}" is wrong for this system--will replace it for a "{ptt}" one')
recreate_partition_table = True
## size check: check that cache fits in the space left by the previously existing partitions
if not recreate_partition_table and not CacheLib.ogCheckNewCacheSize (dis, tch):
SystemLib.ogRaiseError (['log', 'session'], ogGlobals.OG_ERR_CACHE, f'nueva partición de caché no cabe en el hueco actual')
sys.exit (1)
## size check: check that the newly defined partitions fit in the space left by the newly defined cache
disk_sectors = DiskLib.ogGetLastSector (dis)
IOSIZE = 512 ## TODO
if 512 == IOSIZE:
sum_tam_sectors = sum_tam*2
cache_sectors = tch*2
else:
sum_tam_sectors = (sum_tam+3)//4 ## sumamos 3 para que la división entera "redondee al alza"
cache_sectors = (tch+3)//4
## esta comprobacion puede dejar pasar situaciones que más tarde dan error
## la ventana es bastante estrecha, y sumando aquí simplemente un 1 por 1000, ya la cerramos del todo
sum_tam_sectors = int (sum_tam_sectors * 1.001)
space_left_by_cache = disk_sectors - cache_sectors
if sum_tam_sectors > space_left_by_cache:
SystemLib.ogRaiseError (['log', 'session'], ogGlobals.OG_ERR_CACHE, f'las particiones no caben en el disco')
sys.exit (1)
#____________________________________________________
#
# Proceso

View File

@ -364,3 +364,40 @@ def ogUnmountCache():
def initCache (*args):
p = subprocess.run ([f'{ogGlobals.OGSCRIPTS}/initCache.py'] + list(args))
return not p.returncode ## negate shell return code
#/**
# ogCheckNewCacheSize
#@brief Comprueba si un cache de X tamaño cabe en el hueco que dejan las demás particiones
#@param Tamaño de la nueva hipotética cache
#@return Boolean, True si la nueva cache cabría, False si no
#*/ ##
def ogCheckNewCacheSize (disk, kB):
IOSIZE=512 #DiskLib.blablaIoSize (disk)
last_sector = DiskLib.ogGetLastSector (disk)
end_of_last_partition = 0
cachepart = ogFindCache()
if cachepart:
cache_disk, cache_part = cachepart.split()
cache_dev = DiskLib.ogDiskToDev (cache_disk, cache_part)
else:
cache_dev = None
fdisk_l_out = subprocess.run (['fdisk', '-l'], capture_output=True, text=True).stdout
for l in fdisk_l_out.splitlines():
if not re.match ('^/dev', l): ## no empieza por /dev, nos la saltamos
continue
if cache_dev and re.match (f'^{cache_dev}', l): ## es la de cache, nos la saltamos
continue
last_sector_of_partition = int (l.split()[2])
if last_sector_of_partition > end_of_last_partition:
end_of_last_partition = last_sector_of_partition
if 512 == IOSIZE:
cache_sectors = kB*2
else: ## 4096
cache_sectors = (kB+3)//4 ## sumamos 3 para que la división entera "redondee al alza"
if end_of_last_partition + cache_sectors > last_sector:
SystemLib.ogEcho (['session', 'log'], None, f'end_of_last_partition ({end_of_last_partition}) + cache_sectors ({cache_sectors}) > last_sector ({last_sector}), check failed')
return False
return True