How To Create A Shared Directory Between Host And Guest In KVM?


KVM or kernel-based virtual machine is a virtualization module of Linux kernel. It requires hardware with virtualization extensions such as Intel-VT or AMD-V to provide hardware-assisted virtualization. By using VirtIO it provides paravirtualization support for various operating systems such as Linux, FreeBSD, Windows, etc.

While working on VMs sometimes you may need to use a file that is on the host OS. You can do this in various ways one of them is 9p virtio sharing. In this article, we will see how you can create a shared directory between guest and host OS using virt-manager.

Host operating system

Host refers to a bare-metal machine and the operating system that runs directly on it is called the host OS. It installs the required virtualization packages and provides an environment to run an OS in a virtual machine.

Guest operating system

An OS installed on a virtual machine that is created on the top of the host machine is called guest OS. You can run multiple virtual machines simultaneously on a host.

Creating a shared directory

We will share files in /home/lalit/Share on the host. In the guest, they will show up in /home/lalit/Desktop/host_files. Now follow the steps given below to create a shared directory –

1. Create a directory that you want to share between both host and guest OS. Here I am creating this to /home/lalit/Share on the host. To create a directory you can use the following command in the terminal –

mkdir /home/lalit/Share

2. Change the owner of this directory to libvirt-qemu by using the following command –

chown libvirt-qemu /home/lalit/Share

3. Now, In the virt-manager open a guest by double-clicking on it and then click on show virtual hardware details as it is given below –

4. Here, click on Add Hardware option which is highlighted in the below image –

5. This will open a new window to fill the required information. Keep driver default, select mode squash and browse and select the directory which you want to share and insert the target path (not a path) used to refer filesystem. Once done click on finish.

6. Next, it will ask you for the confirmation press yes

7. Now open the terminal and create a directory where you will mount the created filesystem. Use the following command to create a new directory in guest OS terminal –

mkdir /home/guest_user/host_files

In my case, I will run,

mkdir /home/lalit/Desktop/host_files

8. Next thing is to reboot your guest OS to make changes effective. And then run the following command to mount the filesystem to host_files directory in the guest OS terminal –

mount -t 9p -o trans=virtio,version=9p2000.L /hostshare /home/lalit/host_files

Conclusion

The other possibility of creating a shared directory includes NFS or CISF/Sambha. Now I hope this article is helpful to you if you have a query you can write to us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.