Linux implements Kvm+qemu+libvirt virtual machine environments and uses Virsh to manage virtual machines

Source: Internet
Author: User

Description:
The lab environment used in this article is the VMware Workstation 12.5 Pro running on WINDOWS10, the host operating system is UBUNTU16 (machine name is kvm_test), Kvm+qemu+libvirt is installed on kvm_test. The client operating system running on Kvm_test is also Ubuntu16 (machine name is Test_ubuntu).
The software required for this experiment is an iOS image of VMware Workstation, VNC Viewer, and ubuntu16. VNC Viewer needs a registration code, please Baidu search by yourself.
All the operations and files in this experiment are placed in the/home/joe/directory. If you use a different directory, make your own changes to the commands that appear later. **



Experimental steps:
The first thing you need to do is install the host kvm_test in VMware, and be aware that you want to turn on virtualization in the (Virtual machine settings, hardware-and-processor), as indicated by the arrows. Host installation is not the focus of this experiment, do not do too much to repeat.



After installing the host, it's a good idea to check to see if you are networked, since you'll need to download and install Kvm-qemu and Libvirt later.
After the check is complete, use the commands in the terminal lsmod | grep kvm to see if the hardware supports secondary virtualization. (You may not see any information here if you have not previously turned on virtualization in the virtual machine settings.) The information in the diagram indicates that the CPU supports hardware-assisted virtualization.



Next we need to install QEMU-KVM, using the command

sudo apt-get install qemu-kvm

Installing QEMU-KVM packages may take a long time due to the source of the software.



After QEMU is installed, we will install Libvirt and use the command:

sudo apt-get install libvirt-bin

Libvirt, also wait for some time after the installation succeeds.



After installing Libvirt, we need to install Virt-install
Using commands

sudo apt-get install virtinst

We completed the initial software environment configuration after the installation.



Next we'll divide the Test_ubuntu into a disk space as the hard disk for that virtual machine.
Using commands

qemu-img create -f qcow2 test.qcow2 10G

Where 10G represents the size of the disk space, you can adjust as needed. If the installation of Ubuntu16 such an operating system is best to have more than 10G, or the installation will prompt insufficient space.



Create a new Demo.xml file as a configuration file for the virtual machine
Use the command:

touch demo.xml

Use the command gedit demo.xml to edit the file with the following

<domain type= ' KVM ' > <name>test_ubuntu</name>//virtual machine name <memory>1048576</memory> Max memory, Unit K <currentMemory>1048576</currentMemory>//Available memory, Unit K <vcpu>2</vcpu>//virtual CPU per Number <os> <type arch= ' x86_64 ' machine= ' pc ' >hvm</type> <boot dev= ' cdrom '/>// Disc launcher </os> <features> <acpi/> <apic/> <pae/> </ features> <clock offset= ' localtime '/> <on_poweroff>destroy</on_poweroff> <on_reb oot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <emulator >/usr/bin/kvm</emulator> <disk type= ' file ' device= ' disk ' > <driver name= ' qemu ' type= ' Qco         W2 '/> <source file= '/home/joe/test.qcow2 '/>//Destination image path <target dev= ' hda ' bus= ' ide '/>       </disk>  <disk type= ' file ' device= ' cdrom ' > <source file= '/home/joe/ubuntu16.iso '/>//Disc mirroring path <t Arget dev= ' HDB ' bus= ' ide '/> </disk> <interface type= ' bridge ' >//virtual Machine network connection mode &LT;SOURC        E bridge= ' virbr0 '/>//The name of the current host bridge <mac address= "00:16:3e:5d:aa:a8"/>//Assigning MAC addresses to virtual machines, be sure to be unique, otherwise DHCP gets the same IP, causing conflicts </interface> <input type= ' mouse ' bus= ' ps2 '/> <graphics type= ' vnc ' port= ' autoport= ' ' Yes ' listen = ' 0.0.0.0 ' keymap= ' en-US '/>//vnc method login, port number auto-Assign, Auto plus 1, can be queried by Virsh Vncdisplay </devices> < /domain>



After you have finished writing the configuration file, you need to copy the downloaded Ubuntu16 iOS image to the/home/joe/directory.



Then execute the command.

virsh define demo.xml

Defines the virtual machine and its configuration information.

If there is an error here saying that there is no KVM directory can be found in the configuration file this line parameter has a problem.

<emulator>/usr/bin/kvm</emulator>

Some systems may be directories that are/usr/bin/qemu or/USR/LIBEXEC/QEMU-KVM
You can try it all.



Then execute the command to start the virtual machine

virsh start test_ubuntu

Execute command to view the status of the currently running virtual machine

virsh list

Execute command to see the port number of the virtual machine

virsh vncdisplay test_ubuntu

So we can connect the virtual machine remotely using the VNC viewer (Test_ubuntu)



We also need to know the IP address of the current host (that is, Ubuntu that installs KVM) before using the VNC Viewer to connect to the virtual machine. So we use the command to view its network information as follows.

ifconfig




After viewing the IP address, we can use the previously downloaded VNC Viewer client to connect to the virtual machine (Test_ubuntu).



Since this is the first boot, we need to follow the usual method of installing the UBUNTU16 operating system on the Test_ubuntu.



The installation process and installation on the physical machine no difference, we choose to install Ubuntu, and then the simplest way is to always point continue, the next is a long wait.
Reboot required after installation, click Restart now to restart Test_ubuntu.



After rebooting, the system is the desktop of Ubuntu as familiar as the host.



A general picture, the left is the host kvm_test, the right side is the client Test_ubuntu



After we have successfully accessed the client, we shut down the client first. Then use the Virsh command to make some modifications to the common parameters of the virtual machine.
To use commands in a host:

//关闭客户机


`v940000 //表示将test_ubuntu 的内存设置`为940000KB约为896MB

Open System Monitor in Test_ubuntu to view the machine resources as follows.

Comparing the previous diagram, you can see that the memory has changed from 992MB to 886MB.
(As to why the memory is set to 896MB but the last machine only see 886MB I am not very clear, and so later study clearly updated)


Let's change the number of CPUs and try using the command:

4//设置4个虚拟cpu



An error occurred indicating that the number of CPUs set cannot be greater than the number of CPUs defined. However, it seems that it is not possible to adjust the number of CPUs, the following error occurs.



To improve and reduce the number of CPUs, it seems that only in the shutdown situation, modify the virtual machine configuration file to resolve.
Use the command:

edit

Then change the Vcpus property to 4 and use Ctrl+x to save the exit.



Start the client Test_ubuntu again and view the resource values

start test_ubuntu

and open System Monitor to see that as shown, the number of CPUs has changed from 2 to 4.



Virsh In addition to being able to manage memory and CPUs, there are some other management options, and some of the common command parameters are listed below.

VirshList                    #显示本地活动虚拟机VirshList–all#显示本地所有的虚拟机 (active + inactive)Virsh Define Ubuntu.xml#通过配置文件定义一个虚拟机 (This virtual machine is not active yet)Virsh start Ubuntu#启动名字为ubuntu的非活动虚拟机Virsh Create Ubuntu.xml#创建虚拟机 (once created, the virtual machine executes immediately and becomes the active host)Virsh suspend Ubuntu#暂停虚拟机Virsh Resume Ubuntu#启动暂停的虚拟机Virsh shutdown Ubuntu#正常关闭虚拟机Virsh destroy Ubuntu#强制关闭虚拟机Virsh dominfo Ubuntu#显示虚拟机的基本信息Virsh Domname2               #显示id号为2的虚拟机名Virsh domid Ubuntu#显示虚拟机id号Virsh domuuid Ubuntu#显示虚拟机的uuidVirsh domstate Ubuntu#显示虚拟机的当前状态Virsh dumpxml Ubuntu#显示虚拟机的当前配置文件 (may be different from the configuration when the virtual machine is defined because the virtual machine needs to be assigned ID number, UUID, VNC port number, and so on when the VM starts)Virsh setmem Ubuntu512000    #给不活动虚拟机设置内存大小Virsh Setvcpus Ubuntu4       #给不活动虚拟机设置cpu个数Virsh edit Ubuntu#编辑配置文件 (typically after a virtual machine has just been defined)

parameter Description:
Help displays a description of the command
Quit End Virsh, back to Shell
Connect to the specified virtual machine server
Create to start a new virtual machine
Destroy deleting a virtual machine
Start open (defined) non-bootable virtual machine
Define defining a virtual machine from XML
Undefine de-defined virtual machines
Dumpxml setting values for dump virtual machines
List List virtual machines
Reboot restarting the virtual machine
Save stores the state of the virtual machine
Restore replies to the status of a virtual machine
Suspend pausing the execution of a virtual machine
Resume resumes execution of the virtual machine
Dump dumps the virtual machine's kernel to the specified file for analysis and troubleshooting
Shutdown shutting down a virtual machine
Setmem Modifying the size of memory
Setmaxmem setting the maximum memory value
Setvcpus Modifying the number of virtual processors

Resources:
http://blog.csdn.net/panfengyun12345/article/details/16878873
Http://blog.sina.com.cn/s/blog_999d1f4c0101f00g.html

Linux implements Kvm+qemu+libvirt virtual machine environments and uses Virsh to manage virtual machines

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.