Ubuntu server VMs on Macbook Silicon M1 / M2 with Mubuntu-vms


For business or freelancer using Ubuntu virtual machines, the advent of the new Apple Silicon M-series processors hasn't been painless.

Products like VirtualBox are still working hard to bring virtualization to these new processors. Therefore to launch test VMs locally is still not trivial.

Fortunately, Canonical has taken care of the virtualization of Ubuntu for local test machines, developing Multipass.

Multipass is an Ubuntu virtual machine orchestrator using QEMU.

Multipass does most of the work, but the network configuration is completely under its control, so the best solution is to use hostnames.

What I did was write a series of scripts in bash that automate the startup and shutdown of virtual machines and that modify the /etc/hosts file dynamically based on the IPs that Multipass decides to give to the virtual machines.

Eg:
For my project I need 3 test virtual machines for local development:
- vm1
- vm2
- vm3

These servers will be respectively used as database, webserver and NFS.

In the "config.sh" file these 3 machines must be configured as follows:

  
declare -a vms=(
  "vm1->database database-second-name"
  "vm2->webserver webserver-second-name" 
  "vm3->nfs nfs-second-name" 
  )
        

Running the command "bash start.sh" Mubuntu-vms starts all configured servers and writes the following in the /etc/hosts file:

192.168.X.X vm1.mubuntu.local
192.168.X.X database.mubuntu.local
192.168.X.X database-second-name.mubuntu.local

192.168.X.Y vm2.mubuntu.local
192.168.X.Y webserver.mubuntu.local
192.168.X.Y webserver-second-name.mubuntu.local

192.168.X.Z vm3.mubuntu.local
192.168.X.Z nfs.mubuntu.local
192.168.X.Z nfs-second-name.mubuntu.local
        

Mubuntu-vms also copies a public key for ssh access to the VMs.

To stop the VM just run the command "bash stop.sh"

Here Gitlab repository