diff options
author | Neil Williams <codehelp@debian.org> | 2015-08-16 12:21:26 +0200 |
---|---|---|
committer | Neil Williams <codehelp@debian.org> | 2015-08-16 17:02:12 +0200 |
commit | 34252150c3ca24e1dc88712406f73f0a78d9ea55 (patch) | |
tree | ad9168d67c4593d73e2758df793bb1b653a3bcf2 /bin | |
parent | 9d776bb74654b80815fe08d87b5237e577ef1099 (diff) | |
download | vmdebootstrap-34252150c3ca24e1dc88712406f73f0a78d9ea55.tar.gz |
use a constants handler
Add support for using squashfs as an alternative to creating
a tarball of a directory tree.
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/vmdebootstrap | 77 |
1 files changed, 39 insertions, 38 deletions
diff --git a/bin/vmdebootstrap b/bin/vmdebootstrap index 876ec5f..b13d976 100755 --- a/bin/vmdebootstrap +++ b/bin/vmdebootstrap @@ -34,8 +34,8 @@ from vmdebootstrap.grub import GrubHandler from vmdebootstrap.extlinux import ExtLinux from vmdebootstrap.codenames import Codenames from vmdebootstrap.filesystem import Filesystem -from vmdebootstrap.uefi import Uefi, arch_table - +from vmdebootstrap.uefi import Uefi +from vmdebootstrap.constants import arch_table __version__ = '1.0' @@ -168,6 +168,11 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth 'the build is complete.') self.settings.boolean( ['squash'], 'use squashfs on the final image.') + self.settings.string( + ['squash-file'], 'filename for the squashfs ' + '- cannot be used with --image', + metavar='FILE', + default='rootfs.squash') self.settings.boolean( ['configure-apt'], 'Create an apt source based on ' 'the distribution and mirror selected.') @@ -193,9 +198,11 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth handler.define_settings(self.settings) distro = self.handlers[Codenames.name] - if not self.settings['image'] and not self.settings['tarball']: + if not self.settings['image'] and not ( + self.settings['tarball'] or self.settings['squash']): raise cliapp.AppException( - 'You must give disk image filename, or tarball filename') + 'You must give disk image filename or use either a ' + 'tarball filename or use squash') if self.settings['image'] and not self.settings['size']: raise cliapp.AppException( 'If disk image is specified, you must give image size.') @@ -247,7 +254,7 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth roottype = filesystem.devices['roottype'] bootdev = filesystem.devices['bootdev'] if self.settings['swap'] > 0: - self.message("Creating swap space") + base.message("Creating swap space") runcmd(['mkswap', filesystem.devices['swapdev']]) filesystem.mkfs(rootdev, fstype=roottype) rootdir = self.mount(rootdev) @@ -268,12 +275,13 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth grub = self.handlers[GrubHandler.name] extlinux = self.handlers[ExtLinux.name] base = self.handlers[Base.name] + uefi = self.handlers[Uefi.name] filesystem = self.handlers[Filesystem.name] if self.settings['use-uefi']: grub.install_grub_uefi(rootdir) - uefi.configure_efi() + uefi.configure_efi(rootdir) grub.install_extra_grub_uefi(rootdir) - uefi.configure_extra_efi() + uefi.configure_extra_efi(rootdir) elif self.settings['grub']: if not grub.install_grub2(rootdev, rootdir): extlinux.install_extlinux(rootdev, rootdir) @@ -281,7 +289,7 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth extlinux.install_extlinux(rootdev, rootdir) base.append_serial_console(rootdir) self.optimize_image(rootdir) - filesystem.squash() + filesystem.squash_image() def start_ops(self): base = self.handlers[Base.name] @@ -293,6 +301,7 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth rootdev = filesystem.devices['rootdev'] else: rootdir = self.mkdtemp() + rootdev = filesystem.devices['rootdev'] logging.debug("rootdir=%s rootdev=%s", rootdir, rootdev) self.debootstrap(rootdir) filesystem.set_hostname() @@ -316,38 +325,23 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth if self.settings['tarball']: base.create_tarball(rootdir) + else: + filesystem.squash_filesystem() filesystem.chown() except BaseException as e: - self.message('EEEK! Something bad happened...') + base.message('EEEK! Something bad happened...') + rootdir = filesystem.devices['rootdir'] if rootdir: db_log = os.path.join(rootdir, 'debootstrap', 'debootstrap.log') if os.path.exists(db_log): shutil.copy(db_log, os.getcwd()) - self.message(e) + base.message(e) self.cleanup_system() raise else: self.cleanup_system() - def message(self, msg): - logging.info(msg) - if self.settings['verbose']: - print msg - - 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, - env=env, **kwargs) - out, err = p.communicate(stdin) - if p.returncode != 0: - msg = 'command failed: %s\n%s\n%s' % (argv, out, err) - logging.error(msg) - if not ignore_fail: - raise cliapp.AppException(msg) - return out - def mkdtemp(self): dirname = tempfile.mkdtemp() self.remove_dirs.append(dirname) @@ -355,11 +349,12 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth return dirname def mount(self, device, path=None): + base = self.handlers[Base.name] if not path: mount_point = self.mkdtemp() else: mount_point = path - self.message('Mounting %s on %s' % (device, mount_point)) + base.message('Mounting %s on %s' % (device, mount_point)) runcmd(['mount', device, mount_point]) self.mount_points.append(mount_point) logging.debug('mounted %s on %s', device, mount_point) @@ -372,7 +367,8 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth starts at that offset to allow customisation scripts to put bootloader images into the space, e.g. u-boot. """ - self.message('Creating partitions') + base = self.handlers[Base.name] + base.message('Creating partitions') runcmd(['parted', '-s', self.settings['image'], 'mklabel', self.settings['part-type']]) partoffset = 0 @@ -399,13 +395,13 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth partoffset, self.settings['bootoffset']) else: partoffset = self.settings['bootoffset'] / (1024 * 1024) - self.message("Using bootoffset: %smib %s bytes" % (partoffset, self.settings['bootoffset'])) + base.message("Using bootoffset: %smib %s bytes" % (partoffset, self.settings['bootoffset'])) if self.settings['bootsize'] and self.settings['bootsize'] is not '0%': if self.settings['grub'] and not partoffset: partoffset = 1 bootsize = self.settings['bootsize'] / (1024 * 1024) bootsize += partoffset - self.message("Using bootsize %smib: %s bytes" % (bootsize, self.settings['bootsize'])) + base.message("Using bootsize %smib: %s bytes" % (bootsize, self.settings['bootsize'])) logging.debug("Starting boot partition at %sMb", bootsize) runcmd(['parted', '-s', self.settings['image'], 'mkpart', 'primary', 'fat16', str(partoffset), str(bootsize)]) @@ -463,6 +459,7 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth return include def _debootstrap_second_stage(self, rootdir): + base = self.handlers[Base.name] # set a noninteractive debconf environment for secondstage env = { "DEBIAN_FRONTEND": "noninteractive", @@ -472,17 +469,18 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth # add the mapping to the complete environment. env.update(os.environ) # First copy the binfmt handler over - self.message('Setting up binfmt handler') + base.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') + base.message('Running debootstrap second stage') runcmd(['chroot', rootdir, '/debootstrap/debootstrap', '--second-stage'], env=env) def debootstrap(self, rootdir): + base = self.handlers[Base.name] msg = "(%s)" % self.settings['variant'] if self.settings['variant'] else '' - self.message( + base.message( 'Debootstrapping %s [%s] %s' % ( self.settings['distribution'], self.settings['arch'], msg)) include = self._bootstrap_packages() @@ -505,9 +503,10 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth self._debootstrap_second_stage(rootdir) def install_debs(self, rootdir): + base = self.handlers[Base.name] if not self.settings['custom-package']: return - self.message('Installing custom packages') + base.message('Installing custom packages') tmp = os.path.join(rootdir, 'tmp', 'install_debs') os.mkdir(tmp) for deb in self.settings['custom-package']: @@ -533,7 +532,8 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth runcmd(['rm', '-f', zeros]) def setup_networking(self, rootdir): - self.message('Setting up networking') + base = self.handlers[Base.name] + base.message('Setting up networking') distro = self.handlers[Codenames.name] # unconditionally write for wheezy (which became oldstable on 04/25/2015) if distro.was_oldstable(datetime.date(2015, 4, 26)): @@ -556,9 +556,10 @@ class VmDebootstrap(cliapp.Application): # pylint: disable=too-many-public-meth eth.write('iface eth0 inet dhcp\n') def cleanup_system(self): + base = self.handlers[Base.name] # Clean up after any errors. - self.message('Cleaning up') + base.message('Cleaning up') # Umount in the reverse mount order if self.settings['image']: |