KVM

Install

apt-get install qemu-kvm virtinst virt-manager bridge-utils libosinfo-bin
sudo adduser $USER kvm 
#apt-get install qemu-kvm libvirt-bin virtinst bridge-utils cpu-checker
sudo kvm-ok                                                                       #check      
systemctl status libvirtd.service
https://github.com/LuyaoHuang/virsh-bash-completion                               #bash completion

Commands

virsh list                                                       #show running vm´s
virsh list --all                                                 #show all vm´s 
virsh undefine vm1
virsh destroy vm1                                                #forcefully stop a given domain, but leave its resources intact
virsh start vm-name
virsh autostart test                                             #
virsh reboot vm-name
virsh shutdown vm-name
virsh suspend vm-name
virsh resume vm-name
virsh domrename foo bar                                          #rename vm from foo to bar
virsh autostart vm-name
virsh autostart --disable vm-name
virsh domrename $OLD_NAME $NEW_NAME
virsh domrename --domain oldname newname                         #rename domain/ vm       
virsh edit vm-name                                               #edit vm properties
osinfo-query os                                                  #list available os when specify os by "--os-variant" - package "libosinfo-bin" is required!
virsh net-list                                                   #list running network configs
virsh net-list --all                                             #list all network configs
virsh dumpxml vm-name                                            #xml-config to stdout
virsh dumpxml master > master.xml                                #backup vm
virsh define master.xml                                          #restore vm 
virt-manager                                                     #gui to show, manage and create vm´s

snapshot

virsh snapshot-list vm-name                                                 #list all snaps       
virsh snapshot-list vm-name--tree 
virsh snapshot-create-as vm-name
virsh snapshot-create-as --domain vm-name --name snap-name
virsh snapshot-create-as vm-name
virsh snapshot-create-as vm-name "snap1" --diskspec sda,file=/var/backups/snapshots/snap1-vm-name.qcow2 --disk-only --atomic
virsh snapshot-info --domain vm-name --snapshotname snap-name
virsh snapshot-revert --current vm-name
virsh snapshot-delete --current vm-name
virsh snapshot-delete --domain vm-name --snapshotname snap-name
virsh snapshot-create-as --disk-only vm-name
virsh snapshot-edit --rename  --domain vm-name --snapshotname snap-name     #rename
virsh snapshot-delete --current --metadata vm-name                          #delete "disk-snapshot"
qemu-img info --backing-chain test.qcow2
qemu-img info test.qcow2
qemu-img convert -O output_format source destination
virsh domblklist vm-name
virsh blockcommit vm-name sda --active --verbose --pivot

Change Path Of "backing file"

rename-backup-path.sh
#!/bin/bash
 
BACKINGFILE="/kvm/images/master.qcow2"
QCOW2_FILE_TO_CHANGE="master.1586464836"
 
sudo qemu-img rebase \
  -f qcow2 \
  -u \
  -b $BACKINGFILE \
  $QCOW2_FILE_TO_CHANGE

Other:

sudo qemu-img rebase \
  -f qcow2 \
  -b $NEW_BACKING_FILE \
  $QCOW2_FILE_TO_CHANGE

Merge snapshot and backing file into standalone disk

cp test1-base new-master                                 #test1-base = base-disk
qemu-img rebase -b new-master test1-master-0             #snapshot="test1-master-0"
qemu-img commit test1-master-0
qemu-img commit new-master                               #output if success: "Image committed"
Image committed.
qemu-img info new-master

Backup

virsh blockcopy --domain kmaster sda \
  /data/images/backup/copy.qcow2 \
  --wait --verbose --shallow \
  --pivot

Pool

virsh pool-list                                                  #show active storage pool
virsh pool-list --all                                            #show also inactice pool
virsh pool-list --details                                        #show details
virsh pool-info poolname                                         #detailed pool info
virsh pool-autostart poolame
virsh pool-start poolname
virsh pool-undefine poolname                                     #delete pool "default"
virsh pool-destroy poolname                                      #set inactive
virsh pool-edit poolname
virsh pool-define-as --name default --type dir --target /media/work/kvm
virsh pool-refresh --pool default
virsh pool-refresh --pool kvm-vm

Volume

virsh vol-list default
virsh vol-list default --details
virsh vol-list kvm-vm --details
virsh vol-delete volume-name.qcow2  --pool default
virsh vol-create-as default test_vol2.qcow2 2G
virsh vol-clone --pool default vol1.qcow2 vol2.qcow2
qemu-img resize volume-name.qcow2 30G

Pool refesh an volume list:

pool-vol-ref.sh
#!/bin/bash
 
virsh pool-refresh --pool poolname0
virsh pool-refresh --pool poolname1
virsh vol-list poolname0 --details
virsh vol-list poolname1 --details

clone domain/ vm

Simple Guest Clone

clone-vm.sh
#!/bin/bash
 
#author:  Thomas Roehm
#version: 1.0
 
echo ""
echo "Overview VM´s:"
echo ""
virsh list --all
echo ""
read -p "Please enter the name of the vm you want to clone: " SOURCEVM
echo ""
read -p "Please enter the name of the cloned vm: " DESTVM
echo ""
 
virt-clone \
--original $SOURCEVM \
--auto-clone \
--name $DESTVM
 
STATUS=$(echo $?)
 
echo ""
 
if [ $STATUS -eq 0 ]
then
        echo "VM \"$DESTVM\" cloned."
else
        echo "Problem - VM not cloned!"
fi

Log

journalctl -a -b -k -u libvirtd.service
systemctl status libvirtd.service
kvm/kvm.txt · Last modified: 2020/06/20 14:30 by tmade
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki