How this works now:

If the build fails and the modules dir is not available, quits with an error
message suggesting that the user try the --no-modules command-line switch.
If passed, this switch causes the directory to be created.

On Tue, Aug 2, 2011 at 11:25 AM, Ben Lipton <[email protected]> wrote:

> 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               |   24 +++++++++------
>  2 files changed, 40 insertions(+), 14 deletions(-)
>
> diff --git a/instance-p2v-target/scripts/make_ramboot_initrd.py
> b/instance-p2v-target/scripts/make_ramboot_initrd.py
> index ef01b81..6b43d0b 100755
> --- a/instance-p2v-target/scripts/make_ramboot_initrd.py
> +++ b/instance-p2v-target/scripts/make_ramboot_initrd.py
> @@ -108,6 +108,12 @@ def ParseOptions(argv):
>                     help=("generate initrd for kernel named VERSION "
>                           "[currently running kernel]"),
>                     default=None)
> +  parser.add_option("--no-modules", action="store_true",
> dest="no_modules",
> +                    help=("Create directory in /lib/modules to fool"
> +                          " mkinitramfs into building initrd for
> non-modular"
> +                          " kernel"),
> +                    default=False)
> +
>
>   (options, _) = parser.parse_args(argv[1:])
>
> @@ -189,7 +195,7 @@ def AddScript(conf_dir):
>   os.chmod(movetoram_name, 0755)
>
>
> -def BuildInitrd(temp_dir, conf_dir, file_name, version):
> +def BuildInitrd(temp_dir, conf_dir, file_name, version, no_modules):
>   """Build the initrd using mkinitramfs.
>
>   Runs the program mkinitramfs to create an initrd for the specified
> @@ -199,17 +205,32 @@ def BuildInitrd(temp_dir, conf_dir, file_name,
> version):
>   @param conf_dir: temporary configuration directory with added script
>   @param file_name: what to call the initrd file
>   @param version: the kernel version to use
> +  @param no_modules: whether the --no-modules option was given
>
>   @return: the location of the generated initrd in the temp_dir
>
>   @raises Error: call to mkinitramfs failed
>
>   """
> +  mods_dir = os.path.join("/lib/modules", version)
> +  if no_modules and not os.path.exists(mods_dir):
> +    try:
> +      os.mkdir(mods_dir)
> +    except OSError:
> +      raise Error("mkdir failed. Try running as root.")
> +
>    temp_out = os.path.join(temp_dir, file_name)
>   ret = subprocess.call(["mkinitramfs", "-d", conf_dir,
>                          "-o", temp_out, version])
> +
>   if ret != 0:
> -    raise Error("Failed building initramfs")
> +    if not os.path.exists(mods_dir):
> +      raise Error("%s does not exist. On some operating systems, this
> causes"
> +                  " mkinitramfs to fail. If you are using a kernel with
> no"
> +                  " modules, try using the --no-modules option, which
> creates"
> +                  " this directory." % mods_dir)
> +    else:
> +      raise Error("Failed building initramfs.")
>
>   return temp_out
>
> @@ -272,13 +293,14 @@ def main(argv):
>
>       if verbose:
>         print "Building..."
> -      temp_out = BuildInitrd(temp_dir, new_conf_dir, file_name, version)
> +      temp_out = BuildInitrd(temp_dir, new_conf_dir, file_name, version,
> +                             options.no_modules)
>
>       if verbose:
>         print "Installing..."
>       InstallInitrd(temp_out, install_dir, file_name, options.overwrite)
>
> -    except Error, e:
> +    except Exception, e:
>       print e
>       sys.exit(1)
>   finally:
> diff --git a/instance-p2v-target/test/make_ramboot_initrd_test.py
> b/instance-p2v-target/test/make_ramboot_initrd_test.py
> index 73e0d0e..c7958fa 100755
> --- a/instance-p2v-target/test/make_ramboot_initrd_test.py
> +++ b/instance-p2v-target/test/make_ramboot_initrd_test.py
> @@ -185,6 +185,7 @@ class MakeRambootInitrdTest(unittest.TestCase):
>
>   def testBuildInitrdRunsCommand(self):
>     self.mox.StubOutWithMock(mkinitrd.subprocess, "call")
> +    self.mox.StubOutWithMock(os.path, "exists")
>     test_name = "testinitrd"
>     test_tmp = os.path.join("/tmp", "test_makeinitrd")
>     test_out = os.path.join(test_tmp, test_name)
> @@ -195,26 +196,29 @@ class MakeRambootInitrdTest(unittest.TestCase):
>     self.mox.ReplayAll()
>
>     try:
> -      try:
> -        mkinitrd.BuildInitrd(test_tmp, test_conf, test_name, test_version)
> -      except mkinitrd.Error:
> -        self.fail()
> -    finally:
> -      self.mox.VerifyAll()
> +      mkinitrd.BuildInitrd(test_tmp, test_conf, test_name, test_version,
> False)
> +    except mkinitrd.Error:
> +      self.fail()
> +
> +    self.mox.VerifyAll()
>
>   def testBuildInitrdRaisesErrorOnFailure(self):
>     self.mox.StubOutWithMock(mkinitrd.subprocess, "call")
> +    self.mox.StubOutWithMock(os.path, "exists")
>     test_name = "testinitrd"
>     test_tmp = os.path.join("/tmp", "test_makeinitrd")
>     test_out = os.path.join(test_tmp, test_name)
>     test_conf = os.path.join(test_tmp, "conf")
>     test_version = "2.6-test"
> +
>     mkinitrd.subprocess.call(["mkinitramfs", "-d", test_conf,
>                               "-o", test_out, test_version]).AndReturn(1)
> +    os.path.exists(os.path.join("/lib/modules",
> test_version)).AndReturn(False)
> +
>     self.mox.ReplayAll()
>
>     self.assertRaises(mkinitrd.Error, mkinitrd.BuildInitrd, test_tmp,
> -                      test_conf, test_name, test_version)
> +                      test_conf, test_name, test_version, False)
>
>     self.mox.VerifyAll()
>
> @@ -276,7 +280,7 @@ class MakeRambootInitrdTest(unittest.TestCase):
>      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,
> -                         version).AndReturn(temp_out)
> +                         version, False).AndReturn(temp_out)
>     mkinitrd.InstallInitrd(temp_out, boot_dir, file_name, False)
>     mkinitrd.CleanUp(temp_dir)
>
> @@ -307,7 +311,7 @@ class MakeRambootInitrdTest(unittest.TestCase):
>      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,
> -                         version).AndReturn(temp_out)
> +                         version, False).AndReturn(temp_out)
>     mkinitrd.InstallInitrd(temp_out, boot_dir, file_name, False)
>     mkinitrd.CleanUp(temp_dir)
>
> @@ -336,7 +340,7 @@ class MakeRambootInitrdTest(unittest.TestCase):
>      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,
> -                         version).AndRaise(mkinitrd.Error("test!"))
> +                         version, False).AndRaise(mkinitrd.Error("test!"))
>     mkinitrd.CleanUp(temp_dir)
>
>     self.mox.ReplayAll()
> --
> 1.7.3.1
>
>

Reply via email to