Skip to content

Commit 3a71478

Browse files
committed
bootloader: Reject initrd without kernel
Fail when parsing a menu entry that specifies an initrd without a preceding kernel since it is invalid according to GRUB2. Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
1 parent 8207c34 commit 3a71478

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
menuentry 'Linux - Safe Mode' {
2+
initrd /boot/initrd.img-1
3+
}

tests/test_bootloader.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def test_no_multiboot(self):
2929
# A module2 line without a multiboot2 line is an error
3030
with self.assertRaises(RuntimeError):
3131
Bootloader.readGrub2("tests/data/grub-no-multiboot.cfg")
32-
3332
class TestLinuxBootloader(unittest.TestCase):
3433
def setUp(self):
3534
self.tmpdir = mkdtemp(prefix="testbl")
@@ -73,6 +72,12 @@ def test_grub2_newdefault(self):
7372
assert bl.menu["safe"].kernel_args == "ro"
7473
assert bl.menu["safe"].initrd == "/boot/initrd.img-2"
7574

75+
def test_no_kernel(self):
76+
# An initrd line without a kernel line is an error
77+
with self.assertRaises(RuntimeError):
78+
Bootloader.readGrub2("tests/data/grub-linux-no-kernel.cfg")
79+
80+
7681
class TestBootloaderAdHoc(unittest.TestCase):
7782
def setUp(self):
7883
self.bl = Bootloader.readGrub2("tests/data/grub.cfg")

xcp/bootloader.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ def parse_boot_entry(line):
203203
elif l.startswith("linux"):
204204
kernel, kernel_args = parse_boot_entry(l)
205205
elif l.startswith("initrd"):
206+
if not kernel:
207+
raise RuntimeError("Need a kernel")
206208
initrd = l.split(None, 1)[1]
207209
elif l.startswith("search --label --set root"):
208210
root = l.split()[4]

0 commit comments

Comments
 (0)