From 018fcbbb4abb15974463cf2b3f76eecdabb02e5e Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Mon, 2 Sep 2013 16:10:28 -0400 Subject: [PATCH] distroinstaller: Support --initrd-inject for RHEL4 (bz 866608) RHEL4 kernels are gzip'd ext2 images, so unzip it, use debugfs to inject files, and zip it back up. Based on shell code from Patrick J. LoPresti. --- virtinst/distroinstaller.py | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/virtinst/distroinstaller.py b/virtinst/distroinstaller.py index 68f9a9424..2a65ca65f 100644 --- a/virtinst/distroinstaller.py +++ b/virtinst/distroinstaller.py @@ -159,6 +159,61 @@ def _upload_file(conn, meter, destpool, src): return vol +def _rhel4_initrd_inject(initrd, injections): + try: + file_proc = subprocess.Popen(["file", "-z", initrd], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + if not "ext2 filesystem" in file_proc.communicate()[0]: + return False + except: + logging.exception("Failed to file command for rhel4 initrd detection") + return False + + logging.debug("Is RHEL4 initrd") + + # Uncompress the initrd + newinitrd = file(initrd + ".new", "wb") + gzip_proc = subprocess.Popen(["gzip", "-d", "-f", "-c", initrd], + stdout=newinitrd, + stderr=subprocess.PIPE) + gzip_proc.wait() + newinitrd.close() + + debugfserr = "" + for filename in injections: + # We have an ext2 filesystem, use debugfs to inject files + cmd = ["debugfs", "-w", "-R", + "write %s %s" % (filename, os.path.basename(filename)), + newinitrd.name] + logging.debug("Copying %s to the initrd with cmd=%s", filename, cmd) + + debugfs_proc = subprocess.Popen(cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + debugfs_proc.wait() + debugfserr += debugfs_proc.stderr.read() or "" + + gziperr = gzip_proc.stderr.read() + if gziperr: + logging.debug("gzip stderr=%s", gziperr) + if debugfserr: + logging.debug("debugfs stderr=%s", debugfserr) + + # Recompress the initrd + gzip_proc = subprocess.Popen(["gzip"], + stdin=file(newinitrd.name, "rb"), + stdout=file(initrd, "wb"), + stderr=subprocess.PIPE) + gzip_proc.wait() + gziperr = gzip_proc.stderr.read() + if gziperr: + logging.debug("gzip stderr=%s", gziperr) + os.unlink(newinitrd.name) + + return True + + def _perform_initrd_injections(initrd, injections, scratchdir): """ Insert files into the root directory of the initial ram disk @@ -166,6 +221,9 @@ def _perform_initrd_injections(initrd, injections, scratchdir): if not injections: return + if _rhel4_initrd_inject(initrd, injections): + return + tempdir = tempfile.mkdtemp(dir=scratchdir) os.chmod(tempdir, 0775)