-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_default_grub_version.py
62 lines (48 loc) · 2.05 KB
/
set_default_grub_version.py
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
55
56
57
58
59
60
61
62
import os
import re
import subprocess
class GrubConf:
"""Handles operations related to the GRUB configuration file."""
def __init__(self, path):
"""Initialize with the path to the GRUB configuration file."""
if not os.path.exists(path):
raise FileNotFoundError(f"GRUB configuration file does not exist at {path}")
self.path = path
def get_kernel_versions(self):
"""Extract kernel versions from the configuration file."""
pattern = re.compile(r"kernel\s+/boot/vmlinuz-([\d.]+)")
with open(self.path) as file:
return [match.group(1) for match in pattern.finditer(file.read()) if match.group(1)]
def set_default_kernel(self, version):
"""Set the specified kernel version as default in the configuration file."""
sed_command = ["/usr/bin/sed", "-i.bak", f"s/default=.*/default={version}/", self.path]
try:
subprocess.run(sed_command, check=True)
except subprocess.CalledProcessError as e:
raise RuntimeError("Failed to update the GRUB configuration") from e
class KernelManager:
"""Manages kernel versions for the application."""
def __init__(self, grub_conf):
"""Initialize with a GrubConf instance."""
self.grub_conf = grub_conf
def set_highest_kernel_as_default(self):
"""Set the highest kernel version as the default one."""
versions = self.grub_conf.get_kernel_versions()
if not versions:
print("No kernel versions found in GRUB configuration.")
return
highest_version = max(versions)
self.grub_conf.set_default_kernel(highest_version)
print(f"Successfully set default kernel to {highest_version}.")
def main():
GRUB_PATH = "/etc/grub.conf"
try:
grub_conf = GrubConf(GRUB_PATH)
kernel_manager = KernelManager(grub_conf)
kernel_manager.set_highest_kernel_as_default()
except FileNotFoundError as e:
print(e)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
main()