Install KVM and create KVM virtual machine on Galaxy Kirin server system

Install KVM and create KVM virtual machine on Galaxy Kirin server system

    • 1 KVM concept
    • 2. Install KVM components
        • 2.1 Prerequisites for installing KVM
        • 2.2 KVM management tools
        • 2.3 KVM installation
          • 2.3.1 Install kvm components
          • 2.3.2 Start the service and set it to start automatically at boot
    • 3. Create & manage virtual machines
        • 3.1 Create a virtual machine
        • 3.2 Installation process
          • 3.2.1 Select installation method
          • 3.2.2 Select the ISO file to install
          • 3.2.3 Select memory and CPU settings
          • 3.2.4 Configure storage pool
          • 3.2.5 Set the virtual machine name and network
          • 3.2.6 Pre-installation configuration
          • 3.2.7 Start installation
        • 3.3 Installation via virt-install
            • 3.3.1 Parameter introduction
            • 3.3.2 Introduction to raw format and qcow2 disk type
            • 3.3.3 virt-install installation

One KVM concept

? Linux Kernel-based Virtual Machine (KVM) is a Linux open source virtualization software based on hardware virtualization extensions (Intel VT-X and AMD-V) and a modified version of QEMU. There are two implementation modules of KVM, namely: kvm.ko is the infrastructure that provides core virtualization; processor-specific modules kvm-intel.ko and kvm-amd.ko. It is designed to support full hardware emulation when booting multiple unmodified PC operating systems is required. A common Linux process has two operating modes: kernel and user. KVM adds a third mode: guest mode (with its own kernel and user mode). In the kvm model, each virtual machine is a standard process managed by the Linux scheduler.

Kvm consists of two parts: one is a device driver that manages virtual hardware, which uses the character device /dev/kvm as the management interface; the other is a user space component that simulates PC hardware, which is a slightly modified qemu process. Features supported by KVM include:
Supports CPU and memory overcommit
Support paravirtualized I/O (virtio)
Support hot plug (cpu, block device, network device, etc.)
Support symmetric multi-processing (Symmetric Multi-Processing, abbreviated as SMP)
Supports PCI device direct allocation and Single Root I/O Virtualization (SR-IOV)
Supports Kernel Same Page Merging (KSM)
Support NUMA (Non-Uniform Memory Access, non-uniform storage access structure)

2 Install KVM components

2.1 Prerequisites for installing KVM

Need to enable virtual machine support in BIOS

2.2 KVM management tool

Virt-manager graphical management tool

Virsh command line management tool

Virt-clone cloning tool

Virt-install installation tool

2.3 KVM installation
2.3.1 Install kvm components

Execute the following command to install

yum install libvirt* qemu* virt-manager -y
2.3.2 Start the service and set it to start automatically at boot
Start the service:
systemctl start libvirtd
Set up auto-start at boot:
systemctl enable libvirtd

3 Create & manage virtual machines

3.1 Create a virtual machine

*The next steps are very important, otherwise the creation may not be successful*

Execute the command virt-manager to open the KVM graphical management tool

3.2 Installation process
3.2.1 Select installation method

Here select local installation and click File –> New Virtual Machine –> Select local installation media –> Forward. As shown in Figure 1 below.

Figure 1 Select installation method

3.2.2 Select the ISO file to install

Click Browse and select the ISO file –> Forward. As shown in Figure 2 below;

3.2.3 Select memory and CPU settings

Set the memory size and number of CPUs allocated to the virtual machine –> Forward. As shown in Figure 3 below;

3.2.4 Configure storage pool

Method 1: Enable storage for the virtual machine –> Create a disk image for the virtual machine (set the disk space size, the default storage path is /var/lib/libvirt/images) –> Forward;

Method 2: Enable storage for the virtual machine –> Select or create custom storage –> Manage –> Select default storage pool –> Add volume (set volume name, volume size, disk format) –> Select volume –> Forward;

The following example uses method 2 for configuration. As shown in Figure 4 below;

3.2.5 Set the virtual machine name and network

Set the virtual machine name –> Select custom configuration before installation –> Select the network as bridge or NAT (select bridge in the example) –> Complete. As shown in Figure 5 below;

Two ways to configure KVM virtual machine network:

NAT mode and Bridge mode. Bridge mode is suitable for virtualization of server hosts. The NAT method is suitable for virtualization of desktop hosts.

3.2.6 Pre-installation configuration

(1) Add Hardware – Input – Universal USB Keyboard, it cannot be used without adding a keyboard. As shown in Figure 6 below;

(2) Add Hardware – Input – Universal EvTouch USB graphics tablet, which cannot be used without adding a mouse. As shown in Figure 6 below;

(3) Add Hardware – Graphics – Type Select Spice Server

3.2.7 Start installation

Click Start Installation to enter the system installation boot interface. As shown in Figure 8 and Figure 9 below;

Figure 9 Select the installation boot menu to install the system

3.3 Installation through virt-install
3.3.1 Parameter introduction

*Parameter description:*

–name specifies the virtual machine name

–ram allocated memory size

–vcpus allocates the number of CPU cores, the maximum number is the same as the number of CPU cores of the physical machine

–disk specifies the virtual machine image, size specifies the allocation size unit is G

–network network type, the default is used here, generally bridge is used

–accelerate to accelerate

–cdrom specifies the installation image iso

–vnc enables VNC remote management, which must be enabled in general systems.

–vncport specifies the VNC monitoring port. The default port is 5900 and cannot be repeated.

–vnclisten specifies VNC binding IP, the default binding is 127.0.0.1, change it to 0.0.0.0

*General options: Specify the name of the virtual machine, memory size, number of VCPUs and characteristics, etc.; *

? -n NAME, –name=NAME: virtual machine name, needs to be [globally unique];

? -r MEMORY, –ram=MEMORY: the intrinsic size of the virtual machine, in MB;

? –vcpus=VCPUS[,maxvcpus=MAX][,sockets=#][,cores=#][,threads=#]: Number of VCPUs and related configurations;

? –cpu=CPU: CPU mode and features, such as coreduo, etc.; you can use qemu-kvm -cpu ? to obtain the supported CPU mode;

*Installation method: specify the installation method, GuestOS type, etc.;*

? -c CDROM, –cdrom=CDROM: CD installation media;

? -l LOCATION, –location=LOCATION: installation source URL, supports FTP, HTTP and NFS, etc.;

? Such as: http://mirror.centos.org/centos/6.9/os/x86_64/

? –pxe: Complete the installation based on PXE;

? –livecd: Treat the disc as a LiveCD;

? –os-type=DISTRO_TYPE: operating system type, such as linux, unix or windows, etc.;

? –os-variant=DISTRO_VARIANT: Variant of a certain type of operating system, such as rhel5, fedora8, etc.;

? -x EXTRA, –extra-args=EXTRA: Additional options used to pass to the kernel when installing GuestOS according to the method specified by –location;

? For example, specify the location of the kickstart file, –extra-args “ks=http://172.16.0.1/ks.cfg”

? –boot=BOOTOPTS: Specify configuration options after the installation process is completed;

? Such as specifying the boot device sequence, using the specified rather than installed kernel/initrd to boot the system, etc.;

? –boot cdrom,hd,network: Specify the boot sequence;

? –boot kernel=KERNEL,initrd=INITRD,kernel_args=”console=/dev/ttyS0”: Specify the kernel and initrd file to start the system;

? More common in paravirtualization mode, less common in other modes;

?

*Storage configuration: specify storage type, location, attributes, etc.;*

? –disk=DISKOPTS: Specify the storage device and its attributes; the format is –disk /some/storage/path, opt1=val1, opt2=val2, etc.; commonly used options are:

? device: device type, such as cdrom, disk or floppy, etc., the default is disk;

? bus: disk bus type, its value can be ide, scsi, usb, virtio or xen;

? perms: access permissions, such as rw, ro or sh (shared read and write), the default is rw;

? Size: The size of the new disk image, in GB;

? cache: cache model, its values include none, writethrouth (cache read) and writeback (cache read and write);

? format: disk image format, such as raw, qcow2, vmdk, etc.;

? sparse: The disk image uses a sparse format, that is, the specified size of space is not allocated immediately;

? –nodisks: Do not use local disk, commonly used in LiveCD mode;

*Network configuration: Specify the network type of the network interface and interface attributes such as MAC address, driver mode, etc.; *

? -w NETWORK, –network=NETWORK,opt1=val1,opt2=val2: Connect the virtual machine to the host network, where NETWORK can be:

? bridge=BRIDGE: Connect to the bridge device named “BRIDEG”;

? network=NAME: Connect to the network named “NAME”;

? Other commonly used options include:

? Model: The network device model seen in GuestOS, such as e1000, rtl8139 or virtio, etc.;

? mac: fixed MAC address; when this option is omitted, the [random] address will be used, but in any case, for KVM, the first three segments must be 52:54:00;

? It is best not to use random addresses, but to use the random number generator provided by ourselves and generate them for the virtual machine in order to ensure that they will not be repeated.

? There are more than 16 million random numbers (2^24) available, which should be enough. . .

? –nonetworks: The virtual machine does not use network functions;

*Graphics configuration: Define configurations related to virtual machine display functions, such as VNC-related configurations; *

? –graphics TYPE,opt1=val1,opt2=val2: Specify configuration related to graphics display;

? This option does not configure any display hardware (such as a graphics card), but only specifies the interface to access the virtual machine after it is started;

? TYPE: Specify the display type, which can be vnc, sdl, spice or none, etc. The default is vnc;

? port: the port it listens on when TYPE is vnc or spice;

? listen: The IP address to be monitored when TYPE is vnc or spice. The default value is 127.0.0.1. You can define a new default value by modifying /etc/libvirt/qemu.conf;

? Password: When TYPE is vnc or spice, specify the authentication password for the remote access monitoring service;

? –noautoconsole: Disable automatic connection to the virtual machine’s console;

*Device options: specify text console, sound device, serial interface, parallel interface, display interface, etc.;*

? –serial=CHAROPTS: Attach a serial device to the current virtual machine;

? Depending on the device type, different options can be used, the format is “–serial type,opt1=val1,opt2=val2,…”

? –serial pty: Create a pseudo terminal;

? –serial dev,path=HOSTPATH: Attach host device to this virtual machine;

? –video=VIDEO: Specify the graphics card device model, the available values are cirrus, vga, qxl or vmvga;

*Virtualization platform:*

? Virtualization model (hvm or paravirt), simulated CPU platform type, simulated host type, hypervisor type (such as kvm, xen or qemu, etc.) and the UUID of the current virtual machine, etc.;

? -v, –hvm: When the physical machine supports both full virtualization and paravirtualization, specify full virtualization;

? -p, –paravirt: Specify the use of paravirtualization;

? –virt-type: Hypervisor used, such as kvm, qemu, xen, etc.; all available values can be obtained using the ‘virsh capabilities’ command;

*Others:*

? –autostart: Specifies whether the virtual machine starts automatically after physical startup;

? –print-xml: If the virtual machine does not require an installation process (–import, –boot), the generated XML is displayed instead of creating this virtual machine;

? By default, this option still creates a disk image;

? –force: prohibits the command from entering interactive mode. If there is a yes or no option required, the answer is yes automatically;

? –dry-run: Execute the entire process of creating a virtual machine, but do not actually create the virtual machine, change the device configuration information on the host, and notify libvirt of its creation requirements;

? -d, –debug: Display debug information;

Although the virt-install command has many options similar to the above, in actual use, the options it must provide only include –name, –ram, –disk (or –nodisks) and options related to the installation process. In addition, sometimes it is necessary to use the -connect=CONNCT option to specify a connection to a non-default hypervisor. There is no need to provide the “–vcpus” parameter, the default is 1.

3.3.2 Introduction to raw format and qcow2 disk type

(1) raw format

The raw format is the simplest and contains nothing, so it is called raw format. There is not even a header file, it is just a file that is directly read and written by the virtual machine. Raw does not support dynamic growth of space, and the space size must be specified from the beginning. So it consumes quite a lot of disk space.

The raw image format is the format with the best I/O performance for virtual machines. Everyone will refer to raw when using it. The closer the performance is to raw, the better. But raw doesn’t have any other functionality. For the occurrence of sparse files, images such as qcow that allocate space at runtime have no advantage.

(2) qcow2 format

The qcow2 image format is a disk image supported by the QEMU emulator. It can also represent a fixed-size block device disk in the form of a file. Compared with ordinary raw format images, it has the following characteristics:

Smaller space footprint, even if the file system does not support holes;

Supports copy-on-write (COW, copy-on-write), and the image file only reflects changes in the underlying disk;

Supports snapshots, and image files can contain the history of multiple snapshots;

Optional compression method based on zlib;

AES encryption is optional;

qcow2 is a super image format that integrates various technologies. It supports a series of functions such as internal snapshots, encryption, and compression, and its access performance is also constantly improving. But the problem with qcow2 is that it is too bloated and integrates all functions into one.

3.3.3 virt-install installation

(1) Create a bridge network card

If the virtual machine network card is in NAT mode, there is no need to create it.

1) Add the BRIDGE=”br0″ parameter in the network card configuration file. Please configure the NAME, UUID, and DEVICE parameters according to the actual situation. The following examples are for reference only.

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet

BOOTPROTO=static

DEFROUTE=yes

IPV4_FAILURE_FATAL=no

NAME=ens33

UUID=e0d11a2f-6c67-4ead-a36a-847712934acf

DEVICE=ens33

ONBOOT=yes

BRIDGE=”br0″

2) Create the br0 network card. Please modify the specific parameters according to the actual situation. The following example is for reference only.

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0

TYPE=“Bridge”

BOOTPROTO=”static”

DEVICE=”br0″

ONBOOT=“yes”

IPADDR=192.168.146.137

NETMASK=255.255.255.0

GATEWAY=192.168.146.2

(2) Create disk

The following example creates a 30G qcow2 disk file named kylin.img in the /var/lib/libvirt/images directory.

Execute command: qemu-img create -f qcow2 /var/lib/libvirt/images/kylin.img 30G

(3) Create a virtual machine

Please modify the specific parameters according to the actual situation. The following examples are for reference only.

Execute command: virt-install –name=kylin-test –ram 2048 –vcpus=2 –disk path=/var/lib/libvirt/images/kylin.img,format=qcow2,size=30,bus= ide –accelerate –cdrom /root/Kylin-Server-10-SP2-Release-Build09-20210524-x86_64.iso –vnc –vncport=5920 –vnclisten=0.0.0.0 –network bridge=br0,model =virtio –noautoconsole

After the execution is completed, open the virtual system manager and select the installation boot menu to install the system.