Skip to content
mikaelstaldalPublic

About

Run VMs in Linux easily

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

53 Commits

Folders and files

Repository files navigation

virter

Prerequisites

  • Ubuntu 26.04 or later
sudo apt install python3 qemu-system-x86 qemu-utils genisoimage virtiofsd bubblewrap

External host tools use fixed system paths under /usr/bin (virtiofsd uses /usr/libexec/virtiofsd). Install the Ubuntu packages above; executables in the current directory or custom PATH entries are not used. An empty or relative XDG_CACHE_HOME is ignored; the cache then defaults to ~/.cache/virter.

Host confinement

Run virter as an ordinary user with access to /dev/kvm; root execution is refused. Every QEMU launch requires Bubblewrap and working unprivileged user namespaces. Failure to create the sandbox aborts the launch; there is no unconfined fallback. Ubuntu's AppArmor policy must permit the packaged bwrap to create user namespaces.

QEMU runs with separate user, mount, PID, IPC and UTS namespaces, no capabilities, a cleared environment, and explicit seccomp restrictions. Its filesystem contains read-only system runtime files, a private /tmp and /dev/shm, /dev/kvm, the instance directory and the required disk images. Ephemeral instances see their base image read-only; prepare and update can write their selected base image. Other cache images and the user's home are not exposed. Host networking is retained only with --network host (the default for image preparation and updates). This exposes host loopback services and abstract Unix sockets to QEMU. Runs default to --network none, which disables the guest NIC and isolates QEMU in a separate network namespace. --network bridge also isolates QEMU's network namespace and uses a filtered passt backend; see bridge setup and guarantees. A non-VM confinement preflight checks namespace creation and KVM device visibility before modifying images. The host QEMU process cannot create additional user namespaces inside its Bubblewrap sandbox. This does not restrict user namespaces or containers inside the VM, which runs its own kernel.

Directory sharing uses a separate virtiofsd process running as the invoking user, outside this QEMU sandbox. Mounts of your home directory or any ancestor (such as / or /home) are refused in both read-only and writable mode, including with --current-dir and --current-dir-writable. Symlink aliases are resolved first. Overlapping shared directories must use the same access mode; mixing read-only and writable exports is rejected because a writable parent export would bypass a read-only child export.

A shared directory deliberately grants the guest access to its contents according to the requested mount mode. Confinement is defense in depth and does not protect against host kernel vulnerabilities.

Resource limits

A running systemd user manager and cgroup v2 CPU, memory and PID controllers are required. Every invocation runs in a transient user scope covering the launcher, QEMU, virtiofsd and image tools. Scope startup or missing controller limits aborts the launch, without a fallback. The scope retains the invoking environment, working directory and terminal.

Each invocation is limited to 400% CPU (four host cores, matching the four guest vCPUs), requested guest RAM plus 1024 MiB of host overhead, zero swap, and 128 tasks. Memory must be 128–8192 MiB; prepared virtual disks must be 1024–16384 MiB. Imported and existing images exceeding 16384 MiB virtual size or 20 GiB host file size are refused. At most two invocations can run concurrently per host user, across cache directories.

The cache ($XDG_CACHE_HOME/virter, or ~/.cache/virter) must be owned by the current user and have private permissions (0700). New cache directories are created privately; existing unsafe directories are rejected without changing their permissions. Cache paths must not contain symlinks or ancestors writable by other users, except root-owned sticky directories such as /tmp. Each instance uses an atomically created private directory with a random name. Per-base locks allow concurrent readers, while prepare and update wait for exclusive access for their entire operation. Lock files remain in the cache after use; do not remove them while virter invocations are active.

The default wall-clock limit is one hour, including setup; all subcommands accept --timeout SECONDS from 1 to 86400. The entire scope is terminated on timeout, with forced termination after 15 seconds. Interrupted preparation or updates can leave an incomplete base image; inspect it before reuse.

Guest disk I/O is throttled to 256 MiB/s and 16384 operations/s in QEMU. Every descendant has a hard 20 GiB individual file-size limit and cannot produce core dumps. VM console output (including QEMU diagnostics) is limited to 16 MiB; exceeding the limit stops the VM. Virtiofsd output is discarded. Output copied before termination remains untrusted terminal input.

These are not aggregate filesystem quotas: writable exports can create many files or exhaust inodes, and image files still compete with other host data for free space. Use host filesystem space and inode quotas for the cache and every writable export when running untrusted workloads. A disposable directory alone does not provide a quota. Shared-directory I/O bypasses the QEMU disk throttle. Host networking has no bandwidth quota; keep --network none for untrusted workloads or enforce limits externally. Virter does not configure host filesystem quotas or network traffic control.

Prepare image automatically

Only trusted, authenticated, standalone QCOW2 images are supported. A checksum computed from an untrusted image is not authentication. Virter does not provide a sandboxed importer for arbitrary images; image metadata is parsed by host QEMU tools. Backing files and external data files are refused before QEMU is invoked.

For Ubuntu, download the image and signed checksum manifest from Ubuntu Cloud Images. Install the Ubuntu cloud image signing keyring from the authenticated Ubuntu package archive:

sudo apt install ubuntu-cloudimage-keyring curl
BASE_IMAGE=resolute-server-cloudimg-amd64.img
IMAGE_URL=https://cloud-images.ubuntu.com/resolute/current
curl --fail --remote-name "$IMAGE_URL/$BASE_IMAGE"
curl --fail --remote-name "$IMAGE_URL/SHA256SUMS"
curl --fail --remote-name "$IMAGE_URL/SHA256SUMS.gpg"
gpgv --keyring /usr/share/keyrings/ubuntu-cloudimage-keyring.gpg SHA256SUMS.gpg SHA256SUMS

Continue only if signature verification succeeds. Extract the exact image's checksum from the verified manifest and pass it to virter, which checks the copied bytes before opening the image with QEMU:

IMAGE_SHA256=$(awk -v image="$BASE_IMAGE" '$2 == image || $2 == "*" image {print $1}' SHA256SUMS)
./virter prepare -i "$BASE_IMAGE" --sha256 "$IMAGE_SHA256"

If verification fails, stop and download a matching image and manifest; do not substitute a locally computed checksum. Keep the verified manifest unchanged until the digest has been extracted.

Base and instance names (--base and --name) must contain 1–63 ASCII letters, digits or hyphens, starting and ending with a letter or digit. Preparing an existing base is refused; choose a new base name or use update to modify it. Base images must not be symlinks.

Update an image

./virter update

Without a command, update opens an interactive shell. To run a command and then shut down automatically:

./virter update -- sudo apt-get install -y ripgrep

The command runs as the invoking user, with passwordless sudo available. Use --sudo to run the entire command as root (requires a command):

./virter update --sudo -- sh -c 'apt update && apt -y upgrade'

Any changes to the file system will be persisted. Updates shut down orderly after the command finishes, including when it fails. The update exit status reports VM execution; check the console output for command failures. Both guest stdout and stderr, plus QEMU diagnostics, appear on virter's stdout.

Run an ephemeral instance

Changes to the instance disk are discarded when the instance exits. Writes to writable host shares persist on the host, including when the VM fails; deleting the instance disk does not undo them.

The guest user keeps the host UID for writable shares, but has no supplementary groups and can use sudo only for forced poweroff. Use ./virter run --allow-sudo to allow passwordless sudo for guest administration. This affects only the ephemeral instance; prepare and update retain administrative access. Trusted base images must not grant additional privileges through custom sudo rules or other mechanisms.

Runs are offline by default (--network none). To explicitly enable networking:

./virter run --network host

Despite its name, host uses QEMU user-mode networking (NAT), not a bridged guest interface. It permits unrestricted outbound access to the internet, LAN and host services, including host loopback services via the guest-visible host address (normally 10.0.2.2). NAT does not provide an outbound security boundary. Image preparation and updates default to this unrestricted networking for package access. Both accept --network host|bridge|none; for example, ./virter update --network none disables networking. Offline preparation may fail if cloud-init needs to download packages.

Only prepare trusted images and never run untrusted code during update. All guest disk writes during preparation and updates persist in the base image, even with --network none. An implant in the base can access the network when a later instance enables networking.

For outbound Internet/LAN access with direct host-service isolation, use ./virter run --network bridge after the mandatory administrator setup; running it needs no sudo. This uses passt and an IPv4/IPv6 host firewall policy that systemd applies before each start, including protection for services bound to host LAN addresses. It is not a true Ethernet bridge. LAN peers remain accessible; review the documented remote-relay limitations before using it.

The guest inherits the invoking user's LANG, LANGUAGE, and nonempty LC_* locale settings on each prepare, update, and run. Requested locales are generated only during prepare and update, so run update with new locale settings before using them with run. When LANG is unset, C.UTF-8 is used. Language translations depend on the packages installed in the image. The timezone follows TZ when set, otherwise the host system timezone. POSIX TZ rules are passed to the guest shell even when they cannot be used as a system timezone.

With an interactive shell

./virter run

For run, virter returns the guest command's exit status (or the interactive shell's status). It returns 125 if QEMU fails, the guest reports a mount or working-directory failure, or no valid status arrives. The status is advisory: the guest user or root can forge success. A guest command can itself return 125. Preparation and updates report VM execution status; QEMU failures also return 125 for those operations.

With a command and then exit

./virter run -- ls -A /

For run and update, put all virter options before the command. Every token after the command is passed to the guest unchanged, including option-like arguments. An optional -- separates virter options from the command. Omitting the command starts an interactive shell; an empty command name is rejected.

Both stdout and stderr from the instance, plus QEMU diagnostics, end up in stdout from the virter command.

With a directory on the host mounted writable

--mount-writable and --current-dir-writable give the guest persistent write authority over the shared host directory. A guest can alter source code, executables, build configuration or shell startup files that later execute on the host. QEMU confinement does not prevent these authorized writes through virtiofsd. Mounts in either mode overlapping the virter cache (normally $XDG_CACHE_HOME/virter, or ~/.cache/virter) are also refused, protecting base images and other instances. Cloud-init seeds and virtiofs sockets live under /run/user/UID, independently of TMPDIR and XDG_RUNTIME_DIR. Shares overlapping that runtime directory are refused in either mode. Paths are resolved before these checks, including symlink aliases. Other directories inside your home can still be shared writable; avoid sharing credentials or an active checkout.

./virter run --mount-writable DIR
./virter run --current-dir-writable

For untrusted guest work, prefer read-only inputs and a dedicated, disposable staging directory for output. If the guest needs to modify inputs, copy only the required files into staging first. For example: Run this from the input directory, with the temporary directory location outside that directory so the read-only input and writable staging exports do not overlap. Set TMPDIR to a separate location if needed.

staging=$(mktemp -d)
./virter run --mount "$PWD" --mount-writable "$staging"

The staging directory appears at the same absolute path inside the guest. After the VM exits, treat everything in it as untrusted: inspect changes and check file types and symlink targets before manually copying selected results to a separate destination. Do not execute generated scripts, source configuration, or run builds from staging as part of review. There is no automatic result export or trust validation. Remove staging manually when finished; its contents persist until you do.

With a directory on the host mounted read-only

./virter run --mount DIR
./virter run --current-dir

With several directories on the host mounted

--mount and --mount-writable can be repeated and combined with each other and with one of the current directory options. Each directory appears at the same path inside the instance as on the host.

./virter run --mount DIR1 --mount DIR2 --mount-writable DIR3 --current-dir-writable

With a working directory

--workdir DIR changes to DIR inside the instance before starting the shell or command. --current-workdir uses the current host directory, resolved like mounts. The directory must exist inside the instance, so combine it with one of the current directory options to work on host files:

./virter run --current-dir --current-workdir -- make test

Testing

Run the automated tests with Python 3; external commands are mocked and no VM is started.

./verify.sh

License

Copyright 2026 Mikael Ståldal.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Run VMs in Linux easily

Resources

Stars

2 stars

Watchers

0 watching

Forks

Contributors

Languages