Skip to content

Commit 9386963

Browse files
authored
Improve set/get cmdline to support uefi (#4062)
Improve set/get cmdline to support uefi What I did Fix sonic-net/sonic-buildimage#23820 sonic-installer get-fips/set-fips not working on systems in UEFI mode How I did it Improve get_linux_cmdline and set_linux_cmdline to support linuxefi How to verify it Manually verify. Pass all test case Work item tracking Microsoft ADO: 34847974
1 parent 3a7d0b4 commit 9386963

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

sonic_installer/bootloader/grub.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
from .onie import OnieInstallerBootloader
2020

2121
PLATFORMS_ASIC = "installer/platforms_asic"
22+
BOOT_PARAMETER_PREFIX_LINUX = "linux "
23+
BOOT_PARAMETER_PREFIX_LINUXEFI = "linuxefi "
24+
LEN_BOOT_PARAMETER_PREFIX_LINUX = len(BOOT_PARAMETER_PREFIX_LINUX)
25+
LEN_BOOT_PARAMETER_PREFIX_LINUXEFI = len(BOOT_PARAMETER_PREFIX_LINUXEFI)
2226

2327
class GrubBootloader(OnieInstallerBootloader):
2428

@@ -93,8 +97,11 @@ def get_linux_cmdline(self, image):
9397
config.close()
9498
for line in menuentry.split('\n'):
9599
line = line.strip()
96-
if line.startswith('linux '):
97-
cmdline = line[6:].strip()
100+
if line.startswith(BOOT_PARAMETER_PREFIX_LINUXEFI):
101+
cmdline = line[LEN_BOOT_PARAMETER_PREFIX_LINUXEFI:].strip()
102+
break
103+
elif line.startswith(BOOT_PARAMETER_PREFIX_LINUX):
104+
cmdline = line[LEN_BOOT_PARAMETER_PREFIX_LINUX:].strip()
98105
break
99106
return cmdline
100107

@@ -106,8 +113,11 @@ def set_linux_cmdline(self, image, cmdline):
106113
new_menuentry = old_menuentry
107114
for line in old_menuentry.split('\n'):
108115
line = line.strip()
109-
if line.startswith('linux '):
110-
new_menuentry = old_menuentry.replace(line, "linux " + cmdline)
116+
if line.startswith(BOOT_PARAMETER_PREFIX_LINUXEFI):
117+
new_menuentry = old_menuentry.replace(line, BOOT_PARAMETER_PREFIX_LINUXEFI + cmdline)
118+
break
119+
elif line.startswith(BOOT_PARAMETER_PREFIX_LINUX):
120+
new_menuentry = old_menuentry.replace(line, BOOT_PARAMETER_PREFIX_LINUX + cmdline)
111121
break
112122
config = open(HOST_PATH + '/grub/grub.cfg', 'w')
113123
config.write(old_config.replace(old_menuentry, new_menuentry))

tests/installer_bootloader_grub_test.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def test_set_fips_grub():
6868
grub_config = os.path.join(current_path, 'installer_bootloader_input/host/grub/grub.cfg')
6969
tmp_host_path = os.path.join(current_path, 'installer_bootloader_input/_tmp_host')
7070
tmp_grub_path = os.path.join(tmp_host_path, 'grub')
71-
tmp_grub_config = os.path.join(tmp_grub_path, 'grub.cfg')
7271
os.makedirs(tmp_grub_path, exist_ok=True)
7372
shutil.copy(grub_config, tmp_grub_path)
7473

@@ -89,6 +88,41 @@ def test_set_fips_grub():
8988
# Cleanup the _tmp_host folder
9089
shutil.rmtree(tmp_host_path)
9190

91+
92+
@patch(
93+
"sonic_installer.bootloader.grub.HOST_PATH",
94+
os.path.join(
95+
os.path.dirname(os.path.abspath(__file__)),
96+
'installer_bootloader_input/_tmp_host_efi'
97+
)
98+
)
99+
def test_set_fips_grub_efi():
100+
# Prepare the grub.cfg in the _tmp_host folder
101+
current_path = os.path.dirname(os.path.abspath(__file__))
102+
grub_config = os.path.join(current_path, 'installer_bootloader_input/host/grub/grub_efi.cfg')
103+
tmp_host_path = os.path.join(current_path, 'installer_bootloader_input/_tmp_host_efi')
104+
tmp_grub_path = os.path.join(tmp_host_path, 'grub')
105+
os.makedirs(tmp_grub_path, exist_ok=True)
106+
tmp_grub_config = os.path.join(tmp_grub_path, 'grub.cfg')
107+
shutil.copy(grub_config, tmp_grub_config)
108+
109+
image = 'SONiC-OS-internal-202205.57377412-84a9a7f11b'
110+
bootloader = grub.GrubBootloader()
111+
112+
# The the default setting
113+
assert not bootloader.get_fips(image)
114+
115+
# Test fips enabled
116+
bootloader.set_fips(image, True)
117+
assert bootloader.get_fips(image)
118+
119+
# Test fips disabled
120+
bootloader.set_fips(image, False)
121+
assert not bootloader.get_fips(image)
122+
123+
# Cleanup the _tmp_host folder
124+
shutil.rmtree(tmp_host_path)
125+
92126
def test_verify_image():
93127

94128
bootloader = grub.GrubBootloader()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
serial --port=0x3f8 --speed=9600 --word=8 --parity=no --stop=1
2+
terminal_input console serial
3+
terminal_output console serial
4+
5+
set timeout=5
6+
7+
if [ -s $prefix/grubenv ]; then
8+
load_env
9+
fi
10+
if [ "${saved_entry}" ]; then
11+
set default="${saved_entry}"
12+
fi
13+
if [ "${next_entry}" ]; then
14+
set default="${next_entry}"
15+
unset next_entry
16+
save_env next_entry
17+
fi
18+
if [ "${onie_entry}" ]; then
19+
set next_entry="${default}"
20+
set default="${onie_entry}"
21+
unset onie_entry
22+
save_env onie_entry next_entry
23+
fi
24+
25+
menuentry 'SONiC-OS-internal-202205.57377412-84a9a7f11b' {
26+
search --no-floppy --label --set=root SONiC-OS
27+
echo 'Loading SONiC-OS OS kernel ...'
28+
insmod gzio
29+
if [ x = xxen ]; then insmod xzio; insmod lzopio; fi
30+
insmod part_msdos
31+
insmod ext2
32+
linuxefi /image-internal-202205.57377412-84a9a7f11b/boot/vmlinuz-5.10.0-12-2-amd64 root=UUID=df89970c-bf6d-40cf-80fc-a977c89054dd rw console=tty0 console=ttyS0,9600n8 quiet intel_idle.max_cstate=0 net.ifnames=0 biosdevname=0 loop=image-internal-202205.57377412-84a9a7f11b/fs.squashfs loopfstype=squashfs systemd.unified_cgroup_hierarchy=0 apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 acpi_enforce_resources=lax acpi=noirq
33+
echo 'Loading SONiC-OS OS initial ramdisk ...'
34+
initrd /image-internal-202205.57377412-84a9a7f11b/boot/initrd.img-5.10.0-12-2-amd64
35+
}
36+
menuentry 'SONiC-OS-master-11298.116581-1a4f95389' {
37+
search --no-floppy --label --set=root SONiC-OS
38+
echo 'Loading SONiC-OS OS kernel ...'
39+
insmod gzio
40+
if [ x = xxen ]; then insmod xzio; insmod lzopio; fi
41+
insmod part_msdos
42+
insmod ext2
43+
linuxefi /image-master-11298.116581-1a4f95389/boot/vmlinuz-5.10.0-12-2-amd64 root=UUID=df89970c-bf6d-40cf-80fc-a977c89054dd rw console=tty0 console=ttyS0,9600n8 quiet intel_idle.max_cstate=0 sonic_fips=1 net.ifnames=0 biosdevname=0 loop=image-master-11298.116581-1a4f95389/fs.squashfs loopfstype=squashfs systemd.unified_cgroup_hierarchy=0 apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 acpi_enforce_resources=lax acpi=noirq
44+
echo 'Loading SONiC-OS OS initial ramdisk ...'
45+
initrd /image-master-11298.116581-1a4f95389/boot/initrd.img-5.10.0-12-2-amd64
46+
}
47+
menuentry ONIE {
48+
search --no-floppy --label --set=root ONIE-BOOT
49+
echo 'Loading ONIE ...'
50+
chainloader +1
51+
}

0 commit comments

Comments
 (0)