refs #1415 add ogGetCacheSpace, fix a bug
parent
25901173b9
commit
1c6b5ace16
|
@ -193,46 +193,51 @@ def ogGetCacheSize():
|
||||||
#@note El espacio disponible es el que hay entre el límite superior de la partición 3 del disco 1 y el final de dicho disco, y no puede ser superior a la mitad de dicho disco.
|
#@note El espacio disponible es el que hay entre el límite superior de la partición 3 del disco 1 y el final de dicho disco, y no puede ser superior a la mitad de dicho disco.
|
||||||
#*/ ##
|
#*/ ##
|
||||||
def ogGetCacheSpace():
|
def ogGetCacheSpace():
|
||||||
"""
|
|
||||||
Obtiene el espacio libre en la partición de caché en kilobytes.
|
|
||||||
|
|
||||||
:return: Espacio libre en kilobytes.
|
|
||||||
:raises RuntimeError: Si ocurre un error al obtener el espacio libre.
|
|
||||||
"""
|
|
||||||
# Si se solicita, mostrar ayuda.
|
|
||||||
if len(sys.argv) > 1 and sys.argv[1] == "help":
|
|
||||||
ogHelp("ogGetCacheSpace", "help", "ogGetCacheSpace")
|
|
||||||
|
|
||||||
# Error si no se encuentra partición de caché.
|
|
||||||
cachepart = ogFindCache()
|
cachepart = ogFindCache()
|
||||||
if cachepart is None:
|
if not cachepart:
|
||||||
raise RuntimeError(MSG_NOCACHE)
|
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE)
|
||||||
|
return None
|
||||||
|
cachepart = cachepart.split()
|
||||||
|
|
||||||
# Obtener el tamaño del disco y el número de sectores.
|
disk = DiskLib.ogDiskToDev (cachepart[0])
|
||||||
disk = ogDiskToDev(cachepart)
|
if not disk:
|
||||||
try:
|
return None
|
||||||
result = subprocess.run(["sfdisk", "-g", disk], capture_output=True, text=True, check=True)
|
|
||||||
output = result.stdout
|
|
||||||
sectors_per_cylinder = int(output.splitlines()[1].split()[1])
|
|
||||||
total_sectors = int(output.splitlines()[1].split()[0]) * sectors_per_cylinder - 1
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
raise RuntimeError(f"Error al obtener el espacio libre: {e}")
|
|
||||||
|
|
||||||
# Obtener el último sector de la partición 3.
|
sectors = 0
|
||||||
try:
|
disk_bn = os.path.basename (disk)
|
||||||
result = subprocess.run(["sfdisk", "-uS", "-l", disk], capture_output=True, text=True, check=True)
|
with open ('/proc/partitions', 'r') as fd:
|
||||||
output = result.stdout
|
proc_parts = fd.read()
|
||||||
end_sector_part3 = int([line.split()[2] for line in output.splitlines() if cachepart + "3" in line][0])
|
for l in proc_parts.splitlines():
|
||||||
except subprocess.CalledProcessError as e:
|
items = l.split()
|
||||||
raise RuntimeError(f"Error al obtener el espacio libre: {e}")
|
if len(items) < 4: continue
|
||||||
|
if items[3] == disk_bn:
|
||||||
|
sectors = 2 * int (items[2])
|
||||||
|
if not sectors: return None
|
||||||
|
|
||||||
# Calcular el espacio libre en kilobytes.
|
sfdisk_out = subprocess.run (['sfdisk', '-g', disk], capture_output=True, text=True).stdout
|
||||||
if end_sector_part3 > total_sectors // 2:
|
cyls = int (sfdisk_out.split()[1])
|
||||||
free_space_kb = (total_sectors - end_sector_part3) // 2
|
sectors = sectors/cyls * cyls - 1
|
||||||
|
|
||||||
|
## the original code has a hard dependency on the existence of a third partition
|
||||||
|
## if the disk has sda1, sda2 and sda4, the code fails.
|
||||||
|
## this is an improved version
|
||||||
|
endpart3 = 0
|
||||||
|
for trypart in [3, 2, 1]:
|
||||||
|
sfdisk_out = subprocess.run (['sfdisk', '-uS', '-l', disk], capture_output=True, text=True).stdout
|
||||||
|
for l in sfdisk_out.splitlines():
|
||||||
|
items = l.split()
|
||||||
|
if len(items) < 6: continue
|
||||||
|
if f'{disk}{trypart}' == items[0]:
|
||||||
|
endpart3 = int (items[2])
|
||||||
|
break
|
||||||
|
if endpart3: break
|
||||||
|
if not endpart3: return None
|
||||||
|
|
||||||
|
# Mostrar espacio libre en KB (1 KB = 2 sectores)
|
||||||
|
if endpart3 > sectors // 2:
|
||||||
|
return (sectors - endpart3) // 2
|
||||||
else:
|
else:
|
||||||
free_space_kb = total_sectors // 4
|
return sectors // 4
|
||||||
|
|
||||||
return free_space_kb
|
|
||||||
|
|
||||||
|
|
||||||
#/**
|
#/**
|
||||||
|
@ -243,7 +248,12 @@ def ogGetCacheSpace():
|
||||||
#@warning Salidas de errores no determinada
|
#@warning Salidas de errores no determinada
|
||||||
#*/ ##
|
#*/ ##
|
||||||
def ogMountCache():
|
def ogMountCache():
|
||||||
c = ogFindCache().split()
|
c = ogFindCache()
|
||||||
|
if not c:
|
||||||
|
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE)
|
||||||
|
return None
|
||||||
|
|
||||||
|
c = c.split()
|
||||||
m = FileSystemLib.ogMountFs (c[0], c[1])
|
m = FileSystemLib.ogMountFs (c[0], c[1])
|
||||||
if not m:
|
if not m:
|
||||||
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE)
|
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE)
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
from SystemLib import ogHelp
|
||||||
|
from CacheLib import ogGetCacheSpace
|
||||||
|
|
||||||
|
ret = ogGetCacheSpace()
|
||||||
|
|
||||||
|
if ret is not None:
|
||||||
|
if ret == True: sys.exit (0)
|
||||||
|
elif ret == False: sys.exit (1)
|
||||||
|
else: print (ret)
|
Loading…
Reference in New Issue