+ with open(f, 'r') as src:
+ lines = src.readlines()
+ with open(f, 'w') as dest:
+ for line in lines:
+ for i, uuid in new_uuid.items():
+ line = re.sub(orig[i], uuid, line)
+ dest.write(line)
+
+ def _create_filesystems(self, image, partitions):
+ """Fill the image with data. Host file systems that are not currently
+ mounted are binary copied into the image. For mounted file systems, a
+ file system level copy is performed.
+ """
+
+ filesystem = {}
+ orig_dev = {}
+ for p in self.disk.partitions:
+ filesystem[p.number] = self._get_mount_options(p.path)
+ orig_dev[p.number] = p.path
+
+ unmounted = filter(lambda p: filesystem[p.num] is None, partitions)
+ mounted = filter(lambda p: filesystem[p.num] is not None, partitions)
+
+ # For partitions that are not mounted right now, we can simply dd them
+ # into the image.
+ for p in unmounted:
+ self.out.output('Cloning partition %d ... ' % p.num, False)
+ dd('if=%s' % self.disk.device.path, 'of=%s' % image,
+ 'count=%d' % (p.end - p.start + 1), 'conv=notrunc',
+ 'seek=%d' % p.start, 'skip=%d' % p.start)
+ self.out.success("done")
+
+ loop = str(losetup('-f', '--show', image)).strip()
+
+ # Recreate mounted file systems
+ mapped = {}
+ try:
+ for p in mounted:
+ i = p.num
+ mapped[i] = self._map_partition(loop, i, p.start, p.end)
+
+ new_uuid = {}
+ # Create the file systems
+ for i, dev in mapped.iteritems():
+ fs = filesystem[i].fs
+ self.out.output('Creating %s filesystem on partition %d ... ' %
+ (fs, i), False)
+ get_command('mkfs.%s' % fs)(*(MKFS_OPTS[fs] + [dev]))
+
+ # For ext[234] enable the default mount options
+ if re.match('^ext[234]$', fs):
+ mopts = filter(
+ lambda p: p.startswith('Default mount options:'),
+ tune2fs('-l', orig_dev[i]).splitlines()
+ )[0].split(':')[1].strip().split()
+
+ if not (len(mopts) == 1 and mopts[0] == '(none)'):
+ for opt in mopts:
+ tune2fs('-o', opt, dev)
+
+ self.out.success('done')
+ new_uuid[i] = blkid(
+ '-s', 'UUID', '-o', 'value', dev).stdout.strip()
+
+ target = tempfile.mkdtemp()
+ devs = []
+ for i in mapped.keys():
+ fs = filesystem[i].fs
+ mpoint = filesystem[i].mpoint
+ opts = []
+ for opt in filesystem[i].opts.split(','):
+ if opt in ('acl', 'user_xattr'):
+ opts.append(opt)
+ devs.append((mapped[i], mpoint, opts))