Blame tests/bootloader-entries-crosscheck.py

Packit Service 2a3f3d
#!/usr/bin/python3
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# Copyright (C) 2015 Red Hat
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# This library is free software; you can redistribute it and/or
Packit Service 2a3f3d
# modify it under the terms of the GNU Lesser General Public
Packit Service 2a3f3d
# License as published by the Free Software Foundation; either
Packit Service 2a3f3d
# version 2 of the License, or (at your option) any later version.
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# This library is distributed in the hope that it will be useful,
Packit Service 2a3f3d
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2a3f3d
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 2a3f3d
# Lesser General Public License for more details.
Packit Service 2a3f3d
#
Packit Service 2a3f3d
# You should have received a copy of the GNU Lesser General Public
Packit Service 2a3f3d
# License along with this library; if not, write to the
Packit Service 2a3f3d
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Service 2a3f3d
# Boston, MA 02111-1307, USA.
Packit Service 2a3f3d
Packit Service 2a3f3d
import os
Packit Service 2a3f3d
import sys
Packit Service 2a3f3d
Packit Service 2a3f3d
if len(sys.argv) == 1:
Packit Service 2a3f3d
    sysroot = ''
Packit Service 2a3f3d
else:
Packit Service 2a3f3d
    sysroot = sys.argv[1]
Packit Service 2a3f3d
Packit Service 2a3f3d
bootloader = sys.argv[2]
Packit Service 2a3f3d
loaderpath = sysroot + '/boot/loader/entries'
Packit Service 2a3f3d
syslinuxpath = sysroot + '/boot/syslinux/syslinux.cfg'
Packit Service 2a3f3d
Packit Service 2a3f3d
if bootloader == "grub2":
Packit Service 2a3f3d
    sys.stdout.write('GRUB2 configuration validation not implemented.\n')
Packit Service 2a3f3d
    sys.exit(0)
Packit Service 2a3f3d
Packit Service 2a3f3d
def fatal(msg):
Packit Service 2a3f3d
    sys.stderr.write(msg)
Packit Service 2a3f3d
    sys.stderr.write('\n')
Packit Service 2a3f3d
    sys.exit(1)
Packit Service 2a3f3d
Packit Service 2a3f3d
def entry_get_version(entry):
Packit Service 2a3f3d
    return int(entry['version'])
Packit Service 2a3f3d
Packit Service 2a3f3d
def get_ostree_option(optionstring):
Packit Service 2a3f3d
    for o in optionstring.split():
Packit Service 2a3f3d
        if o.startswith('ostree='):
Packit Service 2a3f3d
            return o[8:]
Packit Service 2a3f3d
    raise ValueError('ostree= not found')
Packit Service 2a3f3d
            
Packit Service 2a3f3d
entries = []
Packit Service 2a3f3d
syslinux_entries = []
Packit Service 2a3f3d
Packit Service 2a3f3d
# Parse loader configs
Packit Service 2a3f3d
for fname in os.listdir(loaderpath):
Packit Service 2a3f3d
    path = os.path.join(loaderpath, fname)
Packit Service 2a3f3d
    with open(path) as f:
Packit Service 2a3f3d
        entry = {}
Packit Service 2a3f3d
        for line in f:
Packit Service 2a3f3d
            line = line.strip()
Packit Service 2a3f3d
            if (line == '' or line.startswith('#')):
Packit Service 2a3f3d
                continue
Packit Service 2a3f3d
            s = line.find(' ')
Packit Service 2a3f3d
            assert s > 0
Packit Service 2a3f3d
            k = line[0:s]
Packit Service 2a3f3d
            v = line[s+1:]
Packit Service 2a3f3d
            entry[k] = v
Packit Service 2a3f3d
        entries.append(entry)
Packit Service 2a3f3d
    entries.sort(key=entry_get_version, reverse=True)
Packit Service 2a3f3d
Packit Service 2a3f3d
# Parse SYSLINUX config
Packit Service 2a3f3d
with open(syslinuxpath) as f:
Packit Service 2a3f3d
    in_ostree_config = False
Packit Service 2a3f3d
    syslinux_entry = None
Packit Service 2a3f3d
    syslinux_default = None
Packit Service 2a3f3d
    for line in f:
Packit Service 2a3f3d
        line = line.strip()
Packit Service 2a3f3d
        if line.startswith('DEFAULT '):
Packit Service 2a3f3d
            if syslinux_entry is not None:
Packit Service 2a3f3d
                syslinux_default = line.split(' ', 1)[1]
Packit Service 2a3f3d
        elif line.startswith('LABEL '):
Packit Service 2a3f3d
            if syslinux_entry is not None:
Packit Service 2a3f3d
                syslinux_entries.append(syslinux_entry)
Packit Service 2a3f3d
            syslinux_entry = {}
Packit Service 2a3f3d
            syslinux_entry['title'] = line.split(' ', 1)[1]
Packit Service 2a3f3d
        elif line.startswith('KERNEL '):
Packit Service 2a3f3d
            syslinux_entry['linux'] = line.split(' ', 1)[1]
Packit Service 2a3f3d
        elif line.startswith('INITRD '):
Packit Service 2a3f3d
            syslinux_entry['initrd'] = line.split(' ', 1)[1]
Packit Service 2a3f3d
        elif line.startswith('APPEND '):
Packit Service 2a3f3d
            syslinux_entry['options'] = line.split(' ', 1)[1]
Packit Service 2a3f3d
    if syslinux_entry is not None:
Packit Service 2a3f3d
        syslinux_entries.append(syslinux_entry)
Packit Service 2a3f3d
Packit Service 2a3f3d
if len(entries) != len(syslinux_entries):
Packit Service 2a3f3d
    fatal("Found {0} loader entries, but {1} SYSLINUX entries\n".format(len(entries), len(syslinux_entries)))
Packit Service 2a3f3d
Packit Service 2a3f3d
def assert_matches_key(a, b, key):
Packit Service 2a3f3d
    aval = a[key]
Packit Service 2a3f3d
    bval = b[key]
Packit Service 2a3f3d
    if aval != bval:
Packit Service 2a3f3d
        fatal("Mismatch on {0}: {1} != {2}".format(key, aval, bval))
Packit Service 2a3f3d
Packit Service 2a3f3d
for i,(entry,syslinuxentry) in enumerate(zip(entries, syslinux_entries)):
Packit Service 2a3f3d
    assert_matches_key(entry, syslinuxentry, 'linux')
Packit Service 2a3f3d
    assert_matches_key(entry, syslinuxentry, 'initrd')
Packit Service 2a3f3d
    entry_ostree = get_ostree_option(entry['options'])
Packit Service 2a3f3d
    syslinux_ostree = get_ostree_option(syslinuxentry['options'])
Packit Service 2a3f3d
    if entry_ostree != syslinux_ostree:
Packit Service 2a3f3d
        fatal("Mismatch on ostree option: {0} != {1}".format(entry_ostree, syslinux_ostree))
Packit Service 2a3f3d
Packit Service 2a3f3d
sys.stdout.write('SYSLINUX configuration validated\n')
Packit Service 2a3f3d
sys.exit(0)