2.8k
Connect
  • GitHub
  • Mastodon
  • Twitter
  • Slack
  • Linkedin

Blog

Create and Manage VMs in Your Terminal with nEMU and Flox

Steve Swoyer | 08 November 2024
Create and Manage VMs in Your Terminal with nEMU and Flox

Fun Package Fridays is a series where we ask members of the Flox team to share fun and perhaps not-so-well-known tools and utilities you can find in the Flox Catalog, powered by Nix. Today's entry comes from staff writer Steve Swoyer, who says he knows just enough bash to be dangerous.

Some packages just put the “fun” in Fun Package Fridays(R)(TM). Last week’s portable Doom environment is a great case in point.

Others aren’t exactly “fun,” per se: they just accelerate tasks that could easily devolve into drudgery. This is its own, special class of fun.

This week’s selection, nEMU, falls into the latter category. It’s an ncurses-based terminal UI (TUI) for QEMU, the awesomely versatile cross-platform hypervisor and ISA emulator.

Think of nEMU as a complement to virt-manager, just cooler and awesomer because it runs in your terminal!

Read on to discover how nEMU can fit into your workflow.

Getting It

If you use VMs on Linux or macOS, you might be partial to QEMU. I absolutely am.

Sometimes, however, I also want to … well, I get just a little frustrated with QEMU. (My now-deceased Mum told me I should always aim to say mostly positive things about GPL v2-licensed open source hypervisors/emulators.) QEMU’s trademark configurability and its staggering array of command-line options is a double-edged sword, especially when working with advanced configurations—like specifying granular, machine-specific options; setting up a VLAN or a vhost-user backend; or getting GPU passthrough to work.

nEMU might not help you with the GPU passthrough thing, nor with the most esoteric of QEMU use cases, but it does make it a cinch to create, configure, monitor, manage, and (just as important) change VMs.

With this as preamble, let’s put nEMU to work. First we gotta get it. Let’s assume you've already installed Flox. If you haven’t, you can easily download, install, and play along at home.

Second, we need to create a new Flox environment. For simplicity’s sake, I’m going to do this in the ~/sandbox directory where most of my VMs live. Once I change into this directory, I type:

daedalus@nomos:~/sandbox$ flox init && flox install nemu qemu libvirt virt-manager && flox activate

These commands (a) initialize a Flox environment and (b) install the nemu, qemu, libvirt, and virt-viewer packages before (c) activating—i.e., launching and putting me into—a Flox subshell.

Once I'm in that subshell, I can access and use the software Flox just installed.

For what it's worth, I don’t necessarily need to install QEMU, libvirt, and virt-viewer—they’re already on my system—but it is kind of awesome that I can get up-to-date versions of these dependencies (and hundreds of thousands of others!) from the Flox Catalog, right? This stuff just works, and I don't have to worry about it conflicting with system-wide dependencies.

It takes a moment or so for Flox to download packages and build my environment. When it’s finished I see the following:

✅ 'nemu' installed to environment 'sandbox'
✅ 'qemu' installed to environment 'sandbox'
✅ 'libvirt' installed to environment 'sandbox'
✅ 'virt-viewer' installed to environment 'sandbox'

Now I’m ready to start playin—er, using nEMU.

Configuring It

Running nEMU for the first time triggers a setup wizard that prompts me to generate a default nemu.cfg file and define a default directory for my VMs. It also prompts me to configure other defaults (paths for the QEMU, VNC, and remote-viewer binaries) nEMU uses to run, manage, and connect to VMs.

Note: If you opt to install the Flox Catalog's versions of the qemu and virt-viewer packages, be sure to enter $FLOX_ENV as the default path for both binaries when prompted by the nemu.cfg creation wizard.

By the way, one neat thing of note is that Flox Catalog gives me a much newer version of the qemu package than the one I got from the Ubuntu 22.04 repos: namely, version 9.1.1 for Flox Catalog versus version 6.2.0 for Ubuntu 22.04. The Flox Catalog’s remote-viewer package is much newer, too (11.0 in Flox Catalog, versus 7.0 in Ubuntu 22.04.). Flox makes it easy to get up-to-date versions of both!

Importing a VM

You can import an existing VM using nEMU’s import wizard, which you invoke by pressing SHIFT + A.

From there, you just have to enter some basic information about your VM and press ENTER.

Once I’ve imported my Fedora VM, I can press the e key to edit it.

I can make oodles of other changes, too, as well as perform actions on stopped or running VMs. Pressing

  • i edits network settings, including port forwarding rules
  • b edits boot settings;
  • a starts a wizard to add a virtual disk;
  • y renames a VM
  • l clones a VM;
  • SHIFT + S takes a snapshot;
  • SHIFT + X reverts a snapshot;
  • SHIFT + D deletes a snapshot;
  • SHIFT + C edits viewer settings.

Running the VM is as simple as highlighting it in the left pane of nEMU's TUI and pressing the r key, while pressing p powers it down and pressing z resets it. You can highlight any VM and press the c key to connect to it.

And, oh yes, pressing the h key brings up a help menu displaying a complete list of commands.

Creating a New VM

Creating a greenfield VM is easier than importing an existing one, because you don’t have to worry about getting the initial settings just right. (For example, an existing VM might be configured to use a specific machine type and virtual hardware configuration.)

First press SHIFT + I to bring up nEMU’s “Install VM” wizard. This screen looks a lot like the “Import” wizard above. In the screenshot below, I’ve configured an OpenSUSE Leap VM to boot using an ISO image.

I still need to edit (by pressing e) this VM to change the machine type to q35; modify the viewer settings (by pressing SHIFT+C) to use a custom SPICE port; and configure port forwarding for SSH (by pressing i). But this workflow is orders of magnitude easier than mocking up and debugging a bash startup script from scratch!

So that’s nEMU. It probably isn’t going to revolutionize what you do with QEMU, but if you’re accustomed to working in a terminal and logging into remote systems to create, run, and manage VMs, its TUI can make things much easier. By exploring the options surfaced via its VM, boot, and network configuration wizards, you can pretty much do everything with nEMU that you can do with custom bash startup scripts.

Just much more quickly and easily!

Happy hacking!