Sharing Folders Between Host and Guest in QEMU/KVM Using VirtioFS

Last update: 09-25-2024

Sharing folders between your host and virtual machines can significantly streamline your workflow, especially when you need seamless access to files across environments. In this guide, we'll walk you through setting up a shared folder between your host system and a guest virtual machine (VM) using QEMU/KVM with VirtioFS.

Prerequisites

Step 1: Install VirtioFS Daemon on the Host

First, ensure that your host system is up-to-date and install the virtiofsd package, which facilitates the shared folder functionality.

sudo apt update
sudo apt install virtiofsd

Step 2: Configure the VM in Virt-Manager

  1. Shut Down the VM: If your VM is running, you'll need to turn it off to make hardware configuration changes.
  2. Enable Shared Memory:
    • Open Virt-Manager.
    • Select your VM and click on Open to access its details.
    • Navigate to the Memory settings.
    • Check the option Enable Shared Memory.
    • Click apply.
  3. Add a Shared Filesystem:
    • Click on Add Hardware at the bottom left corner.
    • Choose Filesystem from the list of hardware options.
      • Driver: Select virtiofs.
      • Source Path: Browse and select the folder on your host machine that you wish to share.
      • Target Path: Enter an identifier for the shared folder (e.g., shared_from_host). Note that this is not a path in the guest VM but a label that we'll reference later.
    • Click Finish to add the shared filesystem.
  4. Start the VM: Power on your VM.

Step 3: Mount the Shared Folder in the Guest VM

Once your VM is up and running, you'll need to mount the shared folder to access it.

  1. Create a Mount Point:
    • Open a terminal in your guest VM.
    • Create a directory that will serve as the mount point for the shared folder. You can place it anywhere; for this example, we'll create it in the home directory.
    mkdir ~/shared_folder 
  2. Edit the fstab File:
    • Open the /etc/fstab file with a text editor using sudo privileges.
    sudo nano /etc/fstab 
    • Add the following line at the end of the file, replacing shared_from_host with the target path you specified earlier and /home/your_username/shared_folder with the path to your mount point:
    shared_from_host /home/your_username/shared_folder virtiofs defaults 0 0 
    • Save the file (in nano, press Ctrl+O and then Enter) and exit the editor (Ctrl+X).
  3. Mount the Filesystem:
    • You can either reboot your VM to automatically mount the shared folder or manually mount it using:
    sudo mount -a 

Step 4: Verify the Shared Folder

Navigate to the mount point directory and list its contents to verify that the shared folder is working correctly.

cd ~/shared_folder
ls -l

You should see the files and directories from the shared folder on your host machine. You can now read, edit, and create files that will be accessible from both the host and the guest VM.

Happy virtualizing!

StocksComparison.com ad

0 Comments

Add a new comment: