Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Virtualization IV. KVM Virtualization Technology

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I. Overview of KVM

1 、 KVM

Kernel-based Virtual Machine kernel-based virtual machine

Developed by Qumranet, Israel, it is a module of the kernel.

In 2006, R & D was acquired by RedHat in 2008.

Dependent on HVM technology: Intel VT-X,AMD AMD-V

2. System operation mode after KVM module is loaded.

Kernel mode: GuestOS performs operations of class O of Ihammer, or other special instructions.

User mode: on behalf of the GuestOS to request the operation of class O

Guest mode: GuestOS's non-I guest O operation, in fact, it is called the virtual machine user mode.

3. Components of KVM

Two types of components:

/ dev/kvm: works in hypervisor, and can complete VM creation, startup and other management functions through ioctl () system call in user space. It is a character device.

Functions: create VM, allocate memory to VM, read and write registers of VCPU, inject interrupts into CPU, run VCPU, etc.

Qemu process: works in user space and is mainly used to implement Icano devices that simulate PCs.

4. KVM characteristics

1) memory management

Swap memory allocated to VM to SWAP

Support the use of Huge Page (large memory pages)

Support to use Intel EPT or AMD RVI technology to complete memory address mapping: GVA-- > GPA-- > HPA simplified to GVA-- > HPA

Support for KSM (Kernel Same-page Merging)

This kernel feature enables memory page sharing. KSM scans the memory of each virtual machine to find the same memory pages among the virtual machines, and merges these memory pages into a separate page that is shared by the relevant virtual machines. When a virtual machine attempts to modify the data on this page, KSM re-provides it with a new copy of the page. In practice, there is a high probability that the same memory pages appear between virtual machines with the same GuestOS running on the same physical host, such as shared libraries, kernels or other memory objects. Therefore, KSM technology can reduce the memory footprint and improve the overall performance.

2) hardware support

Depends on the Linux kernel

3) Storage:

Local Storage:

Network attached Storage:

Storage area Network:

Distributed storage: for example, GlustFS

4) Live migration:

Supported GuestOS:

Linux, Windows, OpenBSD, FreeBSD, OpenSolaris

5) device driver:

Full virtualization of IO devices: analog hardware

Paravirtualization of IO devices: installing drivers in GuestOS; virtio

Virtio-blk, virtio-net, virtio-pci, virtio-console, virtio-ballon

5. Limitations of KVM

General limitations:

CPU overcommit

Time recording is difficult to be accurate and depends on the time synchronization mechanism.

MAC address:

When the amount of VM is very large, there is the possibility of conflict.

Live Migration:

Performance limitations:

6. Introduction to the tool stack of KVM

It can be divided into two categories:

Qemu:

Qemu-kvm

Qemu-img

Libvirt

GUI: virt-manager, virt-viewer

CLI: virt-install, virsh

QEMU mainly provides the following parts:

Processor simulator

Emulate IO device

Associate simulated devices to real devices

Debugger

User interface to interact with the simulator

Supplementary information:

VMM: there are three modes for driving IO:

Autonomous VMM:VMM provides its own driver and console

Hybrid VMM: providing drivers with the help of OS

Rely on external OS to implement privileged domain

Self-provided privileged domain

Boarding VMM:

II. KVM installation

1. Install kvm

1) make sure that CPU supports HVM

# grep-E-color=auto "(vmx | svm)" / proc/cpuinfo

2) load module

[root@BAIYU_180 ~] # lsmodModule Size Used byppdev 8537 0 vmware_balloon 7199 0 parport_pc 22690 0 parport 36209 2 ppdev,parport_pcmicrocode 112685 0 snd_ens1371 21587 0 snd_rawmidi 23017 1 snd_ens1371

# modprobe kvm

# modprobe kvm-intel

[root@BAIYU_173 kvm] # lsmodModule Size Used bynls_utf8 1455 1 ipv6 335589 142 vhost_net 29185 0 macvtap 10551 1 vhost_netmacvlan 10324 1 macvtaptun 16934 1 vhost_netkvm_intel 55624 0 kvm 341551 1 kvm_intelppdev 8217 0 parport_pc 22658 0 parport 36209 2 ppdev Parport_pc

3) Calibration:

/ dev/kvm

III. Use of kvm management tool stack

# yum grouplist | grep-I "virtualization" # View all package groups related to virtualization Virtualization: qemu-kvm Virtualization Client: python-virtinst, virt-manager, virt-viewer Virtualization Platform: libvirt, libvirt-client Virtualization Tools # can not use libguestfs

1. Use qemu-kvm management tools

Libvirt-based tools such as virt-manager and virt-install provide a very convenient virtual machine management interface, but they actually encapsulate qemu-kvm tools after secondary development. Therefore, the previous task can also be accomplished by using the qemu-kvm command directly.

On RHEL6, qemu-kvm is located in the / usr/libexec directory. Because this directory is not part of the PATH environment variable, it cannot be used directly, which also prevents you from directly using qemu to create and manage virtual machines. If you want to use the qemu virtual machine, you can do so by linking / usr/libexec/qemu-kvm to / usr/bin/qemu.

1) install qemu-kvm

# yum install qemu-kvm

# ln-sv / usr/libexec/qemu-kvm / usr/bin/qemu-kvm

[root@BAIYU_173 ~] # yum install qemu-kvmDependencies Resolved= Package Arch Version Repository Size=Installing: qemu-kvm x8614 642 qemu-kvm 0.12.1.2-2.491.el6_8.1 updates 1.6 MInstalling for dependencies: celt051 X86'64 0.5.1.3-0.el6 base 50k glusterfs x86'64 3.7.5-19.el6 base 392k glusterfs-api x86'64 3.7.5-19.el6 Base 56 k glusterfs-client-xlators x86'64 3.7.5-19.el6 base 942k glusterfs-libs x86'64 3.7.5-19.el6 base 303 k gpxe-roms-qemu noarch 0.9.7-6.15.el6 base 220k qemu-img x861464 22.491.el6_8.1 updates 0.12.1.2-2.491.el6_8.1 updates 837k seabios x861364 0.6.1.2-30.el6 Base 93k sgabios-bin noarch 0-0.3.20110621svn.el6 base 6.6k spice-server x86164 0.12.4-13.el6.1 updates 346k usbredir x861640.5 .1-3.el6 base 41 k vgabios noarch 0.6b-3.7.el6 base 42 kTransaction Summary=Install 13 Package (s) [root@BAIYU_173 ~] # rpm-ql qemu-kvm/etc/ksmtuned.conf/etc/modprobe.d/blacklist-kvm.conf/etc/rc.d/init .d / ksm/etc/rc.d/init.d/ksmtuned/etc/sasl2/qemu-kvm.conf/etc/sysconfig/ksm/etc/sysconfig/modules/kvm.modules/etc/udev/rules.d/80-kvm.rules/usr/libexec/qemu-kvm/usr/sbin/ksmtuned/usr/share/doc/qemu-kvm-0.12.1.2/COPYING

2) the use of qemu-kvm

The qemu-kvm command uses the format:

"qemu-kvm [options] [disk_p_w_picpath]"

There are many options, but they can be roughly divided into the following categories.

Command options:

Standard options:

Display options:

I386 platform specific options

Character device option

Bluetooth device options

Linux Startup specific option

Debug / expert mode option

Cirros project: a disk image file of a miniature version of Linux provided for cloud environment test vm

Download address on the official website: http://download.cirros-cloud.net/

Create and start the virtual machine:

Qemu-kvm-M128-smp 2-name test-hda / p_w_picpaths/kvm/cirros-0.3.4-i386.disk.img

Specify the disk image file with-drive:

Qemu-kvm-M128-name test-smp 2-drive file=/p_w_picpaths/kvm/cirros-0.3.4-i386-disk.img,if=virtio,media=disk,cache=writeback,format=qcow2

[root@BAIYU_173 ~] # cd / p_w_picpaths/kvm/ [root @ BAIYU_173 kvm] # lscirros-0.3.4-x86_64- disk.img [root @ BAIYU_173 kvm] # qemu-kvm-m 128-smp 2-name test-hda / p_w_picpaths/kvm/cirros-0.3.4-x86_64-disk.img VNC server running on `:: 1 VNC server is started here and you can see: [root@ BAIYU_173 ~] # netstat-nlptu | grep 5900tcp 00:: 1LISTEN 3191/qemu-kvm [root@BAIYU_173 ~] # ps aux | grep qemu-kvmroot 3191 15.08.1 492796 82944 pts/3 Sl+ 22:39 0:14 qemu-kvm-M128-smp 2-name test-hda / p_w_picpaths/kvm/cirros -0.3.4-x86_64-disk.imgroot 3242 0.0 103324 860 pts/0 S+ 22:41 0:00 grep qemu-kvm [root@BAIYU_73 ~] # yum install tigervnc # install vnc client [root@BAIYU_173 ~] # rpm-ql tigervnc/usr/bin/vncviewer/usr/share/applications/vncviewer.desktop/usr/share/doc/tigervnc-1.1.0 [root@BAIYU_173 ~] # vncviewer localhost # connect and see

Press ctrl+alt+2 to jump to the control page, view the information of the virtual machine, and then press ctrl+alt+1 to jump back

Standard options for qemu-kvm:

It mainly involves the specified host type, CPU mode, NUMA, floppy drive devices, optical drive devices and hardware devices.

◇-name name: sets the name of the virtual machine

◇-M machine: specify the type of host to simulate, such as Standard PC, ISA-only PC, Intel-Mac, etc. You can use "qemu-kvm-M?" Get all supported types

◇-m megs: sets the RAM size of the virtual machine

◇-cpu model: set CPU models, such as coreduo, qemu64, etc., using "qemu-kvm-cpu?" Get all supported models

◇-smp n [, cores=cores] [, threads=threads] [, sockets=sockets] [, maxcpus=maxcpus]: set the number of CPU in the simulated SMP architecture, the number of cores of each CPU and the number of socket of CPU, etc. The maximum number of CPU used to specify hot insertion of 255CPU;maxcpus can be simulated on PC.

◇-numa opts: specifies a numa device that simulates multiple nodes

◇-fda file

◇-fdb file: use the specified file (file) as the floppy disk image. File / dev/fd0 means to use the physical floppy drive.

◇-hda file

◇-hdb file

◇-hdc file

◇-hdd file: use the specified file as the hard disk image

◇-cdrom file: use the specified file as the CD-ROM image. Note that-cdrom and-hdc cannot be used at the same time; specify file as / dev/cdrom to directly use the physical optical drive; # I don't know how to use this option

◇-drive option [,...]]: define a hard disk device; there are many suboptions available.

File=/path/to/somefile: hardware image file path

If=interface: specifies the type of interface to which the hard disk device is connected, that is, the controller type, such as ide, scsi, sd, mtd, floppy, pflash, virtio, etc.

Index=index: sets the index number of different devices in the same controller type, that is, identification number

Media=media: defines whether the media type is hard disk (disk) or optical disc (cdrom)

Snapshot=snapshot: specifies whether the current hard disk device supports snapshot: on or off

Cache=cache: defines how to use physical cache to access block data. Available values are none, writeback, unsafe, and writethrough.

Format=format: specify the format of the image file, which can be found in the qemu-img command

◇-boot [order=drives] [, once=drives] [, menu=on | off]: define the boot order of boot devices. Each device is represented by one character. The devices supported by different architectures and their representation characters are different. On x86 PC architecture, an and b represent the floppy drive, c represents the first hard drive, d represents the first optical drive device, and NMAIP represents the network adapter; the default is the hard disk device.

For example:-boot order (boot sequence) = dc,once (used only on first startup) = d

We recreate the virtual machine with the-drive option:

[root@BAIYU_173 kvm] # qemu-img info cirros-0.3.4-x86_64-disk.img # View the format of disk image file p_w_picpath: cirros-0.3.4-x86_64-disk.imgfile format: qcow2virtual size: 39m (41126400 bytes) disk size: 14Mcluster_size: 65536

Start the installation of win7 through cdrom

Qemu-kvm-name winxp-smp 4 sockets1 coresses 2 threadsbooks 2-m 512-drive file=/p_w_picpaths/kvm/winxp.img,if=ide,media=disk,cache=writeback,format=qcow2-drive file=/root/winxp_ghost.iso,media=cdrom

(1) prepare disk image files

[root@BAIYU_173 ~] # qemu-img create-f qcow2-o size=60G / p_w_picpaths/kvm/win7.imgFormatting'/ pendant picpathsUnikvmUnix win7.imgache, fmt=qcow2 size=64424509440 encryption=off cluster_size=65536

(2) upload the installation file of win7

(3) create and start win7

[root@BAIYU_173 kvm] # qemu-kvm-name win7-smp 2-m 1024-drive file=/p_w_picpaths/kvm/win7.img,if=ide,media=disk,cache=writeback,format=qcow2-drive file=/root/cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso,media=cdrom-boot order=dc,once=d

Display options for qemu-kvm:

Display options are used to define the types and properties of the display interface after the virtual machine is started.

◇-nographic: by default, qemu uses SDL to display VGA output; this option is used to disable the graphical interface, at which point

Qemu is similar to a simple command line program in which emulated serial port devices are redirected to the console.

Commands that can be used at this time:

Cmura h print this helpC-a x exit emulatorC-a s save disk data back to file (if-snapshot) Cmura t toggle console timestampsC-a b send break (magic sysrq) Cmura c switch between console and monitorC-a Cmura sends Cmura

SDL: Simple DirectMedia Layer: simple and direct media layer C language development, cross-platform and open source multimedia library files

Use "- sdl" in qemu; # centos does not support it by default

VNC: Virtual Network Computing: virtual network computing uses RFB (Remote FrameBuffer remote frame buffer) protocol to remotely control other hosts

CentOS 6.6 enable vnc

(1) yum install tigervnc-server

(2) vncpasswd

(3) vncserver: n # start a desktop

Qemu-kvm

-vnc display (address + a desktop), option,option

Example:-vnc: n [, password]

When you start qemu-kvm, use the-monitor stdio option extra and use the

Use the change vnc password command to set the password in QEMU mode

Network attribute related options for qemu-kvm

Network attribute-related options are used to define information such as the type of network device interface and its related attributes.

Only the properties of nic, tap and user network interfaces are introduced here. For other types, please refer to the qemu-kvm manual.

◇-net nic [, vlan=n] [, macaddr=mac] [, model=type] [, name=name] [, addr=addr] [, vectors=v]:

Create a new network card device and connect it to vlan n; the default NIC on PC architecture is e1000 Magi Macaddr to specify its MAC address, and name to specify a network device name displayed during monitoring; emu can simulate multiple types of network card devices, such as virtio, i82551, i82557b, i82559er, ne2k_isa, pcnet, rtl8139, E1000, smc91c111, lance and mcf_fec, etc. However, on different platform architectures, the supported types may contain only part of the above list, and you can use "qemu-kvm-net nic,model=?" To get the types supported by the current platform

◇-net tap [, vlan=n] [, name=name] [, fd=h] [, ifname=name] [, script=file] [, downscript=dfile]:

Connect to vlan n through the TAP network interface of the physical machine, use the script specified by script=file (default / etc/qemu-ifup) to configure the current network interface, ifname is used to specify the name of the network card, and use the script specified by downscript=file (default / etc/qemu-ifdown) to unconfigure the interface; using script=no and downscript=no can be used to disable script execution, respectively

If the-net option is not specified when creating a virtual machine, a network card of type-net user will also be created by default.

◇-net user [, option] [, option] [,...]: configure the network stack in user mode, which does not depend on administrative permissions

Valid options are:

Vlan=n: connect to vlan n, default 0

Name=name: specifies the display name of the interface, which is commonly used in monitoring mode

Net= addr [/ mask]: set the IP network visible to GuestOS with optional mask. Default is 10.0.2.0and8.

Host=addr: specify the IP address of the physical machine seen in GuestOS. The default is x.x.x.2, the second one in the specified network.

Dhcpstart=addr: specifies the starting IP of 16 addresses in the DHCP service address pool. The default is the 16th to 31st, namely x.x.x.16-x.x.x.31.

Dns=addr: specifies the address of the dns server visible to GuestOS; the default is the third address in the GuestOS network, x.x.x.3

Tftp=dir: activate the built-in tftp server and use the specified dir as the default root directory of the tftp server

The name of the bootfile=file:BOOTP file, which is used to implement the network boot GuestOS; such as: qemu-hda linux.img-boot n-net user,tftp=/tftpserver/pub,bootfile=/pxelinux.0

Qemu-ifup and qemu-ifdown scripts:

# cat / etc/qemu-ifup #! / bin/bash#bridge=br0 # bro must create if [- n "$1"]; thenip link set $1 upsleep 1brctl addif $bridge $1 [$?-eq 0] & & exit 0 | | exit 1elseecho "Error: no interface specified." # when executing, connect to the network card interface exit 1fi# cat / etc/qemu-ifdown #! / bin/bash#bridge=br0if [- n "$1"] Thenbrctl delif $bridge $1ip link set $1 downexit 0else echo "Error: no interface specified." # when executing, connect the interface exit 1fi of the network card behind

Specify the use of bridged network interfaces:

Qemu-kvm-M128-name test-smp 2-drive file=/p_w_picpaths/kvm/cirros-0.3.4-i386-disk.img,if=virtio,media=disk,cache=writeback,format=qcow2-net nic-net tap,script=/etc/if-up,downscript=no-nographic

Fourth, summarize the contents of this blog post

1. Kvm components:

/ dev/kvm

Qemu

2. The modules that need to be loaded to install KVM:

Modprobe kvm

Modprobe kvm-intel | kvm-amd

3. KVM tool stack:

Qemu-kvm, / usr/libexec

Libvirt:

GUI: virt-manager

CLI: virt-install, virsh

Qemu-kvm

-name 'NAME'

-m megs

-cpu?

-smp n [, sockets=N] [, cores=N] [, threads=N] [, maxcpus=N]

-hda |-hdb |-hdc |-hdd

-cdrom

-drive

File=, media=, if=, cache=, format=, index=, readonly # is opened in read-only mode, and parameters are not required.

-boot [order=drives] [, once=drives] [, menu=on | off]

-sdl

-vnc: 0jol password

-nographic

-monitor stdio # does not open the window and displays the monitor screen locally

-usbdevice tablet # solves the problem of mouse out of sync in virtual machine

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report