[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[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


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]