virsh

Manpage
https://linux.die.net/man/1/virsh

# list all vms
virsh list --all
 
# show VM info
virsh dominfo ${VM_ID}
virt-install \
--name ubuntu1604 \
#--ram 2048 \
--disk path=./ubuntu1604.qcow2,size=8 \
--accelerate  \
--vcpus 2, maxvcpus=3 \
--memory=1024, maxmemory=2048  \
--memballoon virtio \
#--vcpus 2 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty, target_type=serial \
--location "http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/" \
--extra-args "console=ttyS0, 115200n8 serial"
virt-install \
--name ubuntu1604 \
--disk path=./ubuntu1604.qcow2,size=8 \
--accelerate \
--vcpus 2,maxvcpus=3 \
--memory 256,maxmemory=1024 \
--memballoon virtio \
--os-type linux \
--os-variant generic \
--network bridge=virbr0,mac=00:11:22:33:44:55 \
--graphics none \
--console pty,target_type=serial \
--location "http://archive.ubuntu.com/ubuntu/dists/xenial/main/installer-amd64/" \
--extra-args "console=ttyS0,115200n8 serial url=http://preseed.panticz.de/preseed/ubuntu-minimal.seed auto=true"
virt-install \
    --name www.example.com \
    --virt-type kvm \
    --disk /var/lib/libvirt/images/www.example.com_1.qcow2,bus=sata \
    --disk /var/lib/libvirt/images/www.example.com_2.qcow2,bus=sata \
    --check disk_size=off \
    --accelerate \
    --vcpus 4 \
    --ram 6144 \
    --os-type linux \
    --os-variant ubuntu14.04 \
    --network bridge=br0 \
    --graphics vnc \
    --noautoconsole \
    -c /var/lib/libvirt/images/iso/ubuntu-16.04.3-desktop-amd64.iso
 
# --check all=off
# -w bridge=br0 –extra-args=’console=tty0 console=ttyS0,115200n8′
#    -l http://ftp.ca.debian.org/debian/dists/squeeze/main/installer-amd64/ \
# configuration files
/etc/libvirt/qemu/vm01.xml
 
# virsh
virsh reset vm1
virsh shutdown vm1
virsh destroy vm1
 
# restart all
for VM in $(virsh list --state-running --name); do
    virsh shutdown ${VM}
done
sleep 120
for VM in $(virsh list --all --name); do
    virsh start ${VM}
done
 
# virsh: reload xml config
virsh define /etc/libvirt/qemu/vm01.xml
 
# delete vm
virsh undefine vm1
 
# ubuntu cloud image
http://cloud-images.ubuntu.com/xenial/current/
 kvm -net nic -net tap -hda ubuntu-16.04-server-cloudimg-amd64-disk1.img -hdb my-seed.img -m 2048 -vnc :1
 
virt-install \
    --name ubuntu-16.04-server-cloudimg-amd64 \
    --virt-type kvm \
    --disk path=/var/lib/libvirt/images/ubuntu-16.04-server-cloudimg-amd64-disk1.img,format=qcow2,bus=virtio,cache=none \
    --disk path=/var/lib/libvirt/images/my-seed.img,format=qcow2,bus=virtio,cache=none \
    --accelerate \
    --vcpus 2,maxvcpus=2 \
    --ram 2048 \
    --os-type linux \
    --os-variant ubuntu16.04 \
    --network bridge=br0,mac=00:0a:0b:0c:0d:11 \
    --graphics vnc \
    --noautoconsole \
    --boot hd
 
# get vnc port used by vm
virsh vncdisplay vm01
virsh vncdisplay 1
virsh domdisplay vn01
 
# enable autostart for a vm
virsh autostart vm01
 
# disable autostart for a vm
# virsh autostart --disable vm01
 
virsh list --all --name
 
virsh change-media vm1 hda --eject

Attach / detach harddisk
http://manpages.ubuntu.com/manpages/precise/man1/virt-install.1.html
https://www.cyberciti.biz/faq/howto-linux-delete-a-running-vm-guest-on-kvm/virsh-kvm-list-vms/

virsh attach-disk vm1.example.com /var/lib/libvirt/images/vm1.example.com_1.qcow2 vdb --cache none
virsh detach-disk vm1.example.com vdb

Stop all running VMs

for VM in $(virsh list --state-running --name); do
    virsh shutdown ${VM}
done

Destroy all VMs (DANGERUS !!!)

for VM in $(virsh list --all --name); do
    virsh destroy ${VM}
    virsh undefine ${VM}
    rm /var/lib/libvirt/images/${VM}.qcow2
done
apt install libguestfs-tools
 
virsh edit vm1
 
# clone vm
virt-clone \
--original=vm1 \
--name=vm1-clone \
--file=/var/lib/libvirt/images/vm1-clone.qcow2
 
 
# restart service
service libvirt-bin restart
 
# Create a dump of the crashed virtual server using the virsh dump command
virsh dump --memory-only <VS> <dumpfile>
 
# stats
virsh domstats --balloon
virsh domstats --cpu-total
 
# get allocated memory by vm
for VM in $(virsh list --state-running --name); do
    echo -n "${VM} "
    virsh dommemstat ${VM} | grep actual
#    virsh setmem ${VM} 4096
done
 
# start all autostart VMs
for VM in $(virsh list --autostart --all --name); do
    virsh start ${VM} && sleep 30
done

Change memory allocation for multiple VMs

cd /etc/libvirt/
sed -i "s|<memory unit='KiB'>3145728</memory>|<memory unit='KiB'>6291456</memory>|g" *.xml
sed -i "s|<currentMemory unit='KiB'>2097152</currentMemory>|<currentMemory unit='KiB'>4194304</currentMemory>|g" *.xml

Console

virsh console vm1
# exit console
Ctrl+5

VNC

virsh vncdisplay vm1
# remmina -n -t VNC -s 127.0.0.1:11

Forward VNC

ssh root@10.0.0.12 -L 5900:127.0.0.1:5900

Links
https://www.ibm.com/support/knowledgecenter/en/linuxonibm/liaat/liaatvirtinstalloptions.htm