def sync_filesystem(guestfs, guestfs_helper)
      devices = guestfs.list_devices
      in_device = devices.first
      out_device = devices.last
      partitions = guestfs.list_partitions.reject { |i| !(i =~ /^#{in_device}/) }

      @log.debug "Loading SELinux policy to sync filesystem..."
      partitions.size > 0 ? guestfs_helper.mount_partitions(in_device) : guestfs_helper.mount_partition(in_device, '/')
      guestfs_helper.load_selinux_policy
      partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)
      @log.debug "SELinux policy was loaded, we're ready to sync filesystem."

      @log.info "Synchronizing filesystems..."

      # Create mount points in libguestfs
      guestfs.mkmountpoint('/in')
      guestfs.mkmountpoint('/out')
      guestfs.mkmountpoint('/out/in')

      # Create filesystem on destination device
      # https://issues.jboss.org/browse/BGBUILD-321
      guestfs.mkfs(@appliance_config.hardware.partitions['/']['type'], out_device)
      # Set root partition label
      guestfs.set_e2label(out_device, '79d3d2d4') # This is a CRC32 from /

      # Mount empty EC2 disk to /out
      guestfs_helper.mount_partition(out_device, '/out/in')
      partitions.size > 0 ? guestfs_helper.mount_partitions(in_device, '/in') : guestfs_helper.mount_partition(in_device, '/in')

      @log.debug "Copying files..."

      # Copy the filesystem
      guestfs.cp_a('/in/', '/out')

      @log.debug "Files copied."

      # Better make sure...
      guestfs.sync

      guestfs_helper.umount_partition(out_device)
      partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)

      guestfs.rmmountpoint('/out/in')
      guestfs.rmmountpoint('/out')
      guestfs.rmmountpoint('/in')

      @log.info "Filesystems synchronized."

      # Remount the destination disk
      guestfs_helper.mount_partition(out_device, '/')
    end
def sync_filesystem(guestfs, guestfs_helper)
      devices = guestfs.list_devices
      in_device = devices.first
      out_device = devices.last
      partitions = guestfs.list_partitions.reject { |i| !(i =~ /^#{in_device}/) }

      @log.debug "Loading SELinux policy to sync filesystem..."
      partitions.size > 0 ? guestfs_helper.mount_partitions(in_device) : guestfs_helper.mount_partition(in_device, '/')
      guestfs_helper.load_selinux_policy
      partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)
      @log.debug "SELinux policy was loaded, we're ready to sync filesystem."

      @log.info "Synchronizing filesystems..."

      # Create mount points in libguestfs
      guestfs.mkmountpoint('/in')
      guestfs.mkmountpoint('/out')
      guestfs.mkmountpoint('/out/in')

      # Create filesystem on destination device
      # https://issues.jboss.org/browse/BGBUILD-321
      guestfs.mkfs(@appliance_config.hardware.partitions['/']['type'], out_device)
      # Set root partition label
      guestfs.set_e2label(out_device, '79d3d2d4') # This is a CRC32 from /

      # Mount empty EC2 disk to /out
      guestfs_helper.mount_partition(out_device, '/out/in')
      partitions.size > 0 ? guestfs_helper.mount_partitions(in_device, '/in') : guestfs_helper.mount_partition(in_device, '/in')

      @log.debug "Copying files..."

      # Copy the filesystem
      guestfs.cp_a('/in/', '/out')

      @log.debug "Files copied."

      # Better make sure...
      guestfs.sync

      guestfs_helper.umount_partition(out_device)
      partitions.size > 0 ? guestfs_helper.umount_partitions(in_device) : guestfs_helper.umount_partition(in_device)

      guestfs.rmmountpoint('/out/in')
      guestfs.rmmountpoint('/out')
      guestfs.rmmountpoint('/in')

      @log.info "Filesystems synchronized."

      # Remount the destination disk
      guestfs_helper.mount_partition(out_device, '/')
    end
Synchronizes filesystem from one image with an empty disk image.
 Input image can be a partioned image or a partition image itself.
 Output disk is a partition image.

 See also https://bugzilla.redhat.com/show_bug.cgi?id=690819