Deploy KVM ‘Lightning Fast’ with virt-install: Forget the GUI, CLI is King

Virtualization tutorial - IT technology blog
Virtualization tutorial - IT technology blog

When GUI is No Longer an Option: A 2 AM Story

The phone rang relentlessly at 2 AM. A critical staging server had just “vanished” due to a configuration error. The dev team needed an equivalent environment immediately to test a fix before the customers woke up. Unfortunately, the backup physical server was only running Ubuntu Server Minimal. No desktop environment, no X11 forwarding, and certainly no sign of virt-manager.

If you’re planning to fumble with VNC or try to push a GUI over SSH, stop right there. That’s just a waste of precious time. At this moment, virt-install is your only lifesaver. This tool is not only powerful but also extremely stable for automation tasks.

In my homelab setup, even though I manage over 10 VMs on Proxmox, I still prefer using virt-install for pure Linux nodes. The feeling of typing commands and precisely controlling every MB of RAM or CPU cycle always provides a level of peace of mind that point-and-click tools can hardly match.

Why Should You Use virt-install Instead of virt-manager?

virt-install is a command-line tool from the virt-inst package that works directly with the libvirt library. Instead of filling out forms through multi-step wizards, you only need a single command to define all virtual machine parameters.

The biggest selling point is repeatability. With a simple Bash script, you can deploy 20 identical VMs in just a few minutes. This is a must-have skill if you want to advance in DevOps or professional Linux system administration.

Preparing the Real-World Environment

Before starting, ensure your system has all the necessary packages installed. You will need virtinst along with the KVM daemons.

sudo apt update
sudo apt install virt-inst libosinfo-bin -y

Pro tip: Run the osinfo-query os command to find the correct operating system identifier. Specifying the --os-variant parameter accurately helps KVM automatically select the virtio driver. This driver can boost Disk I/O performance by up to 30% compared to standard IDE drivers.

Hands-on: Create an Ubuntu Server VM in a Heartbeat

Suppose you need to set up an Ubuntu 22.04 virtual machine with 2 vCPUs, 2GB RAM, and a 20GB hard drive. Instead of opening an interface, use the following command:

virt-install \
--name ubuntu-server-01 \
--vcpus 2 \
--memory 2048 \
--disk path=/var/lib/libvirt/images/ubuntu-01.qcow2,size=20 \
--os-variant ubuntu22.04 \
--location /var/lib/libvirt/images/ubuntu-22.04-live-server-amd64.iso \
--network bridge=br0 \
--graphics none \
--extra-args 'console=ttyS0,115200n8 serial'

Breaking Down the Key Parameters:

  • –name: The VM name. Keep it short and avoid spaces to make it easier to manage with virsh later.
  • –memory: You can use --memory 2048,maxmemory=4096. This option allows for hot-plugging RAM without shutting down the VM.
  • –disk: The qcow2 image format is the optimal choice as it supports snapshots and only occupies the actual space being used.
  • –location: Points to the ISO file. Note: libvirt runs under its own user, so keep the ISO in /var/lib/libvirt/images/ to avoid “Permission Denied” errors.
  • –graphics none & –extra-args: This technique pipes the entire installation screen to the current console. You will perform the OS installation directly within your open SSH window.

Leveling Up to Pro with Cloud-init

Waiting for a manual installation step-by-step via ISO is a real time-sink. To truly master the workflow, you should use a Cloud Image combined with Cloud-init.

Instead of installing from scratch, you download a pre-built .qcow2 file from the provider (like Ubuntu Cloud Images). Combined with a user-data configuration file, the VM will automatically receive SSH keys and network configurations upon boot. Deployment time is slashed from 15 minutes to under 40 seconds.

virt-install \
--name fast-vm-01 \
--memory 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/fast-vm.qcow2,backing_store=/var/lib/libvirt/images/ubuntu-22.04-cloud.img \
--import \
--network bridge=br0 \
--cloud-init user-data=/path/to/config.yaml \
--noautoconsole

The backing_store parameter helps create a “linked clone“. It’s extremely disk-efficient because multiple VMs can share a single base image.

Unforgettable Mistakes to Avoid

Working with the command line is prone to errors if you’re not careful. Here are the 3 most common mistakes:

  1. File permissions: libvirt usually runs under the libvirt-qemu user. If the ISO file is in the /home/user/ directory, the command will fail immediately. Use chmod or move the file to the standard directory.
  2. Resource duplication: If you delete a VM using virsh destroy but forget virsh undefine, the old configuration remains. virt-install will report that the VM name is already in use.
  3. Actual RAM shortage: Do not allocate more RAM than the host’s available capacity (check with free -m). Otherwise, the VM will hang during the initialization phase.

Summary

Using virt-install isn’t just for “showing off” like a hacker. It’s a core tool that helps you automate infrastructure and handle incidents quickly in a production environment.

Initially, the parameters might seem overwhelming. However, once you get the hang of it, you’ll find clicking through a GUI to be slow and unprofessional. If you encounter any errors during the process, leave a comment below so we can discuss!

Share: