From f23ea63341b6e332a9a6e5c0c446b2a057ce0d55 Mon Sep 17 00:00:00 2001 From: Yolanda Robla Date: Fri, 17 Mar 2017 17:00:37 +0100 Subject: [PATCH] Add dracut-regenerate elements This new element will allow to regenerate dracut on the produced images, to enable different modules. It relies on a yaml blob to specify modules and packages needed. It defaults to installing lvm and crypt. Change-Id: I292fb70cde41ee6053b7b81a67931bcdaaa6d664 --- .../elements/dracut-regenerate/README.rst | 18 +++++ .../elements/dracut-regenerate/element-deps | 3 + .../environment.d/10-dracut-regenerate | 11 +++ .../finalise.d/98-dracut-regenerate | 67 +++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 diskimage_builder/elements/dracut-regenerate/README.rst create mode 100644 diskimage_builder/elements/dracut-regenerate/element-deps create mode 100644 diskimage_builder/elements/dracut-regenerate/environment.d/10-dracut-regenerate create mode 100755 diskimage_builder/elements/dracut-regenerate/finalise.d/98-dracut-regenerate diff --git a/diskimage_builder/elements/dracut-regenerate/README.rst b/diskimage_builder/elements/dracut-regenerate/README.rst new file mode 100644 index 00000000..759d6b11 --- /dev/null +++ b/diskimage_builder/elements/dracut-regenerate/README.rst @@ -0,0 +1,18 @@ +================= +dracut-regenerate +================= +Adds the possibility of regenerating dracut on image build time, giving the +possibility to load extra modules. +It relies on the ``DIB_DRACUT_ENABLED_MODULES`` setting, that will accept +a yaml blob with the following format:: + + - name: + packages: + - + - + - name: + packages: + - + - + +By default, this element will bring lvm and crypt modules. diff --git a/diskimage_builder/elements/dracut-regenerate/element-deps b/diskimage_builder/elements/dracut-regenerate/element-deps new file mode 100644 index 00000000..75e430d4 --- /dev/null +++ b/diskimage_builder/elements/dracut-regenerate/element-deps @@ -0,0 +1,3 @@ +package-installs +select-boot-kernel-initrd + diff --git a/diskimage_builder/elements/dracut-regenerate/environment.d/10-dracut-regenerate b/diskimage_builder/elements/dracut-regenerate/environment.d/10-dracut-regenerate new file mode 100644 index 00000000..f5fff02d --- /dev/null +++ b/diskimage_builder/elements/dracut-regenerate/environment.d/10-dracut-regenerate @@ -0,0 +1,11 @@ +export DIB_DRACUT_ENABLED_MODULES_DEFAULT_CONFIG=" +- name: crypt + packages: + - cryptsetup +- name: lvm + packages: + - lvm2 +" + +DIB_DRACUT_ENABLED_MODULES=${DIB_DRACUT_ENABLED_MODULES:-${DIB_DRACUT_ENABLED_MODULES_DEFAULT_CONFIG}} +export DIB_DRACUT_ENABLED_MODULES diff --git a/diskimage_builder/elements/dracut-regenerate/finalise.d/98-dracut-regenerate b/diskimage_builder/elements/dracut-regenerate/finalise.d/98-dracut-regenerate new file mode 100755 index 00000000..3ad7cf0e --- /dev/null +++ b/diskimage_builder/elements/dracut-regenerate/finalise.d/98-dracut-regenerate @@ -0,0 +1,67 @@ +#!/usr/local/bin/dib-python + +# 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. + +import os +import re +import subprocess +import yaml + + +def main(): + dracut_env = os.getenv('DIB_DRACUT_ENABLED_MODULES') + dracut_objects = yaml.safe_load(dracut_env) + + 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] + 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)) + + # regenerate dracut with the list of installed modules + if len(modules_to_boot) > 0: + cmdline = ["select-boot-kernel-initrd"] + 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 + + kernel_set = out.split(':') + kernel_search = re.match("vmlinuz-(.*)", kernel_set[0]) + kernel_version = "%s" % kernel_search.groups(1) + ramdisk_path = "/boot/%s" % kernel_set[1].strip() + modules_to_boot = ' ' .join(modules_to_boot) + + subp = subprocess.Popen(['dracut', '--force', '--add', modules_to_boot, + '-f', ramdisk_path, kernel_version], + stdout=subprocess.PIPE) + subp.wait() + + +if __name__ == '__main__': + main()