#Environment description: Centos7
Environment preparation:
CPU virtualization is turned on, and enough configuration is allocated to the host’s CPU and memory
Install KVM
1. Install related software packages
yum -y install qemu-kvm libvirt virt-manager virt-install virt-viewer
Introduction to the package:
- qemu-kvm: provides low-level emulation support for kvm
- libvirt: The most used KVM virtualization management tool and application program interface, that is, call KVM to create a virtual machine through libvirt. libvirt is a general access API for KVM. It can not only manage KVM, but also manage VMware, Xen, Hyper-V, virtualBox and other virtualization solutions
- virt-manager: Graphical interface management tool, the bottom layer also calls libvirt API to complete the operation of the virtual machine, including the creation, deletion, start, stop and some simple monitoring functions of the virtual machine, etc.
- virt-install: virtual machine command line installation tool
- virt-viewer: A minimal tool for displaying virtual machine graphical consoles via VNC and SPICE protocols. This tool is in its package of the same name: virtviewer
2. Start the service
systemctl start --now libvirtd
Create a virtual machine
Here are three ways to create a virtual machine:
Before creating, you need to create a directory on the host to store the system image, and then upload it to this directory
mkidr -p /data/isos
1. Use the virt-manager graphical interface to create
#To open graphics, you need to download Xmanager, and enable the following functions in Xshell:
# If the graphical interface is garbled, modify the language to English to solve it
Enter the virt-manager command to open the graphical interface
Create based on ISO image file
browse
select mirror
Set virtual machine memory
set disk size
You can configure it later when installing the Centos7 system
2. Use virt-install to create a virtual machine (manual system installation is required)
Before creating, you need to use the qume-img command to create a virtual disk, as follows:
qemu-img create -f qcow2 /var/lib/libvirt/images/centos7.qcow2 20G
Then use the following command to create a virtual machine:
virt-install --virt-type kvm --name centos7 --ram 1024 --vcpus 2 \ --cdrom=/data/isos/CentOS-7-x86_64-Minimal-2009.iso --disk\ path=/var/lib/libvirt/images/centos7.qcow2 --network=default --graphics\ vnc,listen=0.0.0.0 --noautoconsole --os-variant=centos7.0
The options are parsed as follows:
- –virt-type: Specify the hypervisor name to use (kvm, qemu, xen)
- –name: Specify the name of the kvm virtual machine
- –ram: Specify the memory size of the kvm virtual machine
- –vcpus: Specify the number of CPU cores of the kvm virtual machine
- –cdrom: Specify the path where the ISO image file is located
- –disk: Specify the path where the virtual disk is located
- –network: Specify the network mode of the kvm virtual machine, the default is NAT mode; the bridge mode is –bridge=”network card”
- –graphics: whether to use vnc, none is no
- –noautoconsole: The host does not automatically try to connect to the kvm virtual machine
- –os-variant: specify the system version to install
vnc tool
The default network mode of kvm is NAT, and the host will act as a router to do port mapping for the internal kvm virtual machine. If the vnc connection function is enabled, kvm will open a connection port for each kvm virtual machine. If you can access the device of the host machine, you can use the vnc tool to connect to the kvm virtual machine
The following is the vnc port opened by the host for the kvm virtual machine:
View the started virtual machine:
3. Cooperate with kickstart to realize automatic deployment of kvm virtual machine (no need to manually install the system)
You still need to create a virtual disk before creating it, as follows:
qemu-img create -f qcow2 /var/lib/libvirt/images/centos7.qcow2 20G
Write an answer file:
#Need to create it yourself
vim /root/ks.cfg
#platform=x86, AMD64, or Intel EM64T #version=DEVEL # Install OS instead of upgrade install # Keyboard layouts keyboard 'us' # Root password rootpw --iscrypted $6$Dx1M9/RlV5sB0KJv$19AdCxXsCbBnXSrUbYWhHQvKmtgCd0stJSHb3NrMQXZj.OpZWcNGzOROn/tg6fgxU3HYVptC35dgMcg8rpY.61 # system language #lang zh_CN.UTF-8 lang en_US # System authorization information auth --useshadow --passalgo=sha512 # Use CDROM installation media #cdrom # Use text mode install # Install the operating system using the character interface text # Do not configure the X Window System # Do not configure GUI system skipx # SELinux configuration selinux --disabled # Firewall configuration firewall --disabled # Set to dynamically obtain IP network --bootproto=dhcp --device=eth0 --onboot=on #network --hostname=HOSNAME # Set static IP # network --bootproto=static --device=eth0 --gateway=192.168.122.1 --ip=192.168.122.100 --nameserver=192.168.122.1 --netmask=255.255.255.0 --activate # After the system installation is complete, restart the system #halt reboot # System timezone system time zone timezone Asia/Shanghai # System services The service that starts automatically when the system starts services --enable="chronyd" # System bootloader configuration #System boot mode vda is the name of the first disk when kvm virtual machine bootloader --location=mbr --boot-drive=vda # clear master boot record zerombr # delete the original partition and disk label clearpart --all --initlabel # Disk partition information, here is the lvm automatic partition autopart --type=lvm # The following is the custom partition #part /boot --fstype="xfs" --ondisk=vda --size=200 #part / --fstype="xfs" --ondisk=vda --size=10040 #part /var --fstype="xfs" --ondisk=vda --size=2048 #part /home --fstype="xfs" --ondisk=vda --size=2048 # install software %packages @^minimal @core chrony %end # disable kdump ?don com_redhat_kdump --disable --reserve-mb='auto' %end # The command script executed after the installation system is completed #%post --interpreter=/bin/bash #yum install -y epel-release bash-completion vim-enhanced wget #yum group install "Development Tools" -y #%end # User Password Policy %anaconda pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty %end
#In the above file, rootpw –iscrypted specifies the password of the root account. If you want to change it to another password, you can use the following command to generate an encrypted string and replace it:
python -c 'import crypt,getpass;pw=getpass.getpass();print(crypt.crypt(pw) if (pw==getpass.getpass("Confirm: ")) else exit())' < /pre> <p></p> <p>Run the following command to create a virtual machine:</p> <pre>virt-install -v \ --arch x86_64 --virt-type kvm \ --name centos7\ --memory 1024 \ --vcpus 2 \ --os-type linux\ --location /data/isos/CentOS-7-x86_64-Minimal-2009.iso \ --network default \ --graphics vnc,listen=0.0.0.0 \ --os-variant centos7.0 \ --initrd-inject "/root/ks.cfg" \ --extra-args "ks=file:/ks.cfg \ console=tty0 console=ttyS0,115200n8\ hostname=centos7" \ --disk /var/lib/libvirt/images/centos7.qcow2,cache=writeback,io=threads,bus=virtio
The options are parsed as follows:
- –location: Specify the installation source, here is used to specify the path of the ISO image file
- –os-type: operating system type
- –initrd-inject: specify ks.cfg file path
- –extra-args: When installing the system according to the method specified by location, it is used to pass additional options to the kernel
After installation, enter the account password to log in to the kvm virtual machine:
Account: root
Password: 123.com
Ctrl + } to exit
Clone an existing virtual machine
virt-clone -o centos7 -f /var/lib/libvirt/images/centos7.qcow2 -n centos7-2
Option analysis:
- -o: Specify the name of an existing virtual machine
- -f: Specify the new virtual machine disk file path
- -n: Specifies the name of the new virtual machine
Disk snapshot management
qemu-img manages disk snapshots
Create a snapshot
qemu-img snapshot -c centos7-snapshot1 /var/lib/libvirt/images/centos7.qcow2
Option analysis:
-c: specify the snapshot name
View snapshot
qemu-img snapshot -l /var/lib/libvirt/images/centos7.qcow2
View snapshot details
qemu-img info /var/lib/libvirt/images/centos7.qcow2
Restore snapshot
#Need to be done after shutdown
qemu-img snapshot -a centos7-snapshot /var/lib/libvirt/images/centos7.qcow2
Option analysis:
-a: Specify the name of the snapshot to be restored
Delete snapshot
#Need to be done after shutdown
qemu-img snapshot -d centos7-snapshot /var/lib/libvirt/images/centos7.qcow2
Option analysis:
-d: Specify the snapshot name to be deleted
virsh manages virtual machine snapshots
Create a snapshot
virsh snapshot-create centos7
View snapshot
virsh snapshot-list centos7
Restore snapshot
# No need to shut down
virsh snapshot-revert centos7 --snapshotname 1600593611 --running
Option analysis:
–snapshotname: Check the snapshot first to confirm the name of the snapshot, which is a number, followed by the snapshot name after –snapshotname
–running: After resume, change status to running
Delete snapshot
virsh snapshot-delete centos7 --snapshotname 1600593611
Network management
Configure the virtual machine and the host as a bridged network to achieve network interoperability and connect with Xshell
1) Configure the bridge network of the host:
Modify the host network card file to the following, the mode is empty, NM_CONTROLLED=no, which means that the network is not controlled by NetworkManager, and the bridge name is br0:
BOOTPROTO=none NAME=ens32 DEVICE=ens32 ONBOOT=yes NM_CONTRLLED=no BRIDGE=br0
Create and write the br0 network card file as follows, the mode is static, the type is bridge, not controlled by NM, and the IP is defined:
BOOTPROTO=static TYPE=Bridge NAME=br0 DEVICE=br0 ONBOOT=yes NM_CONTRLLED=no IPADDR=10.0.0.200 PREFIX=24 NETMASK=255.255.255.0 GATEWAY=10.0.0.0.2 DNS=202.106.0.20
Close NetworkManager:
systemctl stop NetworkManager systemctl disable NetworkManager
Restart the network service:
systemctl restart network
Use the brctl show command to view the status of the virtual device. If not, install the package named bridge-utils:
[root@centos7-temp ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c291e1dd4 yes ens32 virbr0 8000.5254006c683e yes virbr0-nic vnet0
#It can be seen from the above that the br0 device is successfully bound to the ens32 network card, stp enabled is no, and you can execute brctl stp br0 on to open it
Use the route command to view routing information:
[root@centos7-temp ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.0.0.2 0.0.0.0 UG 0 0 0 br0
#As can be seen from the above, all addresses, the gateway is 10.0.0.2, and communicate through br0
2) Configure virtual machine bridge network
Modify the /etc/qemu-kvm/bridge.conf file:
allow virbr0 #Add the following line allow br0
Shut down the virtual machine:
virsh shutdown <name>
View the started virtual machine:
virsh list
Modify the network part of the virtual machine configuration file in two places:
vim /etc/libvirt/qemu/centos7.xml
Before the modification is as follows:
change into:
Start the virtual machine:
virsh start <name>
Use virt-manager to open a graphical interface to modify the network card to bridge the network card:
Modify the virtual machine network configuration file:
#Add the commented line
vi /etc/sysconfig/network-scripts/ifcfg-eth0
NAME="eth0" HWADDR="52:54:00:38:8D:43" ONBOOT="yes" NETBOOT="yes" UUID="50682a96-3672-42ac-8469-2c2f20ffde5a" IPV6INIT="yes" BOOTPROTO="static" #Set static IP IPADDR=10.0.0.201 #Select the IP in the same network segment as the host NETMASK=255.255.255.0 #Same network segment as the host GATEWAY=10.0.0.2 #Same gateway as the host DNS=202.106.0.20 #Same DNS as the host TYPE="Ethernet" PROXY_METHOD="none" BROWSER_ONLY="no" DEFROUTE="yes" IPV4_FAILURE_FATAL="no" IPV6_AUTOCONF="yes" IPV6_DEFROUTE="yes" IPV6_FAILURE_FATAL="no"
Close NetworkManager in the virtual machine:
systemctl stop NetworkManager
Restart the network service:
systemctl restart network
3) Verification
Ping the address of the virtual machine on the host to verify whether the network can communicate:
Ping the external network address on the virtual machine to verify whether it can access the external network:
Connect to the virtual machine through the Xshell terminal tool:
Virsh common commands:
View currently started virtual machines: virsh list
View all virtual machines: virsh list –all
Start the virtual machine: virsh start <name>
Graceful shutdown: virsh shutdown <name>
force shutdown::virsh destroy
Suspend a virtual machine:: virsh suspend
Resume a virtual machine: virsh resume
Set the virtual machine to start automatically when the host starts: virsh autostart
Disable the virtual machine from starting when the host starts: virsh autostart
Delete the virtual machine configuration without deleting the disk files: virsh undefine
Delete the virtual machine including disk files: virsh undefine
View virtual machine NIC configuration: virsh domiflist
View the network card address information of the virtual machine: virsh domifaddr