Basic management of kvm virtual machine

Posted by bios on Sun, 01 Dec 2019 08:30:47 +0100

Deploy KVM virtual machine

a.kvm installation

Environment: centos7, cpu supports virtualization, close selinux, close firewalld

yum install libvirt virt-install qemu-kvm -y

libvirt service: managing the life cycle of kvm virtual machine

Virt install tool: create and install virtual machine

QEMU KVM tool: using QEMU img to provide disks for virtual machines

b. start libvirtd service

systemctl start libvirtd

c. install TightVNC on windows

TightVNC official website: http://www.tightvnc.com

vnc is a cross platform remote desktop software, which is used when installing kvm virtual machine system

d. install system for kvm virtual machine

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name test --memory 1024 --vcpus 1 --disk /opt/test.raw,format=raw,size=10 --cdrom /opt/CentOS-7-x86_64-DVD-1708.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

e. enable console connection

grubby --update-kernel=ALL --args="console=ttyS0,115200n8"

 

KVM virtual machine management

virsh list --all    #View virtual machine
virsh start <domain>    #Boot up
virsh shutdown <domain>    #Shutdown
virsh destroy <domain>    #Forced shutdown
virsh suspend <domain>    #Hang up
virsh resume <domain>    #recovery
virsh define <domain>    #Import configuration
virsh undefine <domain>    #delete
virsh edit <domain>    #Modify configuration
virsh dumpxml <domain> > name.xml    #Export configuration
virsh domrename <domain> name    #rename
virsh autostart <domain>    #Auto start
virsh autostart --disable <domain>    #Disable auto start
virsh vncdisplay <domain>    #Display output IP address and port number for VNC

 

KVM disk format conversion

Raw: raw format, large space, not suitable for remote transmission, does not support snapshot function, good performance

qcow2: low (copy on write) space, suitable for transmission, snapshot support, slightly worse performance than raw

#Create virtual disk
qemu-img create test.raw  10G
qemu-img create -f qcow2 test.qcow2 10G

#View virtual disk information
qemu-img info test.raw

#Resize virtual disk capacity
qemu-img resize test.raw +5G

#Disk format conversion
qemu-img convert -f raw -O qcow2 test.raw test.qcow2

 

KVM snapshot management

#Create Snapshot
virsh snapshot-create <domain>

#View snapshots
virsh snapshot-list <domain>

#Restore snapshot
virsh snapshot-revert <domain> --snapshotname 1516574134(unix Timestamp)

#Delete snapshot
virsh snapshot-delete <domain> --snapshotname 1516636570

 

KVM cloning

Complete cloning

virt-clone -o <domain> --auto-clone

Link clone

qemu-img create -f qcow2 -b test.qcow2 test-clone.qcow2
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name test-clone --memory 1024 --vcpus 1 --disk /opt/test-clone.qcow2,format=qcow2,size=10 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

Manual cloning

1: Clone virtual disk file
cp test.qcow2 test-clone.qcow2
2: Generate a new virtual machine profile
virsh dumpxml test > test-clone.xml (modify name,delete uuid,modify disk Route,delete mac address)
3: Test startup
virsh define test-clone.xml

 

KVM virtual machine bridge network

#Create a bridge network
virsh iface-bridge eth0 br0

#Creating virtual machine based on bridge network
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name test --memory 1024 --vcpus 1 --disk /opt/test.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

Change virtual machine to bridge network

virsh edit test

    <interface type='bridge'>
      <mac address='52:54:00:55:aa:fa'/>
      <source bridge='br0'/>

Topics: Linux snapshot network vnc