LibVirt

KVM Virtual Machines

Build a Linux machine

Linux Machine
virt-install \
   --connect qemu:///system \
   --virt-type kvm \
   --name testkvm \
   --memory 2048 \
   --disk /media/KVM/pool/testkvm.qcow,format=qcow2,size=10 \
   --cdrom /media/source/pool/template/iso/debian-12.1.0-amd64-netinst.iso \
   --network bridge=vmbr1 \
   --graphics vnc \
   --os-type linux \
   --os-variant debiantesting

Build a Windows machine

Windows 7 Machine
virt-install \
   --connect qemu:///system \
   --virt-type kvm \
   --name UESX01 \
   --memory 4096 \
   --disk /media/KVM/pool/uesx01.qcow,format=qcow2,size=60 \
   --cdrom /media/source/pool/template/iso/Windows.7.SP1.ENG.x86-x64.ACTiVATED.iso \
   --network bridge=vmbr1 \
   --graphics spice \
   --os-variant=win7
Windows 10 Machine
virt-install \
   --connect qemu:///system \
   --virt-type kvm \
   --name UESX02 \
   --memory 4096 \
   --disk /media/KVM/pool/uesx02.qcow,format=qcow2,size=60 \
   --cdrom /media/source/pool/template/iso/Win10_22H2_English_x64v1.iso \
   --network bridge=vmbr1 \
   --graphics spice \
   --os-variant=win10
Import Disk
virt-install  \
  --name demo  \
  --memory 512 \
  --disk /home/user/VMs/mydisk.img \
  --import

Machine Management

List Machines

virsh -c qemu:///system list --all
 Id Name                 State
----------------------------------
  8 testkvm              shut off

Start a Machine

virsh -c qemu:///system start testkvm
Domain testkvm started

Get Connection info

virsh -c qemu:///system vncdisplay testkvm
127.0.0.1:0

Note

Other available virsh subcommands include:

  1. reboot to restart a virtual machine;

  2. shutdown to trigger a clean shutdown;

  3. destroy, to stop it brutally;

  4. suspend to pause it;

  5. resume to unpause it;

  6. autostart to enable (or disable, with the #.#.disable option) starting the virtual machine automatically when the host starts;

  7. undefine to remove all traces of the virtual machine from libvirtd.

All these subcommands take a virtual machine identifier as a parameter.

Full deletion

virsh -c qemu:///system undefine testkvm --remove-all-storage --delete-snapshots

Connect to Machine

Note

this connection should be automatically started. If not, or if we operate remotely, virt-viewer can be run from any graphical environment to open the graphical console (note that the root password of the remote host is asked twice because the operation requires 2 SSH connections):

virt-viewer --connect qemu+ssh://root@server/system testkvm
root@server's password:
root@server's password:
Local connection
virt-viewer --connect qemu:///system UESX01

Create Storage Pool

virsh pool-create-as srv-kvm dir --target /media/KVM/pool/

Last update: Feb 27, 2025