Overview: Introduction to Virtualization Technology (KVM,QEMU), installation of virtual machines, management of virtual machines (Virt-manger,virsh, etc.).
1. Why should there be virtualization technology?
If your main operating system is windows but you need to work on Linux, like VMware (need to pay ...). Or VirtualBox must be your first choice, so you can switch between the two operating systems, except that we have a lot of virtualization technology removed, such as virtual optical drive, we do not need a real optical drive can also use the CD image, these are our daily contact, But not all of virtualization technology, now the more popular Docker container is also a virtualization technology.
In the enterprise can divide the resources of the same machine, the administrator can set up different virtual machines for different customer groups, isolate different blocks for system upgrade and maintenance, and do not affect the operation of the end user at all. Information managers can build a business-and-personal desktop computer system that effectively isolates software from loading and virus attacks, or builds a computer that can perform different operating systems and software to support different needs or older versions of software. Visible in the Enterprise virtualization technology has a very wide range of applications.
Having said so much, let's take a look at whether your computer supports virtualization, open the terminal type grep--color-e "VMX|SVM"/proc/cpuinfo
If you have information to show then congratulations, you can install the virtualization software
Installation of virtualization software
Yum Install QEMU-KVM qemu-img
Yum install Virt-manager libvirt Libvirt-python python-virtinst
2. Installation of virtual machines
And our most friendly and simplest virtualization technology is the virtual machine, here is a brief introduction to the installation of virtual machines.
2.1) Graphical Installation
Locate Virtual Machine Manager in the System tools or open the Virtual Machine Graphics Management window in the terminal input Virt-manager, and select New in the red circle as a single right button to enter the fictitious creation
Enter this interface to select the first option to install with local media
To change the second option for your disk image path, select the Forward
Choose your virtual machine's memory size here
Here Select the size of the hard disk
Here we choose the name of the virtual machine, which is a sign that we recognize the virtual machine.
After these points finally came to the installed interface, there are two options, the first option is to install redhat7.1 directly, the second option is to test the image and then install.
Choose the language to use when installing the system (in order to continue to emphasize the importance of English, I chose English ...) ) is the language of the system when it is installed is not the system itself!
One of the main interfaces of the installation system is top to bottom, left to right order, is the choice of time Zone (Shanghai), the choice of keyboard type (American keyboard), the System of language support (add Chinese, otherwise encountered in Chinese will appear garbled), the installation of the mirror source is what (local disk image), The choice of packages (to quickly select the minimum installation), to which disk to install and the partition of the disk (the default partition of the disk and my own division is no different ...) is generally/boot 200mb,swap for memory twice Times bar, the rest of the full/), Kdump is a kexec based Linux kernel crash capture mechanism, the kernel crash before the memory image is saved, the programmer by analyzing the file to find out the cause of the kernel crash, thus System improvement. Finally, the network settings for the installation process (we choose not to set).
All set up after we enter the installation of the progress bar interface, such as progress bar read, you can restart into this fresh redhat.
2.2) command to install the virtual machine
In Linux everything is a command install virtual machine can also use commands and very convenient
[Root@foundation3 ~]# virt-install--name mo1--ram 1024--file/var/lib/li bvirt/images/mo1.img--cdrom/root/
Desktop/rhel-server-7.1-x86_64-dvd.iso-
-file-size 8
WARNING No operating system detected, VM performance may Suffer. Specify an OS with
--os-variant for optimal results.
WARNING CDROM Media does not print to the text console by default, so you Likel
y would not. You are might want to use--location. The "man"
page for examples of using--location with CDROM media
starting install ...
Allocating ' mo1.img ' | 8.0 GB 00:00
creating domain ... | 0 B 00:00
Connected to Domain Mo1
This--name option is to determine the name of the virtual machine,--ram is the virtual machine memory,--file for you to the virtual machine disk file storage path,--cdrom is your mirror file path,--file-size is the virtual machine disk size. You can enter the interface of the installation system after you type the command, as described in the previous step.
3 Management of virtual machines
Before introducing the virtual machine management commands, let's start by describing what files were created after the virtual machine was built and the system was installed, or what is the virtual machine for our real machine?
[Root@foundation3 ~]# ls/etc/libvirt/qemu/var/lib/libvirt/images/
/etc/libvirt/qemu:
desktop.xml <span style= "COLOR: #ff0000;" >mo.xml</span> Networks Server.xml
/var/lib/libvirt/images/:
mo1.img Rh124-desktop-vda.qcow2 rh124-server-vda.ovl rh124-server.xml
<span style= "color: #ff0000;" >mo.img</span> rh124-desktop-vdb.ovl rh124-server-vda.qcow2 test.sh
mo_shot.qcow2 Rh124-desktop-vdb.qcow2 rh124-server-vdb.ovl vm1.qcow2
rh124-desktop-vda.ovl rh124-desktop.xml Rh124-server-vdb.qcow2
The red word in the output above is the file we generated, a total of two XML files and a hard drive file. The XML file can be deleted at will, not affect the virtual machine, but if we delete him we can not Virt-manager (Virtual machine Graphics management tool to find him), but we have a way to restore the hard disk file if deleted, then your virtual machine can not be used.
Virt-manager # #开启图形管理工具
Virt-viewer Vmname
# #显示以vmname为名字的虚拟机, when we turn on the virtual machine, the default is not to show him in the foreground but only running state, if we want to show him out, we need to type the above command
Virsh List # #列出正在运行的虚拟机 (by default, root users are required to view)
[Root@foundation3 ~]# virsh list
Id Name State
----------------------------------------------------
It means I don't have a virtual machine running, but if I'm not the root, I'm going to show the output as shown above
[Root@foundation3 ~]# virsh start mo
Domain mo started
[root@foundation3 ~]# virsh list
Id Name state< c10/>----------------------------------------------------
9 mo running
[Root@foundation3 ~] # Su-mo last
login:wed Oct 10:48:59 CST 2016 on PTS/3
[mo@foundation3 ~]$ virsh list
Id Name Sta Te
----------------------------------------------------
So be sure to switch to root user use this command otherwise is not valid, but specifically why only the root user can view or other settings are specified in the configuration file (/etc/libvirt/ qemu.conf), speaking of which I came up with an error that I just started installing the virtual machine, as follows:
[Root@foundation3 ~]# virt-install--name Zhao--ram 1024--file/var/lib/libvirt/images/zhao.img--file-size 8--cdrom/ Root/desktop/rhel-server-7.1-x86_64-dvd.iso WARNING No Operating system detected, VM performance may suffer.
Specify an OS with--os-variant for optimal results. WARNING CDROM Media does not print to the text console by default, so you likely would not. You are might want to use--location.
The "man" page for examples of using--location with CDROM media starting install ... Allocating ' zhao.img ' | 8.0 GB 00:00:00 <span style= "color: #ff0000;" >error </span> Internal Error:process exited while connecting to monitor:2016-10-19t03:00:25.552714z QEMU-KVM: -drive File=/root/desktop/rhel-server-7.1-x86_64-dvd.iso,if=none,id=drive-ide0-0-1,readonly=on,format=raw: < Span style= "color: #ff0000;" >could not open disk image/root/desktop/rhel-server-7.1-x86_64-dvd.iso:could not open file:permission denied</sp
An>Domain installation does not appear to have been successful. If It is, can restart your domain by Running:virsh--connect qemu:///system start Zhao, please otherwise
Your installation.
According to the red word above the error, he actually said I do not have permission, I am root good!! I am very angry, as root user also want to see "Permission denied" insult, I first looked at the next mirrored file permissions, as follows:
[Root@foundation3 ~]# ll/root/desktop/rhel-server-7.1-x86_64-dvd.iso
-rw-r--r--1 qemu qemu 3890216960 Oct 17 22:15 /root/desktop/rhel-server-7.1-x86_64-dvd.iso
The result is that I have read and write permission to him, so the permissions are no problem, after careful access to information, I found a solution, the original permissions on the operation of the virtual machine is still in a configuration file of his provisions!
<span style= "color: #ff0000;" > #user = "root" </span>
# The group for QEMU processes run by the system instance. It can be
# specified in a similar way to user.
<span style= "color: #ff0000;" > #group = "root" </span>
# Whether Libvirt should dynamically change file ownership
# to match the Configu Red User/group above. Defaults to 1.
# Set to 0 to disable file ownership changes.
<span style= "color: #ff0000;" > #dynamic_ownership =0 </span>
Remove the red from our root and have permission to create the virtual machine.
Virsh List--all # #列出所有虚拟机 (also requires root)
[Root@foundation3 ~]# virsh list--all
Id Name State
-------------------------------------------------- --- desktop shut off
- mo shut off
- server shut off
Virsh Start Vmname # #开启名为vmname的虚拟机
Virsh Shutdown Vmname # #正常关闭vmname虚拟机
Virsh Destroy Vmname # #强行结束vmname虚拟机
Virsh Create Vmname.xml # #指定vmname虚拟机前端文件 (*.xml) and displayed on the Graphics management tool, which is equivalent to the temporary recovery virtual machine front-end boot option
Virsh Define Vmname.xml # #永久恢复虚拟机前端引导项
Virsh undefine Vmname.xml # #删除虚拟机前端管理, but the hard drive image will not be deleted.
Below is a careful description of the role of this front-boot and some actions:
The Virtual Machine Graphics management interface and Virsh list--all show the same results
We can remove the front-end files by right-clicking on the "Mo" virtual machine and selecting Delete.
You can also delete its front-end files through the Virsh undefine mo command. The principle of this command is to remove the/etc/libvirt/qemu/mo.xml, so you can restore the front-end files as long as you back up the file in advance. The front-end files can then be recovered using Virsh define */mo.xml.
But I don't have a backup. The Mo.xml file can only be generated by reloading the mirror, but it is also very simple.
[Root@foundation3 ~]# virt-install--name mo--ram 1024
Is it the same as the command to create a virtual machine at first, but there are some changes in parameters, here we are equivalent to the film in the identity of an agent erased, regenerated into an identity, so just specify its new name (Can and as before) and what is the true (is the hard disk file) can, and very fast. This will regenerate the front-end files.
4 Virtual Machine Snapshots
All the students who have used VMware know that a virtual machine has a snapshot feature that works very well and is a fundamental guarantee that we can toss around on a virtual machine.
The red line is where the snapshot is taken. Our KVM, of course, has this function. As follows
[Root@foundation3 ~]# qemu-img create-f qcow2-b/var/lib//libvirt/images/mo.img/var/lib/libvirt/images/test.qcow2
formatting '/var/lib/libvirt/images/test.qcow2 ', fmt=qcow2 size=8589934592 backing_file= '/var/lib//libvirt/ Images/mo.img ' Encryption=off cluster_size=65536 lazy_refcounts=off
[Root@foundation3-~]# ls/var/lib/libvirt/ images/
mo1.img rh124-desktop-vdb.ovl rh124-server-vdb.ovl vm1.qcow2 mo.img Rh124-desktop-vdb.qcow2 rh124-server-vdb.qcow2 zhao.img
mo_shot.qcow2 rh124-desktop.xml Rh124-server.xml
rh124-desktop-vda.ovl rh124-server-vda.ovl <span style= "color: #ff0000;" >test.qcow2</span>
rh124-desktop-vda.qcow2 rh124-server-vda.qcow2 test.sh
[Root@foundation3 ~] #
The red part is the snapshot we created, and then we use the aforementioned
[Root@foundation3 ~]# virt-install--name mo--ram 1024
You can use the snapshot, if the snapshot is broken, and then rebuild a snapshot, still the same as the original virtual machine, but remember that the virtual machine real disk files can not be deleted or play bad, because that will not be able to recover.
To summarize the KVM snapshot is actually based on the original virtual machine disk files, take snapshots, each snapshot system and the original virtual machine is the same, so for the original virtual machine changes, the snapshot will have a corresponding change.
Because the management command is too powerful, but a bit of functionality is not really needed, but also lack of such as a direct reset, such as a simple operation, I encapsulated a script to manage the virtual machine, the code is as follows:
#!/bin/bash create_img () {Virsh undefine $ &>/dev/null qemu-img create-f qcow2-b Ges/$1.img/var/lib/libvirt/images/$1_shot.qcow2 &>/dev/null virt-install--name $--ram 1024--disk/var/lib/l Ibvirt/images/$1_shot.qcow2--import--noautoconsole &>/dev/null virsh Define/etc/libvirt/qemu/$1.xml & >/dev/null} delete_img () {Virsh undefine $ &>/dev/null rm-rf/var/lib/libvirt/images/$1_shot.qcow2 &A Mp;>/dev/null} check_status () {status= ' Virsh list--all| sed-n ' 3, $p ' |sed ' s/[[:space:]]\+/:/g ' |grep $ |cut-d
': '-f4 ' echo $status if [$status = "Shut"];then return 0 elif [$status = ' Running '];then return 1 fi} Case is in--start|-s shift echo "start $ ..." Check_status $ &>/dev/null && virsh start
; >/dev/null;; --VIEW|-V) Shift echo "view $ ..." check_status $ &>/dev/null | |
Virt-viewer $ &>/dev/null &; --shutdowN|-D) Shift echo "shutdown $ ..." check_status $ &>/dev/null | |
Virsh destroy $ &>/dev/null &; --CREATE|-C) Shift Check_status $ &>/dev/null | |
Virsh destroy echo "Create snapshot for $ ..." create_img echo "success create snapshot";; --RESET|-R) Shift Check_status &>/dev/null $ | | |
Virsh destroy $ DELETE_IMG $ create_img $;;
--CHECK|-H) shift echo $ ' check_status ';; *) echo "Vmctl <COMMAND> <OPTIONS>" echo "" echo "Open,show,shutdown,reset ... Virtual Machine "ECHO-E"--start|-s\topen virtual Machine\n--view|-v\tshow the virtual Machine that has been "EC Ho-e "--shutdown|-d\tclose Your virtual Machine" echo-e "--create|-c\tcreate a snapshot for your Virtual Machine" echo
-E "--reset|-r\treset your virtual Machine" ECHO-E "--check|-h\tcheck virtual Machine status Running or shut";;
Esac
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.