dracut-regenerate: catch failures and exit code

This seems to miss the exit code of the dracut process, which actualy
caused some issues in I8511669e188717494daf2bc1384a6dd346f942a4 where
it would have been much clearer to stop after the initramfs generation
failed.

Add some debug messages, and catch any errors from the final call.

Change-Id: I6f89441ec4709f5199535e15a7cc53a3a8af273d
This commit is contained in:
Ian Wienand 2019-08-13 11:13:46 +10:00
parent 4131942356
commit bac8b4246e

View File

@ -14,22 +14,34 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import logging
import os import os
import re import re
import subprocess import subprocess
import yaml import yaml
logging.basicConfig(
level=logging.DEBUG if int(os.getenv('DIB_DEBUG_TRACE', 0)) > 0
else logging.INFO)
def main(): def main():
logging.info("Starting dracut regeneration")
logging.debug("Debug logging enabled")
dracut_env = os.getenv('DIB_DRACUT_ENABLED_MODULES') dracut_env = os.getenv('DIB_DRACUT_ENABLED_MODULES')
dracut_objects = yaml.safe_load(dracut_env) dracut_objects = yaml.safe_load(dracut_env)
logging.debug(dracut_objects)
modules_to_boot = [] modules_to_boot = []
for dracut_object in dracut_objects: for dracut_object in dracut_objects:
# first, install dependent packages # first, install dependent packages
packages = dracut_object.get('packages', []) packages = dracut_object.get('packages', [])
for package in packages: for package in packages:
cmdline = ["install-packages", package] cmdline = ["install-packages", package]
logging.debug("Calling: %s" % cmdline)
subp = subprocess.Popen(cmdline, stdout=subprocess.PIPE) subp = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
out = subp.communicate()[0] out = subp.communicate()[0]
if subp.returncode: if subp.returncode:
@ -43,9 +55,9 @@ def main():
# regenerate dracut with the list of installed modules # regenerate dracut with the list of installed modules
if len(modules_to_boot) > 0: if len(modules_to_boot) > 0:
cmdline = ["select-boot-kernel-initrd"] cmdline = ["select-boot-kernel-initrd"]
logging.debug("Calling: %s" % cmdline)
subp = subprocess.Popen(cmdline, stdout=subprocess.PIPE) subp = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
out, err = subp.communicate() out, err = subp.communicate()
if subp.returncode: if subp.returncode:
e = subprocess.CalledProcessError(subp.returncode, cmdline) e = subprocess.CalledProcessError(subp.returncode, cmdline)
e.output = out e.output = out
@ -57,11 +69,22 @@ def main():
ramdisk_path = "/boot/%s" % kernel_set[1].strip() ramdisk_path = "/boot/%s" % kernel_set[1].strip()
modules_to_boot = ' ' .join(modules_to_boot) modules_to_boot = ' ' .join(modules_to_boot)
subp = subprocess.Popen(['dracut', '--force', '--add', modules_to_boot, cmdline = ['dracut', '--force', '--add', modules_to_boot,
'-f', ramdisk_path, kernel_version], '-f', ramdisk_path, kernel_version]
stdout=subprocess.PIPE) logging.debug("Calling: %s" % cmdline)
subp.wait() 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")
if __name__ == '__main__': if __name__ == '__main__':
main() main()
# Local Variables:
# mode: python
# End: