Skip to contentSkip to conversation

pygrub: fix non-interactive parsing of grub1 config files

PATCH In review Simon Rowe 5 participants 6 messages Oct 27, 2014 – Nov 4, 2014

lore
Share & tools
lore

Simon Rowe

author
#0 lore raw

PATCH 1/1 pygrub: fix non-interactive parsing of grub1 config files

Changes to handle non-numeric default attributes for grub2 caused run_grub() to attempt to index into the images list using a string. Pull out the code that handles submenus into a new function and use that to ensure sel is numeric.

---
 tools/pygrub/src/pygrub |   23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
tools/pygrub/src/pygrub +14 -9
index 1ae34a2..4cc846f 100644
@@ -456,11 +456,9 @@ class Grub:
         del f
         self.cf.parse(buf)
 
-    def run(self):
-        timeout = int(self.cf.timeout)
-
+    def image_index(self):
         if self.cf.default.isdigit():
-            self.selected_image = int(self.cf.default)
+            sel = int(self.cf.default)
         else:
             # We don't fully support submenus. Look for the leaf value in
             # "submenu0>submenu1>...>menuentry" and hope that it's unique.
@@ -472,16 +470,23 @@ class Grub:
                     break
 
             # Map string to index in images array
-            self.selected_image = 0
+            sel = 0
             for i in range(len(self.cf.images)):
                 if self.cf.images[i].title == title:
-                    self.selected_image = i
+                    sel = i
                     break
 
         # If the selected (default) image doesn't exist we select the first entry
-        if self.selected_image > len(self.cf.images):
+        if sel > len(self.cf.images):
             logging.warning("Default image not found")
-            self.selected_image = 0
+            sel = 0
+
+        return sel
+
+    def run(self):
+        timeout = int(self.cf.timeout)
+        self.selected_image = self.image_index()
+
         self.isdone = False
         while not self.isdone:
             self.run_main(timeout)
@@ -626,7 +631,7 @@ def run_grub(file, entry, fs, cfg_args):
     if interactive and not list_entries:
         curses.wrapper(run_main)
     else:
-        sel = g.cf.default
+        sel = g.image_index()
 
     # set the entry to boot as requested
     if entry is not None:
Signature · 1 line
1.7.10.4
#1 lore raw

for-xen-4.5 Was:Re: [PATCH] pygrub: fix non-interactive parsing of grub1 config files

Quoted 7 lines · Simon Rowe

Changes to handle non-numeric default attributes for grub2 caused run_grub() to attempt to index into the images list using a string. Pull out the code that handles submenus into a new function and use that to ensure sel is numeric.

CC-ing Boris as he had a question on this patch last time.

And also adding the 'for-xen-4.5' to help identify this.

Thank you!

Quoted 62 lines
---
 tools/pygrub/src/pygrub |   23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
tools/pygrub/src/pygrub +14 -9
index 1ae34a2..4cc846f 100644
@@ -456,11 +456,9 @@ class Grub:
         del f
         self.cf.parse(buf)
 
-    def run(self):
-        timeout = int(self.cf.timeout)
-
+    def image_index(self):
         if self.cf.default.isdigit():
-            self.selected_image = int(self.cf.default)
+            sel = int(self.cf.default)
         else:
             # We don't fully support submenus. Look for the leaf value in
             # "submenu0>submenu1>...>menuentry" and hope that it's unique.
@@ -472,16 +470,23 @@ class Grub:
                     break
 
             # Map string to index in images array
-            self.selected_image = 0
+            sel = 0
             for i in range(len(self.cf.images)):
                 if self.cf.images[i].title == title:
-                    self.selected_image = i
+                    sel = i
                     break
 
         # If the selected (default) image doesn't exist we select the first entry
-        if self.selected_image > len(self.cf.images):
+        if sel > len(self.cf.images):
             logging.warning("Default image not found")
-            self.selected_image = 0
+            sel = 0
+
+        return sel
+
+    def run(self):
+        timeout = int(self.cf.timeout)
+        self.selected_image = self.image_index()
+
         self.isdone = False
         while not self.isdone:
             self.run_main(timeout)
@@ -626,7 +631,7 @@ def run_grub(file, entry, fs, cfg_args):
     if interactive and not list_entries:
         curses.wrapper(run_main)
     else:
-        sel = g.cf.default
+        sel = g.image_index()
 
     # set the entry to boot as requested
     if entry is not None:
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#2 lore raw
In reply to Simon Rowe
Quoted 7 lines · Simon Rowe

Changes to handle non-numeric default attributes for grub2 caused run_grub() to attempt to index into the images list using a string. Pull out the code that handles submenus into a new function and use that to ensure sel is numeric.

Quoted 59 lines
---
  tools/pygrub/src/pygrub |   23 ++++++++++++++---------
  1 file changed, 14 insertions(+), 9 deletions(-)
tools/pygrub/src/pygrub +14 -9
index 1ae34a2..4cc846f 100644
@@ -456,11 +456,9 @@ class Grub:
          del f
          self.cf.parse(buf)
 
-    def run(self):
-        timeout = int(self.cf.timeout)
-
+    def image_index(self):
          if self.cf.default.isdigit():
-            self.selected_image = int(self.cf.default)
+            sel = int(self.cf.default)
          else:
              # We don't fully support submenus. Look for the leaf value in
              # "submenu0>submenu1>...>menuentry" and hope that it's unique.
@@ -472,16 +470,23 @@ class Grub:
                      break
 
              # Map string to index in images array
-            self.selected_image = 0
+            sel = 0
              for i in range(len(self.cf.images)):
                  if self.cf.images[i].title == title:
-                    self.selected_image = i
+                    sel = i
                      break
 
          # If the selected (default) image doesn't exist we select the first entry
-        if self.selected_image > len(self.cf.images):
+        if sel > len(self.cf.images):
              logging.warning("Default image not found")
-            self.selected_image = 0
+            sel = 0
+
+        return sel
+
+    def run(self):
+        timeout = int(self.cf.timeout)
+        self.selected_image = self.image_index()
+
          self.isdone = False
          while not self.isdone:
              self.run_main(timeout)
@@ -626,7 +631,7 @@ def run_grub(file, entry, fs, cfg_args):
      if interactive and not list_entries:
          curses.wrapper(run_main)
      else:
-        sel = g.cf.default
+        sel = g.image_index()
 
      # set the entry to boot as requested
      if entry is not None:
#5 lore raw
In reply to Simon Rowe
Quoted 7 lines · Simon Rowe

Changes to handle non-numeric default attributes for grub2 caused run_grub() to attempt to index into the images list using a string. Pull out the code that handles submenus into a new function and use that to ensure sel is numeric.

Reviewed-and-tested-by Andrew Cooper <[email protected]>
Quoted 59 lines
---
 tools/pygrub/src/pygrub |   23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
tools/pygrub/src/pygrub +14 -9
index 1ae34a2..4cc846f 100644
@@ -456,11 +456,9 @@ class Grub:
         del f
         self.cf.parse(buf)
 
-    def run(self):
-        timeout = int(self.cf.timeout)
-
+    def image_index(self):
         if self.cf.default.isdigit():
-            self.selected_image = int(self.cf.default)
+            sel = int(self.cf.default)
         else:
             # We don't fully support submenus. Look for the leaf value in
             # "submenu0>submenu1>...>menuentry" and hope that it's unique.
@@ -472,16 +470,23 @@ class Grub:
                     break
 
             # Map string to index in images array
-            self.selected_image = 0
+            sel = 0
             for i in range(len(self.cf.images)):
                 if self.cf.images[i].title == title:
-                    self.selected_image = i
+                    sel = i
                     break
 
         # If the selected (default) image doesn't exist we select the first entry
-        if self.selected_image > len(self.cf.images):
+        if sel > len(self.cf.images):
             logging.warning("Default image not found")
-            self.selected_image = 0
+            sel = 0
+
+        return sel
+
+    def run(self):
+        timeout = int(self.cf.timeout)
+        self.selected_image = self.image_index()
+
         self.isdone = False
         while not self.isdone:
             self.run_main(timeout)
@@ -626,7 +631,7 @@ def run_grub(file, entry, fs, cfg_args):
     if interactive and not list_entries:
         curses.wrapper(run_main)
     else:
-        sel = g.cf.default
+        sel = g.image_index()
 
     # set the entry to boot as requested
     if entry is not None:
Acked-by
#3 lore raw
In reply to Boris Ostrovsky
Quoted 10 lines · Boris Ostrovsky
Quoted 7 lines · Simon Rowe

Changes to handle non-numeric default attributes for grub2 caused run_grub() to attempt to index into the images list using a string. Pull out the code that handles submenus into a new function and use that to ensure sel is numeric.

And applied as a bug fix (I'm not sure if Konrad's response was intended as a release-ack).

Quoted 61 lines
Quoted 60 lines
---
  tools/pygrub/src/pygrub |   23 ++++++++++++++---------
  1 file changed, 14 insertions(+), 9 deletions(-)
tools/pygrub/src/pygrub +14 -9
index 1ae34a2..4cc846f 100644
@@ -456,11 +456,9 @@ class Grub:
          del f
          self.cf.parse(buf)
 
-    def run(self):
-        timeout = int(self.cf.timeout)
-
+    def image_index(self):
          if self.cf.default.isdigit():
-            self.selected_image = int(self.cf.default)
+            sel = int(self.cf.default)
          else:
              # We don't fully support submenus. Look for the leaf value in
              # "submenu0>submenu1>...>menuentry" and hope that it's unique.
@@ -472,16 +470,23 @@ class Grub:
                      break
 
              # Map string to index in images array
-            self.selected_image = 0
+            sel = 0
              for i in range(len(self.cf.images)):
                  if self.cf.images[i].title == title:
-                    self.selected_image = i
+                    sel = i
                      break
 
          # If the selected (default) image doesn't exist we select the first entry
-        if self.selected_image > len(self.cf.images):
+        if sel > len(self.cf.images):
              logging.warning("Default image not found")
-            self.selected_image = 0
+            sel = 0
+
+        return sel
+
+    def run(self):
+        timeout = int(self.cf.timeout)
+        self.selected_image = self.image_index()
+
          self.isdone = False
          while not self.isdone:
              self.run_main(timeout)
@@ -626,7 +631,7 @@ def run_grub(file, entry, fs, cfg_args):
      if interactive and not list_entries:
          curses.wrapper(run_main)
      else:
-        sel = g.cf.default
+        sel = g.image_index()
 
      # set the entry to boot as requested
      if entry is not None:
#4 lore raw
Quoted 10 lines · Ian Campbell
Quoted 9 lines · Boris Ostrovsky
Quoted 7 lines · Simon Rowe

Changes to handle non-numeric default attributes for grub2 caused run_grub() to attempt to index into the images list using a string. Pull out the code that handles submenus into a new function and use that to ensure sel is numeric.

Quoted 6 lines
Quoted 1 line

And applied as a bug fix (I'm not sure if Konrad's response was intended as a release-ack).

Yes. Thank you for checking this patch in.

Quoted 64 lines
Quoted 62 lines
Quoted 61 lines

---
  tools/pygrub/src/pygrub |   23 ++++++++++++++---------
  1 file changed, 14 insertions(+), 9 deletions(-)
tools/pygrub/src/pygrub +14 -9
index 1ae34a2..4cc846f 100644
@@ -456,11 +456,9 @@ class Grub:
          del f
          self.cf.parse(buf)
 
-    def run(self):
-        timeout = int(self.cf.timeout)
-
+    def image_index(self):
          if self.cf.default.isdigit():
-            self.selected_image = int(self.cf.default)
+            sel = int(self.cf.default)
          else:
              # We don't fully support submenus. Look for the leaf value in
              # "submenu0>submenu1>...>menuentry" and hope that it's unique.
@@ -472,16 +470,23 @@ class Grub:
                      break
 
              # Map string to index in images array
-            self.selected_image = 0
+            sel = 0
              for i in range(len(self.cf.images)):
                  if self.cf.images[i].title == title:
-                    self.selected_image = i
+                    sel = i
                      break
 
          # If the selected (default) image doesn't exist we select the first entry
-        if self.selected_image > len(self.cf.images):
+        if sel > len(self.cf.images):
              logging.warning("Default image not found")
-            self.selected_image = 0
+            sel = 0
+
+        return sel
+
+    def run(self):
+        timeout = int(self.cf.timeout)
+        self.selected_image = self.image_index()
+
          self.isdone = False
          while not self.isdone:
              self.run_main(timeout)
@@ -626,7 +631,7 @@ def run_grub(file, entry, fs, cfg_args):
      if interactive and not list_entries:
          curses.wrapper(run_main)
      else:
-        sel = g.cf.default
+        sel = g.image_index()
 
      # set the entry to boot as requested
      if entry is not None: