Shared folder in QEMU Virtual Machine (Linux)

In this article, we would discuss how to get a shared folder in QEMU Virtual Machine. We will consider two scenarios wherein we have to make a shared folder available between –

  1. Ubuntu host and Debian guest VM and,
  2. Ubuntu host and CentOS guest VM.

Since we would utilize QEMU’s built-in SMB server. Therefore, we will install Samba in our host Operating System i.e. Ubuntu. Then, with the help of cifs-utils package – we could get a shared folder in Virtual Machine.

Although there are other network protocols like SSH, NFS, HTTP and FTP which could be utilized to share data between host and guest. But, for the purpose of this article, we would stick with Samba/CIFS.

Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.

Install Samba in Ubuntu host Operating System

As the package is already available through standard Ubuntu repository. Therefore, open a terminal and issue the following –

sudo apt update
sudo apt install samba
I. Shared folder between Ubuntu host and Debian guest VM

We need to use following options with qemu-system-x86_64/qemu-system-i386 command-line utility before running our guest Virtual machine –

-net nic -net user,smb=shared_folder_path

So, issue –

qemu-system-x86_64 -net nic -net user,smb=<shared_folder_path> -smp <number of cores> -cpu host -enable-kvm -m <memory_size> -drive format=qcow2,file=/path/to/disk_image.qcow2

For instance,

qemu-system-x86_64 -net nic -net user,smb=/dev/shm/ -smp 2 -cpu host -enable-kvm -m 2048 -drive format=qcow2,file=/path/to/disk_image.qcow2

Read our article, Setup Virtual Machine using QEMU in Ubuntu to know more about the command-line options used. Next, we will have to install the package cifs-utils inside our guest Virtual Machine (Debian). Open a terminal and login as superuser –

su -l

Then, update the Debian repository and install the package cifs-utils

apt update
apt install cifs-utils

We will have to mount shared_folder directory –

mount -t cifs //10.0.2.4/qemu/ mount_point

For instance,

mount -t cifs //10.0.2.4/qemu/ /mnt/

You will be asked to authenticate yourself. If you get the following error –

Unable to find suitable address.

Then, your Virtual Machines’ firewall may be refusing to connect. Make necessary changes and see if it lets you mount the <shared_folder>.

To unmount –

umount -a -t cifs -l
II. Shared folder between Ubuntu host and CentOS guest VM

Similarly, we would be have to provide following options with qemu-system-x86_64/qemu-system-i386 command-line utility before running our guest Virtual machine –

-net nic -net user,smb=shared_folder_path

Therefore, open a terminal and issue the following –

qemu-system-x86_64 -net nic -net user,smb=<shared_folder_path> -smp <number of cores> -cpu host -enable-kvm -m <memory_size> -drive format=qcow2,file=/path/to/disk_image.qcow2

For instance,

qemu-system-x86_64 -net nic -net user,smb=/dev/shm/ -smp 2 -cpu host -enable-kvm -m 2048 -drive format=qcow2,file=/path/to/disk_image.qcow2

Now, we will have to install cifs-utils package inside our guest Virtual Machine (CentOS). First, we will login as superuser –

su -l

To install the package –

yum install cifs-utils

and, follow on-screen instructions. Once the package is successfully installed, we could mount shared_folder by –

mount -t cifs //10.0.2.4/qemu/ mount_point

For instance,

mount -t cifs //10.0.2.4/qemu/ /mnt/

It will ask you to authenticate.

To unmount

umount -a -t cifs -l

In conclusion, we have discussed how to get a Shared folder in QEMU Virtual Machine for Debian and CentOS guest Virtual Machines.