Skip to contentSkip to conversation

Make PyGrub run first entry in grub config when invalid default boot option provided

PATCH In review Michal Novotny 2 participants 4 messages Mar 20, 2009 – Mar 24, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Make PyGrub run first entry in grub config when invalid default boot option provided

Hello everybody,
I have created a patch for pygrub and I am sending it as an attachment. This patch mainly solves the issue of invalid default boot option in VM's configuration. If the entry doesn't exist, it doesn't exit with IndexError python exception but it uses first entry there to boot up the machine - same way like standard (real) grub does...

tools/pygrub/src/pygrub +5 -1
diff -r e1562a36094e tools/pygrub/src/pygrub
@@ -530,7 +530,11 @@
         print "No kernel image selected!"
         sys.exit(1)
 
-    img = g.cf.images[sel]
+    try:
+        img = g.cf.images[sel]
+    except:
+        log.debug("PyGrub: Default selection is not valid, using first boot configuration...")
+        img = g.cf.images[0]
 
     grubcfg = { "kernel": None, "ramdisk": None, "args": None }
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw

Michal Novotny writes ("[Xen-devel] [PATCH] Make PyGrub run first entry in grub config when invalid default boot option provided"):

tools/pygrub/src/pygrub line 535

Quoted 4 lines
tools/pygrub/src/pygrub +3 -1
-    img = g.cf.images[sel]
+    try:
+        img = g.cf.images[sel]
+    except:

It is not correct to do this on all exceptions. (This is a common mistake in Python.) I assume that in your error case the images array doesn't have the relevant entry and that your code should read:
+ img = g.cf.images[sel]
+ except KeyError:

Ian.

#2 lore raw

Hi,
thanks for advice. Here's new and corrected version of the patch. Well, the error here is IndexError, when an error occurs so the IndexError is catched and not KeyError. I have also tried it with KeyError but it was not working so IndexError is catched here.

Michal

Quoted 8 lines · Ian Jackson

Michal Novotny writes ("[Xen-devel] [PATCH] Make PyGrub run first entry in grub config when invalid default boot option provided"):

Review

Quoted 5 lines
(quoted diff) +3 -1
-    img = g.cf.images[sel]
+    try:
+        img = g.cf.images[sel]
+    except:
Quoted 8 lines

It is not correct to do this on all exceptions. (This is a common mistake in Python.) I assume that in your error case the images array doesn't have the relevant entry and that your code should read:
+ img = g.cf.images[sel]
+ except KeyError:

Ian.

tools/pygrub/src/pygrub +5 -1
diff -r e1562a36094e tools/pygrub/src/pygrub
@@ -530,7 +530,11 @@
         print "No kernel image selected!"
         sys.exit(1)
 
-    img = g.cf.images[sel]
+    try:
+        img = g.cf.images[sel]
+    except IndexError:
+        img = g.cf.images[0]
+
 
     grubcfg = { "kernel": None, "ramdisk": None, "args": None }
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Acked-by
#3 lore raw

Michal Novotny writes ("Re: [Xen-devel] [PATCH] Make PyGrub run first entry in grub config when invalid default boot option provided"):

Quoted 4 lines

thanks for advice. Here's new and corrected version of the patch. Well, the error here is IndexError, when an error occurs so the IndexError is catched and not KeyError. I have also tried it with KeyError but it was not working so IndexError is catched here.

Err, yes, I didn't double-check with the docs or the rest of the code. Your version is very likely correct.

Ian.