Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

You can also add Virtual Network Adapters with Hyper-V, but network adapters are configure in modern Ubuntu configured in a rather complicated file /etc/netplan/50-cloud-init.yaml . There is a manual, but it is better to tell you how to modify existing network adapters rather than to add new ones. Once the adapter is created and configured, you can still use Hyper-V manager to disconnect it from a network or move it to another network (although if you have defined static IP addresses you may need to change them for the new network). Networks will be described later in this article.An example of the command to create a that you have to change to define the new adapter. It is better to add the adapter when multipass creates the VM and then the file will at least start up with all adapters defined and only require simpler edits.

An example of the command to create a VM is:

multipass launch -n vmname --cpus 2 --disk 127G --mem 1024M --network Sandbox 22.04

The last and only positional parameter is the image name “22.04” taken from the list of image names shown above.

This example excludes a cloud-init file. This file contains a list of things that Multipass does on the VM after it boot up but before it is turned over to you. There are many things you want to do to customize your VM, but some of these things you do to every VM. It is annoying to have to repeat 10 minutes of configuration for every VM. There is a cloud-init parameter and afile format provided with Multipass that allows you to do most of the configuration automatically during the execution of the “multipass launch”.

It can create groups and users, install SSH keys and CA Certificates, install additional packages and applications, and change configuration filesapply the most recent Ubuntu maintenance. A full discussion of this file format cannot be provided here, so read the manual.

Having tried to Every time I type the “multipass launch” command interactively, I find that it is almost impossible to remember all the parameters you want to set, and generally you want to do the same thing for every VM you create. Therefore, it is a really good idea to turn this as soon as I hit Enter I remember some parameter I accidentally left out. It is better to combine all the stuff you always do into a script file like Powershell. Then you can add a few options that you don’t always want or need.When you start scripting the “multipass launch” command, a useful trick is to pull the cloud-init file from standard input, which following common Linux use means you use . If you need to make some changes, copy the script file and edit them in.

A useful trick in Powershell scripting is to put the text of the cloud-init file in the script as a multiline text literal that gets piped to the “multipass launch” and is then picked up by a “--cloud-init -” parameter where putting a dash “-” instead of a file name . Then you can pipe means “read from standard input” which is to say from the pipe. An alternative is to build a separate script that constructs the cloud-init data file from parameters passed to the script file itself, which in Powershell means you can use the multiline text literal syntax, and then use that script to generate the text piped to the “multipass launch” command. Here is an example with inline text:

Code Block
param (
  [string] $vmname 
)

# Start a Multiline text literal until the ending YAML that ends in a line beginning with "@
@"
#cloud-config

# Create my normal userid with its normal ssh key and sudo privileges
# The docker group will only exist if docker is in the package install list
users:
 - name: gilbert
   gecos: Howard Gilbert
   shell: /bin/bash
   groups: docker
   ssh_authorized_keys:
   - "ssh-rsa AA...094347"
   sudo:  ALL=(ALL) NOPASSWD:ALL

packages:# Packages to - avahi-install with sudo apt install ...
packages:
 - avahi-daemon
 - cifs-utils
 - cockpit
 
 # Do apt update and upgrade with latest changes
package_update: true 
package_upgrade: true 

# Add a snap. I like Powershell
snap:
  commands:
        - snap install powershell --classic

# Reboot after everything to activate all the changes
power_state:
  mode: reboot 

# End of text literal which gets piped to the multipass command as stdin
"@ | 
multipass launch -n $vmname -d 127G -timeout 600 --cloud-init - --network Sandbox 22.04

...

  • avahi-daemon adds support for the mDNS protocol, where this VM can be reference from other VMs as hostname.local (where in this example the hostname is the $vmname parameter of the script).

  • cifs-utils allows the VM to connect to the Windows host computer and access shared disk files.

  • cockpit is a Web based management tool for Linux. Open a browser to https://hostname.local:9090/

  • linux-image-virtual linux-tools-virtual linux-cloud-tools-virtual are optional packages for running Linux in a VM, including Hyper-V. The main benefit is that Hyper-V Manager can display Network addresses for all virtual adapters in the VM. The downside is that the install of the last package hangs for 2 minutes until it times out, and then because of the failure does not start the services until the next reboot (which the “power-state” at the end of the cloud-init takes care of). They are not listed here but you may want to add them.

There is nothing that cloud-init does that you could not new by adding scripted “multipass exec” commands after the multipass launch. It is just more compact.

If you specify an image name that has not been previously used, or if the image has been updated with new maintenance since you downloaded your last copy, a new Ubuntu image name (22.04 here) a new image file will be downloaded from the Internet and saved in the cache first and then the rest of the launch will run.

Run Commands as “ubuntu” user

Multipass creates a user named “ubuntu” with sudo privileges.

Multipass commands that run things on Linux, copy files, and connect to a shell all run as the “ubuntu” user.

Multipass generates its own ssh credentials and uses an internal captive ssh client to talk to the VM over the Default network.

This user does not have a password, but to be safe, the first thing you should do is to connect to the VM with

multipass shell <vmname>

and in this ssh session, generate a password with “sudo passwd ubuntu”generates a ssh keys on install and copies the public key to the /home/ubuntu/.ssh directory of every VM it creates.

Multipass has its own internal version of ssh and scp programs. It uses them to connect to the VM to pass commands, open a shell, copy files, and mount shared directories.

You should always assign a password to the ubuntu user on each VM you create just in case the multipass ssh gets clobbered. Then you can login to the VM using a standard Hyper-V console window for the VM.

IMPORTANT: READ THIS

Periodically Sometimes Multipass screws up, and in other cases Windows screws up.

Specifically, it has been common for a major annual feature upgrade of Windows (say from 21H2 to 22H2) to fail to migrate the Multipass files because they were stored in a directory Microsoft thinks it controls and is free to empty. The old Multipass stuff is stored temporarily in C:\Windows.old, but there are no instructions to fix a broken install or copy the old files back to the new Windows. If you do a fresh install of the latest version of Multipass it will choose a better place to load its files, but still …

You should assume that Multipass will periodically screw up. The VMs themselves will still be OK because they are standard Hyper-V VM files (although you may have to change their location if they were moved). If you reinstall Multipass it will generate new ssh keys and forget the old ones.

So plan for such a failure. Mostly this means making sure that you can login to your VMs through Hyper-V or real ssh without depending on the multipass command and its built in ssh substitutes.

Sometimes you screw up and clobber Multipass files.

Otherwise, Microsoft’s annual upgrade (from say 21H2 to 22H2 screws up).

Your Multipass VMs are real Hyper-V VMs. You can run them from Hyper-V, login to them with a Hyper-V console window, and generally ignore Multipass if you want. However, the special services provided by the multipass command only work if the Multipass directories don’t get clobbered where the ssh key is deleted or overwritten.

Until Version 1.9 Multipass stored its files in a directory that Microsoft believed it could delete on any annual major version upgrade of Windows 10 or 11. Even if that is fixed, you are given an opportunity to delete everything whenever Multipass updates its software. It is just too easy to screw up.

So you should always create Multipass VMs that can stand on their own without a multipass command. You should also take advantage of Hyper-V tools to checkpoint and backup and export VMs. You should also find and backup the multipass ssh keys.

If you upgrade to 22H2 and Multipass appears to have been deleted, remember that the old files are saved for a month or so in the C:\Windows.old directory.

  • On each VM, do a “sudo passwd ubuntu” and give the built in ubuntu user a real password and not just SSH keys.

  • Add a second user with your own normal SSH login key and a password, and give it SUDO privileges.

  • Find the Multipass ssh key and save the file separately (it has no passcode).

...