KVM

DOMAIN-NAME == VM hostname

Basic Virsh Commands

Power on a VM

virsh start DOMAIN-NAME

Reboot a VM

virsh reboot DOMAIN-NAME

Power off a VM

virsh shutdown DOMAIN-NAME

Force shutdown a VM

virsh destroy DOMAIN-NAME

List all VMs, powered on & off

virsh list --all

VMs assigned IP address

virsh domifaddr DOMAIN-NAME

VM enable or disable autostart

virsh autostart DOMAIN-NAME
virsh autostart --disable DOMAIN-NAME

Script to Shutdown all VMs

$ for i in `sudo virsh list | grep running | awk '{print $2}'` do
    sudo virsh shutdown $i
done

Physically Remove a VM Image

virsh destroy DOMAIN-NAME 2> /dev/null
virsh undefine DOMAIN-NAME
virsh pool-refresh default
virsh vol-delete --pool default DOMAIN-NAME.qcow2

Connect to a VM console

virsh console DOMAIN-NAME

Suspend & Resume VM

virsh suspend DOMAIN-NAME
virsh resume DOMAIN-NAME

Modify VMs

Add more RAM to VM

RAM=2048
VMNAME=DOMAIN-NAME
virsh setmaxmem $VMNAME $RAM --config; virsh setmem $VMNAME $RAM --config

Add more CPUs to VM

VCPU=2
VMNAME=DOMAIN-NAME
virsh setvcpus $VMNAME $VCPU --config --maximum; virsh setvcpus $VMNAME $VCPU --config

Build a new VM

list OS types

osinfo-query os

Create a new image

DISK_SIZE=20G
OS=centos-7.6
VMNAME=DOMAIN-NAME
virt-builder $OS \
--format qcow2 \
--size $DISK_SIZE -\
o /var/lib/libvirt/images/$VMNAME.qcow2 \
--root-password password

Install VM form the image

VCPU=2
RAM=2048
VMNAME=DOMAIN-NAME
OSVARIANT=centos7.6
virt-install --name $VMNAME \
--ram $RAM \
--disk path=/var/lib/libvirt/images/$VMNAME.qcow2,bus=virtio \
--vcpus $VCPU \
--os-type linux \
--os-variant $OSVARIANT \
--network bridge=br0 \
--graphics none \
--serial pty \
--console pty \
--boot hd --import

Misc

Add a serial console support to guest.

Add the following to /etc/default/grub:

GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=38400 --unit=0 --word=8 --parity=no --stop=1"
GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0"

Run:

grub2-mkconfig -o /boot/grub2/grub.cfg

Install and enable QEMU guest agent

yum -y install qemu-guest-agent
systemctl start qemu-guest-agent

To convert ESXi vmdk VM disk image to qcow2 use the descriptor file, not the disk image itself

qemu-img convert -f vmdk /mnt/vms-nfs/zoneminder-centos/zoneminder-centos_0.vmdk zoneminder-centos_0.qcow2

Import and install an image converted from ESXi host (vmdk -> qcow2, ESXi -> KVM)

Notes: - bus=sata should make it happy, if not use bus=virtio or bus=ide

virt-install \
--import \
--name DOMAIN-NAME \
--memory 512 \
--vcpus 1 \
--cpu host \
--disk backup-prod.qcow2,format=qcow2,bus=sata \
--network bridge=br0,model=virtio \
--os-type=linux \
--os-variant=centos7.0 \
--graphics spice,listen=0.0.0.0 \
--noautoconsole

Esxi to KVM Migration Steps

copy SSH pub key to /etc/ssh/root/authorized_keys on ESXi host

eval `ssh-agent`
ssh-add
export LIBGUESTFS_BACKEND=direct

From the KVM hypervisor (or temp conversion machine):

virt-v2v -i vmx -it ssh "ssh://root@192.168.1.100/vmfs/volumes/datastore1/linux-server-vm-image.vmx" -o local -os /home/kvm

cd /home/kvm

virt-install --name linux-server-vm-image --memory 512 --vcpus 1 --disk linux-server-vm-image-sda --import --os-variant rhel7

Convert to KVM image

qemu-img convert -O qcow2 <input vmdk file> <ouput qcow2 file>

qemu-img convert -O qcow2 centos6-vm-migration-flat.vmdk centos6-vm-migration.qcow2

XML definition files location

/etc/libvirt/qemu

Add a new disk image

Create the new disk image (example: 1TB)

cd /var/lib/libvirt/images/
qemu-img create -f qcow2 large-new-disk 1000G

List device names on the VM CLI and note the existing disk device name (vda)

fdisk -l | grep '^Disk /dev/vd[a-z]'
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 sectors

Attach the disk to the virtual machine, use vdb for the new device name:

virsh attach-disk large-new-disk /var/lib/libvirt/images/large-new-disk vdb --cache none

Use fdisk mkfs mount to partition, create FS, and mount the new drive.


Published

Category

KVM

Tags

Contact