Exemplo n.º 1
0
def activate_modules(tmp_file_name, config):
    with util.tempdir() as tdir:
        devname = create_loopback(tmp_file_name, PART_OFFSET)
        with cmd_undo(['losetup', '-d', devname]):
            # Mount it
            root_dir = os.path.join(tdir, 'mnt')
            os.makedirs(root_dir)
            util.subp(['mount', devname, root_dir])
            # Run your modules!
            with cmd_undo(['umount', root_dir]):
                return run_modules(root_dir, config)
Exemplo n.º 2
0
 def _adjust_real_root(self, arch_path):
     if self.root_file:
         print("Oh you really meant %s, finding that file..." % (util.quote(self.root_file)))
         # Extract and then copy over the right file...
         with util.tempdir() as tdir:
             arch_dir = os.path.join(tdir, 'archive')
             os.makedirs(arch_dir)
             util.subp(['tar', '-xzf', arch_path, '-C', arch_dir])
             root_gz = util.find_file(self.root_file, arch_dir)
             if not root_gz:
                 raise RuntimeError(("Needed file %r not found in"
                                     " extracted contents of %s") 
                                     % (self.root_file, arch_path))
             else:
                 util.copy(root_gz, arch_path)
     return arch_path
Exemplo n.º 3
0
def extract_into(tmp_file_name, fs_type, config):
    with util.tempdir() as tdir:
        # Download the image
        # TODO (make this a true module that can be changed...)
        tb_down = tar_ball.TarBallDownloader(dict(config['download']))
        arch_fn = tb_down.download()

        # Extract it
        devname = create_loopback(tmp_file_name, PART_OFFSET)
        with cmd_undo(['losetup', '-d', devname]):
            # Mount it
            root_dir = os.path.join(tdir, 'mnt')
            os.makedirs(root_dir)
            util.subp(['mount', devname, root_dir])
            # Extract it
            with cmd_undo(['umount', root_dir]):
                print("Extracting 'root' tarball %s to %s." % 
                                        (util.quote(arch_fn), 
                                         util.quote(root_dir)))
                util.subp(['tar', '-xzf', arch_fn, '-C', root_dir])
                # Fixup the fstab
                fix_fstab(root_dir, fs_type)
Exemplo n.º 4
0
def ec2_convert(raw_fn, out_fn, out_fmt, strip_partition, compress):
    # Extract the ramdisk/kernel
    devname = create_loopback(raw_fn, PART_OFFSET)
    with util.tempdir() as tdir:
        img_dir = os.path.join(tdir, 'img')
        root_dir = os.path.join(tdir, 'mnt')
        util.ensure_dirs([img_dir, root_dir])
        with cmd_undo(['losetup', '-d', devname]):
            print("Copying off the ramdisk and kernel files.")
            # Mount it
            util.subp(['mount', devname, root_dir])
            with cmd_undo(['umount', root_dir]):
                # Find the right files
                fns = {}
                for fn in os.listdir(util.abs_join(root_dir, 'boot')):
                    if fn.endswith('.img') and fn.startswith('initramfs-'):
                        fns['ramdisk'] = fn
                    if fn.startswith('vmlinuz-'):
                        fns['kernel'] = fn
                    if fn.startswith('initrd-') and fn.endswith('.img'):
                        fns['base'] = fn
                rd_fn = fns.get('ramdisk')
                k_fn = fns.get('kernel')
                if (not rd_fn and not k_fn) and 'base' in fns:
                    kid = fns['base']
                    kid = kid[0:-len('.img')]
                    kid = kid[len('initrd-'):]
                    cmd = ['chroot', root_dir,
                           '/sbin/mkinitrd', '-f',
                           os.path.join('/boot', fns['base']),
                           kid]
                    util.subp(cmd, capture=False)
                    if os.path.isfile(util.abs_join(root_dir, "boot", 
                                     "initramfs-%s.img" % (kid))):
                        rd_fn = "initramfs-%s.img" % (kid)
                    if os.path.isfile(util.abs_join(root_dir, "boot",
                                      "vmlinuz-%s" % (kid))):
                        k_fn = "vmlinuz-%s" % (kid)
                if not rd_fn:
                    raise RuntimeError("No initramfs-*.img file found")
                if not k_fn:
                    raise RuntimeError("No vmlinuz-* file found")
                shutil.move(util.abs_join(root_dir, 'boot', rd_fn), 
                            util.abs_join(img_dir, rd_fn))
                shutil.move(util.abs_join(root_dir, 'boot', k_fn), 
                            util.abs_join(img_dir, k_fn))
            # Copy off the data (minus the partition info)
            if strip_partition:
                print("Stripping off the partition table.")
                print("Please wait...")
                part_stripped_fn = dd_off(devname, tdir)
        # Replace the orginal 'raw' file
        if strip_partition:
            shutil.move(part_stripped_fn, raw_fn)
        # Apply some tune ups
        cmd = [
            'tune2fs',
            # Set the volume label of the filesystem
            '-L', 'root',
            raw_fn
        ]
        util.subp(cmd, capture=False)
        # Convert it to the final format and compress it
        out_base_fn = os.path.basename(out_fn)
        img_fn = out_base_fn
        if img_fn.endswith('.tar.gz'):
            img_fn = img_fn[0:-len('.tar.gz')]
        img_fn += "." + out_fmt
        img_fn = util.abs_join(img_dir, img_fn)
        straight_convert(raw_fn, img_fn, out_fmt)
        # Make a nice helper libvirt.xml file
        util.write_file(util.abs_join(img_dir, 'libvirt.xml'),
                        make_virt_xml(util.abs_join(img_dir, k_fn),
                                      util.abs_join(img_dir, rd_fn),
                                      util.abs_join(img_dir, img_fn)))
        # Give every file written a hash/checksum file
        for fn in os.listdir(img_dir):
            src_fn = util.abs_join(img_dir, fn)
            hash_fn = src_fn + "." + HASH_ROUTINE
            hash_file(src_fn, hash_fn, HASH_ROUTINE)
        # Compress it or just move the folder around
        if compress:
            with closing(tarfile.open(out_fn, 'w:gz')) as tar_fh:
                for fn in os.listdir(img_dir):
                    src_fn = util.abs_join(img_dir, fn)
                    transfer_into_tarball(src_fn, fn, tar_fh)
        else:
            shutil.move(img_dir, out_fn)