About
There are several tools to manage KVM.
Here we use virsh commands. virsh stands for VIrtual SHell
Help
The post helps you to refer the most used admin commands which we use daily. Always you can get all details by help command virsh –help
Basic Operations
virsh list – List only running guests
virsh list –all – List all guest including down ones
virsh nodeinfo – KVM Server resource details CPU/Memory
virsh dominfo GUESTNAME – KVM Guest resource details
virsh start GUESTNAME – Start the Guest
virsh shutdown GUESTNAME – Graceful Guest Shutdown
virsh destroy GUESTNAME – Ungraceful Guest shutdown
virsh reboot GUESTNAME – Reboot Guest
virsh suspend GUESTNAME – Suspend Guest temporarily
virsh resume GUESTNAME – Resume the suspended Guest
Installation
Guest can be installed with qemu-kvm, virt-manager or virt-install
qemu-kvm – Can be installed with the below commands. You can use ssh tunnel and VNC Viewer to get the console.
qemu-img create -f qcow2 /var/lib/libvirt/images/GUEST01.img 10G
qemu-kvm -name GUEST01 -hda /var/lib/libvirt/images/GUEST01.img -cdrom /home/user/centos7.iso -boot d -m 1024
virt-manager – KVM server should be installed with X Windows Software and Xming should be installed on your local desktop to connect tru Putty sessions. This GUI tool is very easy to install and maintain.
virt-install & virt-clone – Can be installed or cloned with the below commands
qemu-img create -f qcow2 /var/lib/libvirt/images/GUEST01.img 10G
virt-install –name=centos7 –memory=1024 –vcpus=1–disk \ /var/lib/libvirt/images/GUEST01.img –os-variant=centos7.0 –cdrom /home/user1/iso/CentOS-7-x86_64-Minimal-1810.iso
Create an SSH tunnel to the hypervisor and use a VNC client – ssh -L 5924:localhost:5924 hyperviosr01
vncviewer localhot:5924 (Complete the installation)
Now you can clone the Guest to create another guest02
qemu-img create -f qcow2 /var/lib/libvirt/images/GUEST02.img 10G
virt-clone –connect qemu:///system –original-xml \ /var/lib/libvirt/images/GUEST01.xml –name GUEST02 \
–file /var/lib/libvirt/images/GUEST02.img
Migration (10.99.0.1 to 10.99.0.2)
10.99.0.1: Dump the Guest Config to XML – virsh dumpxml GUESTNAME01 > /tmp/GUESTNAME01.xml
10.99.0.1: Copy the XML to destination server – scp /tmp/GUESTNAME01.xml root@10.99.0.2:/tmp
10.99.0.1: Copy the disk to destination server – scp /data/GUESTNAME01.img root@10.99.0.2:/data/ (Storage Path Varies)
10.99.0.1: Copy if any additional disk images
10.99.0.2: Update XML with required info like correct network interface, disk etc – vi /tmp/GUESTNAME01.xml
10.99.0.2: Define the Guest in Destination server – virsh define /tmp/GUESTNAME01.xml
10.99.0.2: virsh list –all
10.99.0.2: virsh start GUESTNAME01
10.99.0.2: virsh autostart GUESTNAME01
Storage Management
List both active and inactive storage pools – virsh pool-list –all
Retrieves information on the storage pool named – vdisk – virsh pool-info pool-01
Storage pool can be created with Disk or Directory. Its easy to mount any disk on directory and use it. Supported pool types – dir,disk,fs,netfs,gluster,iscsi,scsi,lvm,mpath
Its easy to mount any disk on directory and use it
Supported pool types – dir,disk,fs,netfs,gluster,iscsi,scsi,lvm,mpath
mkdir -p /data01 (LVM Partition mounted to data01)
Create storage directory if not available
mkdir /var/lib/libvirt/storage/
cat /var/lib/libvirt/storage/pool-1.xml
<pool type="dir">
<name>pool-01</name>
<target>
<path>/var/storage/data01</path>
</target>
</pool>
Define the Pool – virsh pool-define /var/lib/libvirt/storage/pool-1.xml
Check the Status – virsh pool-list –all (Pool will be inactive)
Start the pool – virsh pool-start pool-01
Make automatically start at boot – virsh pool-autostart pool-01
Check the Status – virsh pool-list –all
Add new Disk to existing Guest –
Make sure you are proving unique disk file name and hard disk path
qemu-img create -f raw /var/lib/libvirt/images/GUESTNAME02-1.img 20G
virsh attach-disk GUESTNAME02 –source \ /var/lib/libvirt/images/GUESTNAME02-1.img –target vdb –persistent
Network management
Check the Bridges – brctl show
Delete existing Bridge – brctl delbr BRIDGENAME01
Add New Bridge to single Interface – brctl addif BRIDGENAME02 eth0
Add New Bridge to multiple Interfaces – brctl addif BRIDGENAME02 eth0 eth1
Check MAC Address – brctl showmacs BRIDGENAME02
Enable/Disable Spanning Tree (useful when multiple bridges) – brctl stp BRIDGENAME02 on / brctl stp BRIDGENAME02 off
Display STP Parameter Values – brctl BRIDGENAME02 virbr0
Populating VLAN without rebooting KVM Server
vi /etc/sysconfig/network-scripts/ifcfg-brvlan101
DEVICE=brvlan101
ONBOOT=yes
BOOTPRO=none
NOZEROCONF=yes
TYPE=Bridge
DELAY=0
vi /etc/sysconfig/network-scripts/ifcfg-bond0.101
DEVICE=bond0.101
ONBOOT=yes
BRIDGE=brvlan101
VLAN=yes
ifup ifcfg-brvlan101
ifup ifcfg-bond0.101
virt-sysprep
The virt-sysprep tool can be used to reset or unconfigure a guest virtual machine so that clones can be made from it.
List available Opetaions – virt-sysprep –list-operations (you can add many operations seprated by comma)
virt-sysprep -d GUEST01 –enable udev-persistent-net
virt-sysprep -d GUEST01 –root-password password:PASSWORDHERE