]> git.siccegge.de Git - forks/vmdebootstrap.git/blobdiff - vmdebootstrap
Add environment support to runcmd and pass noninteractive environment to second-stage...
[forks/vmdebootstrap.git] / vmdebootstrap
index d7edde26646b82dc03358baea9d2ef3697117607..694d1f6d94679b96c557f7cdb33db90b6df4f963 100755 (executable)
@@ -20,7 +20,6 @@ import crypt
 import logging
 import os
 import re
-import time
 import shutil
 import subprocess
 import tempfile
@@ -33,9 +32,8 @@ __version__ = '0.3'
 class VmDebootstrap(cliapp.Application):
 
     def add_settings(self):
-        default_arch = self.runcmd(
-            ["dpkg", "--print-architecture"],
-            ignore_fail=False).strip()
+        default_arch = subprocess.check_output(
+                ["dpkg", "--print-architecture"]).strip()
 
         self.settings.boolean(['verbose'], 'report what is going on')
         self.settings.string(['image'], 'put created disk image in FILE',
@@ -111,6 +109,11 @@ class VmDebootstrap(cliapp.Application):
         self.settings.boolean(['configure-apt'],
                               'Create an apt source based on the distribution '
                               'and mirror selected.')
+        self.settings.boolean(['mbr'],
+                              'Run install-mbr (no longer done by default)')
+        self.settings.boolean(['grub'],
+                              'Install and configure grub2 - disables '
+                              'extlinux.')
 
     def process_args(self, args):
         if not self.settings['image'] and not self.settings['tarball']:
@@ -132,7 +135,8 @@ class VmDebootstrap(cliapp.Application):
             if self.settings['image']:
                 self.create_empty_image()
                 self.partition_image()
-                self.install_mbr()
+                if self.settings['mbr']:
+                    self.install_mbr()
                 (rootdev, bootdev) = self.setup_kpartx()
                 self.mkfs(rootdev, type=roottype)
                 rootdir = self.mount(rootdev)
@@ -160,7 +164,9 @@ class VmDebootstrap(cliapp.Application):
                 self.configure_apt(rootdir)
             self.customize(rootdir)
             if self.settings['image']:
-                if self.settings['extlinux']:
+                if self.settings['grub']:
+                    self.install_grub2(rootdev, rootdir)
+                elif self.settings['extlinux']:
                     self.install_extlinux(rootdev, rootdir)
                 self.append_serial_console(rootdir)
                 self.optimize_image(rootdir)
@@ -193,11 +199,11 @@ class VmDebootstrap(cliapp.Application):
         if self.settings['verbose']:
             print msg
 
-    def runcmd(self, argv, stdin='', ignore_fail=False, **kwargs):
-        logging.debug('runcmd: %s %s' % (argv, kwargs))
+    def runcmd(self, argv, stdin='', ignore_fail=False, env=None, **kwargs):
+        logging.debug('runcmd: %s %s %s' % (argv, env, kwargs))
         p = subprocess.Popen(argv, stdin=subprocess.PIPE,
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                             **kwargs)
+                             env=env, **kwargs)
         out, err = p.communicate(stdin)
         if p.returncode != 0:
             msg = 'command failed: %s\n%s\n%s' % (argv, out, err)
@@ -281,6 +287,9 @@ class VmDebootstrap(cliapp.Application):
         else:
             necessary_packages = ['acpid']
 
+        if self.settings['grub']:
+            necessary_packages.append('grub2')
+
         include = self.settings['package']
 
         if not self.settings['no-kernel']:
@@ -295,8 +304,9 @@ class VmDebootstrap(cliapp.Application):
             include.append('sudo')
 
         args = ['debootstrap', '--arch=%s' % self.settings['arch']]
-        args.append(
-            '--include=%s' % ','.join(necessary_packages + include))
+        if self.settings['package'] and len(necessary_packages) > 0:
+            args.append(
+                '--include=%s' % ','.join(necessary_packages + include))
         if self.settings['foreign']:
             args.append('--foreign')
         if self.settings['variant']:
@@ -307,12 +317,22 @@ class VmDebootstrap(cliapp.Application):
         logging.debug(" ".join(args))
         self.runcmd(args)
         if self.settings['foreign']:
+            # set a noninteractive debconf environment for secondstage
+            env = {
+                "DEBIAN_FRONTEND": "noninteractive",
+                "DEBCONF_NONINTERACTIVE_SEEN": "true",
+                "LC_ALL": "C"
+            }
+            # add the mapping to the complete environment.
+            env.update(os.environ)
             # First copy the binfmt handler over
+            self.message('Setting up binfmt handler')
             shutil.copy(self.settings['foreign'], '%s/usr/bin/' % rootdir)
             # Next, run the package install scripts etc.
             self.message('Running debootstrap second stage')
             self.runcmd(['chroot', rootdir,
-                         '/debootstrap/debootstrap', '--second-stage'])
+                         '/debootstrap/debootstrap', '--second-stage'],
+                         env=env)
 
     def set_hostname(self, rootdir):
         hostname = self.settings['hostname']
@@ -443,6 +463,26 @@ class VmDebootstrap(cliapp.Application):
             with open(inittab, 'a') as f:
                 f.write('\nS0:23:respawn:%s\n' % serial_command)
 
+    def install_grub2(self, rootdev, rootdir):
+        self.message("Configuring grub2")
+        # rely on kpartx using consistent naming to map loop0p1 to loop0
+        install_dev = os.path.join('/dev', os.path.basename(rootdev)[:-2])
+        self.runcmd(['mount', '/dev', '-t', 'devfs', '-obind',
+                    '%s' % os.path.join(rootdir, 'dev')])
+        self.runcmd(['mount', '/proc', '-t', 'proc', '-obind',
+                    '%s' % os.path.join(rootdir, 'proc')])
+        self.runcmd(['mount', '/sys', '-t', 'sysfs', '-obind',
+                    '%s' % os.path.join(rootdir, 'sys')])
+        try:
+            self.runcmd(['chroot', rootdir, 'update-grub'])
+            self.runcmd(['chroot', rootdir, 'grub-install', install_dev])
+        except cliapp.AppException as e:
+            self.message("Failed to configure grub2. Using extlinux.")
+        self.runcmd(['umount', os.path.join(rootdir, 'sys')])
+        self.runcmd(['umount', os.path.join(rootdir, 'proc')])
+        self.runcmd(['umount', os.path.join(rootdir, 'dev')])
+        self.install_extlinux(rootdev, rootdir)
+
     def install_extlinux(self, rootdev, rootdir):
         if not os.path.exists("/usr/bin/extlinux"):
             self.message("extlinux not installed, skipping.")
@@ -458,8 +498,13 @@ class VmDebootstrap(cliapp.Application):
                     return os.path.join('boot', basename)
             raise cliapp.AppException('Cannot find match: %s' % pattern)
 
-        kernel_image = find('vmlinuz-.*')
-        initrd_image = find('initrd.img-.*')
+        try:
+            kernel_image = find('vmlinuz-.*')
+            initrd_image = find('initrd.img-.*')
+        except cliapp.AppException as e:
+            self.message("Unable to find kernel. Not installing extlinux.")
+            logging.debug("No kernel found. %s. Skipping install of extlinux." % e)
+            return
 
         out = self.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
                            '-s', 'UUID', rootdev])
@@ -536,10 +581,17 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
 
     def customize(self, rootdir):
         script = self.settings['customize']
-        if script:
-            self.message('Running customize script %s' % script)
-            with open('/dev/tty', 'w') as tty:
-                cliapp.runcmd([script, rootdir], stdout=tty, stderr=tty)
+        if not script:
+            return
+        if not os.path.exists(script):
+            example = os.path.join("/usr/share/vmdebootstrap/examples/", script)
+            if not os.path.exists(example):
+                self.message("Unable to find %s" % script)
+                return
+            script = example
+        self.message('Running customize script %s' % script)
+        with open('/dev/tty', 'w') as tty:
+            cliapp.runcmd([script, rootdir], stdout=tty, stderr=tty)
 
     def create_tarball(self, rootdir):
         # Create a tarball of the disk's contents