Matthew Booth
2010-May-07 14:03 UTC
[Libguestfs] [PATCH] Check a grub kernel exists before selecting it
Handle the case where grub refers to a kernel which doesn't exist by
ignoring
that kernel.
---
lib/Sys/VirtV2V/GuestOS/RedHat.pm | 46 +++++++++++++++++-------------------
1 files changed, 22 insertions(+), 24 deletions(-)
diff --git a/lib/Sys/VirtV2V/GuestOS/RedHat.pm
b/lib/Sys/VirtV2V/GuestOS/RedHat.pm
index 77d9332..10ff77d 100644
--- a/lib/Sys/VirtV2V/GuestOS/RedHat.pm
+++ b/lib/Sys/VirtV2V/GuestOS/RedHat.pm
@@ -350,39 +350,37 @@ sub get_default_kernel
$default = $g->aug_get('/files/boot/grub/menu.lst/default');
};
+ # Get the grub filesystem
+ my $grub = $self->{desc}->{boot}->{grub_fs};
+
+ # Look for a kernel, starting with the default
+ my @paths;
+ push(@paths, $g->aug_match("/files/boot/grub/menu.lst/".
+ "title[$default]/kernel")) if
defined($default);
+ push(@paths,
$g->aug_match('/files/boot/grub/menu.lst/title/kernel'));
+
my $kernel;
- if(defined($default)) {
- # Grub's default is zero-based, but augeas arrays are 1-based.
- $default += 1;
+ foreach my $path (@paths) {
+ $kernel = $g->aug_get($path);
- # Check it's got a kernel entry
- eval {
- $kernel -
$g->aug_get("/files/boot/grub/menu.lst/title[$default]/kernel");
- };
- }
+ # Prepend the grub filesystem to the kernel path
+ $kernel = "$grub$kernel" if(defined($grub));
- # If we didn't find a default, find the first listed kernel
- if(!defined($kernel)) {
- eval {
- my @paths =
$g->aug_match('/files/boot/grub/menu.lst/title/kernel');
+ # Prepend the grub filesystem to the kernel path
+ $kernel = "$grub$kernel" if(defined($grub));
- $kernel = $g->aug_get($paths[0]) if(@paths > 0);
- };
+ # Check the kernel exists
+ last if($g->exists($kernel));
+
+ $kernel = undef;
+ print STDERR user_message(__x("WARNING: grub refers to ".
+ "{path}, which doesn't
exist."));
}
# If we got here, grub doesn't contain any kernels. Give up.
die(user_message(__"Unable to find a default kernel"))
unless(defined($kernel));
- my $desc = $self->{desc};
-
- # Get the grub filesystem
- my $grub = $desc->{boot}->{grub_fs};
-
- # Prepend the grub filesystem to the kernel path to get an absolute path
- $kernel = "$grub$kernel" if(defined($grub));
-
# Work out it's version number
my $kernel_desc = inspect_linux_kernel ($g, $kernel, 'rpm');
--
1.6.6.1
Richard W.M. Jones
2010-May-07 14:21 UTC
[Libguestfs] [PATCH] Check a grub kernel exists before selecting it
On Fri, May 07, 2010 at 03:03:45PM +0100, Matthew Booth wrote:> Handle the case where grub refers to a kernel which doesn't exist by ignoring > that kernel.ACK. BTW in libguestfs we always put RHBZ#... into the commit message so that it's easy to find out which bugs are fixed between releases. I think this would be useful for virt-v2v too. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html