Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ซอฟต์แวร์ >> เครื่องเสมือน

เชี่ยวชาญ KVM Virtualization: คู่มือฉบับสมบูรณ์สำหรับ Kernel-Based Virtual Machines

ถ้าคุณอ่าน Virtualization section ของผม คุณจะรู้ว่า focus มาเป็นส่วนใหญ่ VMware และ VirtualBox พร้อม cloud stuff และ image remastering เล็กน้อย ถึงเวลา branch out วันนี้จะ formally begin whole new era of tutorials กับ KVM ภายหลังจะมี Xen และ weird beasts อื่นๆ แต่ตอนนี้ topic คือ KVM

เริ่มจาก guide นี้ เราจะเรียนรู้ KVM (Kernel-based Virtual Machine) technology developed by RedHat available as free open-source alternative to other commercial solutions เราจะเรียนรู้ how to download, install และ setup KVM, what tools we can use to manage virtual machines, what options are available, command-line usage และ scripting และมากกว่านั้น เราจะ look at advanced networking และ storage configurations และ many other cool things เริ่มกันเลย

เชี่ยวชาญ KVM Virtualization: คู่มือฉบับสมบูรณ์สำหรับ Kernel-Based Virtual Machines

KVM - Glossary

เริ่มด้วย brief overview of how KVM works ไม่ major แค่ tiny intro ให้รู้ basic terminology 别担心 所有东西很快都会很简单

KVM functions by utilizing CPU virtualization technology extensions บน modern Intel และ AMD processors ที่รู้จักว่า Intel-VT และ AMD-V ใช้ kernel module loaded into memory KVM utilizes processor และ via user-mode driver based on modified QEMU 它 emulates a hardware layer upon which virtual machines can be created and run KVM 也可以 executed without CPU extensions แต่จะ run in pure emulation mode using QEMU resulting in significant performance penalty

KVM can be managed either via graphical management tool 类似于 VMware products 或 VirtualBox 或 via command line using several methods

Most popular GUI 叫做 Virtual Machine Manager (VMM) developed by RedHat tool 也知道โดย generic package name virt-manager มาพร้อม supporting tools 包括 virt-install, virt-clone, virt-image, 和 virt-viewer 用于 provision, clone, install, 和 view virtual machines それぞれ VMM 也支持 Xen machines

Generic KVM command interface provided by virsh 具体来说 你可以使用 supporting tools 像 virt-install 创建你的 virtual machines 在 Ubuntu 有特殊的 ubuntu-vm-builder tool 可以用于 provisioning Ubuntu builds developed by Canonical

The multitude of names can be confusing 但 take it easy 你会得到 idea 然而 to make it just a little bit more confusing KVM 也有自己的 syntax 类似于 QEMU 它不是 recommended way of managing virtual machines

更多信息请查阅这些页面:

  • The Kernel Virtual Machine - Community Ubuntu Documentation
  • KVM tutorials on howtoforge.com
  • KVM/Virsh - Community Ubuntu Documentation

好的 那是 step 1

KVM - Pros and cons

Should you use KVM? Well 那取决于你想 achieve 什么

如果你是 casual home user virtualization hobbyist 或 intermediate geek KVM 可能有点 difficult to start with 它 primarily a command-line tool 不直观或友好像 VMware 和 Virtualbox products KVM 感觉 many years behind the competition 在 GUI management 虽然它 just as powerful 当它 comes to delivering 事实上 因为它 designed to be used as a command-line tool 你 enjoy better productivity 和 more control in the long run 因此 KVM makes most sense in the business environment

此外 如果 CPU ของคุณ不支持 virtualization KVM 将是 big waste of time for you 因为你将 running in extremely slow and inefficient emulation mode KVM 也 known to conflict with Virtualbox 但更多关于那个在 separate tutorial

因此 KVM 是为 people with a keen interest in virtualization 最可能是 administrators and professionals 它不是你的 cuddliest home toy 可能很多年也不会是 但如果你 willing to dig in 你将 master a complete new language 这将 later serve you as a force multiplier with other technologies 虽然 VMware 或 Virtualbox prefer 你使用他们的 GUI tools KVM oriented at the raw efficiency of command-line scripting

To sum it up KVM is good for you if you are looking for a free modern virtualization solution with an unlimited usage mode and without additional licensing fees or feature tiering a powerful command line interface and you're not afraid to dirty your hands KVM is not good for you if your CPU does not have virtualization extensions you're afraid of writing scripts have no desire to dabble in code and prefer administratively simpler solutions like VMware Server, ESXi or Virtualbox

All right 让我们开始

Test platform

您可以在 pretty much any Linux distro 使用 KVM 一般来说 RedHat flavors favor KVM RHEL ships with KVM enabled in the operating system 所以您也可以在 RedHat-based distros 找到它 像 CentOS, Scientific Linux, 或 Fedora

既然我在家里 mostly running Ubuntu 我决定 test 在 Ubuntu installed on my rather brand-newish HP laptop which has a revvy i5 processor that supports virtualization 稍后今年 我计划 deploy either the newest release of CentOS 或 Scientific Linux on my Linux-dedicated multi-boot machine 所以你会看到 additional tutorials for those distros too 我们会到达 Xen 那里 too

这个 tutorial shows how to setup KVM on a 64-bit Ubuntu Lucid the Long Term Release Sequel guides and howtos will cover many other topics related to KVM 包括 using it alongside other virtualization technologies and resolving conflicts advanced management functions scripting 和 more

现在 终于 让我们开始认真地

Prerequisites

您需要 CPU that supports virtualization 这可以检查 thusly:

egrep -c '(vmx|svm)' /proc/cpuinfo

如果您得到 non-zero value 你 good BUT ... 你也需要 check that virtualization technology is enabled in BIOS 确实 在启用 feature 后 您将必须 cold power-cycle the machine for the change to take effect

一旦完成 boot into your operating system and get KVM installed

要检查 请运行 kvm-ok:

เชี่ยวชาญ KVM Virtualization: คู่มือฉบับสมบูรณ์สำหรับ Kernel-Based Virtual Machines

Download & install KVM

要 get KVM install 请 get these packages (true for apt commands):

apt-get install qemu-kvm libvirt-bin

Optionally 您可以 install these - most recommended:

apt-get install bridge-utils virt-manager python-virtinst

P.S. Package names will vary between distributions 例如 virt-install may come branded as python-virt-install or python-virtinst or even virt-install dependencies for virt-clone, virt-image and virt-viewer should be automatically resolved

Contrary to what most tutorials mention 您 do NOT need bridge utilities 您只需要它们如果您想要 bridge your virtual adapters with your physical cards 我已经在 Virtualbox networking tutorial 显示了 detailed example 原则是相同的

Most tutorials also mention the fact that most Wireless network interfaces do not support bridging 这可能是个案例 但在 all my testing 我能够 successfully bridge my Wireless adapters 所以 keep your hopes high

VMM (virt-manager) is highly recommended 同样您想要 all the helper tools 包括 virt-viewer, virt-install, virt-image, 和 virt-clone

最后 您也可以 opt for ubuntu-vm-builder:

apt-get install ubuntu-vm-builder

您也将 get a whole bunch of dependencies auto-resolved 所以 your downloads can take a while P.S. On RedHat use yum install On SUSE use zypper install

Conflict with Virtualbox

Again contrary to what most tutorials say: you can have both KVM and Virtualbox installed on the same host 您 may not get them running both at the same time 换句话说 one or the other kernel module needs to be unloaded from memory 但 there's no reason to rush ahead Try see if it works for you and if not only then take corrective actions

But this can be done in-vivo 所以 no worries there 您不会需要 reboot 或 anything as drastic as that 我将 show a separate tutorial that handles this case 事实上 我有 both installed and running on the test host

Using KVM

现在 真正有趣的部分 我们将从 VMM 开始 因为它是 GUI tool 和这是大多数人将和应该开始的东西 对于 all practical purposes 它非常类似于 other tools you know 像 VMware console 或 perhaps Virtualbox

Virtual Machine Manager (VMM)

让我们 tour 一下

第一次 launch program 它将显示 two categories 都 not connected 这些指的是 your localhost KVM modules 仍然不在使用中 要使用 right-click 和 choose connect

要 add new connections 点击 File > Add Connection 这将 open a window that lets you specify the Hypervisor type and the connection type VMM can use both local and remote connections 包括 QEMU/KVM 和 Xen 您可以 connect using all kinds of authentication methods

您也可以 autoconnect 意味着 next time you fire up VMM these connections will be ready for you 这类似于什么 VMware Server asks you when you start the program Just for reference:

Kernel versus Usermode

您可能在问什么区别 between normal/default 和 Usermode? Well Usermode allows you to run without administrative privileges 但 your network capability will be limited All right 让我们 move on

VMM overview, continued

让我们看 other functions 之前我们 create a virtual machine

Network functionality can be reviewed or changed under Host Details 我们将有 separate tutorial that teaches how to work with KVM network 和我们将 setup bridge utilities too 所以别担心

同样您可以 change the storage settings 会有 dedicated guide 我保证:

Change preferences

VMM 有 handful of options you can tweak to make it look and behave smarter 您可以 enable the system tray icon set a statistics polling interval enable polling for disk and network metrics which are deselected by default configure console keyboard grabbing console scaling audio options 和 set confirmation windows for machine activities

เชี่ยวชาญ KVM Virtualization: คู่มือฉบับสมบูรณ์สำหรับ Kernel-Based Virtual Machines

เชี่ยวชาญ KVM Virtualization: คู่มือฉบับสมบูรณ์สำหรับ Kernel-Based Virtual Machines

稍后 all of these will allow us to view our virtual machine metrics in more detail and with better control 例如 showing an Ubuntu guest CPU, Disk I/O and Network I/O:

这是 system tray icon:

最后 我们准备 create a new virtual machine

Create virtual machine

我们可以做这个 using VMM 或 command line 我们将开始 with the GUI 第一步 quite intuitive 输入 name and the install media location 您可以使用 local storage in the form of ISO images 或 CD/DVDs, HTTP 或 FTP servers, NFS area, 或 PXE

我们将使用 local install media 接下来 choose either a physical device or an image 在我们的例子 我们将使用 ISO 然后 choose the OS type and the version 这不必 precise 但 educated guess is good 它 allows additional virtual machine optimization

Allocate RAM and CPUs

第四步 quite interesting - storage 您可以 create a new image 或 use an existing one 您可以 set the size and preallocate all space which is good for performance plus it reduces fragmentation

我们将 focus soon quite a bit more on the storage 现在 让我们 either assume we have a disk ready - 或 that we will create one in the default storage pool 请注意如果您 running in Usermode 您 won't be able to write to /var which is the default storage pool location 您将需要 create a new storage pool that is accessible by non-admin users 但更多关于那个在 separate article

Step 5 是 summary with some advanced options 您可以 change your network type set a fixed Mac address choose the virtualization type and the target architecture 如果您 running in Usermode your network options will be limited 如果您 not using bridge utilities 您 won't be able to bridge your interfaces 最后 如果您有 no CPU virtualization extensions Virt Type: field will read QEMU and not KVM 我们将 see an example of this later on when we discuss the major drawbacks of running in the emulation mode 现在 这里是 typical Ubuntu virtual machine setup looks like:

Our machine is ready to use 我们将 soon power it up and use it

Virtual machine options

像 VMM 本身 VM console 有 a few interesting options 您可以 send signals to your guest 您可以 switch virtual consoles force reboot and power off clone migrate take screenshots 和 more 又非常类似于 competition

这里是一对 screenshots teasers for the clone and migrate options Down the road 我们将有 detailed tutorials focusing both on the GUI and CLI functionality

Virtual machines running

The fun part 这里是一把 nice screenshots ...

让我们从 Ubuntu 10.10 Maverick 32-bit version boot menu 开始:

Puppy Linux shown against a lovely desktop:

Ubuntu running NAT-ed no worries 注意 CPU utilization 很低 Important pay attention for later on when we discuss the emulation only mode 我在唠叨你 但请 bear with my ultra-turbo education

您也可以 scale the console window to fit guest 这是 another option available in the viewer menu 然后您也可以使用 preferences to autoscale the consoles 这里是 Puppy 和 Ubuntu side by side:

这里是 system usage minimal 以这种 smart consumption 您可以 generously overcommit 和 enjoy more virtual machines than your physical hardware would normally permit with a single machine bound to a core

最后 您也可以 delete a machine and its associated files:

现在我们 comfortable with basic usage 让我们做一些 command line

Command line

现在 让我们看 infamous command line 例如 使用 virsh to list all available virtual machines

virsh 'list --all'

这里是 sequence of commands to get a virtual machine created and running using virt-install 请注意 我不是 KVM guru 您有 template code in the man pages 和你可以找到 a billion examples online

The actual command is:

virt-install --connect qemu://system -n puppy -r 512 -f puppy.img -c lupu-520.iso --vnc --noautoconsole --os-type linux --accelerate --network=network:default

So what do we have here?

--connect qemu:///system defines the type of hypervisor used system is used when running on bare metal kernel as root session is used for non-root users 您可以 recall the differences in VMM with Usermode right-click connect you get the idea 这也 works for xen

-n puppy is a unique virtual machine name 您可以使用 virsh to rename or delete the guest if you're not satisfied with your choice

-r 512 specifies the RAM

-f specifies a disk file 在此例中 我使用 a puppy.img disk image I created using a dd command To conserve space 我创建了 image as a sparse file 您可以 learn more about how to create sparse files in my second cool Linux hacks article

-c specifies the CD-ROM either a physical device or an ISO image

--vnc setups a guest console and exports it as a VNC server --noautoconnect will not automatically open the console when the virtual machine is powered on

--os-type specifies the operating system for the guest

--accelerate allows KVM to use optimization functions that accelerate the performance of the guest system if possible

--network defines the network type 我们使用 default connection

我们 could have used many other functions 像 setting up the number of cores or bounding the machine to a CPU core setup a fixed MAC address for NIC use no storage change the keyboard map 和 more

All of these are listed in the man pages Not really intuitive 但 becomes easier with use 事实上 learning curve is not that high despite the initial impression 一旦你 get the hang of virt-install 你可以 fiddle with other commands start and stop machines with virsh 和 Bob's your uncle

Other things

A few more details worth mentioning

Running in pure emulation mode (don't)

我告诉过你这 completely inefficient 现在 a proof 例如 看 Host Details Overview 在 Performance graph CPU usage is max. at 100% of the available resources for the virtual machine 在此例中 one core which translates to 25% utilization Almost like DOS 这意味着 four virtual machines 无论多么 tiny or hungry 将 cripple a four-core host

此外 performance will be horrible 如果一个 Ubuntu guest takes approx. one minute to boot with the image loaded on the internal 7,200rpm disk when running with Intel-VT extensions enabled 它 takes about 20 minutes to do that in the emulation mode 我认为这是 a downside of QEMU/KVM compared to other solutions which still offer fairly decent performance even without CPU extensions

More good reading

我 highly recommend the KVM series on howtoforge.com linked above

然后您也应该读这些:

  • Discover the Linux Kernel Virtual Machine
  • KVM management tools

Conclusion

我 sincerely hope you've liked this tutorial 它 fairly long and detailed even though it documents only the first steps to get started with KVM 但它 does offer an insight into a lot of things 包括 initial setup and prerequisites command-line and GUI tools management options how to create and run multiple virtual machines 和 more

There are going to be a ton of sequels which will cover advanced storage and networking configurations 包括 bridged networking how to clone and migrate virtual machines how to resolve conflicts with other virtualization software and more Not strictly related 也会有 a whole bunch of articles on Xen and ESXi too

As you can see KVM is quite powerful even though it may appear a bit daunting to new users Its management options are definitely less newb-friendly than some of its rivals 但 luckily some of the fear and confusion have been spirited away today

Well 我猜那就是全部了 Enjoy!

Cheers.