blog.shukriadams.com

Game devops and other things

Tips for coding in a VM - sharing files between host and guest

I'm a big fan of coding in VMs, but it's not without its challenges. Normally I used Vagrant to manage my coding VMs, but Vagrant is for headless VMs only, and sometimes I need a VM with a graphical desktop. In the case of the latter I still want to use Vagrant's concept of mounting files from the host into the guest, so the guest disk is as small as possible. Fortunately it's fairly easy to manaully set up mounts using NFS.

My host has all my code projects nested inside a single main directory, /home/myuser/projects. This contains multiple sub directories, each one a seperate git checkout. I'm specifically going for a simplified layout like this because I want to set up NFS once on the host, and then later add new guest VMs per project, as needed.

My host system is Ubuntu 24, and my guests are various flavors of Ubuntu. Change the package manager commands below to suite your distro. In theory this setup will also work for Windows guests as Windows claims to have NFS support, but I can't confirm this as I no longer work on Windows. I'm using KVM (libvirt) as my hypervisor, but any hypervisor will do, as long as your have network support. I'm also assuming your host machine has a static IP, there are other ways of managing networking but I'm going with the simplest setup.

Host setup

Install NFS server components with

sudo apt-get install nfs-common nfs-kernel-server -y

Then manage your NFS shares

sudo nano /etc/exports

Share the projects directory by adding this line to the end of the file

/home/myuser/projects 192.168.122.0/24(rw,no_root_squash)

Save and exit. This assumes your libvirt VMs are running at the IP range 192.168.122.*, /24 means any IP in this range will be able to access the share. This should keep out other machines on your local network.

Then restart NFS

sudo service nfs-common start
sudo service nfs-kernel-server start

VM Client setup

Install NFS client components with

sudo apt install nfs-common -y

Create a projects directory in the VM,

mkdir /home/myVMuser/projects

Edit your mounts

sudo nano /etc/fstab

And add the host share to this, substituting your host machine's static IP

<host-machine-ip>:/home/myyuser/projects /home/myVMuser/projects    nfs    defaults,nofail    0    0

Save and exit, then refresh mounts

sudo mount -a

Done. You should see the contents of your host project file in the guest VM. There are of course many other NFS commands that you can use for troubleshooting the above, but this is the basics of a setup that should feel quite familiar if you're used to working in Vagrant.