How to share a KVM host’s drive with a VM, for extra space

  devops, linux
# Add a new drive on the host, which will later be shared with the VM.
# In this case, we want to store a lot of gitlab files in this location, 
# from a gitlab VM.

# on the host, create a directory for the VM, ex: "gitlab"
sudo mkdir -p /opt/hostshare/gitlab
sudo chown $USER:$USER /opt/hostshare/gitlab

# on the running VM, create a directory to point to the host
sudo mkdir -p /opt/hostshare/gitlab
sudo chown $USER:$USER /opt/hostshare/gitlab

# If running, shut down the VM, go to Edit, Preferences, enable EML editing. # Double click the VM that's shut down, click the light bulb for details.
# The 2nd tab next to Details is XML. Edit the XML:

# Under this section:
# ... </currentMemory>, on the next line,
# add the following:
<memoryBacking>
	<source type="memfd"/>
	<access mode="shared"/>
</memoryBacking>

Click Apply

# go to the VM setting, add a new Filesystem:
Add Hardware Button
Filesystem
You're on the Details tab, click the XML tab next to it:

# click on XML and where it says accessmode="mapped" change this to accessmode="passthrough"
# add a line <driver type="virtiofs"/>

The whole XML for the Filesystem entry will look like this:
<filesystem type="mount" accessmode="passthrough">
	<driver type="virtiofs"/>
	<source dir="/opt/hostshare/gitlab"/>
	<target dir="hostshare-mount"/>
</filesystem>

# click finish, start the VM

# you still have to mount the hostshare-mount filesystem:

# on the vm machine:
sudo mkdir /opt/hostshare/gitlab

# mount the filesystem there
sudo mount -t virtiofs hostshare-mount /opt/hostshare/gitlab

sudo su
cd /opt/hostshare/gitlab
sudo df -h .

# you should see the big drive now, mounted here from the host system.



# Automount this on VM startup:
sudo nano /etc/fstab

# and add at the bottom:
hostshare-mount /opt/hostshare/gitlab virtiofs rw,noatime,_netdev 0 2

# and reboot
sudo shutdown -r now

# and check
df -h /opt/hostshare/gitlab

If you want to see the XML for the entire VM, using:

sudo virsh list --all
sudo edit gitlab

The edited sections will look like this:

and

To automount this on VM startup, you can do:

sudo nano /etc/fstab
# and add at the bottom:
hostshare-mount /opt/hostshare/gitlab virtiofs rw,noatime,_netdev 0 2

# and reboot
sudo shutdown -r now

# and check
df -h /opt/hostshare/gitlab
# if that doesn't work, here's a crontab on reboot mount:
sudo su
crontab -e

# and add:
@reboot mount -t virtiofs hostshare-mount /opt/hostshare/gitlab

# and try it out
sudo shutdown -r now

df -h /opt/hostshare/gitlab