Linux Fu: Easy VMs

It wasn’t long ago that we looked at easily creating Docker containers from the command line so you could just easily spin up a virtual environment for development. Wouldn’t it be nice if you could do the same for virtual machines? You can. Using Multipass from Canonical, the makers of Ubuntu, you can easily spin up virtual machines under Linux, Mac, or Windows. Granted, most of the virtual machines in question are variations of Ubuntu, but there are some additional images available, and you can create your own.

Once you have it installed, starting up a new Ubuntu instance is trivial. If you have a set configuration, you can even set up predefined setups using a YAML file.

Installation

The process varies by platform, but on Ubuntu, installing Multipass is as easy as:

sudo snap install multipass

You should be sure that you are on a computer that can support virtual machines. The /proc/cpuinfo file should have a vmx or svm flag (actually, several of them; one for each core).

Catalog

You can see all the images available by running:

multipass find

Naturally, most of the images are Ubuntu versions, although there are a few other appliances like anbox and nextcloud available.

Suppose you want to do some testing in a Ubuntu bionic (18.04) instance. You can launch a default instance by issuing the command:

multipass launch bionic

However, you usually want a little more control since the default is one CPU core, a gigabyte of RAM, and 5G of disk storage. Say you want 4 CPUs, 6G of RAM, and a 10G disk. You can also provide a name for the virtual machine:

multipass launch -c 4 -m 6G -d 10G -n hackaday-vm bionic

On the Run

Once you have a bunch of virtual machines, you might want to see how they are doing:

multipass list

You can control them with start and stop. You can also delete a machine.

multipass stop hackaday-vm
multipass start hackday-vm
multipass stop hackaday-vm
multipass delete hackaday-vm

Access

So what do you do with it? By default, the machine starts up with a private network that you can only access from the host machine. There are ways to route traffic around if you need to do that.

However, many times you just want a shell into the new machine. That’s easy:

multipass shell hackaday-vm

If you want to share data, you can mount a host folder into the virtual machine:

multipass mount ~/hackaday hackaday-vm

If you lose track of everything, you can ask about a particular machine:

multipass info hackaday-vm

Name:           hackaday-vm 
State:          Running 
IPv4:           10.134.147.131 
Release:        Ubuntu 18.04.6 LTS 
Image hash:     5269cad5bc26 (Ubuntu 18.04 LTS) 
Load:           0.78 0.41 0.16 
Disk usage:     1.2G out of 9.5G 
Memory usage:   143.2M out of 5.8G 
Mounts:         /home/alw/hackaday => /home/alw/hackaday 
                   UID map: 0:default 
                   GID map: 0:default

It really is that simple. There are other commands available to run programs in a machine or set up networking. There’s also a GUI that sits in your system tray, but if you aren’t running virtual machines as your own user, that isn’t as useful to you.

Configuration

If you have things you always have to set up, you can automate that. You create a yaml file (known as a cloud init file) and set things up like users, packages, ssh keys, and the like. Note that the standard allows for several formats, but apparently Multipass only supports YAML.

The other thing you can do is use Packer to package a new image of your own. That’s a bit involved, but you can read the documentation to find out how.

Have you ever set up a development environment and then a few years later found out it was all broken because of updates? With virtual machines that never has to happen again. You can archive entire environments. This can be important in production environments when you need to go back to the exact way code was built to resolve a problem. It is also extremely important for safety-critical software where you sometimes have a requirement to be able to produce the exact same executable and prove that you can do it. Virtual machines make that much easier and Multipass is an easy way to create and use at least some kinds of virtual machines. The fact that it runs on multiple platforms itself is also a great feature.

34 thoughts on “Linux Fu: Easy VMs

      1. How can you have a “walled garden” on any machine where the user can bring up an administrative prompt and proceed to install or uninstall or modify anything they want? It doesn’t make any sense.

        1. There is only one “snap” store, controlled exclusively by one corporation.
          No other 3rd-party stores are allowed nor is side-loading snaps from other sources.
          The “snap” store is closed source.
          “Snaps” update automatically whether you want them to or not.
          Snapd is installed without permission or notice when some other software is installed (Chrome).

          That all is an obvious attempt to create a walled garden.

    1. Do you use classic LXC (lxc-create, lxc-start, lxc-attach, …) or the newer LXD that loosely resembles Docker?
      Classic LXC is where I typically dabble with unprivileged containers and custom networking on a CI environment. It’s certainly not a go-to “container solution” for newbies but it’s quite flexible, if you put in enough effort.

    2. Classic LXC or the snap-based LXD?
      I only use the former and typically use it for unprivileged containers on a CI environment.
      It’s definitely not a tool I’d recommend for newbies but it can be quite flexible if you put in the effort needed.

    3. i also love lxc (in preference to docker or lxd or virtualization) for my purposes but i’m surprised, or maybe disappointed, at the suggestion that it’s not as good a tool for beginners.

      docker is an *awful* tool for beginners. it’s all gun and foot. there’s nothing to do but shoot and nothing to aim for but your feet. docker is *much* more complex than lxc, but everyone that thinks it is easy. it’s because they’re cut-and-pasting recipes, which inevitably winds up diffusing real host root access across your userbase in ways you don’t appreciate unless you really understand docker well beyond the newbie level.

      lxc is a lot easier to get to the bottom of, and if you don’t bother to invest that effort, you will still typically not shoot yourself in the foot in this way. it’s true that there are fewer cut-and-paste tutorials for it, and that’s a flaw in our community. the tremendous proliferation of bad advice regarding docker is an even larger flaw in our community. oh well.

    1. Indeed, though its only with your pointing out I realise it could have been about something other than what I expected this time. Seems like ambiguous acronyms should be banned from article titles.

      1. Coming from the perspective of a thrown-in Windows sys admin and most of my Linux experience being on Pi HW, I’ve recently built a ProxMox machine in my fledgling homelab. I know these are different types of HVs but I’d love to get input on how Canonical’s solution is comparable to Vagrant or something similar.

        Also have a deep rooted dislike of snap installs, mainly from years of reading HaD comments. Again, would love to hear otherwise

        1. Well in many ways there isn’t anything wrong with snap from a users perspective, the flaws are rather more about what it is and how most of us think stuff should be done over actual function.

          As for the VM stuff its partly down to what do you really want to do. For instance on something like my Pi4 I’ll just invoke a script that directly calls qemu’s Arm variant with all the options required – super lightweight making the VM(s) really quite performant, really usable and can handle quite a few at once. And from the point of being very simple windows like the GUI on Virtual Machine Manager (libvirt based) is very nice and fully featured so you shouldn’t need CLI or config editing to create and use the VM. But there are quite a few options out there…

        2. Clement Lefebre (maintainer of Linux Mint, a distro I’ve used daily for 6 years and am thrilled with) has good explanations of his distaste for Snap. Search “Linux Mint Snap” and you’ll find some good articles.

    1. agreed, no matter how potentially useful it is (reinvention of the wheel aside) I’m not going to consider it until it’s available as something else thanks to canonical’s previous poor behavior around snaps

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.