]> git.siccegge.de Git - forks/vmdebootstrap.git/blobdiff - vmdebootstrap
Cleanups and --lock-root-password and no default root password
[forks/vmdebootstrap.git] / vmdebootstrap
index 5275d96d90d843e0500f1af46d2252f82c0ea1b9..c8fc426c1d8ec295fc150069beb63980a7cdac43 100755 (executable)
@@ -48,9 +48,15 @@ class VmDebootstrap(cliapp.Application):
                              metavar='NAME',
                              default='stable')
         self.settings.string_list(['package'], 'install PACKAGE onto system')
+        self.settings.string_list(['custom-package'],
+                                  'install package in DEB file onto system '
+                                    '(not from mirror)',
+                                  metavar='DEB')
         self.settings.boolean(['enable-dhcp'], 'enable DHCP on eth0')
         self.settings.string(['root-password'], 'set root password',
                              metavar='PASSWORD')
+        self.settings.boolean(['lock-root-password'], 
+                              'lock root account so they cannot login?')
         self.settings.string(['customize'],
                              'run SCRIPT after setting up system',
                              metavar='SCRIPT')
@@ -61,6 +67,11 @@ class VmDebootstrap(cliapp.Application):
         self.settings.string_list(['user'],
                                   'create USER with PASSWORD',
                                   metavar='USER/PASSWORD')
+        self.settings.boolean(['serial-console'], 
+                              'configure image to use a serial console')
+        self.settings.boolean(['sudo'], 
+                              'install sudo, and if user is created, add them '
+                                'to sudo group')
 
     def process_args(self, args):
         if not self.settings['image']:
@@ -81,6 +92,7 @@ class VmDebootstrap(cliapp.Application):
             self.debootstrap(rootdir)
             self.set_hostname(rootdir)
             self.create_fstab(rootdir)
+            self.install_debs(rootdir)
             self.set_root_password(rootdir)
             self.create_users(rootdir)
             self.remove_udev_persistent_rules(rootdir)
@@ -89,10 +101,10 @@ class VmDebootstrap(cliapp.Application):
             self.customize(rootdir)
         except BaseException, e:
             self.message('EEEK! Something bad happened...')
-            self.cleanup()
+            self.cleanup_system()
             raise
         else:
-            self.cleanup()
+            self.cleanup_system()
 
     def message(self, msg):
         if self.settings['verbose']:
@@ -161,12 +173,14 @@ class VmDebootstrap(cliapp.Application):
         self.message('Debootstrapping')
 
         if self.settings['arch'] == 'i386':
-            kernel_arch = 'i686'
+            kernel_arch = '686'
         else:
             kernel_arch = self.settings['arch']
         kernel_image = 'linux-image-2.6-%s' % kernel_arch
 
         include = [kernel_image] + self.settings['package']
+        if self.settings['sudo'] and 'sudo' not in include:
+            include.append('sudo')
 
         self.runcmd(['debootstrap', 
                      '--arch=%s' % self.settings['arch'],
@@ -195,18 +209,42 @@ class VmDebootstrap(cliapp.Application):
             f.write('proc /proc proc defaults 0 0\n')
             f.write('/dev/sda1 / ext4 errors=remount-ro 0 1\n')
 
+    def install_debs(self, rootdir):
+        if not self.settings['custom-package']:
+            return
+        self.message('Installing custom packages')
+        tmp = os.path.join(rootdir, 'tmp', 'install_debs')
+        os.mkdir(tmp)
+        for deb in self.settings['custom-package']:
+            shutil.copy(deb, tmp)
+        filenames = [os.path.join('/tmp/install_debs', os.path.basename(deb))
+                     for deb in self.settings['custom-package']]
+        out, err, exit = \
+            self.runcmd_unchecked(['chroot', rootdir, 'dpkg', '-i'] + filenames)
+        logging.debug('stdout:\n%s' % out)
+        logging.debug('stderr:\n%s' % err)
+        out = self.runcmd(['chroot', rootdir, 
+                     'apt-get', '-f', '--no-remove', 'install'])
+        logging.debug('stdout:\n%s' % out)
+        shutil.rmtree(tmp)
+
     def set_root_password(self, rootdir):
         if self.settings['root-password']:
             self.message('Setting root password')
             self.set_password(rootdir, 'root', self.settings['root-password'])
-        else:
+        elif self.settings['lock-root-password']:
             self.message('Locking root password')
             self.runcmd(['chroot', rootdir, 'passwd', '-l', 'root'])
+        else:
+            self.message('Give root an empty password')
+            self.runcmd(['chroot', rootdir, 'passwd', '-d', 'root'])
 
     def create_users(self, rootdir):
         def create_user(user):
             self.runcmd(['chroot', rootdir, 'adduser', '--gecos', user,
                          '--disabled-password', user])
+            if self.settings['sudo']:
+                self.runcmd(['chroot', rootdir, 'adduser', user, 'sudo'])
 
         for userpass in self.settings['user']:
             if '/' in userpass:
@@ -272,19 +310,29 @@ timeout 1
 
 label linux
 kernel %(kernel)s
-append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet
+append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet %(kserial)s
+%(extserial)s
 ''' % {
     'kernel': kernel_image,
     'initrd': initrd_image,
     'uuid': uuid,
+    'kserial': 
+        'console=ttyS0,115200' if self.settings['serial-console'] else '',
+    'extserial': 'serial 0 115200' if self.settings['serial-console'] else '',
 })
         f.close()
+        
+        if self.settings['serial-console']:
+            logging.debug('adding getty to serial console')
+            inittab = os.path.join(rootdir, 'etc/inittab')
+            with open(inittab, 'a') as f:
+                f.write('\nS0:23:respawn:/sbin/getty -L ttyS0 115200 vt100\n')
 
         self.runcmd(['extlinux', '--install', rootdir])
         self.runcmd(['sync'])
         import time; time.sleep(2)
         
-    def cleanup(self):
+    def cleanup_system(self):
         # Clean up after any errors.
 
         self.message('Cleaning up')