[node-patches] Change in ovirt-node[master]: Add utils.system.Bootloader

rbarry at redhat.com rbarry at redhat.com
Wed Oct 2 19:22:53 UTC 2013


Ryan Barry has uploaded a new change for review.

Change subject: Add utils.system.Bootloader
......................................................................

Add utils.system.Bootloader

Add a class for manipulating GRUB arguments.

Change-Id: I5a796a4c07bd99b927070ec62a7b1d990d4e1f9b
Signed-off-by: Ryan Barry <rbarry at redhat.com>
---
M src/ovirt/node/utils/system.py
1 file changed, 115 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/11/19811/1

diff --git a/src/ovirt/node/utils/system.py b/src/ovirt/node/utils/system.py
index 7613f0f..719efec 100644
--- a/src/ovirt/node/utils/system.py
+++ b/src/ovirt/node/utils/system.py
@@ -88,6 +88,23 @@
     return os.path.exists("/dev/HostVG")
 
 
+def has_filesystem(label):
+    """Determines whether a filesystem with a given label is present on this
+    system
+    """
+    try:
+        process.call(["partprobe", "/dev/mapper/*"])
+        process.call(["udevadm", "settle"])
+        if process.check_call(["/sbin/blkid", "-c", "/dev/null", "-l",
+                               "-o", "device", "-t", 'LABEL="%s"' % label
+                               ]) == 0:
+            return True
+        else:
+            return False
+    except process.CalledProcessError:
+        return False
+
+
 def which(cmd):
     """Simulates the behavior of which
 
@@ -518,3 +535,101 @@
                           ("delete-bootnum", None)])
 
         return True
+
+
+class Bootloader(base.Base):
+    """Figures out where grub.conf can be found and which bootloader is used"""
+
+    def __init__(self):
+        super(Bootloader, self).__init__()
+        self.is_grub2 = self.__is_grub2()
+        self.grub_cfg = self._find_grub_cfg()
+
+    def __is_grub2(self):
+        # If grub2 exists on hte image, assume we're using it
+        return os.path.exists("/sbin/grub2-install")
+
+    def _find_grub_cfg(self):
+        cfg_path = None
+
+        if has_filesystem("Boot"):
+            cfg_path = "/boot/grub/grub.conf"
+        elif os.path.ismount("/dev/.initramfs/live"):
+            if not self.__is_grub2():
+                cfg_path = "/dev/.initramfs/live/grub/grub.conf"
+            else:
+                cfg_path = "/dev/.initramfs/live/grub2/grub.cfg"
+        elif os.path.ismount("/run/initramfs/.live"):
+            cfg_path = "/liveos/grub/grub.conf"
+
+        return cfg_path
+
+    def _get_grublines(self):
+        return open(self.grub_cfg, "r").readlines()
+
+    def get_args(self, arg=None):
+        lines = self._get_grublines()
+        kernel = [line for line in lines if
+                  re.match(r'[^#].*?vmlinuz', line)][0]
+        kernel = re.sub(r'^\s*?(kernel|linux)\s+?\/vmlinuz.*?\s+', '', kernel)
+        params = {}
+        for param in kernel.split():
+            match = re.match(r'(.*?)=(.*)', param)
+            if match:
+                params[match.groups()[0]] = match.groups()[1]
+            else:
+                params[param] = True
+        if arg is not None:
+            if re.match(r'^(.*?)=', arg):
+                argument = re.match(r'^(.*?)=', arg).groups()[0]
+                flags = re.match(r'^.*?=(.*)', arg).groups()[0]
+                if argument in params and params[argument] == flags:
+                    return params[argument]
+            elif arg in params:
+                return params[arg]
+        else:
+            return params
+
+    def has_args(self, arg):
+        return self.get_args(arg) is not ""
+
+    def update_args(self, arg, remove=False):
+        replacement = arg
+        # Check if it's parameterized
+        if '=' in arg:
+            arg = re.match(r'^(.*?)=.*', arg).groups()[0]
+        self.__remount(self.__find_mount(self.grub_cfg), True)
+        lines = self._get_grublines()
+        with open(self.grub_cfg, "w") as f:
+            for line in lines:
+                if re.match(r'.*?\s%s' % arg, line):
+                    if remove:
+                        line = re.sub(r' %s(=.*?\s?)?' % arg, '', line)
+                    else:
+                        if arg != replacement:
+                            line = re.sub(r'%s(=.*?\s?)?' % arg, ' %s ' % \
+                                          replacement, line)
+                elif re.match(r'^.*?vmlinuz', line):
+                    # Not in the kernel line. Add it.
+                    line = line.strip() + " %s\n" % replacement
+                f.write(line)
+        self.__remount(self.__find_mount(self.grub_cfg))
+
+    def remove_args(self, arg):
+        self.update_args(arg, remove=True)
+
+    def __remount(self, path, rw=False):
+        try:
+            if rw:
+                utils.process.check_call(["mount", "-o", "rw,remount"
+                                          "%s" % self.__find_mount(path)])
+            else:
+                utils.process.check_call(["mount", "-o", "ro,remount"
+                                          "%s" % self.__find_mount(path)])
+        except:
+            LOGGER.exception("Can't remount %s!" % path)
+
+    def __find_mount(self, path):
+        while not os.path.ismount(path) and path != "/":
+            path = os.path.dirname(path)
+        return path


-- 
To view, visit http://gerrit.ovirt.org/19811
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5a796a4c07bd99b927070ec62a7b1d990d4e1f9b
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Ryan Barry <rbarry at redhat.com>



More information about the node-patches mailing list