How to use virt-make-fs to make data available inside VM ?

Today I got a requirement to share large amount of data with Virtual machine. I came to know about that command virt-make-fs is suitable for this task. In this article, I am going to show you the usage of same command.

From man page of virt-make-fs which is provided by libguestfs-tools.

virt-make-fs – Make a filesystem from a tar archive or files

Step 1 : I created an image from the data.tar file which I want to access from VM. Image name is test.img.

[root@host Downloads]# virt-make-fs data.tar /home/host/VirtualMachines/test.img

[root@host Downloads]# cd /home/host/VirtualMachines/

[root@host VirtualMachines]# ll test.img
-rw-r–r–. 1 root root 69550694 Dec 29 21:35 test.img

Step 2 : Attached the same image to Virtual machine.

[root@host VirtualMachines]# virsh list
Id    Name                           State
—————————————————-
14    idmserver1                     running

[root@host VirtualMachines]# virsh domblklist 17
Target     Source
————————————————
vda        /home/host/VirtualMachines/rhel6.6.1451397281
hdb        /home/host/VirtualMachines/test.img

Step 3 : Inside the virtual machine I can see that new image is detected.

[root@guest ]# file /dev/sda
/dev/sda: block special

Created loopback device to mount that image inside VM.

[root@guest ]# losetup /dev/loop0 /dev/sda

[root@guest ]# losetup -a
/dev/loop0: [0005]:6211 (/dev/sda)

Mounted it successfully.

[root@guest ]# mount /dev/loop0 /mnt

[root@guest ]# df -Ph /mnt
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0       65M   62M     0 100% /mnt

I am able to access the content of the tar file from inside the VM.

[root@guest ~]# cd /mnt
[root@guest mnt]# ls

By default filesystem is created of type ext2 but you can specify the filesystem while creating the image. Please refer the man page for the same.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s