5.2. Install a virtual machine on Ubuntu
The process of installing a virtual machine on Ubuntu is similar to that on CentOS, so I will briefly talk about it here.
The first step is to create a virtual machine image file
Use the command "qemu-img" to create a blank virtual machine image, the format is qcow2, the image name is lisq.qcow2, and the image size is 20G, as shown below:
root@kvm02:~# qemu-img create -f qcow2 lisq.qcow2 20G
Formatting 'lisq.qcow2', fmt=qcow2 size=21474836480 cluster_size=65536 lazy_refcounts=off refcount_bits=16
The -f option is used to specify the image format, there are two formats of raw and qcow2, and even VMware's vmdk, vdi, vhd and other formats. The qcow2 format is the most commonly used image format for Qemu. qcow2 uses copy-on-write technology to optimize performance. Here I use the qcow2 format. lisq.qcow2 is the name of the image file, and 20G is the size of the image file. (qcow2 will bring speed advantages compared to raw)
Use the command "ls" to view the information of the lisq.qcow2 image, as shown below:
root@kvm02:~# ls -l lisq.qcow2 # 分配了20G空间
-rw-r--r-- 1 root root 196928 10月 20 15:59 lisq.qcow2
Use the command "du -h" to view the space used by the lisq.qcow2 image, as follows:
root@kvm02:~# du -h lisq.qcow2 # 虽然分配了20G空间,但是当前还没有使用
196K lisq.qcow2
The second step is to upload the operating system
Install the WinSCP tool on the local computer. WinSCP uses port 22 to upload, so you need to ensure that the SSH service of the host kvm02 is enabled remotely, as shown in Figure 1.
Note that I have not created a root user on Ubuntu, so I can only log in as the "lisq" user. After successful login, the interface shown in Figure 2 will be displayed. The left window of the interface is the window of the local computer, and the right window is the window of the host machine kvm02.
In the local computer window on the left, select an operating system image, drag it directly to the host kvm02 window on the right, and upload the operating system image to the host kvm02, as shown in Figure 3.
After the upload is complete, we will see the host kvm02 window on the right showing the uploaded operating system ISO, which can also be viewed through "ls", as shown below:
root@kvm02:~# ls -l /home/lisq/
总用量 2999972
drwxr-xr-x 2 lisq lisq 4096 10月 17 18:45 公共的
drwxr-xr-x 2 lisq lisq 4096 10月 17 18:45 模板
drwxr-xr-x 2 lisq lisq 4096 10月 17 18:45 视频
drwxr-xr-x 2 lisq lisq 4096 10月 17 18:45 图片
drwxr-xr-x 2 lisq lisq 4096 10月 17 18:45 文档
drwxr-xr-x 2 lisq lisq 4096 10月 17 18:45 下载
drwxr-xr-x 2 lisq lisq 4096 10月 17 18:45 音乐
drwxr-xr-x 2 lisq lisq 4096 10月 17 18:45 桌面
-rw-r--r-- 1 lisq lisq 3071934464 9月 8 09:38 ubuntu-20.04.3-desktop-amd64.iso
Move the ubuntu-20.04.3-desktop-amd64.iso from /home/lisq/ to /root/ using the "mv" command, as follows:
root@kvm02:~# mv /home/lisq/ubuntu-20.04.3-desktop-amd64.iso /root/
root@kvm02:~# pwd
/root
root@kvm02:~# ls
lisq.qcow2 snap ubuntu-20.04.3-desktop-amd64.iso
The third step is to create a virtual machine
Use the command "qemu-system-x86_64" to create a virtual machine and install the operating system ISO just uploaded. There are two ways to create a virtual machine. One way is to create a virtual machine locally on the host kvm02, that is, to create a virtual machine locally. Another way is to use Xshell to create a virtual machine remotely. In the remote way, Xmanager software needs to be installed. Here the virtual machine is created locally.
Open the terminal on the desktop of the host machine kvm02, and use the command "qemu-system-x86_64" to create a virtual machine, as shown in Figure 4.
root@kvm02:~# qemu-system-x86_64 -enable-kvm -m 6G -smp 4 -cdrom ubuntu-20.04.3-desktop-amd64.iso lisq.qcow2 # qemu-kvm自带工具,添加-enable-kvm选项
-m specifies the memory size of the virtual machine, the default unit is MB, -smp 4 specifies that the virtual machine is a symmetric multiprocessor structure and allocates 4 vCPUs, and -cdrom allocates the virtual machine CD-ROM. Friendly reminder, it will be uncomfortable if the memory allocation of the virtual machine is less than 6G, 6G is the bottom line, otherwise this kind of experiment based on VMware virtual machine simulation host kvm01 cannot be done. Note that the KVM acceleration item "-enable-kvm" still needs to be added here. I used Ubuntu's KVM virtualization method 1 to build it. For details, see the figure in "KVM Virtualization Solution Series - KVM Deployment (3-4)" 1.
The fourth step is to access the QEMU window
After executing the command to create a virtual machine, you can automatically access the QEMU window in the host kvm02 interface, as shown in Figure 5. Note that the Ubuntu system can automatically access the QEMU window, while the CentOS system also requires VNC access to the QEMU window.
Note that we have added the "-enable-kvm" option to the qemu-system-x86_64 tool, and then we can use KVM to drive the virtual machine. Otherwise, using pure soft virtualization qemu to drive the virtual machine is so inefficient that he vomits blood. After accessing the QEMU window, we can press the key combination "Ctrl+Alt+2" in the QEMU window to switch to the QEMU monitor window (Ctrl+Alt+1 to switch back to the normal client), and execute the command "info kvm" to view the current Whether QEMU uses KVM to drive the virtual machine, as shown in Figure 6, is displayed as using KVM to start the virtual machine (kvm support: enabled).
If we do not add the "-enable-kvm" option in the qemu-system-x86_64 tool, then this QEMU cannot use KVM to drive the virtual machine, it just uses QEMU to drive the virtual machine, and the efficiency is much lower than that of the KVM driver, such as Figure 7 shows.
The fifth step, install the operating system
The efficiency of QEMU's virtual machine is too low. Even if the "-enable-kvm" option is added to the qemu-system-x86_64 tool, even if the virtual machine is allocated 6G memory, the efficiency is still low. Press Crtl+C to cancel the installation detection and speed up the installation, as shown in Figure 8.
The virtual machine operating system is being installed, as shown in Figure 9. The installation process in the middle is omitted, and the efficiency of the entire installation process is very low, which takes about two or three hours.
The sixth step, restart the virtual machine system
After the system installation is complete, the virtual machine needs to be restarted to use the newly installed system, as shown in Figure 10.
Like the normal Linux system installation, after the installation is complete, restart the system to enter the virtual machine operating system just installed, as shown in Figure 11, after removing the installation media, press the Enter key to start the operating system from the hard disk .
We return to the Xshell interface of the kvm02 host and remove the operating system we uploaded, as shown below:
root@kvm02:~# ls
lisq.qcow2 snap ubuntu-20.04.3-desktop-amd64.iso
root@kvm02:~# mv /root/ubuntu-20.04.3-desktop-amd64.iso /home/lisq/
root@kvm02:~# ls
lisq.qcow2 snap
Then return to the red interface in Figure 11, press the Enter key, and the virtual machine system starts to restart. The startup process is too slow. With 6G memory of the virtual machine, the startup time is almost half an hour. After vomiting blood, the virtual machine operating system is finally started successfully, as shown in Figure 12.
After the virtual machine starts successfully, we log in to the virtual machine, and the login is successful, as shown in Figure 13.
The seventh step, start the virtual machine
After the virtual machine system is installed, you can use the image file to start and log in to the system you installed. You can use the following command to start a virtual machine, as shown in Figure 14.
root@kvm02:~# qemu-system-x86_64 -enble-kvm -m 6G -smp 4 /root/lisq.qcow2
After a long wait, the virtual machine finally started successfully, as shown in Figure 15.
After the virtual machine is successfully started, the interface for successful login is shown in Figure 16.