Creating multiple Virtual Machines Fast using multipass

Here in this post, we will discuss a lesser-known way of creating a virtual machine. This post is beneficial for folks who prefer to create Virtual machines(VM) frequently. Until a few months back, I created virtual machines on my windows laptop using the virtual box(a great tool). However, creating virtual machines using virtual boxes was painful because it is slow and involves manual configuration Eg: configurations like VDI, mem, CPU, network, etc. For example, suppose I wanted to have three VMs up and running; I repeated the same manual settings three times.

Thanks to multipass by canonical, creating virtual machines has never been easier. multipass use the following technologies as the backend for virtualization:

  1. windows Host: Hyper-V or Virtual Box
  2. Linux: KVM
  3. macOS: HyperKit 

multipass is an interface to efficiently manage virtual machines by leveraging the earlier mentioned technologies to create VM. Also, Don’t forget to check the “cloud-init”  to utilize the multipass fully. Without cloud-init usage, multipass is not complete. 

Installation:

Multipass is available for Windows, Linux, and macOS. Installation instructions are available here. Installation of multipass is effortless and does not require any special instructions for any of the host types. If you are using Windows Host and installing multipass, the preferred way is to have Hyper-V enabled in windows(pro). If you are running Windows Home edition, don’t get disheartened. You can install virtual box and use it as the backend.

Learn Multipass:

List the installed VM. 

To list the virtual machines present on the host, run the following command:

multipass list

Create a Virtual Machine using default values:

Creating a virtual machine with default values is simple as running two commands on “power shell” or “cmd prompt.” For example, the following command would spawn a VM with the default values. Run this command on Power shell or Linux terminal as per your host OS.

multipass launch
The Default values are as follow:
  • name: random-name will be assigned to the VM
  • Image: Ubuntu image
  • Disk size: 5G
  • Ram: 1G
  • CPU: 1 core

multipass

Creating an Ubuntu VM using default values

Create a VM with a name 

Let’s start customization to the VM by using custom values. Starting by name. Except for the name, other settings like disk usage, ram, CPU, memory would remain the same as the default. 

mutipass launch --name <my-new-vm>
multipass
Launching a VM with a custom name

Display the detailed view of the VM configuration

multipass info 
multipass
Detailed view of the VM

Use Custom Memory, CPU, Disk for Virtual Machine

To override the default values while creating the VM, use the following command. If any flag is not used, multipass will fall back to default values. In the below snippet notice the values are non-default(as set in the below command).

multipass launch -n custom-vm-name -c 2 -m 2G -d 10G
or
multipass launch --name custom-vm-name --cpus 2 --mem 2G --d isk 10G
multipass
multipass create VM with Custom properties

Running command inside the VM 

There are multiple methods to run commands inside the VM. If you are familiar with containers, you must be familiar with this syntax. 

multipass exec  -- 
The run command inside the VM

Log into the VM — Method-I

This method is a subset of the previous way; instead of calling any arbitrary command, call a shell here to login into the VM.

multipass exec  -- bash
Loggin into the VM

Log into the VM — Method-II

Multipass provide a sub-command called “shell” to login into the VM. 

multipass shell 

Log into the VM — Method-III(IMPORTANT)

In this section will discuss SSH and how to SSH into the Virtual Machine. First, let’s try to SSH the IP of the VM; in the output, the SSH is failing because public key authentication failed. The following procedure will get you started however, this is not the final/best way to SSH. Keep reading to the VM configuration using cloud-init. 

technekey.com-> multipass list
Name State IPv4 Image
custom-vm-name Running 172.28.187.180 Ubuntu 20.04 LTS
technekey.com-> ssh [email protected]
The authenticity of host '172.28.187.180 (172.28.187.180)' can't be established.
ECDSA key fingerprint is SHA256:G1PHkJckv8D6IdKVxx2nuoe7152w+D4sWt94+KGLhpw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.28.187.180' (ECDSA) to the list of known hosts.
[email protected]: Permission denied (publickey).
technekey.com->

You need to provide the private key path while running the SSH command. In Windows, the multipass keeps the virtual machines’ private keys at the following location.

technekey.com-> ssh [email protected] -i C:\Windows\System32\config\systemprofile\AppData\Roaming\multipassd\ssh-keys\id_rsa
multipass
Windows SSH private key path

Mounting the host directory to the VM:

By default, host mounting is disabled in windows and can be enabled by running:

multipass set local.privileged-mounts=true

Once the above setting is enabled,  you can mount the shared directory as below.  Note that, “C:\Users\technekey\windows-dir\” from the windows host machine is mounted on the guest VM name “custom-vm-name”” at “/home/ubuntu/shared” directory. 

Example:

technekey.com-> multipass mount 'C:\Users\technekey\windows-dir\' custom-vm-name:/home/ubuntu/shared
technekey.com-> multipass exec custom-vm-name -- bash
[email protected]:~$ ls -lrt
total 4
drwxrwxrwx 1 ubuntu ubuntu 0 Feb 2 19:07 shared
drwx------ 3 ubuntu ubuntu 4096 Feb 2 19:20 snap
[email protected]:~$ cd shared/
[email protected]:~/shared$ ls
[email protected]:~/shared$ touch from_ubuntu
[email protected]:~/shared$ exit
technekey.com-> cd .\windows-dir\
technekey.com-> dir
Directory: C:\Users\technekey\windows-dir
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/2/2022 7:20 PM 0 from_ubuntu
technekey.com->
 

Deleting the Virtual Machines (send them into Recycle bin)

To delete any virtual Machine run the following command, this command would send the Virtual Machine to “into the Recycle Bin” like state. Meaning, recovering is possible. multipass will not allow the use of a deleted VM unless purged(see below)

multipass delete 
technekey.com-> multipass list
Name State IPv4 Image
courteous-mayfly Running 172.28.185.21 Ubuntu 20.04 LTS
custom-vm-name Running 172.28.187.180 Ubuntu 20.04 LTS
technekey.com->
technekey.com->
technekey.com-> multipass delete courteous-mayfly
technekey.com->
technekey.com-> multipass list
Name State IPv4 Image
courteous-mayfly Deleted -- Not Available
custom-vm-name Running 172.28.187.180 Ubuntu 20.04 LTS

Recovering the deleted Pod:

Let’s write some content into a text file inside the VM. For example, I am writing “hello” into a file called “foobar” inside a VM called “courteous-mayfly”.

technekey.com-> multipass shell courteous-mayfly
To run a command as administrator (user "root"), use "sudo ".
See "man sudo_root" for details.

[email protected]:~$ echo hello > foobar

Now, delete the VM.

technekey.com-> multipass delete courteous-mayfly
technekey.com->
technekey.com-> multipass list
Name State IPv4 Image
courteous-mayfly Deleted -- Not Available
custom-vm-name Running 172.28.187.180 Ubuntu 20.04 LTS

We are now recovering the VM. Pulling it out of the Recycle bin State and validating the content of the VM is the same as before deletion. 

technekey.com-> multipass recover courteous-mayfly
technekey.com-> multipass list
Name State IPv4 Image
courteous-mayfly Stopped -- Ubuntu 20.04 LTS
custom-vm-name Running 172.28.187.180 Ubuntu 20.04 LTS
technekey.com-> multipass start courteous-mayfly
technekey.com-> multipass list
Name State IPv4 Image
courteous-mayfly Running 172.28.186.91 Ubuntu 20.04 LTS
custom-vm-name Running 172.28.187.180 Ubuntu 20.04 LTS
technekey.com-> multipass shell courteous-mayfly
To run a command as administrator (user "root"), use "sudo ".
See "man sudo_root" for details.

[email protected]:~$ ls
foobar
[email protected]:~$ cat foobar
hello
[email protected]:~$

Completely deleting a VM.

To completely get rid of a VM, we need to perform two actions.  First, we need to delete the VM(send to recycle bin) and then purge it from there. Here purge command will remove all the traces of the deleted VM from the system. Recovery is not possible. 

multipass delete 
multipass purge

To delete all the VM from the host:

multipass delete --all
multipas purge

Networking:

All the Virtual machines created by multipass are by default NAT’d, meaning they will have a different subnet than the host LAN.  This is not a problem for the most, however, if you wish to have the VM created access from the LAN then you can use the bridge network flag.  To see the available networks, run the following command. 

multipass networks
Name Type Description
Default Switch switch Virtual Switch with internal networking
Ethernet ethernet Intel(R) Ethernet Connection (11) I219-LM
WSL switch Virtual Switch with internal networking
Wi-Fi wifi Intel(R) Wi-Fi 6 AX201 160MHz

Assuming you would want to make your VM accessible over WIFI. you need to run the command as below:

multipass launch --name  --network "Wi-Fi"

The above command would assign a LAN IP to the VM instance. 

Changing the Multipass backend:

You can switch the backend between different available backends. For example, I was using Hyper-V and now I wanted to use Virtual box. Following are the commands:

technekey-> multipass get local.driver
hyperv
technekey-> multipass set local.driver=virtualbox             #Use this command to revert back if needed.

 

technekey-> multipass get local.driver
virtualbox

technekey-> multipass launch –name multipass-virtualbox
Launched: multipass-virtualbox

technekey-> multipass list
Name State IPv4 Image
foobar Running N/A Ubuntu 20.04 LTS
multipass-virtualbox Running N/A Ubuntu 20.04 LTS

Now you can see the virtual box created by multipass in the virtual box. Note that, to see the VMs in the virtual box, you need to run the virtual box as a system user.  As per the official documentation from the canonical, you need to follow this procedure to open the virtual box as a system user.

Finding Multipass instances in VirtualBox

Multipass runs as the System account, so to see the instances in VirtualBox, or through the VBoxManage command, you have to run those as that user via PsExec -s. Download and unpack PSTools.zip in your Downloads folder, and in an administrative PowerShell, run:

PS> & $env:USERPROFILE\Downloads\PSTools\PsExec.exe -s -i $env:VBOX_MSI_INSTALL_PATH\VirtualBox.exe

Leave a Comment

Your email address will not be published.

Scroll to Top