Preferences

Semi-related question... I recently spent an evening trying to familiarize myself with virtualization tooling and my initial impression was that the CLI experience for kvm and hyper-v was rather clunky compared to let's say docker, gcloud, ignite and kubectl. Also a lot of the learning material seems to be oriented towards GUI. It could be that I just haven't spent enough time with kvm and hyper-v. I've spent a LOT of time on those other tools so maybe I'm just more used to them.

Is there good CLI tooling available for virtualization? I would say that I'm looking for the "on-prem" experience where I start by configuring a virtual network, download some unmodified ISOs directly from the operating system vendor, provision my headless virtual machines with a combination of Powershell, Answer files, Bash and Kickstart. I then provide services like DHCP and DNS using my virtual machines rather than the built-in mechanism provided by the virtualization tooling.


kashyapc
There's a lot of useful command-line tooling for KVM- and QEMU-based virt. Here's a small selection of them:

virsh — This[1] is libvirt's shell interface; and gives you access to the rich set of libvirt APIs.

virt-builder — Use this for rapidly building minimal or customized virtual machines; it's greatly flexible; check out its man page[2]. And here's[3] a quick example that connects both virt-builder and virsh together.

virt-install — Use this if you don't like the default build of the template images from virt-builder; it lets you create "headless" servers via 'kickstart' and Linux OS trees from the command-line.

guestfish and libguestfs suite[4] — This rich set of tools help you in a variety of use-cases: repairing your broken disk images, editing, cloning, debugging disk images, and more. It has saved my behind a lot of times.

qemu-img[5] – This Swiss Army knife lets you powerfully manipulate disk images (QCOW2, raw, et al) offline. Example operations include: create images, backing chains, offline snapshots, disk image merging, ability to convert disk images from one format to another, and more.

[1] https://libvirt.org/manpages/virsh.html

[2] https://libguestfs.org/virt-builder.1.html

[3] https://developer.fedoraproject.org/tools/virt-builder/about...

[4] http://libguestfs.org/

[5] https://qemu.readthedocs.io/en/latest/tools/qemu-img.html

throwawayboise
virsh and/or lxc is all I ever use. All that cloud stuff, docker, kub-whatever is overkill to run a few VMs or containers at a small scale on-prem. KISS.
voidfunc
In terms of speed, how is virt-builder compare to say Terraform?

Terraform is dog slow sometimes due to cloud provisioning. Would be nice to just be able to build the VM locally then push the AMI/VHD into AWS or Azure.

virt-builder can usually build out a disk image in 15-60 seconds if the template has already been downloaded and you're running on baremetal. Might be 120 seconds if you have to use nested KVM or TCG.

  $ virt-builder fedora-33
  [   1.5] Downloading: http://builder.libguestfs.org/fedora-33.xz
  [   2.3] Planning how to build this image
  [   2.3] Uncompressing
  [   8.6] Opening the new disk
  [  13.8] Setting a random seed
  [  13.8] Setting passwords
  virt-builder: Setting random password of root to mZbPJw9d1ZHgowBk
  [  14.8] Finishing off
                   Output file: fedora-33.img
                   Output size: 6.0G
                 Output format: raw
            Total usable space: 6.0G
                    Free space: 4.7G (79%)
By the way, interesting virt-builder factoid: It uses a STRIPS-based planner to optimize the order of steps when building the image, so it doesn't waste time doing multiple copies. https://en.wikipedia.org/wiki/Stanford_Research_Institute_Pr... https://github.com/rwmjones/guestfs-tools/blob/0cffcbb7848af... https://github.com/libguestfs/libguestfs-common/blob/74bc5c5... https://github.com/libguestfs/libguestfs-common/blob/74bc5c5...
kashyapc
I don't know how Terraform works, but `virt-builder` will cache the xz-compressed template images locally on your first pull. So for example, if you build a Fedora image:

    $ virt-builder fedora-32 --size 10G --format qcow2
Then the (xz-compressed) template Fedora 32 image will be cached under ~/.cache/virt-builder. So your subsequent Fedora 32 image provisioning will be much faster.

You might want to give it a whirl and see if it satisfies your needs; `virt-builder` should be available on most major Linux distributions.

QEMU can be daunting at first (like ffmpeg, also by Fabrice Bellard!). But check out this 2018 guide to using QEMU by Drew Devault:

https://drewdevault.com/2018/09/10/Getting-started-with-qemu...

I stopped bothering with libvirt and other frontends for local dev environments. Now I use shell scripts to start my VMs. I only create one per month or two so it isn't that much of a hassle.

I second this! I have a shell script to download if necessary Debian, run its installer with setting matching production and then run VM with necessary development directories exposed to it. I tried initially to do that with virsh, but then I gave up. Raw qemu commands in fact rather straightforward indeed.
megous
Yeah, if you have a few virtual machines for specific purposes, there's not much point bothering with some abstraction on top of qemu.

I also have just a few 20 line scripts to run my VMs, and that's all. Works with no changes/hassle for 5 years already or more. No extra SW to learn. Man qemu gives me all the answers. Easy.

I guess if you want to manage some complicated setup and dynamically add/remove disks, network cards, or whatever, all the time, migrate machines, etc., some solution like virsh would be good, but for having everything on one bridge, in one subnet, to run some throwaway VM with Windows/browsers for web testing, the simpler setup is so nice.

btdmaster
Another nice guide is on ArchWiki: https://wiki.archlinux.org/index.php/QEMU.
arsome
You're comparing VM tooling with container tooling, there's going to be substantial differences in level of abstraction. I might suggest you look at the libvirt platform, it's a wrapper around KVM essentially, but there's virsh which I've found reasonably straight forward to work with.
ktpsns
Yes, virsh is really a nice tool to work with. It has multiple times saved my ass when the graphical virt-manager refused to work (for whatever X11-reason).
viraptor
You're looking at different abstraction levels. I'd make comparisons like: kvm:unshare, qemu-system:docker, kube:virsh.

If your want high level networking stuff, use virsh or virt-manager GUI. Or maybe proxmox if you want a whole distribution for it.

xorcist
For small installations or a single host, plain old virsh is probably what you are looking for. Point it to an iso and you're good to go. Not sure if it does any post install provisioning, but that's what kickstart and ansible is for.

Larger virtualization installations will already have a management layer, something like oVirt or vSphere, that has similar tools available.

tenebrisalietum
I had some QEMU VMs running for a while on a home server that were more or less started by command line. You can specify everything as command line parameters.

I wrote a script that pulled some info from a sourced-in bash "config file" for a given VM (such as amount of CPUs, RAM, and where the disks were), executed the appropriate ip commands to create the taps needed for network access (including a private inter-VM network), and then built/ran the long QEMU command with it in a screen session.

Which file is the ISO for the virtual CD ROM is just another QEMU command line parameter.

I had my own bind running in a VM and created a view on the same subnet as the private VM network. Since this was only a few VMs I didn't bother with DHCP, I just statically assigned IPv4s. I used VNC to setup the OS in them.

swiley
Just run qemu-system-blah. I thought the CLI for that was pretty intuitive.
kevindurb
You should try out vagrant, let’s you have a config file for your vm and you can choose different providers like libvirt, virtualbox, etc
houseofzeus
I'm not as familiar with Hyper-V but certainly for KVM you would probably interact with it (indirectly) using virsh which I find pretty capable. No sane human should be interacting directly with the qemu command line if they can help it, it's written more for consumption by other software.
Look at LXC/LXD you get both VMs and containers with the same CLI.
weinzierl
How do you get VMs with LXC/LXD?

Also LXD's command line tool being

    lxc
(with c instead of d) while LXC's being

    lxc-*
is an abomination. Who wouldn't be confused by that?
you just add --vm to lxc launch
weinzierl
Whoa, crazy, I never saw that option before. Same interface for container and VM is really nice.
nonameiguess
I'd recommend Packer and Terraform for this. Packer to provision the machine images from unmodified vendor ISOs and then Terraform to provision the network and VMs.

https://www.packer.io/docs/builders/qemu

https://github.com/dmacvicar/terraform-provider-libvirt

https://blog.ruanbekker.com/blog/2020/10/08/using-the-libvir...

I include the blog link because there is some nuance in how to get the path right for community Terraform providers that aren't in the Hashicorp registry. The documentation on the GitHub project isn't quite up to date with respect to how the latest versions of Terraform expect the plugin paths to be set up.

I've done this pretty successfully with all the major Linux distros minus Arch, which requires some bootstrapping to get an iso that Packer can work with (no such thing as an answers file for Arch). It's not that big a deal, though. Just find some instructions on how to create and mount a cloud-init iso in addition to the installer iso and use that to add an ssh public key so you can script the installation steps externally. I actually think Packer can do this, but I just haven't gotten it to work yet and have relied on shell scripts.

Hyper-V actually has a very comprehensive PowerShell module that is pretty well documented, by the way: https://docs.microsoft.com/en-us/powershell/module/hyper-v/?.... I've found it pretty easy to use and actually got the Arch auto-provision working on Hyper-V in Windows before I got it working in KVM in Linux.

Another thing is you can just use the cloud images and cloud-init for bootstrapping everything pretty easily, even on-prem. cloud-init has a "no cloud" config option, as mentioned above, where you just mount an iso with the config data as a DVD drive and cloud-init will find it automatically when the distro iso boots.

This guy has a pretty comprehensive example of how to set up a kubernetes homelab entirely using the libvirt Terraform provider from Ubuntu cloud images bootstrapped with cloud-init: https://github.com/zloeber/k8s-lab-terraform-libvirt

economusty (dead)
AdrianB1
For hyper-v you can use PowerShell to manage VM's, see the details here: https://docs.microsoft.com/en-us/virtualization/hyper-v-on-w...

I use Hyper-v for years and I rarely create new VMs, this is why I use GUI, it is convenient if you don't do this often enough to remember the syntax and params. Most people that I know are in the same boat, but for mass deployments or automated deployments PowerShell is the way to go.

This item has no comments currently.