Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory filled with data at /var/backups/disk1 that I want to convert into a virtual disk image which I'll then be able to boot using QEMU or KVM (the directory contains the file system for a virtual machine, copied out via rsync).

While there are plenty of instructions out there for converting a complete physical disk to a virtual disk, packaging up only the contents of a single directory into a virtual disk image is turning out to be much more difficult than I expected. Any ideas?

By the way, I know I can use qemu-img to convert a block device into a virtual disk (e.g., "qemu-img convert -f /dev/sdc -O qcow2 disk.qcow2"), so if only I could get the directory /var/backups/disk1 to appear to be a block device, then theoretically I should be able to accomplish my goal using qemu-img. I've thought about creative ways to expose the directory as a block device using NBD or a loopback device, however, and have not had success.

share|improve this question

First, create a raw image of the required size. I'll assume 10G is enough. Using seek creates a sparse file, which saves space.

dd if=/dev/null of=example.img bs=1M seek=10240

Next, create a filesystem on it.

mkfs.ext4 -F example.img

(Note that you need the -F option for mkfs.ext4 to operate on a file as opposed to a disk partition)

Then, mount it.

mkdir /mnt/example
mount -t ext4 -o loop example.img /mnt/example

Now you can copy your files to /mnt/example. Once this is done, unmount it and you can use example.img as a drive in a virtual machine. If you want you can convert it from a raw image to another format like qcow2e using qemu-img, but this isn't required.

share|improve this answer
qemu-img and fallocate are a slightly more convenient than dd. – Ciro Santilli 巴拿馬文件 六四事件 法轮功 May 20 '15 at 16:14

Seems like libguestfs can do it, see virt-make-fs(1) and maybe virt-copy-in(1).

share|improve this answer

What about accessing the directory as a network share from a VM that is also mounted to your target virtual disk image? You than than just perform a simple copy from the share to the mounted disk image.

share|improve this answer
That should work, and is similar to my current strategy, which is to build a VM and rsync the data in. It would be great if there were a quicker and cleaner solution, though. Copying the data into a new machine gets pretty messy because you have to worry about making sure you exclude certain files that shouldn't be copied, like fstab and most things in /boot – Chris Mar 13 '11 at 21:22
"exclude certain files that shouldn't be copied, like fstab and most things in /boot" - may not be that much of an issue when the VM target is an additional volume. – user48838 Mar 14 '11 at 12:32

Using Michaels strategy in Fedora 23:

# dnf install -y libguestfs-tools-c
# virt-make-fs --format=raw --label mylabel the_directory myfs.raw

This creates ext2 file system, that you can mount to some directory:

# mount file.fs /mnt 

Now /mnt contains all your files

share|improve this answer
If file.fs is a file, shouldn't the mount command be: mount -o loop file.fs /mnt? – kasperd Nov 9 '15 at 22:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.