Esempio n. 1
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))
Esempio n. 2
0
def hash_file(path, out_fn, routine):
    hasher = hashlib.new(routine)

    def hash_cb(_byte_am, chunk):
        hasher.update(chunk)

    base_name = os.path.basename(path)
    with open(path, 'rb') as in_fh:
        byte_size = os.path.getsize(path)
        with open(os.devnull, 'wb') as out_fh:
            util.pretty_transfer(in_fh, out_fh,
                name="%s hashing %s" % (routine.capitalize(), base_name),
                chunk_cb=hash_cb, max_size=byte_size)

    # The md5 sum program produces this output format, so mirror that...
    digest = hasher.hexdigest().lower()
    contents = "%s  %s\n" % (digest, os.path.basename(path))
    util.write_file(out_fn, contents)
Esempio n. 3
0
 def download(self):
     (cache_pth, exists_there) = self._check_cache()
     if exists_there:
         return cache_pth
     print("Downloading from: %s" % (util.quote(self.where_from)))
     util.ensure_dirs([os.path.dirname(cache_pth)])
     print("To: %s" % (util.quote(cache_pth)))
     util.download_url(self.where_from, cache_pth)
     try:
         meta_js = {
             'cached_on': util.time_rfc2822(),
             'from': self.where_from,
             'root_file': self.root_file,
         }
         util.write_file("%s.json" % (cache_pth),
                         "%s\n" % (json.dumps(meta_js, indent=4)))
         return self._adjust_real_root(cache_pth)
     except:
         util.del_file(cache_pth)
         raise
Esempio 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)