Introduction to VM, libvirt, KVM
Robert Wang 2014/5/26
What is a virtual machine
Virtual Machine
• System VM
• provide a environment for executing an OS
• usually does hardware virtualization
• Process VM
• executing a single process
Why do we need virtual machines
Resource Sharing/Consolidation
• Example: Need to run web, mail, file, x, y, z, total of 10 servers. Do we need 10 physical servers?
• Put them into one server?
• How?
Massive Deployment
• Example: Build 100 the web frontend servers.
• Install manually?
• Install via scripts?
Isolation
Run Legacy Software
Software Development
Snapshot
System VMs
Concept
Hardware Hypervisor
Guest OS Guest OS Guest OS
App App App App App App App App App
Hypervisor
• also called virtual machine monitor (VMM)
• manage mappings
• disks, memory
• manage sharing
• cpus, network
x86 Protection Ring: No Virtualization
Hardware Guest OS
App App App
Guest OS executes privileged instructions for App.
x86 Protection Ring: With Virtualization
Hardware Guest OS
App App App
Hypervisor
Hypervisor executes privileged instructions for Guest OS.
Types of Hypervisor
• Type 1: native (bare metal)
• need to implement its own drivers
• (eg. Xen, VMware ESX/ESXi)
• Type 2: hosted
• can reuse OSes’
implementations.
• (eg. VirtualBox, VMware Workstation)
Types of Hardware Virtualization
• Full Virtualization
• Para-virtualization
• Hardware-assisted virtualization
!
• [NOTE: they are not disjoint.]
Full Virtualization
• Complete simulation of hardware devices.
• Not quite possible on x86 until some hardware support.
• Binary translation.
• Slow!
Para-virtualization
• Modified OS to avoid most trapping instructions.
• Custom I/O drivers.
• (eg. Virtio)
Hardware-assisted Virtualization
• Aim to provide high performance full virtualization.
• Intel VT-x, AMD-V instruction set.
Kernel Virtual Machine
KVM
• Strictly speaking, KVM is a linux kernel module.
• Provides a set of APIs to accelerate virtualization.
• Utilizing Intel VT-x or AMD-V.
• I/O part is handled by QEMU.
KVM
Using KVM — Simple Way
• Create a virtual disk file
• qemu-img create -f qcow2 vdisk.img 10G
• Boot the machine (w/ install CD)
• kvm -hda vdisk.img -cdrom /path/to/boot-media.iso - boot d -m 512
• (“kvm” may be named “qemu-system-x86_64” on some distributions.)
Using KVM — The complex way
• qemu-system-x86_64 -machine accel=kvm:tcg -name robertabcd -S - machine pc-q35-1.6,accel=kvm,usb=off -cpu host -m 1024 -realtime mlock=off -smp 4,sockets=4,cores=1,threads=1 -uuid
f68ef371-4cd6-2406-d5e0-41ad6210c22c -nographic -no-user-config - nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/
qemu/robertabcd.monitor,server,nowait -
monchardev=charmonitor,id=monitor,mode=control -rtc base=utc -no- shutdown -device i82801b11-bridge,id=pci.1,bus=pcie.0,addr=0x1 - device pci-bridge,chassis_nr=2,id=pci.2,bus=pci.1,addr=0x1 -
device piix3-usb-uhci,id=usb,bus=pci.2,addr=0x1.0x2 -drive file=/
dev/vmhost/robertabcd.img,if=none,id=drive-virtio-
disk0,format=raw -device virtio-blk-pci,scsi=off,bus=pci.
2,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 - netdev tap,fd=25,id=hostnet0,vhost=on,vhostfd=32 -device virtio- net-pci,netdev=hostnet0,id=net0,mac=52:54:00:b3:08:a8,bus=pci.
2,addr=0x3 -chardev pty,id=charserial0 -device isa-
serial,chardev=charserial0,id=serial0 -device virtio-balloon- pci,id=balloon0,bus=pci.2,addr=0x4
libvirt
What is “libvirt”?
• A toolkit to interact with virtualization softwares.
• A long term stable API.
• Supports a wide range of hypervisors:
Setup VMs
• libvirt uses XML for definitions.
• You “define/undefine” a virtual machine.
• You “start/destroy” a domain (think a running virtual machine).
“virtinst” Package
• virt-install
--connect qemu:///system -n "robertabcd"
-r 1024
--vcpus 2
--accelerate
--disk /dev/vg/disk,bus=virtio,format=raw -w bridge=br0
--nographics
-x console=ttyS0 --location “…”
Hate CLI?
• It has a GUI manager: “virt-manager”.
• Think twice before using GUI.
Reference
• Getting Started (Debian/Ubuntu)
• https://help.ubuntu.com/12.04/serverguide/libvirt.html
• See libvirt documentation for tunable options and APIs
• http://libvirt.org/