English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Overview of virtualization technology, installation and management manual of virtual machine

Overview: Introduction to virtualization technology (kvm, qemu), installation of virtual machines, management of virtual machines (virt-manger, virsh, etc.).

1Why do we need virtualization technology?

If your main operating system is Windows but you need to work on Linux, VMware (which requires payment...) or virtualbox must be your first choice. This way, you can switch between the two operating systems freely. In addition to this, we have also resolved many virtualization technologies; such as virtual CD-ROM, we can use CD image without a real CD-ROM, all of which are what we encounter in daily life, but they are not all of virtualization technology. Now, the relatively popular docker container is also a virtualization technology.

In enterprises, the resources of the same machine can be divided, administrators can set different virtual machines for different customer groups, isolate different blocks for system upgrades and maintenance, without affecting the work of end-users. Information managers can build a commercial and personal desktop computer system, effectively isolate the loading of software and the attack of viruses; or build a computer that can run different operating systems and software to meet different needs or support old software. It is clear that virtualization technology has a wide range of applications in enterprises.

After all this talk, let's first see if your computer supports virtualization. Open the terminal and type grep --color -E "vmx|svm" /proc/cpuinfo

If there is information displayed, congratulations, you can install virtualization software.


Installation of virtualization software

yum install qemu-kvm qemu-img
yum install virt-manager libvirt libvirt-python python-virtinst libvirt-client

2Installation of the virtual machine

The most friendly and simplest virtualization technology we have is the virtual machine, here is a brief introduction to the installation of the virtual machine.

2.1Graphical installation

Find Virtual Machine Manager in System Tools or enter virt in the terminal-manager to open this virtual machine graphical management window, click the right mouse button on the red circle and select NEW to enter virtual creation


Enter this interface and select the first option, use local media to install

  

Modify the second option to the path of your disk image, select it and click Forward


Here, select the memory size of your virtual machine


Here, select the size of the hard disk


Here, select the name of the virtual machine, which is a sign to identify the virtual machine


After these points are completed, we finally arrive at the installation interface, where there are two options, the first option is to install redhat directly7.1The second option is to test the image before installation.


Select the language used to install the system (to emphasize the importance of English again, I chose English...), this is the language when installing the system, not the language of the system itself!


The main interface for installing the system, from top to bottom and left to right, is the selection of time zones (Shanghai), the selection of keyboard types (US keyboard), the language support of the system (add Chinese, otherwise乱码 will appear when Chinese characters are encountered), what the installation image source is (local disk image), the selection of software packages (to install quickly, I chose the minimum installation), where to install and the partition of the disk (the default disk partition is not much different from the one I partitioned myself... generally/boot 200MB, swap is for memory2The rest is all for/), Kdump is a Linux kernel crash capture mechanism based on kexec, which saves the memory image before the kernel crash and allows programmers to analyze the file to find the cause of the kernel crash, thereby improving the system. Finally, there is the network setting of the installation process (we choose not to set it).


After everything is set up, we enter the installation progress bar interface, wait for the progress bar to finish, and then restart to enter this fresh redhat.


2.2)Command to install a virtual machine

Everything is a command in Linux, installing a virtual machine can also be done with commands, and it is very convenient

[root@foundation]3 ~]# virt-install --name mo1 --ram 1024 --文件 /var/lib/li 
bvirt/images/mo1.img --cdrom /root/Desktop/rhel-server-7.1-x86_64-dvd.iso - 
-文件-大小 8 
WARNING No operating system detected, VM performance may suffer. Specify an OS 
with --操作系统-变体以获得最佳结果。 
WARNING CDROM media does not print to the text console by default, so you likel 
y will not see text install output. You might want to use --location.See the man 
 page for examples of using --带有CDROM媒体的安装位置 
开始安装... 
Allocating 'mo'1.img'         | 8.0 GB  00:00 
Creating domain...         | 0 B  00:00 
Connected to domain mo1 

Among them--name option is to determine the name of the virtual machine--ram is the memory of the virtual machine--file is the storage path of the virtual machine's disk file--cdrom is the path of your image file--文件-size is the size of the virtual machine's disk. Enter the command to enter the system installation interface, which has been introduced in the previous step.

3)Virtual Machine Management

Before introducing the virtual machine management commands, let's first introduce what files are generated after the virtual machine is created and the system is installed, or what virtual machines are for us in reality?

[root@foundation]3 ~]# 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 text in the output above is the file we generated, a total of two, one xml file and one hard disk file. The xml file can be deleted at will without affecting the virtual machine, but if it is deleted, we can no longer virt-manager (find it on the virtual machine graphical management tool), but we have methods to restore it; if the hard disk file is deleted, your virtual machine will also be unusable.

virt-manager ## Turn on the graphical management tool
virt-viewer vmname 
## Display the virtual machine named vmname, as we start the virtual machine, it will not be displayed in the foreground by default, but only in the running state. If we want to display it, we need to enter the command above

virsh list ## List the running virtual machines (by default, only the root user can view them)

[root@foundation]3 ~]# virsh list 
 Id Name       State 
---------------------------------------------------- 

This indicates that I have no virtual machines running, but if I am not the root user, the output as shown in the figure above will also be displayed

[root@foundation]3 ~]# virsh start mo 
Domain mo started 
[root@foundation]3 ~]# virsh list 
 Id Name       State 
---------------------------------------------------- 
 9  mo        running 
[root@foundation]3 ~]# su - mo 
Last login: Wed Oct 19 10:48:59 CST 2016 on pts/3 
[mo@foundation3 ~]$ virsh list 
 Id Name       State 
---------------------------------------------------- 

Therefore, it is necessary to switch to the root user to use this command, otherwise it is invalid, but the specific reasons why only the root user can view or other settings are all specified in the configuration file (/etc/libvirt/qemu.conf),说到这里我想到了一个我刚开始安装虚拟机时出现的错误,如下:

[root@foundation]3 ~]# virt-install --名称zhao --ram 1024 --文件 /var/lib/libvirt/images/zhao.img --文件-大小 8 --cdrom /root/Desktop/rhel-server-7.1-x86_64-dvd.iso 
警告:未检测到操作系统,虚拟机性能可能会受到影响。请使用以下命令指定操作系统: --操作系统-变体以获得最佳结果。 
警告:默认情况下,CDROM媒体不会打印到文本控制台,因此您可能看不到文本安装输出。您可能想使用 --位置。请参阅手册页面以获取使用示例 --带有CDROM媒体的安装位置 
开始安装... 
正在分配 'zhao.img'            | 8.0 GB 00:00:00  
<span style="color:#ff0000;">错误 </span> 内部错误:在连接到监控器时进程已退出: 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;">无法打开磁盘镜像 /root/Desktop/rhel-server-7.1-x86_64-dvd.iso:无法打开文件:权限被拒绝</span> 
域安装似乎没有成功。 
如果确实如此,您可以通过运行以下命令来重新启动您的域: 
 virsh --连接qemu:///系统启动zhao 
否则,请重新启动您的安装。 

According to the error message in the red text, it said I didn't have permission, I'm root, aren't I!! I'm very angry. As a root user, I still have to see the insult of 'Permission denied'. First, I checked the permissions of the image file, as follows:

[root@foundation]3 ~]# 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 shows that I have read and write permissions on it, so there is no permission issue. After careful research, I found the solution. It turns out that the permissions for virtual machine operations are still specified in a configuration file!

<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 configured user/group above. Defaults to 1. 
# Set to 0 to disable file ownership changes. 
<span style="color:#ff0000;">#dynamic_ownership =0 </span> 

Remove the # at the red spot and our root has the permission to create virtual machines.

virsh list --all  ## List all virtual machines (also requires root user)

[root@foundation]3 ~]# virsh list --all 
 Id Name       State 
---------------------------------------------------- 
 -  desktop      shut off 
 -  mo        shut off 
 -  server       shut off

virsh start vmname ## Start the virtual machine named vmname
virsh shutdown vmname ## Normally shut down the vmname virtual machine
virsh destroy vmname ## Forcefully terminate the vmname virtual machine
virsh create vmname.xml ## Specify the vmname virtual machine front-end file (*.xml), and display it on the graphical management tool, which is equivalent to temporarily restoring the virtual machine front-end boot options
virsh define vmname.xml ## Permanently restore virtual machine front-end boot item

virsh undefine vmname.xml ## Delete virtual machine front-end management, but will not delete the hard disk image.

The following will carefully explain the role of the front-end boot and some operations:

Virtual Machine Graphical Management Interface and virsh list --all shows the same result

We can right-click on the virtual machine 'mo' and select Delete to delete the front-end file.


You can also delete its front-end file by using the virsh undefine mo command. The principle of this command is to delete/etc/libvirt/qemu/mo.xml, so just back up this file in advance and you can restore the front-end file. Then use virsh define */mo.xml can restore the front-end file.


But I don't have a backup. I can only generate the mo.xml file by reloading the image, which is also very simple.

[root@foundation]3 ~]# virt-install --name mo --ram 1024 --disk /var/lib/libvirt/images/mo.img

Whether it is the same as the command used to create the virtual machine at the beginning, only the parameters have changed. Here we are equivalent to removing an agent's identity in a movie and generating a new one, so we only need to specify its new name (which can be the same as before) and what the real body is (that is, the hard disk file), and it is very fast. This will regenerate the front-end file.


4)Virtual Machine Snapshot

Students who have used vmware know that virtual machines have a very useful snapshot feature, which is also the basic guarantee that we can mess around on virtual machines freely


The red line is where the snapshot is taken. Of course, our kvm also has this feature. As follows

[root@foundation]3 ~]# 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@foundation]3 ~]# 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@foundation]3 ~]# 

The red part is the snapshot we created, and next we use the above-mentioned

[root@foundation]3 ~]# virt-install --name mo --ram 1024 --disk /var/lib/libvirt/images/test.qcow2 
Snapshots can be used. If the snapshot is broken, just create a new snapshot again, and it will still be the same as the original virtual machine. However, please note that the actual disk file of the virtual machine cannot be deleted or damaged, because otherwise it cannot be restored.

In summary, the snapshot of kvm is actually based on the original virtual machine disk file, taking a snapshot. Each snapshot system is exactly the same as the original virtual machine, so any changes to the original virtual machine will also be reflected in the snapshot.

Since the built-in management commands are too powerful, but some features are actually not very necessary, and there are also missing simple one-click operations such as direct reset, I have encapsulated a script to manage virtual machines. The code is as follows:

#!/bin/bash 
create_img() 
{ 
 virsh undefine $1 &>/dev/null 
 qemu-img create -f qcow2 -b /var/lib/libvirt/images/$1.img /var/lib/libvirt/images/$1_shot.qcow2 &>/dev/null 
 virt-install --name $1 --ram 1024 --disk /var/lib/libvirt/images/$1_shot.qcow2 --import --noautoconsole &>/dev/null 
 virsh define /etc/libvirt/qemu/$1.xml &>/dev/null 
} 
delete_img() 
{ 
 virsh undefine $1 &>/dev/null 
 rm -rf /var/lib/libvirt/images/$1_shot.qcow2 &>/dev/null 
} 
check_status() 
{ 
 status=`virsh list --all| sed -n '3,$p'|sed 's/[[:space:]]\+/:/g'|grep $1 |cut -d:' -f4` 
 echo $status 
 if [ $status = "shut" ];then 
  return 0 
 elif [ $status = 'running' ];then 
  return 1 
 fi 
} 
case $1 in 
--start|-s) 
shift 
echo "start $1 ..." 
check_status $1 &>/dev/null && virsh start $1 &>/dev/null 
;; 
--view|-v) 
shift 
echo "view $1 ..." 
check_status $1 &> /dev/null || virt-viewer $1 &>/dev/null & 
;; 
--shutdown|-d) 
shift 
echo "shutdown $1 ..." 
check_status $1 &> /dev/null || virsh destroy $1 &>/dev/null & 
;; 
--create|-c) 
shift 
check_status $1 &> /dev/null || virsh destroy $1 
echo "create snapshot for $1 ..." 
create_img $1 
echo "success create snapshot" 
;; 
--reset|-r) 
shift 
check_status &>/dev/null $1 || virsh destroy $1 
delete_img $1 
create_img $1 
;; 
--check|-h) 
shift 
echo $1 `check_status $1` 
;; 
*) 
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 opened 
echo -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 

That's all for this article. Hope it helps everyone's learning and also hope everyone will support the Yelling Tutorial.

Statement: The content of this article is from the Internet, and the copyright is owned by the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You may also like