From 210a70fc73ff5a1ef21da914eee320d969a95b06 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Thu, 25 Jul 2024 16:37:06 +0200 Subject: utils: add set_linux_hostname Add set_linux_hostname function to redefine the hostname of a Linux install by overwriting the contents of /etc/hostname --- src/utils/postinstall.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/utils/postinstall.py b/src/utils/postinstall.py index 7fef437..374a1cd 100644 --- a/src/utils/postinstall.py +++ b/src/utils/postinstall.py @@ -66,6 +66,25 @@ def set_windows_hostname(disk, partition, name): umount(mountpoint) +def set_linux_hostname(disk, partition, name): + logging.info(f'Configuring hostname') + + device = get_partition_device(disk, partition) + mountpoint = device.replace('dev', 'mnt') + + if not mount_mkdir(device, mountpoint): + raise OgError(f'Unable to mount {device} into {mountpoint}') + + try: + hostname_path = f'{mountpoint}/etc/hostname' + with open(hostname_path, 'w') as f: + f.write(name) + except OSError as e: + raise OgError(f'Unable to configure Linux hostname: {e}') from e + finally: + umount(mountpoint) + + def configure_os_custom(disk, partition): if not shutil.which('configureOsCustom'): raise OgError('configureOsCustom not found') @@ -149,6 +168,9 @@ def install_grub(disk, partition): def configure_os_linux(disk, partition): + hostname = gethostname() + set_linux_hostname(disk, partition, hostname) + configure_fstab(disk, partition) if is_uefi_supported(): -- cgit v1.2.3-18-g5258