Change initrd script not to crash when there is no modules dir for the
selected kernel, in case we are using a non-modular kernel. Instead,
cheat by creating an empty directory where the modules dir should be.

Signed-off-by: Ben Lipton <[email protected]>
---
 instance-p2v-target/scripts/make_ramboot_initrd.py |   30 ++++++++++++++++++++
 .../test/make_ramboot_initrd_test.py               |    7 ++++
 2 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/instance-p2v-target/scripts/make_ramboot_initrd.py 
b/instance-p2v-target/scripts/make_ramboot_initrd.py
index ef01b81..9cb84dc 100755
--- a/instance-p2v-target/scripts/make_ramboot_initrd.py
+++ b/instance-p2v-target/scripts/make_ramboot_initrd.py
@@ -242,6 +242,34 @@ def InstallInitrd(temp_out, install_dir, file_name, 
overwrite=False):
                 " write to the selected boot directory (%s) or select a new"
                 " one with the -b option." % install_dir)
 
+def CheckModules(version):
+  """Make sure that mkinitramfs gets the modules it's looking for.
+
+  Some versions of mkinitramfs will fail to build an initrd if the modules
+  directory for the specified kernel does not exist. However, some kernels
+  don't have any modules. So, if the user wants, fool mkinitramfs by creating
+  an empty dir where the modules dir ought to be.
+
+  @type version: str
+  @param version: Kernel version to check for.
+
+  """
+  mods_dir = os.path.join("/lib/modules", version)
+  if not os.path.exists(mods_dir):
+    response = raw_input("The directory %s does not exist. On some"
+                         " operating systems, this will cause the build to"
+                         " fail. If you were expecting to use modules on the"
+                         " instance, please make sure they are installed on"
+                         " the node so the initrd can be built. However, If"
+                         " your instance will use a kernel with no modules, we"
+                         " can make it work by creating an empty directory"
+                         " there. Should I do this? y/[n]: " % mods_dir)
+    if response == "y":
+      try:
+        os.mkdir(mods_dir)
+      except OSError:
+        raise Error("mkdir failed. Try running as root.")
+
 
 def main(argv):
   options = ParseOptions(argv)
@@ -263,6 +291,8 @@ def main(argv):
                     " If you wish to overwrite it, please pass the -f option" %
                     (file_name, install_dir))
 
+      CheckModules(version)
+
       if verbose:
         print "Configuring..."
       temp_dir, new_conf_dir = CreateTempDir(conf_dir)
diff --git a/instance-p2v-target/test/make_ramboot_initrd_test.py 
b/instance-p2v-target/test/make_ramboot_initrd_test.py
index 73e0d0e..a29d7b5 100755
--- a/instance-p2v-target/test/make_ramboot_initrd_test.py
+++ b/instance-p2v-target/test/make_ramboot_initrd_test.py
@@ -267,12 +267,14 @@ class MakeRambootInitrdTest(unittest.TestCase):
     file_name = "testinitrd.img"
     version = "2.6-test"
 
+    self.mox.StubOutWithMock(mkinitrd, "CheckModules")
     self.mox.StubOutWithMock(mkinitrd, "CreateTempDir")
     self.mox.StubOutWithMock(mkinitrd, "AddScript")
     self.mox.StubOutWithMock(mkinitrd, "BuildInitrd")
     self.mox.StubOutWithMock(mkinitrd, "InstallInitrd")
     self.mox.StubOutWithMock(mkinitrd, "CleanUp")
 
+    mkinitrd.CheckModules(version)
     mkinitrd.CreateTempDir(old_conf_dir).AndReturn((temp_dir, new_conf_dir))
     mkinitrd.AddScript(new_conf_dir)
     mkinitrd.BuildInitrd(temp_dir, new_conf_dir, file_name,
@@ -298,12 +300,14 @@ class MakeRambootInitrdTest(unittest.TestCase):
     version = platform.release()
     file_name = "initrd.img-%s-ramboot" % version
 
+    self.mox.StubOutWithMock(mkinitrd, "CheckModules")
     self.mox.StubOutWithMock(mkinitrd, "CreateTempDir")
     self.mox.StubOutWithMock(mkinitrd, "AddScript")
     self.mox.StubOutWithMock(mkinitrd, "BuildInitrd")
     self.mox.StubOutWithMock(mkinitrd, "InstallInitrd")
     self.mox.StubOutWithMock(mkinitrd, "CleanUp")
 
+    mkinitrd.CheckModules(version)
     mkinitrd.CreateTempDir(old_conf_dir).AndReturn((temp_dir, new_conf_dir))
     mkinitrd.AddScript(new_conf_dir)
     mkinitrd.BuildInitrd(temp_dir, new_conf_dir, file_name,
@@ -327,12 +331,14 @@ class MakeRambootInitrdTest(unittest.TestCase):
     version = platform.release()
     file_name = "initrd.img-%s-ramboot" % version
 
+    self.mox.StubOutWithMock(mkinitrd, "CheckModules")
     self.mox.StubOutWithMock(mkinitrd, "CreateTempDir")
     self.mox.StubOutWithMock(mkinitrd, "AddScript")
     self.mox.StubOutWithMock(mkinitrd, "BuildInitrd")
     self.mox.StubOutWithMock(mkinitrd, "InstallInitrd")
     self.mox.StubOutWithMock(mkinitrd, "CleanUp")
 
+    mkinitrd.CheckModules(version)
     mkinitrd.CreateTempDir(old_conf_dir).AndReturn((temp_dir, new_conf_dir))
     mkinitrd.AddScript(new_conf_dir)
     mkinitrd.BuildInitrd(temp_dir, new_conf_dir, file_name,
@@ -350,6 +356,7 @@ class MakeRambootInitrdTest(unittest.TestCase):
   def testMainExitsEarlyIfInstallDirUnwritable(self):
     # none of these should be called, because there's no point in doing
     # the work if you're going to discard it later
+    self.mox.StubOutWithMock(mkinitrd, "CheckModules")
     self.mox.StubOutWithMock(mkinitrd, "CreateTempDir")
     self.mox.StubOutWithMock(mkinitrd, "AddScript")
     self.mox.StubOutWithMock(mkinitrd, "BuildInitrd")
-- 
1.7.3.1

Reply via email to