2020-07-20 06:14:47 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-03-17 16:00:37 +00:00
|
|
|
|
|
|
|
# Copyright 2017 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2019-09-04 13:34:04 +00:00
|
|
|
import glob
|
2019-08-13 01:13:46 +00:00
|
|
|
import logging
|
2017-03-17 16:00:37 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
2019-08-13 01:13:46 +00:00
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.DEBUG if int(os.getenv('DIB_DEBUG_TRACE', 0)) > 0
|
|
|
|
else logging.INFO)
|
|
|
|
|
|
|
|
|
2017-03-17 16:00:37 +00:00
|
|
|
def main():
|
2019-08-13 01:13:46 +00:00
|
|
|
logging.info("Starting dracut regeneration")
|
|
|
|
logging.debug("Debug logging enabled")
|
|
|
|
|
2017-03-17 16:00:37 +00:00
|
|
|
dracut_env = os.getenv('DIB_DRACUT_ENABLED_MODULES')
|
|
|
|
dracut_objects = yaml.safe_load(dracut_env)
|
|
|
|
|
2019-08-13 01:13:46 +00:00
|
|
|
logging.debug(dracut_objects)
|
|
|
|
|
2017-03-17 16:00:37 +00:00
|
|
|
modules_to_boot = []
|
|
|
|
for dracut_object in dracut_objects:
|
|
|
|
# first, install dependent packages
|
|
|
|
packages = dracut_object.get('packages', [])
|
|
|
|
for package in packages:
|
|
|
|
cmdline = ["install-packages", package]
|
2019-08-13 01:13:46 +00:00
|
|
|
logging.debug("Calling: %s" % cmdline)
|
2017-03-17 16:00:37 +00:00
|
|
|
subp = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
|
|
|
|
out = subp.communicate()[0]
|
|
|
|
if subp.returncode:
|
|
|
|
e = subprocess.CalledProcessError(subp.returncode, cmdline)
|
|
|
|
e.output = out
|
|
|
|
raise e
|
|
|
|
|
|
|
|
# second, compose the list of modules to boot
|
|
|
|
modules_to_boot.append(dracut_object.get('name', None))
|
|
|
|
|
2019-09-04 13:34:04 +00:00
|
|
|
conf_overrides = glob.glob('/etc/dracut.conf.d/*.conf')
|
|
|
|
|
|
|
|
# regenerate dracut with the list of installed modules and conf overrides
|
|
|
|
if modules_to_boot or conf_overrides:
|
2017-03-17 16:00:37 +00:00
|
|
|
cmdline = ["select-boot-kernel-initrd"]
|
2019-08-13 01:13:46 +00:00
|
|
|
logging.debug("Calling: %s" % cmdline)
|
2017-03-17 16:00:37 +00:00
|
|
|
subp = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
|
|
|
|
out, err = subp.communicate()
|
|
|
|
if subp.returncode:
|
|
|
|
e = subprocess.CalledProcessError(subp.returncode, cmdline)
|
|
|
|
e.output = out
|
|
|
|
raise e
|
|
|
|
|
2017-05-30 14:57:57 +00:00
|
|
|
kernel_set = out.decode().split(':')
|
2017-03-17 16:00:37 +00:00
|
|
|
kernel_search = re.match("vmlinuz-(.*)", kernel_set[0])
|
|
|
|
kernel_version = "%s" % kernel_search.groups(1)
|
|
|
|
ramdisk_path = "/boot/%s" % kernel_set[1].strip()
|
|
|
|
|
2020-12-09 14:35:18 +00:00
|
|
|
# list installed modules of dracut
|
|
|
|
cmdline = ['dracut', '--list-modules']
|
2021-01-12 07:40:30 +00:00
|
|
|
cmdline += [ramdisk_path, kernel_version]
|
2020-12-09 14:35:18 +00:00
|
|
|
logging.debug("Calling: %s" % cmdline)
|
|
|
|
subp = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
|
|
|
|
out, err = subp.communicate()
|
|
|
|
if subp.returncode:
|
|
|
|
e = subprocess.CalledProcessError(subp.returncode, cmdline)
|
|
|
|
e.output = out
|
|
|
|
raise e
|
|
|
|
|
2019-09-04 13:34:04 +00:00
|
|
|
cmdline = ['dracut', '--force']
|
2020-12-09 14:35:18 +00:00
|
|
|
|
2019-09-04 13:34:04 +00:00
|
|
|
if modules_to_boot:
|
|
|
|
modules_to_boot = ' ' .join(modules_to_boot)
|
|
|
|
cmdline += ['--add', modules_to_boot]
|
|
|
|
cmdline += ['-f', ramdisk_path, kernel_version]
|
2019-08-13 01:13:46 +00:00
|
|
|
logging.debug("Calling: %s" % cmdline)
|
|
|
|
subp = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
|
|
|
|
out, err = subp.communicate()
|
|
|
|
if subp.returncode:
|
|
|
|
e = subprocess.CalledProcessError(subp.returncode, cmdline)
|
|
|
|
e.output = out
|
|
|
|
raise e
|
|
|
|
|
|
|
|
logging.info("dracut regenerate done")
|
2017-03-17 16:00:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2019-08-13 01:13:46 +00:00
|
|
|
|
|
|
|
# Local Variables:
|
|
|
|
# mode: python
|
|
|
|
# End:
|