X-Git-Url: https://git.siccegge.de//index.cgi?a=blobdiff_plain;f=vmdebootstrap;h=e32c7bb0cf3ae35044a0e31660d80f51d39de0ca;hb=060cc85bbdef67156e2654b63969e16306088d4f;hp=295bdc74f7017ef4e551c431a1f16baf6d030610;hpb=8eae76bc2cbb5ca1ff197c0568afd91c2b74473a;p=forks%2Fvmdebootstrap.git diff --git a/vmdebootstrap b/vmdebootstrap index 295bdc7..e32c7bb 100755 --- a/vmdebootstrap +++ b/vmdebootstrap @@ -1,5 +1,6 @@ #!/usr/bin/python -# Copyright 2011 Lars Wirzenius +# Copyright 2011, 2012 Lars Wirzenius +# Copyright 2012 Codethink Limited # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -36,6 +37,8 @@ class VmDebootstrap(cliapp.Application): 'create a disk image of size SIZE (%default)', metavar='SIZE', default='1G') + self.settings.string(['tarball'], "tar up the disk's contents in FILE", + metavar='FILE') self.settings.string(['mirror'], 'use MIRROR as package source (%default)', metavar='URL', @@ -55,6 +58,8 @@ class VmDebootstrap(cliapp.Application): 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') @@ -72,21 +77,26 @@ class VmDebootstrap(cliapp.Application): 'to sudo group') def process_args(self, args): - if not self.settings['image']: - raise cliapp.AppException('You must give image filename.') - if not self.settings['size']: - raise cliapp.AppException('You must give image size.') + if not self.settings['image'] and not self.settings['tarball']: + raise cliapp.AppException('You must give disk image filename, ' + 'or tarball filename') + if self.settings['image'] and not self.settings['size']: + raise cliapp.AppException('If disk image is specified, ' + 'You must give image size.') self.remove_dirs = [] self.mount_points = [] try: - self.create_empty_image() - self.partition_image() - self.install_mbr() - rootdev = self.setup_kpartx() - self.mkfs(rootdev) - rootdir = self.mount(rootdev) + if self.settings['image']: + self.create_empty_image() + self.partition_image() + self.install_mbr() + rootdev = self.setup_kpartx() + self.mkfs(rootdev) + rootdir = self.mount(rootdev) + else: + rootdir = self.mkdtemp() self.debootstrap(rootdir) self.set_hostname(rootdir) self.create_fstab(rootdir) @@ -95,16 +105,20 @@ class VmDebootstrap(cliapp.Application): self.create_users(rootdir) self.remove_udev_persistent_rules(rootdir) self.setup_networking(rootdir) - self.install_extlinux(rootdev, rootdir) + if self.settings['image']: + self.install_extlinux(rootdev, rootdir) self.customize(rootdir) + if self.settings['tarball']: + self.create_tarball(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): + logging.info(msg) if self.settings['verbose']: print msg @@ -230,9 +244,12 @@ class VmDebootstrap(cliapp.Application): 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.delete_password(rootdir, 'root') def create_users(self, rootdir): def create_user(user): @@ -248,11 +265,15 @@ class VmDebootstrap(cliapp.Application): self.set_password(rootdir, user, password) else: create_user(userpass) + self.delete_password(rootdir, userpass) def set_password(self, rootdir, user, password): encrypted = crypt.crypt(password, '..') self.runcmd(['chroot', rootdir, 'usermod', '-p', encrypted, user]) + def delete_password(self, rootdir, user): + self.runcmd(['chroot', rootdir, 'passwd', '-d', user]) + def remove_udev_persistent_rules(self, rootdir): self.message('Removing udev persistent cd and net rules') for x in ['70-persistent-cd.rules', '70-persistent-net.rules']: @@ -327,15 +348,16 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet %(kserial)s self.runcmd(['sync']) import time; time.sleep(2) - def cleanup(self): + def cleanup_system(self): # Clean up after any errors. self.message('Cleaning up') - for mount_point in self.mount_points: - self.runcmd(['umount', mount_point], ignore_fail=True) + if self.settings['image']: + for mount_point in self.mount_points: + self.runcmd(['umount', mount_point], ignore_fail=True) - self.runcmd(['kpartx', '-d', self.settings['image']], ignore_fail=True) + self.runcmd(['kpartx', '-d', self.settings['image']], ignore_fail=True) for dirname in self.remove_dirs: shutil.rmtree(dirname) @@ -344,7 +366,14 @@ append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet %(kserial)s script = self.settings['customize'] if script: self.message('Running customize script %s' % script) - self.runcmd([script, rootdir]) + 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 + # shell out to runcmd since it more easily handles rootdir + self.message('Creating tarball of disk contents') + self.runcmd(['tar', '-cf', self.settings['tarball'], '-C', rootdir, '.']) if __name__ == '__main__':