Commits

Rodrigo Bistolfi committed b4e5865

Added some sanity to backend.bootloader

Comments (0)

Files changed (1)

vinstall/backend/bootloader.py

 
     """
     def __init__(self, target, default_os=None, timeout=0,
-            vga_mode="nornal", include_running_os=False):
+            vga_mode="790"):
 
         self.target = target
         self.default_os = default_os
         self.vga_mode = vga_mode
-        self.include_running_os = include_running_os
         self.operating_systems = []
-        self._timeout = timeout
-        self.buffer = StringIO()
+        self.timeout = timeout
+        self.buffer = []
         self.lilo_root = utils.get_mounted("/")
         tamu = "/boot/tamu"
         if not os.path.exists(tamu):
                 umount = True
 
             # copy kernel
-            kernel_from = os.path.join(partition.mountpoint,
+            kernel_from = os.path.join(partition.mountpoint, "boot",
                     system.kernel_name)
             kernel_dest = os.path.join("/boot/tamu", system.kernel)
             shutil.copy2(kernel_from, kernel_dest)
                         system.initrd)
                 initrd_dest = os.path.join("/boot/tamu",
                         system.initrd)
-                shutil.copy2(initrd_from, initrd_dest)
+                if not os.path.exists(initrd_dest):
+                    shutil.copy2(initrd_from, initrd_dest)
 
             # umount if needed
             if umount:
 
             # write config lines
             tab = " " * 4
-            self.buffer.write("# -- %s on %s --\n" % (system.label,
+            self.buffer.append("# -- %s on %s --" % (system.label,
                 system.root))
-            self.buffer.write("image = %s\n" % system.kernel)
-            self.buffer.write("%s label = %s\n" % (tab, system.label))
+            self.buffer.append("image = %s" % system.kernel)
+            self.buffer.append("%s label = %s" % (tab, system.label))
             if system.initrd:
-                self.buffer.write("%s initrd = %s\n" % (tab,
+                self.buffer.append("%s initrd = %s" % (tab,
                     system.initrd))
             if system.appendline:
                 if "splash" not in system.appendline:
-                    self.buffer.write('%s append = "%s splash=silent"\n') % (tab,
-                            system.appendline)
+                    self.buffer.append('%s append = "%s splash=silent"' % (tab,
+                            system.appendline))
                 else:
-                    self.buffer.write('s append = "%s"\n' % (tab,
+                    self.buffer.append('s append = "%s"' % (tab,
                             system.appendline))
-            self.buffer.write("%s read-only\n" % tab)
-            self.buffer.write("# -- \n\n")
+            self.buffer.append("%s read-only" % tab)
+            self.buffer.append("# --")
 
             self.operating_systems.append(system)
 
         """Wait for <timeout> seconds before booting the default OS
 
         """
-        self._timeout = int(seconds) * 10
+        self.timeout = int(seconds) * 10
 
     def set_vga_mode(self, resolution, quality):
         """Set the VGA mode
 
         """
 
-    def write_config(self):
+    def write_config(self, config_path="/etc/lilo.conf"):
         """Write lilo.conf.
 
         """
         newline = "\n"
-        config_path = "/etc/lilo.conf"
         header = (
             "# LILO configuration file.",
             "# ",
             for i in header:
                 liloconf.write(i + newline)
             for line in self.buffer:
-                liloconf.write(line)
+                liloconf.write(line + newline)
 
     def install(self):
         """Install the bootloader
         """
         return subprocess.check_call(["/sbin/lilo"])
 
+    def test_install(self, config_file, bootdev):
+        """Install routine for testing
+        
+        """
+        cmd = "/sbin/lilo -b %s -C %s" % (bootdev, config_file)
+        return subprocess.check_call(cmd.split())
+
 
 class Grub2(object):
     """Grub2 bootloader
                 "1280x1024": "1280x1024x16"}
         self.vga_mode = modes.get(resolution, "800x600")
 
-    def write_config(self, filepath="/etc/default/grub"):
+    def backup_config(self):
+        pass
+
+    def write_config(self, config_path="/etc/default/grub"):
         """The grub2 config file is automatically generated, so we use
         this method to save the values to /etc/default/grub which is
         parsed while the config file is generated
                  "# END OF /etc/default/grub",""]
 
         # save the new file
-        with open(filepath, "w") as f:
+        with open(config_path, "w") as f:
             f.writelines("\n".join(ndata))
 
         # after these are set, we just need to run grub-mkconfig
         """
         subprocess.check_call(["grub-install", "--no-floppy", self.target])
 
+    def test_install(self, config_file, bootdev):
+        subprocess.check_call(["grub-install", "--no-floppy", bootdev])
+
 
 class OperatingSystem(object):
     """An OS to be added to a bootloader
             running_os.type = "linux"
             if os.path.exists('/boot/initrd'):
                 running_os.initrd = '/boot/initrd'
-            running_os.root = None #XXX
+            running_os.root = utils.get_mounted("/")
             yield running_os
 
         for i in found_os:
     return dict(root=root, kernel=kernel, initrd=initrd, append=append)
 
 
-class FakeDevice(unittest.TestCase):
+class FakeDeviceTestCase(unittest.TestCase):
 
     def setUp(self):
         (fd, self.path) = tempfile.mkstemp(prefix="fake-device-")
         os.unlink(self.path)
 
 
+class LiloTestCase(FakeDeviceTestCase):
+
+    def test_lilo(self):
+        lilo = Lilo(self.path)
+        for ops in OperatingSystem.all(include_running_os=True):
+            lilo.add_os(ops)
+        lilo.write_config(config_path="/tmp/lilo-test")
+        lilo.test_install("/tmp/lilo-test", self.path)
+        mbr = MasterBootRecord(self.path)
+        self.assertEqual(mbr.bootloader(), LILO)
+
+
+class Grub2TestCase(FakeDeviceTestCase):
+
+    def test_grub2(self):
+        return True
+        grub2 = Grub2(self.path)
+        grub2.write_config(config_path="/tmp/grub2-test")
+        grub2.test_install("/tmp/grub2-test", self.path)
+        mbr = MasterBootRecord(self.path)
+        self.assertEqual(mbr.bootloader(), GRUB2)
+
+
 if __name__ == "__main__":
     unittest.main()