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
master
Alejandro Sirgo Rica 2024-07-25 16:37:06 +02:00
parent adbf02d170
commit 210a70fc73
1 changed files with 22 additions and 0 deletions

View File

@ -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():