Skip to contentSkip to conversation

pyGrub: Use proper bootloader class when entering command manually

PATCH In review Michal Novotny 2 participants 5 messages May 25, 2010 – May 26, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pyGrub: Use proper bootloader class when entering command manually

Hi,
this is the patch to use the proper bootloader class when entering the boot commands manually (i.e. using the 'c' option). Before this patch the bootloader was always treated to be Grub but when user is using Grub2/ExtLinux or Lilo it's rather confusing. After applying this patch the proper bootloader image class is being used, e.g. Grub2Image for Grub2 etc. when you define the boot commands manually using the 'c' command in pyGrub.

Also, fix for using isconfig has been applied since if there is not fs set in the run_grub() method the read_config() would fail since it's trying to access undefined self.cf which is now being set to parser() from cfg_list.

Signature · 2 lines
Michal Novotny<[email protected]>, RHCE
Virtualization Team (xen userspace), Red Hat
tools/pygrub/src/pygrub +17 -4
diff -r 10ad9b50b4ca tools/pygrub/src/pygrub
@@ -356,7 +356,7 @@ class Grub:
                     continue
 
                 # if we got boot, then we want to boot the entered image 
-                img = grub.GrubConf.GrubImage(lines)
+                img = self.imgcl(lines)
                 self.cf.add_image(img)
                 self.selected_image = len(self.cf.images) - 1
                 self.isdone = True
@@ -392,15 +392,28 @@ class Grub:
 
         if not fs:
             # set the config file and parse it
-            self.cf.filename = fn
-            self.cf.parse()
-            return
+            for f,parser in cfg_list:
+                self.cf = parser()
+                self.cf.filename = fn
+                self.cf.parse()
+                return
 
         for f,parser in cfg_list:
             if fs.file_exists(f):
                 print >>sys.stderr, "Using %s to parse %s" % (parser,f)
                 self.cf = parser()
                 self.cf.filename = f
+
+                # Get the bootloader image file constructor to imgcl
+                if type(self.cf) == type(grub.LiloConf.LiloConfigFile):
+                    self.imgcl = grub.LiloConf.LiloImage
+                elif type(self.cf) == type(grub.GrubConf.Grub2ConfigFile):
+                    self.imgcl = grub.GrubConf.Grub2Image
+                elif type(self.cf) == type(grub.ExtLinuxConf.ExtLinuxConfigFile):
+                    self.imgcl = grub.ExtLinuxConf.ExtLinuxImage
+                else:
+                    self.imgcl = grub.GrubConf.GrubImage
+
                 break
         if self.__dict__.get('cf', None) is None:
             raise RuntimeError, "couldn't find bootloader config file in the image provided."

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

#1 lore raw

Ok, I found that the infrastructure of ExtLinuxImage and LiloImage is different so I rewrote it a little (but according to the code the old behaviour should be preserved) and also fixed the isconfig bug (since no img.initrd is accessible that time).

So please ignore the previous version of my patch and use this one.

Thanks,
Michal

Quoted 13 lines · Michal Novotny

Hi,
this is the patch to use the proper bootloader class when entering the boot commands manually (i.e. using the 'c' option). Before this patch the bootloader was always treated to be Grub but when user is using Grub2/ExtLinux or Lilo it's rather confusing. After applying this patch the proper bootloader image class is being used, e.g. Grub2Image for Grub2 etc. when you define the boot commands manually using the 'c' command in pyGrub.

Also, fix for using isconfig has been applied since if there is not fs set in the run_grub() method the read_config() would fail since it's trying to access undefined self.cf which is now being set to parser() from cfg_list.

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

Signature · 2 lines
Michal Novotny<[email protected]>, RHCE
Virtualization Team (xen userspace), Red Hat
tools/pygrub/src/ExtLinuxConf.py +6 -6
diff -r 10ad9b50b4ca tools/pygrub/src/ExtLinuxConf.py
@@ -16,8 +16,8 @@ import GrubConf
 import GrubConf
 
 class ExtLinuxImage(object):
-    def __init__(self, lines, path):
-        self.reset(lines, path)
+    def __init__(self, title, lines, path = ""):
+        self.reset(lines, title, path)
 
     def __repr__(self):
         return ("title: %s\n"
@@ -26,10 +26,10 @@ class ExtLinuxImage(object):
                 "  args: %s\n"
                 "  initrd: %s\n" %(self.title, self.root, self.kernel,
                                    self.args, self.initrd))
-    def reset(self, lines, path):
+    def reset(self, lines, title, path):
         self._initrd = self._kernel = self._readonly = None
         self._args = ""
-        self.title = ""
+        self.title = title
         self.lines = []
         self.path = path
         self.root = ""
@@ -143,7 +143,7 @@ class ExtLinuxConfigFile(object):
             # new image
             if l.lower().startswith("label"):
                 if len(img) > 0:
-                    self.add_image(ExtLinuxImage(img, path))
+                    self.add_image(ExtLinuxImage("", img, path))
                 img = [l]
                 continue
 
@@ -162,7 +162,7 @@ class ExtLinuxConfigFile(object):
                 logging.warning("Unknown directive %s" %(com,))
 
         if len(img) > 0:
-            self.add_image(ExtLinuxImage(img, path))
+            self.add_image(ExtLinuxImage("", img, path))
 
     def hasPassword(self):
         return False
tools/pygrub/src/LiloConf.py +6 -6
diff -r 10ad9b50b4ca tools/pygrub/src/LiloConf.py
@@ -7,8 +7,8 @@ import GrubConf
 import GrubConf
 
 class LiloImage(object):
-    def __init__(self, lines, path):
-        self.reset(lines, path)
+    def __init__(self, title, lines, path = ""):
+        self.reset(lines, title, path)
 
     def __repr__(self):
         return ("title: %s\n"
@@ -17,10 +17,10 @@ class LiloImage(object):
                 "  args: %s\n"
                 "  initrd: %s\n" %(self.title, self.root, self.kernel,
                                    self.args, self.initrd))
-    def reset(self, lines, path):
+    def reset(self, lines, title, path):
         self._initrd = self._kernel = self._readonly = None
         self._args = ""
-        self.title = ""
+        self.title = title
         self.lines = []
         self.path = path
         self.root = ""
@@ -118,7 +118,7 @@ class LiloConfigFile(object):
             # new image
             if l.startswith("image"):
                 if len(img) > 0:
-                    self.add_image(LiloImage(img, path))
+                    self.add_image(LiloImage("", img, path))
                 img = [l]
                 continue
 
@@ -136,7 +136,7 @@ class LiloConfigFile(object):
                 logging.warning("Unknown directive %s" %(com,))
 
         if len(img) > 0:
-            self.add_image(LiloImage(img, path))
+            self.add_image(LiloImage("", img, path))
 
     def hasPassword(self):
         return False
tools/pygrub/src/pygrub +18 -5
diff -r 10ad9b50b4ca tools/pygrub/src/pygrub
@@ -356,7 +356,7 @@ class Grub:
                     continue
 
                 # if we got boot, then we want to boot the entered image 
-                img = grub.GrubConf.GrubImage(lines)
+                img = self.imgcl("entered", lines)
                 self.cf.add_image(img)
                 self.selected_image = len(self.cf.images) - 1
                 self.isdone = True
@@ -392,15 +392,28 @@ class Grub:
 
         if not fs:
             # set the config file and parse it
-            self.cf.filename = fn
-            self.cf.parse()
-            return
+            for f,parser in cfg_list:
+                self.cf = parser()
+                self.cf.filename = fn
+                self.cf.parse()
+                return
 
         for f,parser in cfg_list:
             if fs.file_exists(f):
                 print >>sys.stderr, "Using %s to parse %s" % (parser,f)
                 self.cf = parser()
                 self.cf.filename = f
+
+                # Get the bootloader image file constructor to imgcl
+                if type(self.cf) == grub.LiloConf.LiloConfigFile:
+                    self.imgcl = grub.LiloConf.LiloImage
+                elif type(self.cf) == grub.GrubConf.Grub2ConfigFile:
+                    self.imgcl = grub.GrubConf.Grub2Image
+                elif type(self.cf) == grub.ExtLinuxConf.ExtLinuxConfigFile:
+                    self.imgcl = grub.ExtLinuxConf.ExtLinuxImage
+                else:
+                    self.imgcl = grub.GrubConf.GrubImage
+
                 break
         if self.__dict__.get('cf', None) is None:
             raise RuntimeError, "couldn't find bootloader config file in the image provided."
@@ -689,7 +702,7 @@ if __name__ == "__main__":
     if isconfig:
         chosencfg = run_grub(file, entry, fs, incfg["args"])
         print "  kernel: %s" % chosencfg["kernel"]
-        if img.initrd:
+        if chosencfg["ramdisk"]:
             print "  initrd: %s" % chosencfg["ramdisk"]
         print "  args: %s" % chosencfg["args"]
         sys.exit(0)

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

#2 lore raw
Quoted 6 lines · Michal Novotny

Ok, I found that the infrastructure of ExtLinuxImage and LiloImage is different so I rewrote it a little (but according to the code the old behaviour should be preserved) and also fixed the isconfig bug (since no img.initrd is accessible that time).

So please ignore the previous version of my patch and use this one.

Perhaps instead of these two hunks:

tools/pygrub/src/pygrub Tue May 25 17:10:32 2010 +0200 renamed from tools/pygrub/src/pygrub Tue May 25 11:28:58 2010 +0100 +1 -1
@@ -356,7 +356,7 @@ class Grub:
                     continue
 
                 # if we got boot, then we want to boot the entered image 
-                img = grub.GrubConf.GrubImage(lines)
+                img = self.imgcl("entered", lines)
                 self.cf.add_image(img)
                 self.selected_image = len(self.cf.images) - 1
                 self.isdone = True

[...]
self.cf = parser()
self.cf.filename = f
+
+ # Get the bootloader image file constructor to imgcl
+ if type(self.cf) == grub.LiloConf.LiloConfigFile:
+ self.imgcl = grub.LiloConf.LiloImage
+ elif type(self.cf) == grub.GrubConf.Grub2ConfigFile:
+ self.imgcl = grub.GrubConf.Grub2Image
+ elif type(self.cf) ==
grub.ExtLinuxConf.ExtLinuxConfigFile:
+ self.imgcl = grub.ExtLinuxConf.ExtLinuxImage
+ else:
+ self.imgcl = grub.GrubConf.GrubImage
+
break
if self.__dict__.get('cf', None) is None:
raise RuntimeError, "couldn't find bootloader config file
in the image provided."

We could add a method to each of the self.cf classes which returns a new image from the title+lines given. Then the first hunk becomes something like:

- img = grub.GrubConf.GrubImage(lines)
+ img = self.cf.new_image("entered", lines)

and the second bit goes away. This would be nice since it avoids hardcoding another list of bootloaders.

Ian.

#3 lore raw

Yeah, that's right Ian. However adding a new method called new_image() to all of the boot loader configuration classes (*ConfigFile classes) will still be necessary. I wrote a new version of my patch to add support for new_image() method all the existing classes and did the modifications to pygrub.

As in previous version of my patch, the isconfig debug option has been fixed too.

Keir, please use this version to be committed.

Thanks,
Michal

Quoted 56 lines · Ian Campbell
Quoted 7 lines · Michal Novotny

Ok, I found that the infrastructure of ExtLinuxImage and LiloImage is different so I rewrote it a little (but according to the code the old behaviour should be preserved) and also fixed the isconfig bug (since no img.initrd is accessible that time).

So please ignore the previous version of my patch and use this one.

Perhaps instead of these two hunks:

tools/pygrub/src/pygrub Tue May 25 17:10:32 2010 +0200 renamed from tools/pygrub/src/pygrub Tue May 25 11:28:58 2010 +0100 +8 -1
@@ -356,7 +356,7 @@ class Grub:
                      continue
 
                  # if we got boot, then we want to boot the entered image
-                img = grub.GrubConf.GrubImage(lines)
+                img = self.imgcl("entered", lines)
                  self.cf.add_image(img)
                  self.selected_image = len(self.cf.images) - 1
                  self.isdone = True
 
[...]
                  self.cf = parser()
                  self.cf.filename = f
+
+                # Get the bootloader image file constructor to imgcl
+                if type(self.cf) == grub.LiloConf.LiloConfigFile:
+                    self.imgcl = grub.LiloConf.LiloImage
+                elif type(self.cf) == grub.GrubConf.Grub2ConfigFile:
+                    self.imgcl = grub.GrubConf.Grub2Image
+                elif type(self.cf) ==

grub.ExtLinuxConf.ExtLinuxConfigFile:

tools/pygrub/src/pygrub Tue May 25 17:10:32 2010 +0200 renamed from tools/pygrub/src/pygrub Tue May 25 11:28:58 2010 +0100 +4 -0
+                    self.imgcl = grub.ExtLinuxConf.ExtLinuxImage
+                else:
+                    self.imgcl = grub.GrubConf.GrubImage
+
                  break
          if self.__dict__.get('cf', None) is None:
              raise RuntimeError, "couldn't find bootloader config file

in the image provided."

We could add a method to each of the self.cf classes which returns a new image from the title+lines given. Then the first hunk becomes something like:

tools/pygrub/src/pygrub Tue May 25 17:10:32 2010 +0200 renamed from tools/pygrub/src/pygrub Tue May 25 11:28:58 2010 +0100 +1 -1
-                img = grub.GrubConf.GrubImage(lines)
+                img = self.cf.new_image("entered", lines)

and the second bit goes away. This would be nice since it avoids hardcoding another list of bootloaders.

Ian.

Signature · 2 lines
Michal Novotny<[email protected]>, RHCE
Virtualization Team (xen userspace), Red Hat
tools/pygrub/src/ExtLinuxConf.py +6 -0
diff -r 15f4ead9e686 tools/pygrub/src/ExtLinuxConf.py
@@ -118,6 +118,12 @@ class ExtLinuxConfigFile(object):
 
         if fn is not None:
             self.parse()
+
+    def new_image(self, title, lines):
+        # ExtLinuxImage constructor doesn't have title but since path
+        # is being used by get_{kernel|initrd} functions we pass
+        # empty string rather than None (see lines above)
+        return ExtLinuxImage(lines, "")
 
     def parse(self, buf = None):
         if buf is None:
tools/pygrub/src/GrubConf.py +7 -1
diff -r 15f4ead9e686 tools/pygrub/src/GrubConf.py
@@ -252,6 +252,9 @@ class GrubConfigFile(_GrubConfigFile):
     def __init__(self, fn = None):
         _GrubConfigFile.__init__(self,fn)
         
+    def new_image(self, title, lines):
+        return GrubImage(title, lines)
+
     def parse(self, buf = None):
         if buf is None:
             if self.filename is None:
@@ -345,7 +348,10 @@ class Grub2ConfigFile(_GrubConfigFile):
 class Grub2ConfigFile(_GrubConfigFile):
     def __init__(self, fn = None):
         _GrubConfigFile.__init__(self, fn)
-        
+       
+    def new_image(self, title, lines):
+        return Grub2Image(title, lines)
+ 
     def parse(self, buf = None):
         if buf is None:
             if self.filename is None:
tools/pygrub/src/LiloConf.py +6 -0
diff -r 15f4ead9e686 tools/pygrub/src/LiloConf.py
@@ -147,6 +147,12 @@ class LiloConfigFile(object):
     def add_image(self, image):
         self.images.append(image)
 
+    def new_image(self, title, lines):
+        # LiloImage constructor doesn't have title but since path
+        # is being used by get_{kernel|initrd} functions we pass
+        # empty string rather than None (see lines above)
+        return LiloImage(lines, "")
+
     def _get_default(self):
         for i in range(len(self.images)):
             if self.images[i].title == self._default:
tools/pygrub/src/pygrub +7 -5
diff -r 15f4ead9e686 tools/pygrub/src/pygrub
@@ -356,7 +356,7 @@ class Grub:
                     continue
 
                 # if we got boot, then we want to boot the entered image 
-                img = grub.GrubConf.GrubImage(lines)
+                img = self.cf.new_image("entered", lines)
                 self.cf.add_image(img)
                 self.selected_image = len(self.cf.images) - 1
                 self.isdone = True
@@ -392,9 +392,11 @@ class Grub:
 
         if not fs:
             # set the config file and parse it
-            self.cf.filename = fn
-            self.cf.parse()
-            return
+            for f,parser in cfg_list:
+                self.cf = parser()
+                self.cf.filename = fn
+                self.cf.parse()
+                return
 
         for f,parser in cfg_list:
             if fs.file_exists(f):
@@ -689,7 +691,7 @@ if __name__ == "__main__":
     if isconfig:
         chosencfg = run_grub(file, entry, fs, incfg["args"])
         print "  kernel: %s" % chosencfg["kernel"]
-        if img.initrd:
+        if chosencfg["ramdisk"]:
             print "  initrd: %s" % chosencfg["ramdisk"]
         print "  args: %s" % chosencfg["args"]
         sys.exit(0)

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

Acked-by
#4 lore raw
Quoted 9 lines · Michal Novotny

Yeah, that's right Ian. However adding a new method called new_image() to all of the boot loader configuration classes (*ConfigFile classes) will still be necessary. I wrote a new version of my patch to add support for new_image() method all the existing classes and did the modifications to pygrub.

As in previous version of my patch, the isconfig debug option has been fixed too.

Quoted 6 lines

Keir, please use this version to be committed.

Thanks,
Michal

Quoted 60 lines · Ian Campbell
Quoted 56 lines · Michal Novotny
Quoted 8 lines

Ok, I found that the infrastructure of ExtLinuxImage and LiloImage is different so I rewrote it a little (but according to the code the old behaviour should be preserved) and also fixed the isconfig bug (since no img.initrd is accessible that time).

So please ignore the previous version of my patch and use this one.

Perhaps instead of these two hunks:

tools/pygrub/src/pygrub Tue May 25 17:10:32 2010 +0200 renamed from tools/pygrub/src/pygrub Tue May 25 11:28:58 2010 +0100 +8 -1
@@ -356,7 +356,7 @@ class Grub:
                      continue
 
                  # if we got boot, then we want to boot the entered image
-                img = grub.GrubConf.GrubImage(lines)
+                img = self.imgcl("entered", lines)
                  self.cf.add_image(img)
                  self.selected_image = len(self.cf.images) - 1
                  self.isdone = True
 
[...]
                  self.cf = parser()
                  self.cf.filename = f
+
+                # Get the bootloader image file constructor to imgcl
+                if type(self.cf) == grub.LiloConf.LiloConfigFile:
+                    self.imgcl = grub.LiloConf.LiloImage
+                elif type(self.cf) == grub.GrubConf.Grub2ConfigFile:
+                    self.imgcl = grub.GrubConf.Grub2Image
+                elif type(self.cf) ==

grub.ExtLinuxConf.ExtLinuxConfigFile:

tools/pygrub/src/pygrub Tue May 25 17:10:32 2010 +0200 renamed from tools/pygrub/src/pygrub Tue May 25 11:28:58 2010 +0100 +4 -0
+                    self.imgcl = grub.ExtLinuxConf.ExtLinuxImage
+                else:
+                    self.imgcl = grub.GrubConf.GrubImage
+
                  break
          if self.__dict__.get('cf', None) is None:
              raise RuntimeError, "couldn't find bootloader config file

in the image provided."

We could add a method to each of the self.cf classes which returns a new image from the title+lines given. Then the first hunk becomes something like:

tools/pygrub/src/pygrub Tue May 25 17:10:32 2010 +0200 renamed from tools/pygrub/src/pygrub Tue May 25 11:28:58 2010 +0100 +1 -1
-                img = grub.GrubConf.GrubImage(lines)
+                img = self.cf.new_image("entered", lines)

and the second bit goes away. This would be nice since it avoids hardcoding another list of bootloaders.

Ian.