From 567fea276e16902be01f80cfb7be335f8d414622 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Mon, 22 Apr 2024 10:59:55 +0200 Subject: utils: add postinstall.py Replace ConfigureOs script by native calls to: - update BCD via hivex using bcd.py and winreg.py infrastructure. - restore efi bootloader restore_windows_efi_bootloader(). Call legacy scripts for remaining postinstall procedures to replace them incrementally. Define variable CONFIGUREOS_LEGACY_ENABLED as False by default. Run legacy configureOs when CONFIGUREOS_LEGACY_ENABLED = True. This serves as a auxiliar method to keep the restoration functional in case of problems with the new configure_os logic. --- src/live/ogOperations.py | 9 +-- src/utils/image.py | 21 ------ src/utils/postinstall.py | 170 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 25 deletions(-) create mode 100644 src/utils/postinstall.py diff --git a/src/live/ogOperations.py b/src/live/ogOperations.py index 7958e9e..d4817f6 100644 --- a/src/live/ogOperations.py +++ b/src/live/ogOperations.py @@ -23,6 +23,7 @@ from src.live.partcodes import GUID_MAP from src.live.parttypes import get_parttype from src.utils.image import * +from src.utils.postinstall import configure_os from src.utils.net import ethtool from src.utils.menu import generate_menu from src.utils.fs import * @@ -455,14 +456,14 @@ class OgLiveOperations: return result def image_restore(self, request, ogRest): - disk = request.getDisk() - partition = request.getPartition() + disk = int(request.getDisk()) + partition = int(request.getPartition()) name = request.getName() repo = request.getRepo() ctype = request.getType() profile = request.getProfile() cid = request.getId() - partdev = get_partition_device(int(disk), int(partition)) + partdev = get_partition_device(disk, partition) self._ogbrowser_clear_logs() self._restartBrowser(self._url_log) @@ -477,7 +478,7 @@ class OgLiveOperations: elif ctype == 'TIPTORRENT': self._restore_image_tiptorrent(repo, name, partdev) - configureOs(disk, partition) + configure_os(disk, partition) self.refresh(ogRest) diff --git a/src/utils/image.py b/src/utils/image.py index fe48a9c..a12c221 100644 --- a/src/utils/image.py +++ b/src/utils/image.py @@ -234,24 +234,3 @@ def restoreImageCustom(repo_ip, image_name, disk, partition, method): except OSError as e: raise OgError(f'Error processing restoreImageCustom: {e}') from e return proc.returncode - - -def configureOs(disk, partition): - """ - """ - if shutil.which('configureOsCustom'): - cmd_configure = f"configureOsCustom {disk} {partition}" - else: - cmd_configure = f"configureOs {disk} {partition}" - - try: - proc = subprocess.run(cmd_configure, - stdout=PIPE, - encoding='utf-8', - shell=True, - check=True) - out = proc.stdout - except OSError as e: - raise OgError(f'Error processing configureOsCustom: {e}') from e - - return out diff --git a/src/utils/postinstall.py b/src/utils/postinstall.py new file mode 100644 index 0000000..f6332b0 --- /dev/null +++ b/src/utils/postinstall.py @@ -0,0 +1,170 @@ +# +# Copyright (C) 2022 Soleta Networks +# +# This program is free software: you can redistribute it and/or modify it under +# the terms of the GNU Affero General Public License as published by the +# Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. + +import subprocess +import shutil +import logging +import hivex +from src.log import OgError +from src.utils.bcd import update_bcd +from src.utils.probe import * +from src.utils.disk import * +from src.utils.winreg import * +from src.utils.fs import * +from src.utils.uefi import * +from socket import gethostname + +CONFIGUREOS_LEGACY_ENABLED = False + + +def configure_os_custom(disk, partition): + if not shutil.which('configureOsCustom'): + raise OgError('configureOsCustom not found') + + cmd_configure = f"configureOsCustom {disk} {partition}" + + try: + proc = subprocess.run(cmd_configure, + stdout=subprocess.PIPE, + encoding='utf-8', + shell=True, + check=True) + out = proc.stdout + except OSError as e: + raise OgError(f'Error processing configureOsCustom: {e}') from e + + +def windows_register_c_drive(disk, partition): + cmd_configure = f"ogWindowsRegisterPartition {disk} {partition} C {disk} {partition}" + + try: + proc = subprocess.run(cmd_configure, + stdout=subprocess.PIPE, + encoding='utf-8', + shell=True, + check=True) + except OSError as e: + raise OgError(f'Error processing ogWindowsRegisterPartition: {e}') from e + + +def configure_mbr_boot_sector(disk, partition): + cmd_configure = f"ogFixBootSector {disk} {partition}" + + try: + proc = subprocess.run(cmd_configure, + stdout=subprocess.PIPE, + encoding='utf-8', + shell=True, + check=True) + except OSError as e: + raise OgError(f'Error processing ogFixBootSector: {e}') from e + + +def configure_grub_in_mbr(disk, partition): + cmd_configure = f"ogGrubInstallMbr {disk} {partition} TRUE" + + try: + proc = subprocess.run(cmd_configure, + stdout=subprocess.PIPE, + encoding='utf-8', + shell=True, + check=True) + except OSError as e: + raise OgError(f'Error processing ogGrubInstallMbr: {e}') from e + + +def configure_fstab(disk, partition): + cmd_configure = f"ogConfigureFstab {disk} {partition}" + + try: + proc = subprocess.run(cmd_configure, + stdout=subprocess.PIPE, + encoding='utf-8', + shell=True, + check=True) + except OSError as e: + raise OgError(f'Error processing ogConfigureFstab: {e}') from e + + +def install_grub(disk, partition): + cmd_configure = f"ogGrubInstallPartition {disk} {partition}" + + try: + proc = subprocess.run(cmd_configure, + stdout=subprocess.PIPE, + encoding='utf-8', + shell=True, + check=True) + except OSError as e: + raise OgError(f'Error processing ogGrubInstallPartition: {e}') from e + + +def configure_os_linux(disk, partition): + configure_fstab(disk, partition) + + if is_uefi_supported(): + _, _, esp_part_number = get_efi_partition(disk, enforce_gpt=True) + configure_grub_in_mbr(disk, esp_part_number) + + install_grub(disk, partition) + + +def configure_os_windows(disk, partition): + if is_uefi_supported(): + restore_windows_efi_bootloader(disk, partition) + + _, _, esp_part_number = get_efi_partition(disk, enforce_gpt=True) + configure_grub_in_mbr(disk, esp_part_number) + else: + configure_mbr_boot_sector(disk, partition) + + update_bcd(disk, partition) + windows_register_c_drive(disk, partition) + + +def configure_os_legacy(disk, partition): + cmd_configure = f"configureOs {disk} {partition}" + + try: + proc = subprocess.run(cmd_configure, + stdout=subprocess.PIPE, + encoding='utf-8', + shell=True, + check=True) + out = proc.stdout + except OSError as e: + raise OgError(f'Error processing configureOs: {e}') from e + + +def configure_os(disk, partition): + if CONFIGUREOS_LEGACY_ENABLED: + configure_os_legacy(disk, partition) + + if shutil.which('configureOsCustom'): + configure_os_custom(disk, partition) + + return + + device = get_partition_device(disk, partition) + mountpoint = device.replace('dev', 'mnt') + + if not mount_mkdir(device, mountpoint): + raise OgError(f'Cannot probe OS family. Unable to mount {device} into {mountpoint}') + + os_family = get_os_family(mountpoint) + umount(mountpoint) + + extend_filesystem(disk, partition) + + if os_family == OSFamily.WINDOWS: + configure_os_windows(disk, partition) + elif os_family == OSFamily.LINUX: + configure_os_linux(disk, partition) + + if shutil.which('configureOsCustom'): + configure_os_custom(disk, partition) -- cgit v1.2.3-18-g5258