Procedure and summary of creating a VM Based on KVM

Source: Internet
Author: User
Tags windows remote desktop kvm hypervisor xen hypervisor
1. Preface


Currently, we are engaged in cloud computing IAAs. virtualization is an important part of IAAs. Therefore, during this period, we have studied Virtual Machine technologies and tools. The purpose of this research is not only to use this tool, however, by studying and understanding the implementation mechanism and principles of technology, we need to know exactly what it means. This article is the beginning of my entry into this technology field. While recording my own progress, hope to help colleagues who have similar pursuits. To better understand the steps, the installation steps do not adopt the graphical method.

KVM (not a keyboard, display, or mouse) is a Linux kernel-based virtualization tool. The principle is being studied.

Let's get down to the truth:

2. Test Environment

Hardware: Dell poweredge 2 CPU 8g mem

Operating System: centos 5.5

Hypervisor: KVM (kenerl-based virtualization lization machine)

3. Installation Steps 3.1 install the Operating System

This step is not much said. You need to note that during the installation process, select the custom function and choose KVM under virtualization, because in centos5.5, virtualization supports xen by default. After the installation is complete, the packages required for virtualization, such as KVM, KVM-qemu, libvirt, and python, are automatically installed.

After the installation is complete, run the virsh command to check whether the virtualization environment is normal. The specific command is as follows:

Virsh-C qemu: // system list

If an empty list is displayed, the operation is normal.

Note: The virsh command is a command line tool provided by the libvirt library and can be used to manage virtual machines. What is libvirt? Well, this is an important topic. I will use an article to describe it.
3.2 Network Configuration

After the OS is installed and supports virtualization-related packages, a virtual bridge named virbr0 is automatically created. When multiple NICs exist, the bridge is bound to the eth1 Nic by default. To provide the networking function for virtual machines, KVM uses virbr0 as the default network device. Therefore, in actual applications, You need to bind virbr0 to the NIC connected to the actual network, for example, eth0.

The specific method is to modify the NIC configuration file under the/etc/sysconfig/network-scripts directory, such as binding etho, then modify the ifcfg-eth0 and ifcfg-virbr0

The modified configuration file is as follows:

1) ifcfg-eth0

Device = eth0
Onboot = Yes
Bridge = virbr0
Bootproto = DHCP
Type = Ethernet

2) ifcfg-virbr0

Device = virbr0
Type = Bridge
Onboot = Yes
Delay = 0
Bootproto = none
Ipaddr = 10.100.0.3
Netmask = 255.255.255.0
Gateway = 10.100.0.254

After the modification, restart the network service: Service Network restart.
The default virbr0 settings are saved in the/etc/libvirt/qemu/networks Directory, which contains a default. xml file, which is the default configuration of virbr0.

Note: The virtual bridge is created by the brctl tool in the bridge-utils package. This package is automatically installed after the virtualization-related package is installed. You can use the brctl addbr bridge name to add a bridge, use brctl show to display Existing Bridges
3.3 create a file used by the VM

The mechanism for establishing a virtual machine on a server is similar to that for creating a virtual machine on a PC using vmware workstation and virtualbox. Each virtual machine uses a physical file to save the installed system and simulate it as a disk, this file can be created and formatted as a specialized Virtual File Format (such as qcow2) or specified during VM creation, automatically generated by the VM builder.

The files used by the virtual machine can be created with the qemu-IMG command, or by using virt-manager and virt-install to create the virtual machine. The following is the syntax of qemu-IMG:

Qemu-IMG create-F qcow2 win7.img 20g <--- create a file in the format of qcow2 with a size of 20 GB

After the creation is complete, it will show: formatting 'win7. IMG ', FMT = qcow2, size = 20971520 KB

Run the following command to check the file format:A. File win7.imgWin7.img: x86 Boot Sector; partition 1: Id = 0x7, active, starthead 32, startsector 2048,204 800 sectors; partition 2: Id = 0x7, starthead 223, startsector 206848,407 51104 sectorsB. qemu-IMG info win7.imgImage: VM-Win7-1.img File Format: Raw virtual size: 20G (20971520000 bytes) disk size: 20g
Note: There are many formats of virtual files, which will be described and analyzed in an independent article.
3.4 create a virtual machine

After completing the above preparations, you can create a virtual machine. Here you can use the virt-install command to complete this task. The command is as follows:

Virt-install -- name VM-Win7-2 \ -- hvm \ -- Ram 512 \ -- vcpus 2 \ -- Disk Path =/opt/images/win7.img, size = 20 \ -- network: default -- accelerate \ -- VNC -- vncport = 5911 \ -- OS-variant rhel5.4 \ -- CDROM/opt/ISO/windows7.iso-d each parameter has the following meanings: -- name Virtual Machine name -- hvm uses full virtualization (in pairs with para-irtualization). If para and full virtualization lization can be used on a host, connect to the xen hypervisor of a machine without hardware virtualization support. This parameter is invalid, this parameter means to connect to the qemu-based hypervisor-ram virtual machine memory size-vcpus Virtual Machine virtual CPU count-disk Virtual Machine disk (File) path, in this example, use the File Created in step 1 to place the file in the/opt/images directory-network settings. Use the default settings, the default setting is to use the virbr0 bridge -- VNC described in step 1 to set the VNC port to connect to the desktop environment, in this example, the 5911 -- OS-variant directive further optimizes the client (guest) for a specific operating system variant (or version) configure-CDROM to set the path for the optical drive to obtain the virtual optical drive file-d Indicates starting from the optical drive (-c Indicates starting from the hard drive)

After the preceding command is executed, a series of prompts are displayed, mainly including the VM creation progress and Vm configuration based on command parameters. The VM configuration file is stored in XML files. By viewing this process, you can have a more intuitive understanding of the VM creation process. The following is the output of the above command:

Thu, 17 Nov 2011 18:19:49 debug requesting libvirt URI default description: The default uri of the request connection. Based on the KVM hypervisor Uri, It is qemu: // systemthu, 17 nov 2011 18:19:49 debug received libvirt URI 'qemu: // system 'thu, 17 Nov 2011 18:19:49 debug requesting virt method 'hvm 'Description: use full virtualization Thu, 17 nov 2011 18:19:49 debug received virt method 'hvm 'thu, 17 Nov 2011 18:19:49 debug hypervisor name is 'kvm 'thu, 17 Nov 2011 18:19:49 debug parse_disk: returning ('/opt/images/win7.img', none, none, 'disk', none, false, false, 20.0, true, none) Thu, 17 nov 2011 18:19:49 debug setting OS type to 'linux 'For variant' rhel5. 4 'Note: Thu is optimized based on RHEL Linux variants. 17 nov 2011 18:19:49 debug distroinstaller location is a local file/path:/opt/ISO/windows7.isothu, 17 nov 2011 18:19:49 debug setting size for existing storage to '2. 38835525513 'thu, 17 Nov 2011 18:19:49 debug detected storage as type 'file' starting install... thu, 17 Nov 2011 18:19:49 debug setting size for existing storage to '2. 38835525513 'thu, 17 Nov 2011 18:19:49 debug detected storage as type 'file' creating storage file... | 20 GB 18:19:49 Thu, 17 Nov 2011 debug creating guest from: <domain type = 'kvm '> -- Note: The following is the virtual machine configuration file, term "Domain" refers to virtual machine <Name> VM-Win7-2 </Name> <currentmemory> 524288 </currentmemory> <memory> 524288 </memory> <UUID> 8da64796-97aa-a6ae-5ecb-30a312139c19 </UUID>> <type Arch = 'x86 _ 64'> hvm </type> <boot Dev = 'cdrom '/> </OS> <features> <ACPI/> <APIC/> <PAE/> </features> <clock offset = "UTC"/> <on_poweroff> destroy </on_poweroff> <on_reboot> destroy </on_reboot> <on_crash> destroy </on_crash> <vcpu> 2 </vcpu> <devices> <emulator>/usr/libexec/qemu-KVM </emulator> <console type = 'PTY'/> <disk type = 'file' device = 'disk'> <driver name = 'qemu' cache = 'none'/> <source file = '/opt/images/win7.img'/> <target Dev = 'vda' bus = 'virtio '/> </disk> <disk type = 'file' device = 'cdrom'> <source file = '/opt/ISO/windows7.iso' /> <target Dev = 'hdc' bus = 'ide '/> <readonly/> </disk> <interface type = 'network'> <source network = 'default '/> <MAC address = '54: 52: 00: 1b: BC: d0'/> <model type = 'virtio '/> </interface> <input type = 'mouse' bus = 'ps2 '/> <graphics type = 'vnc' Port = '000000' keymap = 'en-us'/> </devices> </domain> creating domain... | 0 B 18:19:50 Thu, 17 Nov 2011 debug created guest, looking to see if it is running Description: after creating a guest, check whether Thu is running, 17 nov 2011 18:19:50 debug launching console callback Thu, 17 Nov 2011 18:19:50 debug saving XML boot config: <domain type = 'kvm '> <Name> VM-Win7-2 </Name> <currentmemory> 524288 </currentmemory> <memory> 524288 </memory> <UUID> 8da64796-97aa-a6ae-5ecb-30a312139c19 </UUID> <OS> <type Arch = 'x86 _ 64'> hvm </type> <boot Dev = 'hd'/> </OS> <features> <ACPI/> <APIC /> <PAE/> </features> <clock offset = "UTC"/> <on_poweroff> destroy </on_poweroff> <on_reboot> restart </on_reboot> <on_crash> restart </on_crash> <vcpu> 2 </vcpu> <devices> <emulator>/usr/libexec/qemu-KVM </emulator> <console type = 'PTY'/> <Disk type = 'file' device = 'disk'> <driver name = 'qemu' cache = 'none'/> <source file = '/opt/images/win7.img'/> <target Dev = 'vda' bus = 'virtio '/> </disk> <disk type = 'file' device = 'cdrom'> <target Dev = 'hdc' bus =' IDE '/> <readonly/> </disk> <interface type = 'network'> <source network = 'default'/> <MAC address = '54: 52: 00: 1b: BC: d0'/> <model type = 'virtio '/> </interface> <input type = 'mouse' bus = 'ps2 '/> <graphics type = 'vnc' Port = '000000' keymap = 'en-us'/> </devices> </domain> (virt-Viewer: 10164): GTK-warning **: cannot open display: Thu, 17 Nov 2011 18:19:53 debug domain state after install: 1 domain installation still in progress. you can reconnect to the console to complete the installation process.

The following error indicates that virt-Viewer cannot connect to VNC through 5911, which is related to VNC settings, but this does not affect the continued installation of the operating system and running of the virtual machine, with this error, we can see that virt-install calls virt-viewer at the end of the production Virtual Machine to open the Virtual Machine GUI and install the operating system. You can use VNC or virt-manager to open the Virtual Machine GUI, continue to install the guest operating system. VNC can install RealVNC.

Connect to the new virtual machine through the VNC client or virt-manager and continue to install the operating system in the Virtual Machine. The installation process is the same as that on the physical machine. After the operating system is installed, You can remotely connect to the system through VNC, just as if you have another machine. If you have installed a Windows operating system, you can also use RDP to connect to the system, windows Remote Desktop Connection.

4. Summary

During virtual machine configuration

1) the above work can be completed through the graphical wizard interface in virt-manager, virt-manager can be opened by entering virt-manager in the terminal or from the menu Application> System Tools

2) You can use the qemu-KVM command to create a VM. [similar functions such as openstack and eucalyptus are provided in the libvirt API) the tool calls this function to create a VM]

The command is as follows:
Qemu-KVM-M 1024-localtime-m PC-SMP 1-drive file =/opt/images/win7.img, cache = writeback, boot = on-net Nic, macaddr = 52: 54: 00: 12: 34: 80-net tap-CDROM/opt/ISO/windows7.iso-boot D-name win7, process = kvm-win7-VNC: 2-USB device Tablet

Parameters are described as follows:
-M 1024: Set the virtual system memory to 1024 MB.
-Localtime: Make the virtual system consistent with the host system time
-M PC: the virtual system type is PC.
-SMP: 1 CPU
-Drive file = windows7_x86.img, cache = writeback, boot = on
Hard Disk option. The virtual disk is windows7_x86.img, And the cache mode is writeback.
-Net Nic, macaddr = 52: 54: 00: 12: 34: 80 Nic option, manually specify the MAC address.
-Net tap type network, equivalent to "Bridge Mode"
-CDROM windows 7.iso Optical Drive
-Boot D startup sequence. D Represents the optical drive.
-Name kvm-win7, process = kvm-win7 name the Virtual Machine for easy identification
-VNC: 2 here is the control window through VNC connection, and here is the port 5902.
-USB device tablet enables the tablet function on the USB device. Enable this function to synchronize the mouse inside and outside the VM

The parameters are basically the same as those of virt-install.
 
After the disk and nic semi-virtualization driver are installed, you can add If = virtio to-drive to use the disk semi-virtualization, add model = virtio to-net Nic to use the semi-virtualization driver of the NIC. Virio Versions later than kenerl 2.6.24 can be used better.

3) In fact, no matter which method is used to create a KVM-based virtual machine, qemu-KVM is used to start and manage the VM process. The following shows a virtual machine process displayed by the liunx process:

Qemu-KVM-S-M rhel5.4.0-M 1024-SMP 2-name VM-win7-UUID Plugin-no-KVM-pit-reinjection-monitor Pty-pidfile/var/run/libvirt/ qemu // VM-ubuntu-1104.pid-boot C-drive file =/opt/images/ubuntu-11.04-desktp.img, if = virtio, Index = 0, boot = on, cache = none
-Drive file =, if = IDE, media = CDROM, Index = 2-net Nic, macaddr = 54: 52: 00: 0a: 55: 05, VLAN = 0, model = virtio-net tap, FD = 17, script =, VLAN = 0, ifname = vnet0-serial Pty-parallel none-USB-VNC 0.0.0.0: 11-k En-US

A virtual machine occupies a process and can manage and control the virtual machine through the process.

4) The related configurations of the virtual machine are saved in a configuration file. Therefore, you can manually write a configuration file and use the virsh command line to build the Virtual Machine. After the configuration file is changed, you need to use virsh define to redefine it before it takes effect.

5) default storage location of VM-related files created based on KVM
KVM virtual machine configuration file location/etc/libvirt/qemu/<--- all XML files
KVM Virtual Machine File Location: var/lib/libvirt/images/<--- you can specify

6) related files and storage locations of the created virtual machine during runtime
When a virtual machine is running, the PID file and configuration file of the virtual machine are stored in the/var/run/libvirt/qemu directory, the configuration file is the same as the Virtual Machine file in the/etc/libvirt/qemu directory. The PID file contains the Virtual Machine Process number. The virtual machine log files are stored in the/var/log/libvirt/qemu directory. Each virtual machine has one file named: Virtual Machine name (or UUID) + ". log"

7) qemu does not use the vncserver in Linux to provide the VNC service. Therefore, if the service is not started, it can also be connected to the virtual machine through the VNC client.

 

The above configuration verification shows that the virtualization on the server is basically the same as the virtualization on the desktop, and a virtual machine is built through the relevant configuration, all types of peripherals of this virtual machine, such as disks, memory, CPU, and nic, are obtained through virtual physical resources, forming a logical computer and saving it through a file. The difference is that the virtualization on the desktop uses software simulation, and there are many implementation mechanisms on the server, from VMware ESX, Microsoft hyper-V, which does not depend on the Operating System (bare metal, KVM integrated with the operating system and bare
Metal mechanism requires an operating system to manage virtual machines, such as xen. These are called hypervisor. In fact, they are transparent to users. They do not need to know how the virtual machines they use are implemented.

 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.