The full name of KVM is Kernel-Based Virtual Machine. In other words, KVM is implemented based on the Linux kernel.
KVM has a kernel module called kvm.ko, which is only used to manage virtual CPU and memory.
Then who will realize the virtualization of IO, such as storage and network equipment?
This is done by the Linux kernel and Qemu.
To put it bluntly, as a hypervisor, KVM itself only focuses on virtual machine scheduling and memory management. The task of IO peripherals is handed over to the Linux kernel and Qemu
Check whether the cpu supports virtualization
[ root@localhost ~]# grep -E '(vmx|svm)'/proc/cpuinfo **
Install qemu-kvm (user mode management tool), libvirt (command line management tool), virt-install (install kvm tool), bridge-utils (bridge device management tool)
[ root@localhost ~]# yum install -y qemu-kvm libvirt virt-install bridge-utils
Make sure to load the kvm module
[ root@localhost ~]# lsmod |grep kvm
kvm_intel 1748410
kvm 5785181 kvm_intel
irqbypass 135031 kvm
# If it is not loaded, run the command
[ root@localhost ~]# modprobe kvm
[ root@localhost ~]# modprobe kvm-intel
Start libvirtd service
[ root@localhost ~]# systemctl enable libvirtd
[ root@localhost ~]# systemctl start libvirtd
[ root@localhost ~]# systemctl status libvirtd
Configure kvm bridge mode
[ root@localhost ~]# cd /etc/sysconfig/network-scripts/[root@localhost network-scripts]# cp ifcfg-ens32 ifcfg-br0
[ root@localhost network-scripts]# vim ifcfg-br0
NAME=br0
DEVICE=br0
ONBOOT=yes
NETBOOT=yes
IPV6INIT=no
BOOTPROTO=static
NM_CONTROLLED=no
TYPE=Bridge
IPADDR=192.168.0.127
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=8.8.8.8[root@localhost network-scripts]# vim ifcfg-ens32
NAME=ens32
DEVICE=ens32
BOOTPROTO=none
NM_CONTROLLED=no
ONBOOT=yes
BRIDGE=br0
View bridge
[ root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29d1267b no ens32
virbr0 8000.52540063d8f4 yes virbr0-nic
Delete virbr0
[ root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29d1267b no ens32
virbr0 8000.52540063d8f4 yes virbr0-nic
[ root@localhost ~]# virsh net-list
Name State Autostart Persistent
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - default active yes yes
[ root@localhost ~]# virsh net-destroy default
Network default destroyed
[ root@localhost ~]# virsh net-undefine default
Network default has been undefined
[ root@localhost ~]# systemctl restart libvirtd.service
[ root@localhost ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c29d1267b no ens32
Because you want to install the virtual machine graphically with virt-manager, you also need to install the desktop
[ root@localhost ~]# yum grouplist
[ root@localhost ~]# yum groupinstall "GNOME Desktop"-y
Configure Chinese Desktop
grep -E "^[ \t]*export[ \t]+LANG[ \t]*="/etc/profile&& y="yes"|| y="no"if[["$y"="yes"]]; then
sed -r -i -e '/^[ \t]*export[ \t]+LANG[ \t]*=/c\export LANG="zh_CN.UTF-8"'/etc/profile
else
echo 'export LANG="zh_CN.UTF-8"'>>/etc/profile
fi
source /etc/profile
Install virt-manager
[ root@localhost ~]# yum -y install virt-manager
xshell link
[ root@localhost ~]# virt-manager
Store the ISO system image in a directory on the server in advance, such as /data/iso
[ root@localhost ~]# mkdir -p /data/{iso,kvmstorage}[root@localhost ~]# cd /data/iso/[root@localhost iso]# ll
Total amount 4217748-rw-r--r--1 root root 95000985611 2215:24 CentOS-7-x86_64-Minimal-1804.iso
- rw-r--r--1 root root 33689620483 month 232012 cn_windows_server_2008_r2.iso
New virtual machine
The management of KVM virtual machine is mainly through the virsh command to manage the virtual machine
Command help
[ root@localhost ~]# virsh --help
View virtual machine status
[ root@localhost ~]# virsh list --all
Id name status
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 4 win2k8r2 running
Shutdown
[ root@localhost ~]# virsh shutdown win2k8r2
Force power off
[ root@localhost ~]# virsh destroy win2k8r2
Create a virtual machine from a configuration file
[ root@localhost ~]# virsh create /etc/libvirt/qemu/win2k8r2.xml
Set the virtual machine to start automatically
[ root@localhost ~]# virsh autostart win2k8r2
[ root@localhost ~]# ll /etc/libvirt/qemu/autostart/
Total amount 0
lrwxrwxrwx 1 root root 301, 2413:06 win2k8r2.xml ->/etc/libvirt/qemu/win2k8r2.xml
Virtual machine configuration files everywhere
[ root@localhost ~]# virsh dumpxml win2k8r2 >/etc/libvirt/qemu/win2k8r2_bak.xml
Delete the virtual machine (this command only deletes the configuration file, not the disk file)
[ root@localhost ~]# virsh undefine win2k8r2
Restore the definition of the original KVM virtual machine by exporting the backup configuration file, and redefine the virtual machine.
[ root@localhost ~]# mv /etc/libvirt/qemu/win2k8r2_bak.xml /etc/libvirt/qemu/win2k8r2.xml
[ root@localhost ~]# virsh define /etc/libvirt/qemu/win2k8r2.xml
Edit configuration file
[ root@localhost ~]# virsh edit win2k8r2
Hang
[ root@localhost ~]# virsh suspend win2k8r2
restore
[ root@localhost ~]# virsh resume win2k8r2
Create storage volume
[ root@localhost ~]# qemu-img create -f qcow2 /data/kvmstorage/centos7.qcow2 20G
Formatting '/data/kvmstorage/centos7.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off
[ root@localhost ~]# ll //data/kvmstorage
Total amount 7437168-rw-r--r--1 root root 1971 January 2413:21 centos7.qcow2
- rw-------1 qemu qemu 42956488704 January 2413:21 win2k8r2.qcow2
Generate virtual machine
[ root@localhost ~]# virt-install --virt-type kvm --name centos --ram 1024 \
- - disk /data/kvmstorage/centos7.qcow2,format=qcow2 \
- - network bridge=br0 \
- - graphics vnc,listen=0.0.0.0--noautoconsole \
- - os-type=linux --os-variant=rhel7 \
- - location=/data/iso/CentOS-7-x86_64-Minimal-1804.iso
[ root@localhost ~]# virsh list --all
Id name status
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 5 win2k8r2 running
7 centos running
Recommended Posts