summaryrefslogtreecommitdiffstats
path: root/src/utils/bios.py
blob: 9c62b38886c2562886d5a9e93d152a11ddd0dedc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#
# Copyright (C) 2023 Soleta Networks <info@soleta.eu>
#
# 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 logging
import os
from src.log import OgError


def get_grub_boot_params(mountpoint, device):
    grub_conf = f'{mountpoint}/etc/default/grub'
    res = []

    with open(grub_conf, 'r') as f:
        for line in f:
            if line.find('=') == -1:
                continue
            key, value = line.split('=', 1)
            if key == 'GRUB_CMDLINE_LINUX' or key == 'GRUB_CMDLINE_LINUE_DEFAULT':
                value = value.replace('\n', '')
                value = value.strip('"')
                res.append(value)
    res.append(f'root={device}')
    return " ".join(res)

def get_vmlinuz_path(mountpoint):
    linuz_dir = os.path.join(mountpoint, 'boot')
    target_file = None

    for file in sorted(os.listdir(linuz_dir)):
        if file.startswith('vmlinuz-'):
            target_file = file

    if not target_file:
        raise OgError(f'vmlinuz not found in {initrd_dir}')

    return os.path.join(linuz_dir, target_file)

def get_initrd_path(mountpoint):
    initrd_dir = os.path.join(mountpoint, 'boot')
    target_file = None

    for file in sorted(os.listdir(initrd_dir)):
        if file.startswith('initrd.img-') or file.startswith('initramfs-') and file.endswith('.img'):
            target_file = file

    if not target_file:
        raise OgError(f'initrd not found in {initrd_dir}')

    return os.path.join(initrd_dir, target_file)