示例#1
0
def modify(name, root, cfg):
    rpms = expand_rpms(cfg.get('rpms'))
    if not rpms:
        return
    util.print_iterable(rpms,
                        header=("Installing the following rpms"
                                " in module %s" % (util.quote(name))))
    util.ensure_dir(util.abs_join(root, 'tmp'))
    cleanup_fns = []
    for fn in rpms:
        cp_to = util.abs_join(root, 'tmp', os.path.basename(fn))
        util.copy(fn, cp_to)
        cleanup_fns.append(cp_to)
    real_fns = []
    for fn in rpms:
        real_fns.append(os.path.join('/tmp', os.path.basename(fn)))
    cmd = ['chroot', root,
           'yum', '--nogpgcheck', '-y',
           'localinstall']
    cmd.extend(real_fns)
    try:
        util.subp(cmd, capture=False)
    finally:
        # Ensure cleaned up
        for fn in cleanup_fns:
            util.del_file(fn)
示例#2
0
def fix_fstab(root_dir, fstype):
    # /etc/fstab format
    # <file system>        <dir>        
    # <type>    <options>             <dump> <pass>
    lines = [
        '# Generated on %s' % (util.time_rfc2822()),
        '%s%14s%14s%14s%14s%6s' % ('LABEL=root', 
                                   '/', fstype, 'defaults', '0', '0')
    ]
    contents = "\n".join(lines)
    print("Writing a new fstab:")
    print(contents)
    util.write_file(util.abs_join(root_dir, 'etc', 'fstab'),
                    "%s\n" % (contents))
示例#3
0
def make_virt_xml(kernel_fn, ram_fn, root_fn):
    params = {
        'name': uuid.uuid5(uuid.NAMESPACE_URL,
                          # Just a fake url to get a uuid
                          'http://images.yahoo.com/%s/%s/%s' % 
                          (urllib.quote(root_fn),
                           urllib.quote(kernel_fn),
                           urllib.quote(ram_fn))),
        # 512 MB of ram should be enough for everyone
        'memory': (512 * 1024 * 1024),
        # Add a fake basepath on, to ensure
        # that users replace this since it apparently
        # requires a fully specified path to work
        'kernel': "{basepath}/" + os.path.basename(kernel_fn),
        'initrd': "{basepath}/" + os.path.basename(ram_fn),
        'root': "{basepath}/" + os.path.basename(root_fn),
    }
    tpl_c = util.load_file(util.abs_join('templates', 'virt.xml'))
    tpl = tempita.Template(tpl_c)
    return tpl.substitute(**params)
示例#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)