Vous êtes sur la page 1sur 19

Cemng 1o know 1he Llnux kernel

vlrLual Machlne (kvM)


8yan Maueson
mauy91[gmall.com
hup://prefeLch.neL
Cvervlew
1onlghL l am golng Lo dlscuss kvM, and show
how Lo congure a hosL Lo use kvM
l plan Lo spllL my presenLauon lnLo 3 parLs:
arL 1 wlll provlde an overvlew of Lhe Lechnology
arL 2 wlll show you how Lo use Lhe Lechnology
arL 3 wlll be a C&A perlod
WhaL ls kvM?
kvM (kernel vlrLual Machlne) ls a full vlrLuallzauon soluuon LhaL
allows you Lo run unmodled guesLs (Llnux, Solarls, Wlndows) on
x86 and x64 sysLems LhaL supporL hardware vlrLuallzauon
exLenslons
kvM ls lmplemenLed as Lwo componenLs:
A kernel module LhaL manages Lhe hardware resources (Lhe
hypervlsor)
A userland process (a modled verslon of CLMu) LhaL provldes
C plauorm emulauon
When a new kvM guesL ls booLed, lL becomes a process (qemu-kvm
on ledora and CenLCS hosLs) of Lhe underlylng operaung sysLem
and ls scheduled llke any oLher process*
1he qemu-kvm process ls a modled verslon of CLMu, and
communlcaLes wlLh Lhe kvM hyper vlsor Lhrough Lhe /dev/kvm
characLer devlce
* 1here ls one excepuon. 1he modled CLMu processes run ln guesL" mode vs. user or kernel mode
Why would l wanL Lo use kvM?
Allows you Lo run muluple operaung sysLem lnsLances on a
slngle sysLem, whlch ls greaL for envlronmenLs LhaL need a
place Lo LesL Lhlngs, or for companles LhaL are consolldaung
hosLs Lo beuer uullze exlsung hardware resources
SupporLs llve mlgrauon, whlch allows you Lo move runnlng
guesLs beLween sysLems
LlbvlrL has been enhanced Lo manage kvM guesLs, allowlng
you Lo re-use provlslonlng and managemenL lnfrasLrucLure
bullL on Lop of lL
kvM has been parL of Lhe malnllne kernel source slnce
2.6.20 was released, so you don'L need Lo apply a slew of
kernel paLches Lo geL kvM up and operauonal!
uoes my machlne supporL kvM?
kvM uullzes hardware vlrLuallzauon exLenslons from
lnLel and AMu, whlch are avallable ln modern Cus
?ou can check /proc/cpulnfo Lo see lf you have Lhe
necessary vlrLuallzauon exLenslons:
$ egrep '^ags.*(vmx|svm)' /proc/cpulnfo
ags : fpu vme de pse Lsc msr pae mce cx8 aplc sep mLrr pge mca cmov paL
pse36 clush mmx fxsr sse sse2 hL syscall nx mmxexL fxsr_opL rdLscp lm 3dnowexL
3dnow rep_good nopl pnl cx16 lahf_lm cmp_legacy svm exLaplc cr8_legacy 3dnow
lf you see one of Lhe followlng ags, your golden:
vmx lndlcaLes Lhe Cu supporL lnLel's vlrLuallzauon
exLenslons
Svm lndlcaLes Lhe Cu supporLs AMu's vlrLuallzauon
exLenslons
Managlng kvM CuesLs
kvM guesLs can be managed ln one of Lwo ways:
ulrecLly Lhrough Lhe modled CLMu execuLable
lndlrecLly Lhrough llbvlrL (Lhls res up Lhe CLMu blnary
under Lhe covers)
1he CLMu qemu-sysLem-x86_64 /qemu-kvm blnary
can be used Lo dlrecLly manage kvM guesLs
LlbvlrL provldes Lhe vlrsh (vlrLual shell) Lo manage
kvM guesLs, and Lhe vlrL-lnsLall uullLy Lo
congurauon and provlslon new guesLs
1he examples ln Lhls presenLauon wlll use llbvlrL
kvM Pardware SupporL
kvM / CLMu provlde devlce emulauon for several
Lypes of devlces:
luL dlsks
SCSl dlsks
uS8, arallel, Serlal
vlruo for acceleraLed neLwork and dlsk performance
kvM / CLMu also several forms of hosL neLworklng:
user neLworklng
rlvaLe brldge neLworklng
ubllc brldge neLworklng
vlrLual dlsLrlbuLed LLherneL
Pow do l creaLe a kvM guesL?
CuesLs can be lnsLalled from uvus and lSC lmages, or
Lhough your favorlLe neLwork lnsLallauon meLhod
1o creaLe a new guesL, you can pass one or more
parameLers Lo Lhe vlrL-lnsLall command:
$ vlrL-lnsLall --connecL qemu:///sysLem \
--name puppeL --ram 312 \
--le /nfs/vms/puppeL.lmg \
--neLwork=brldge:br0 \
--acceleraLe -s 36 --pxe -d \
--noauLoconsole \
--mac=34:32:00:33:20:00 \
--nographlcs --nonsparse
8ooung and accesslng kvM guesLs
Cnce a guesL ls creaLed, you can manage lL Lhrough Lhe vlrLual shell
(vlrsh)
1o booL a kvM guesL, you can use Lhe vlrsh sLarL" command:
$ vlrsh sLarL <guesL>
1o desLroy a runnlng guesL (l.e., pull Lhe power plug), you can use
Lhe vlrsh desLroy" command:
$ vlrsh desLroy <guesL>
1o access Lhe hosLs console, you can use Lhe vlrsh console"
command (Lhls requlres Lhe hosL Lo be congured Lo wrlLe Lo Lhe
serlal console):
$ vlrsh console <guesL>
!"#$ of addluonal opuons, whlch are documenLed ln vlrsh(1)
Addlng nlCs Lo kvM guesLs
nlCs can be added aL guesL creauon ume by appendlng
several --neLwork" opuons Lo Lhe vlrL-lnsLall command llne:
$ vlrL-lnsLall --neLwork=brldge:br0 -mac=x \
--neLwork=brldge:br0 -max=? .
lor exlsung guesLs, Lhe vlrsh edlL" command can be used Lo
edlL Lhe guesL congurauon, and a sLanza slmllar Lo Lhe
followlng can be added Lo creaLe a new nlC:
<lnLerface Lype='brldge'>
<mac address='34:32:00:33:20:00'/>
<source brldge='br0'/>
</lnLerface>
Addlng ulsks Lo kvM guesLs
ulsks can be added aL guesL creauon ume by appendlng several --
le" opuons Lo Lhe vlrL-lnsLall command llne
$ vlrL-lnsLall -le /nfs/vms/puppeL/dlsk1.lmg -s 18 \
--le /nfs/vms/puppeL/dlsk2.lmg -s 18 ..
1o auach a dlsk Lo an exlsung hosL, you wlll rsL need Lo creaLe a
dlsk lmage wlLh elLher dd or qemu-creaLe:
$ qemu-lmg creaLe /nfs/vms/puppeL/dlsk3.lmg 18C
1he vlrsh edlL" command can be used Lo edlL Lhe xML denluon,
and a sLanza slmllar Lo Lhe followlng can be added Lo add a new
vlrLual dlsk Lo Lhe guesL:
<dlsk Lype='le' devlce='dlsk'>
<source le='/nfs/vms/puppeL/dlsk3.lmg'/>
<LargeL dev='hda' bus='lde'/>
</dlsk>
Congurlng Console Access
kvM guesL consoles can be accessed Lhrough
vnC, or vla a vlrLual serlal console
1o enable vnC access, you can add Lhe -
vnc" and -vncporL" opuons Lo Lhe vlrL-lnsLall
command llne
1o congure serlal access, you can add Lhe -
nographlcs" opuon Lo Lhe vlrL-lnsLall command
llne (Lhe guesL operaung sysLems need Lo be
congured Lo send ouLpuL Lo uyS0)
kvM Mlgrauon
kvM supporLs llve mlgrauon, whlch allows you Lo move acuve kvM
guesLs from one hosL Lo anoLher
Mlgrauon requlres LhaL Lhe Lhe les backlng Lhe guesL are locaLed
on some Lype of share sLorage (e.g., ClS2 le sysLem, CClS2 le
sysLem, nlS, CllS share, eLc.)
1o mlgraLe a guesL from Lhe currenL hosL Lo a machlne named
dlsarm", you can run Lhe vlrsh mlgraLe command wlLh Lhe -llve"
opuon, Lhe name of Lhe guesL Lo mlgraLe, and a connecuon sLrlng:
vlrsh # &'()*+, -.'/, 0/&123,4 5,&67+.89::3'8*)&:8;8+,&
1he connecuon sLrlng llsLed above conLalns Lhe Lhe drlver (qemu)
and LransporL proLocol (Lls) Lo use, Lhe machlne (dlsarm) Lo connecL
Lo, and Lells vlrsh Lo connecL Lo Lhe sysLem process
8acklng up and 8esLorlng kvM guesLs
1o back up Lhe congurauon of a guesL, you
can use Lhe vlrsh dumpxml" command:
$ vlrsh dumpxml <guesL>
1o resLore a guesL from an xML le, you can
use Lhe vlrsh dene" command:
$ vlrsh dene <paLhLoxmlle>
kvM Logglng
lf you encounLer a fallure, Lhe rsL place Lo
check ls Lhe sysLem and guesL logs:
/var/log/messages
/var/log/llbvlrL/<guesL>.log
lf Lhe logs don'L conLaln sumclenL daLa Lo
debug an lssue, you can lncrease Lhe log level
by adusung Lhe log_level and log_ouLpuLs ln
Lhe llbvlrLd.conf congurauon le
CoLchas
kvM ls a relauvely new Lechnology, and wlLh any
new Lechnology comes some growlng palns
lf you encounLer an lssue, check Lhe ledora, kvM
and llbvlrL bug Lrackers and malllng llsLs
1o avold maor lssues, make sure you are runnlng
a relauvely currenL verslon of CLMu, kvM and
llbvlrL (Lhe examples shown ln Lhe presenLauon
came from a ledora 11 hosL runnlng Lhe laLesL
packages from rawhlde)
Concluslon
Whlle kvM ls relauvely new, lL ls shaklng ouL
Lo be one of Lhe leaders ln Llnux vlrLuallzauon
lf you are lnLeresLed ln playlng wlLh kvM, lL ls
mosL llkely a <lnserL your favorlLe package
manager> lnsLall away
All examples ln Lhls arucle should work, and
were LesLed on a ledora 11 hosL runnlng a
Llnux 2.6.30 kernel wlLh Lhe laLesL
vlrLuallzauon updaLes
Cuesuons?
8eferences
KVM website:
http://www.linux-kvm.org/page/Main_Page
Configuring Linux hosts to log to a serial console:
http://prefetch.net/blog/index.php/2009/06/17/
redirecting-the-centos-and-fedora-linux-console-to-a-
serial-port-virsh-console-edition/
Libvirt Website:
http://libvirt.org/
QEMU Website:
http://www.nongnu.org/qemu/

Vous aimerez peut-être aussi