From b668a526b021ebbd1ac30e22f779d7fd33da27d7 Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Tue, 12 Nov 2024 10:52:26 +0100 Subject: [PATCH] refs #1101 improve ogGetImageInfo() a bit --- client/lib/engine/bin/ImageLib.py | 69 +++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/client/lib/engine/bin/ImageLib.py b/client/lib/engine/bin/ImageLib.py index b9bef13..f4ef326 100644 --- a/client/lib/engine/bin/ImageLib.py +++ b/client/lib/engine/bin/ImageLib.py @@ -149,7 +149,7 @@ def ogRestoreImageSyntax (imgfile, part, tool=None, level=None): 'bzip': ' bzip -dc ', }.get (level, '') print (f'tool ({tool}) level ({level}) compressor ({compressor})') - if compressor is '': + if compressor == '': SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, f'Compressor no valid {level}') ## original bash code is broken: 'return' is never called #return @@ -213,22 +213,38 @@ def ogGetImageInfo (imgfile): lc_all = os.getenv ('LC_ALL') os.environ['LC_ALL'] = 'C' partclone_info = subprocess.run (['partclone.info', filehead], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True).stdout + #partclone_info = subprocess.run (['cat', '/tmp/foo-partclone'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True).stdout + #Partclone v0.3.13 http://partclone.org + #Unknown mode + #File system: NTFS + #Device size: 29.2 GB = 7138019 Blocks + #Space in use: 26.6 GB = 6485355 Blocks + #Free Space: 2.7 GB = 652664 Blocks + #Block size: 4096 Byte + # + #image format: 0002 + #created on a: 64 bits platform + #with partclone: v0.3.13 + #bitmap mode: BIT + #checksum algo: CRC32 + #checksum size: 4 + #blocks/checksum: 256 if lc_all is not None: os.environ["LC_ALL"] = lc_all else: del os.environ["LC_ALL"] + if 'size' in partclone_info: tools = 'PARTCLONE' sizefactor = 1000000 if 'GB' in partclone_info else 1024 - fs_lines = list (filter (lambda l: 'File system' in l, partclone_info.splitlines())) - fs = fs_lines[0].split (':')[1].strip() + m = re.search (r'File system *: *(\S+)', partclone_info) + fs = m.group(1) if m else '' if fs in ['HFS', 'HFSPLUS', 'FAT32']: ## TODO #FSPLUS=$(echo $PARTCLONEINFO | awk '{gsub(/\: /,"\n"); print toupper($9);}') - #echo $PARTCLONEINFO | grep GB > /dev/null && SIZEFACTOR=1000000 || SIZEFACTOR=1024 fsplus = 'PLUS' if fsplus: - fs += fsplus + fs += fsplus ## 'HFS' -> 'HFSPLUS' size = 42 else: size = 42 @@ -242,24 +258,51 @@ def ogGetImageInfo (imgfile): if False == imgdetect and not os.path.exists ('/dev/loop2'): filehead_contents = Path (filehead).read_bytes() + ntfscloneinfo = '' if b'ntfsclone-image' in filehead_contents: print (f'shelling out "cat {filenead} | ntfsclone --restore --overwrite /dev/loop2 - 2>&1"') ntfscloneinfo = subprocess.run (f'cat {filenead} | ntfsclone --restore --overwrite /dev/loop2 - 2>&1', shell=True, capture_output=True, text=True).stdout + #ntfscloneinfo = subprocess.run (['cat', '/tmp/foo-ntfsclone'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True).stdout + if 'ntfsclone' in ntfscloneinfo: tools = 'NTFSCLONE' - size_lines = list (filter (lambda l: '__TODO__' in l, ntfscloneinfo.splitlines())) ## TODO - size = 42 #int (size_lines[0].split (':')[1].strip()) ## TODO + m = re.search (r'__TODO__ *: *(\S+)', ntfscloneinfo) ## TODO + size = 42 #float (m.group(1))/1000 if m else 0 ## TODO fs = 'NTFS' imgdetect = True if False == imgdetect: partimageinfo = subprocess.run (['partimage', '-B', 'gui=no', 'imginfo', filehead], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True).stdout + #partimageinfo = subprocess.run (['cat', '/tmp/foo-partimage'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True).stdout + #Volume number:.........0 + #Volume size:...........1,27 MiB + #Compression level: ....0 -> ninguno + #Identificator:.........12442509728668372730=ACACACACCB9ECEFA + #Filesystem:............ntfs + #Description:...........Sin descripcion + #Original device:......./dev/nvme0n1p2 + #Original filepath:.... stdout + #Flags:.................0: Bandera sin activar + #Creation date:.........Mon Nov 11 21:00:22 2024 + #Partition size:........476,84 GiB + #Hostname:..............ING-LTR-083 + #Compatible Version:....0.6.1 + #Encryption algorithm:..0 -> ninguno + #MBR saved count:.......0 + print (f'partimageinfo bef ({partimageinfo})') + partimageinfo = re.sub (r':\s*\.+', ' : ', partimageinfo) + print (f'partimageinfo aft ({partimageinfo})') if 'Partition' in partimageinfo: - tools = 'TOOLS=PARTIMAGE' - fs_lines = list (filter (lambda l: '__TODO__' in l, partimageinfo.splitlines())) ## TODO - fs = 'EXTFS' #fs_lines[0].split (':')[1].strip() ## TODO - size_lines = list (filter (lambda l: '__TODO__' in l, partimageinfo.splitlines())) ## TODO - size = 42 #int (size_lines[0].split (':')[1].strip()) ## TODO + tools = 'PARTIMAGE' + m = re.search (r'Filesystem *: *(\S+)', partimageinfo) + fs = m.group(1).upper() if m else '' + + m = re.search (r'Partition size *: *(\S+)', partimageinfo) + size = m.group(1) if m else '' + size = re.sub (r' [MGT]i?B$', '', size) + size = float (size.replace (',', '.')) + size = int (size*1024*1024) + imgdetect = True if 'boot sector' in subprocess.run (['file', filehead], capture_output=True, text=True).stdout: tools = 'partclone.dd' @@ -271,5 +314,5 @@ def ogGetImageInfo (imgfile): SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_IMAGE, f'Image format is not valid {imgfile}') return - compressor = compressor.lower() + compressor = compressor.upper() return ':'.join ([tools, compressor, fs, str (size)])