src: check if the system is hibernated before /image/create

Mount the system partition in readonly mode and check for the
hiberfil.sys file if the target system is a Windows.

Fail the image creation process if the target system is hibernated.
master
Alejandro Sirgo Rica 2024-10-02 12:13:25 +02:00
parent a2baad8c0b
commit cc70274079
3 changed files with 24 additions and 4 deletions

View File

@ -648,6 +648,16 @@ class OgLiveOperations:
if not fstype: if not fstype:
raise OgError(f'No filesystem detected in {padev}. Aborting image creation') raise OgError(f'No filesystem detected in {padev}. Aborting image creation')
ro_mountpoint = padev.replace('dev', 'mnt')
if not mount_mkdir(padev, ro_mountpoint, readonly=True):
raise OgError(f'Cannot mount {ro_mountpoint} as readonly')
try:
if is_hibernation_enabled(ro_mountpoint):
raise OgError(f'Target system in {padev} has hibernation enabled')
finally:
umount(ro_mountpoint)
if change_access(user=self._smb_user, pwd=self._smb_pass) == -1: if change_access(user=self._smb_user, pwd=self._smb_pass) == -1:
raise OgError('remount of /opt/opengnsys/images has failed') raise OgError('remount of /opt/opengnsys/images has failed')

View File

@ -29,7 +29,7 @@ def find_mountpoint(path):
return path return path
def mount_mkdir(source, target): def mount_mkdir(source, target, readonly=False):
""" """
Mounts and creates the mountpoint directory if it's not present. Mounts and creates the mountpoint directory if it's not present.
@ -43,18 +43,21 @@ def mount_mkdir(source, target):
return False return False
if not os.path.ismount(target): if not os.path.ismount(target):
return mount(source, target) return mount(source, target, readonly)
return True return True
def mount(source, target): def mount(source, target, readonly):
""" """
Mounts source into target directoru using mount(8). Mounts source into target directoru using mount(8).
Return true if exit code is 0. False otherwise. Return true if exit code is 0. False otherwise.
""" """
cmd = f'mount {source} {target}' if readonly:
cmd = f'mount -o ro {source} {target}'
else:
cmd = f'mount {source} {target}'
proc = subprocess.run(cmd.split(), stderr=DEVNULL) proc = subprocess.run(cmd.split(), stderr=DEVNULL)
return not proc.returncode return not proc.returncode

View File

@ -178,3 +178,10 @@ def os_probe(mountpoint):
return getwindowsversion(winreghives) return getwindowsversion(winreghives)
else: else:
return 'unknown' return 'unknown'
def is_hibernation_enabled(mountpoint):
if get_os_family(mountpoint) != OSFamily.WINDOWS:
return False
hiberfile_path = f'{mountpoint}/hiberfil.sys'
return os.path.exists(hiberfile_path)