KVM Test
Lab environment
VMwareWorkstation15 pro+Ubuntu18.10 64bit server VM function as hostserver
Pre-installation checklist
Check that your CPU supports hardware virtualization
vmx(Intel),svm(AMD)
$egrep -o '(svm|vmx)' /proc/cpuinfo
vmx vmx vmx vmx
$sudo kvm-ok
INFO: /dev/kvm exists KVM acceleration can be used
Use a 64bit kernel (if possible)
$egrep -c 'lm' /proc/cpuinfo
$uname -m
Install KVM
Install Necessary Packages
$vi /etc/apt/sources.list
deb http://mirrors.163.com/ubuntu cosmic main restricted deb http://mirrors.163.com/ubuntu cosmic-updates main restricted deb http://mirrors.163.com/ubuntu cosmic universe deb http://mirrors.163.com/ubuntu cosmic-updates universe deb http://mirrors.163.com/ubuntu cosmic multiverse deb http://mirrors.163.com/ubuntu cosmic-updates multiverse deb http://mirrors.163.com/ubuntu cosmic-backports main restricted universe multiverse deb http://mirrors.163.com/ubuntu cosmic-security main restricted deb http://mirrors.163.com/ubuntu cosmic-security universe deb http://mirrors.163.com/ubuntu cosmic-security multiverse
$sudo apt update
$sudo apt-get install qemu-kvm qemu-system libvirt-daemon-system libvirt-clients bridge-utils vlan
The libvirt-bin package was split into two parts:
libvirt-daemon-system
libvirt-clients
In most cases you probably want both of them at the same time.
Verify Installation
$virsh list --all
Config libvirtd service
$sudo service libvirtd start
$sudo update-rc.d libvirtd enable
$sudo service libvirtd status
Network configuraton
Config network bridge:br0
$sudo vi /etc/netplan/50-cloud-init.yaml
network: ethernets: ens33: dhcp4: no dhcp6: no version: 2 bridges: br0: interfaces: [ens33] dhcp4: no addresses: [192.168.1.6/24] gateway4: 192.168.1.1 nameservers: addresses: [192.168.1.1]
Try network configuration
$sudo netplan try
Apply configuration
$sudo netplan apply
Check network bridge status
$sudo networkctl status -a
● 2: ens33 Link File: /lib/systemd/network/99-default.link Network File: /run/systemd/network/10-netplan-ens33.network Type: ether State: carrier (configured) Path: pci-0000:02:01.0 Driver: e1000 Vendor: Intel Corporation Model: 82545EM Gigabit Ethernet Controller (Copper) (PRO/1000 MT Single Port Adapter) HW Address: 00:0c:29:0e:d3:06 (VMware, Inc.) ● 3: br0 Link File: /lib/systemd/network/99-default.link Network File: /run/systemd/network/10-netplan-br0.network Type: bridge State: routable (configured) Driver: bridge HW Address: be:82:2a:0d:c9:ef Address: 192.168.1.6 fe80::bc82:2aff:fe0d:c9ef Gateway: 192.168.1.1 (China Mobile (Hangzhou) Information Technology Co., Ltd.) fe80::1 (China Mobile (Hangzhou) Information Technology Co., Ltd.) DNS: 192.168.1.1
$ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.6 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::bc82:2aff:fe0d:c9ef prefixlen 64 scopeid 0x20<link> ether be:82:2a:0d:c9:ef txqueuelen 1000 (Ethernet) RX packets 2993 bytes 871683 (871.6 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1918 bytes 179158 (179.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:0e:d3:06 txqueuelen 1000 (Ethernet) RX packets 230935 bytes 179349586 (179.3 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2565 bytes 244642 (244.6 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Install Desktop GUI
$sudo apt-get install xinit
$sudo apt-get install gdm3
$sudo apt-get install kubuntu-desktop
Create VM
virt-manager GUI
Prepare image
Download ‘cirros-0.3.6-x86_64-disk.img’ from http://download.cirros-cloud.net/
Put it to /var/lib/libvirt/images/ using winscp
Start virt-manager GUI
$virt-manager
Check VMs
VM list
$virsh list
Id Name State ---------------------------------------------------- 1 kvm1 running
$virsh dominfo kvm1
Id: 1 Name: kvm1 UUID: 9e6ef307-0fb5-4865-842f-4ab7297a64d9 OS Type: hvm State: running CPU(s): 1 CPU time: 65.3s Max memory: 204800 KiB Used memory: 204800 KiB Persistent: yes Autostart: disable Managed save: no Security model: apparmor Security DOI: 0 Security label: libvirt-9e6ef307-0fb5-4865-842f-4ab7297a64d9 (enforcing)
$ps -elf | grep kvm1
6 S libvirt+ 3176 1 1 80 0 - 237920 - 14:23 ? 00:03:11 qemu-system-x86_64 -enable-kvm -name guest=kvm1,debug-threads=on -S -object secret,id=masterKey0,format=raw,file=/var/lib/libvirt/qemu/domain-1-kvm1/master-key.aes -machine pc-i440fx-cosmic,accel=kvm,usb=off,vmport=off,dump-guest-core=off -cpu Broadwell-IBRS -m 200 -realtime mlock=off -smp 1,sockets=1,cores=1,threads=1 -uuid 9e6ef307-0fb5-4865-842f-4ab7297a64d9 -no-user-config -nodefaults -chardev socket,id=charmonitor,fd=24,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -boot strict=on -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x5.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x5 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x5.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x5.0x2 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 -drive file=/var/lib/libvirt/images/cirros-0.3.6-x86_64-disk.img,format=qcow2,if=none,id=drive-ide0-0-0 -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1 -netdev tap,fd=26,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=52:54:00:d7:db:a9,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -spice port=5900,addr=127.0.0.1,disable-ticketing,image-compression=off,seamless-migration=on -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vram64_size_mb=0,vgamem_mb=16,max_outputs=1,bus=pci.0,addr=0x2 -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -chardev spicevmc,id=charredir0,name=usbredir -device usb-redir,chardev=charredir0,id=redir0,bus=usb.0,port=1 -chardev spicevmc,id=charredir1,name=usbredir -device usb-redir,chardev=charredir1,id=redir1,bus=usb.0,port=2 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny -msg timestamp=on 0 R cloudfan 4122 2829 0 80 0 - 1564 pipe_w 18:47 pts/1 00:00:00 grep --color=auto kvm1
VM network
Check from host
$virsh domiflist kvm1
Interface Type Source Model MAC ------------------------------------------------------- vnet0 bridge br0 rtl8139 52:54:00:d7:db:a9
$brctl show
bridge name bridge id STP enabled interfaces br0 8000.be822a0dc9ef no ens33 vnet0 virbr0 8000.5254007269b9 yes virbr0-nic
$ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.6 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::bc82:2aff:fe0d:c9ef prefixlen 64 scopeid 0x20<link> ether be:82:2a:0d:c9:ef txqueuelen 1000 (Ethernet) RX packets 5703 bytes 934182 (934.1 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3983 bytes 448100 (448.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:0e:d3:06 txqueuelen 1000 (Ethernet) RX packets 98865 bytes 87021446 (87.0 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5538 bytes 602984 (602.9 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 218 bytes 16609 (16.6 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 218 bytes 16609 (16.6 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:72:69:b9 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::fc54:ff:fed7:dba9 prefixlen 64 scopeid 0x20<link> ether fe:54:00:d7:db:a9 txqueuelen 1000 (Ethernet) RX packets 1547 bytes 119988 (119.9 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2576 bytes 284810 (284.8 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
$virsh net-list
Name State Autostart Persistent ---------------------------------------------------------- default active yes yes
$virsh net-info default
Name: default UUID: da4cf618-8698-42b0-b0a9-23a081296caf Active: yes Persistent: yes Autostart: yes Bridge: virbr0
$virsh net-dumpxml default
<network> <name>default</name> <uuid>da4cf618-8698-42b0-b0a9-23a081296caf</uuid> <forward mode='nat'> <nat> <port start='1024' end='65535'/> </nat> </forward> <bridge name='virbr0' stp='on' delay='0'/> <mac address='52:54:00:72:69:b9'/> <ip address='192.168.122.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.122.2' end='192.168.122.254'/> </dhcp> </ip> </network>
$virsh iface-list --all
Name State MAC Address --------------------------------------------------- br0 active be:82:2a:0d:c9:ef lo inactive 00:00:00:00:00:00 virbr0 inactive 52:54:00:72:69:b9
$virsh iface-dumpxml br0
<interface type='bridge' name='br0'> <mtu size='1500'/> <bridge stp='off' delay='1500'> <interface type='ethernet' name='ens33'> <link speed='1000' state='up'/> <mac address='00:0c:29:0e:d3:06'/> </interface> </bridge> </interface>
$virsh iface-dumpxml virbr0
<interface type='bridge' name='virbr0'> <mtu size='1500'/> <bridge stp='on' delay='200'> <interface type='ethernet' name='virbr0-nic'> <link state='down'/> <mac address='52:54:00:72:69:b9'/> </interface> </bridge> </interface>
Check in guest VM
$virsh console kvm1
Connected to domain kvm1 Escape character is ^] login as 'cirros' user. default password: 'cubswin:)'. use 'sudo' for root. cirros login: cirros Password:
$ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:D7:DB:A9 inet addr:192.168.1.8 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::5054:ff:fed7:dba9/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1888 errors:0 dropped:0 overruns:0 frame:0 TX packets:1724 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:134245 (131.0 KiB) TX bytes:133202 (130.0 KiB) Interrupt:11 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
VM xml
$virsh dumpxml kvm1
<domain type='kvm' id='1'> <name>kvm1</name> <uuid>9e6ef307-0fb5-4865-842f-4ab7297a64d9</uuid> <memory unit='KiB'>204800</memory> <currentMemory unit='KiB'>204800</currentMemory> <vcpu placement='static'>1</vcpu> <resource> <partition>/machine</partition> </resource> <os> <type arch='x86_64' machine='pc-i440fx-cosmic'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> <apic/> <vmport state='off'/> </features> <cpu mode='custom' match='exact' check='full'> <model fallback='forbid'>Broadwell-IBRS</model> <feature policy='require' name='vme'/> <feature policy='require' name='f16c'/> <feature policy='require' name='rdrand'/> <feature policy='require' name='hypervisor'/> <feature policy='require' name='arat'/> <feature policy='disable' name='erms'/> <feature policy='require' name='xsaveopt'/> <feature policy='require' name='abm'/> </cpu> <clock offset='utc'> <timer name='rtc' tickpolicy='catchup'/> <timer name='pit' tickpolicy='delay'/> <timer name='hpet' present='no'/> </clock> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <pm> <suspend-to-mem enabled='no'/> <suspend-to-disk enabled='no'/> </pm> <devices> <emulator>/usr/bin/kvm-spice</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/var/lib/libvirt/images/cirros-0.3.6-x86_64-disk.img'/> <backingStore/> <target dev='hda' bus='ide'/> <alias name='ide0-0-0'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <controller type='usb' index='0' model='ich9-ehci1'> <alias name='usb'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/> </controller> <controller type='usb' index='0' model='ich9-uhci1'> <alias name='usb'/> <master startport='0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/> </controller> <controller type='usb' index='0' model='ich9-uhci2'> <alias name='usb'/> <master startport='2'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/> </controller> <controller type='usb' index='0' model='ich9-uhci3'> <alias name='usb'/> <master startport='4'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/> </controller> <controller type='pci' index='0' model='pci-root'> <alias name='pci.0'/> </controller> <controller type='ide' index='0'> <alias name='ide'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <controller type='virtio-serial' index='0'> <alias name='virtio-serial0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </controller> <interface type='bridge'> <mac address='52:54:00:d7:db:a9'/> <source bridge='br0'/> <target dev='vnet0'/> <model type='rtl8139'/> <alias name='net0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <serial type='pty'> <source path='/dev/pts/2'/> <target type='isa-serial' port='0'> <model name='isa-serial'/> </target> <alias name='serial0'/> </serial> <console type='pty' tty='/dev/pts/2'> <source path='/dev/pts/2'/> <target type='serial' port='0'/> <alias name='serial0'/> </console> <channel type='spicevmc'> <target type='virtio' name='com.redhat.spice.0' state='disconnected'/> <alias name='channel0'/> <address type='virtio-serial' controller='0' bus='0' port='1'/> </channel> <input type='mouse' bus='ps2'> <alias name='input0'/> </input> <input type='keyboard' bus='ps2'> <alias name='input1'/> </input> <graphics type='spice' port='5900' autoport='yes' listen='127.0.0.1'> <listen type='address' address='127.0.0.1'/> <image compression='off'/> </graphics> <sound model='ich6'> <alias name='sound0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </sound> <video> <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/> <alias name='video0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <redirdev bus='usb' type='spicevmc'> <alias name='redir0'/> <address type='usb' bus='0' port='1'/> </redirdev> <redirdev bus='usb' type='spicevmc'> <alias name='redir1'/> <address type='usb' bus='0' port='2'/> </redirdev> <memballoon model='virtio'> <alias name='balloon0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </memballoon> </devices> <seclabel type='dynamic' model='apparmor' relabel='yes'> <label>libvirt-9e6ef307-0fb5-4865-842f-4ab7297a64d9</label> <imagelabel>libvirt-9e6ef307-0fb5-4865-842f-4ab7297a64d9</imagelabel> </seclabel> <seclabel type='dynamic' model='dac' relabel='yes'> <label>+64055:+106</label> <imagelabel>+64055:+106</imagelabel> </seclabel> </domain>
Storage Virtualization
Directory Storage Pool
$sudo cat /etc/libvirt/storage/default.xml
<!-- WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh pool-edit default or other application using the libvirt API. --> <pool type='dir'> <name>default</name> <uuid>faf15c8e-d5c4-4ac6-8bcb-57f065e17134</uuid> <capacity unit='bytes'>0</capacity> <allocation unit='bytes'>0</allocation> <available unit='bytes'>0</available> <source> </source> <target> <path>/var/lib/libvirt/images</path> </target> </pool>
Add disk virt-manager
Check disk from host
$sudo ls -l /var/lib/libvirt/images
total 539032 -rw-r--r-- 1 libvirt-qemu kvm 14811136 Jan 28 21:17 cirros-0.3.6-x86_64-disk.img -rw------- 1 libvirt-qemu kvm 537198592 Jan 28 21:13 kvm1.qcow2
$virsh domblklist kvm1
Target Source ------------------------------------------------ hda /var/lib/libvirt/images/cirros-0.3.6-x86_64-disk.img vda /var/lib/libvirt/images/kvm1.qcow2
$virsh domblkinfo kvm1 --all
Target Capacity Allocation Physical ----------------------------------------------------- hda 41126400 14766080 14811136 vda 536870912 537202688 537198592
Check disk in VM
$sudo fdisk -l
Disk /dev/sda: 41 MB, 41126400 bytes 255 heads, 63 sectors/track, 5 cylinders, total 80325 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Device Boot Start End Blocks Id System /dev/sda1 * 16065 80324 32130 83 Linux Disk /dev/vda: 536 MB, 536870912 bytes 16 heads, 63 sectors/track, 1040 cylinders, total 1048576 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/vda doesn't contain a valid partition table
Check storage pool
$virsh pool-list
Name State Autostart ------------------------------------------- default active yes
$virsh pool-info default
Name: default UUID: faf15c8e-d5c4-4ac6-8bcb-57f065e17134 State: running Persistent: yes Autostart: yes Capacity: 29.40 GiB Allocation: 11.95 GiB Available: 17.45 GiB
$virsh pool-dumpxml default
<pool type='dir'> <name>default</name> <uuid>faf15c8e-d5c4-4ac6-8bcb-57f065e17134</uuid> <capacity unit='bytes'>31569383424</capacity> <allocation unit='bytes'>12830777344</allocation> <available unit='bytes'>18738606080</available> <source> </source> <target> <path>/var/lib/libvirt/images</path> <permissions> <mode>0711</mode> <owner>0</owner> <group>0</group> </permissions> </target> </pool>
Check volume
$virsh vol-list default
Name Path ------------------------------------------------------------------------------ cirros-0.3.6-x86_64-disk.img /var/lib/libvirt/images/cirros-0.3.6-x86_64-disk.img kvm1.qcow2 /var/lib/libvirt/images/kvm1.qcow2
$virsh vol-info kvm1.qcow2 --pool default
Name: kvm1.qcow2 Type: file Capacity: 512.00 MiB Allocation: 512.32 MiB
$virsh vol-dumpxml kvm1.qcow2 --pool default
<volume type='file'> <name>kvm1.qcow2</name> <key>/var/lib/libvirt/images/kvm1.qcow2</key> <source> </source> <capacity unit='bytes'>536870912</capacity> <allocation unit='bytes'>537202688</allocation> <physical unit='bytes'>537198592</physical> <target> <path>/var/lib/libvirt/images/kvm1.qcow2</path> <format type='qcow2'/> <permissions> <mode>0600</mode> <owner>64055</owner> <group>106</group> </permissions> <timestamps> <atime>1548710024.906562804</atime> <mtime>1548710024.506558808</mtime> <ctime>1548710024.902562764</ctime> </timestamps> <compat>1.1</compat> <features> <lazy_refcounts/> </features> </target> </volume>
*‘IDE’ do not support hot plug, new disk can only be used after a reboot of the VM,’ *
For a disk created from virt-manager, if you want to delete it, you need to delete it from virt-manager also