One, kvm
Define the
commonly used virtual machine under linux system is kvm (kernel-based virtual machine), which is an open source system virtualization module.
Installation conditions
inter cpu-------->vmx
amd cpu ------------>svm
#Executable cat /proc/cpuinfo to view the cpu type, when linux is inter cpu, and there are vmx instruction-level parameters, kvm can be installed and used with this; if it is amd cpu, svm must be an instruction parameter.
Two, kvm virtualization installation
Preparations: add hard disks to the virtual machine:
shut down the virtual machine and edit the virtual machine settings.
Allocate 6144Mb of memory to the virtual machine.
Assign a processor to it, each processor is assigned 4 cores, choose the virtualization Intel VT-x/EPT or AMD-V/RVI(V) below.
Choose to add a hard disk, pay attention to select the SATA mode, the next step is to select 60 G
Note: Do not add to the c drive
Turn on the virtual machine and configure the software installation environment
in the virtual machine. Build a software warehouse in the vm virtual machine:
select the CD icon in the lower right corner, right-click to select settings, and change the device status to connected
df command to view, edit westos.repo in /etc/yum.repos. d/ and
use dnf command to list hidden groups
dnf group list --hidden
There is a Virtualization Client, Virtualization Hypervisor in the available groups in the hidden (Virtualization core suite), Virtualization Tools (virtualization tools)
Install the above three groups
dnf group install "Virtualization Client" "Virtualization Hypervisor" "Virtualization Tools" -y
virtual machine management tool installation is complete!
Format the hard disk in the virtual machine and mount it to the
fdisk /dev/sdb # partition in the virtual machine storage directory . Except for the first input n and the last input wq, the rest are all Enter
mkfs.xfs /dev/ sdb1 -K #Format the device
mount /dev/sdb1 /var/lib/libvirt/images/ #Directly mount it
to permanently mount the software warehouse, mount /dev/sdb1 /var/ Write lib/libvirt/images/ to /etc/rc.d/rc.local, then give /etc/rc.d/rc.local a permission, chmod a+x /etc/rc.d/rc.local
Three, sakvm virtualization related information
Service name: libvirtd
Open this service: systemctl enable --now libvirtd
Virtualization core: qemu/kvm
Virtualization storage directory (virtual machine hard disk)
/var/lib/libvirt/images/xxx.qcow2
Virtualization hardware information
/etc/libvirt/qemu/xxx.xml
This xxx.xml will record the virtual machine's network card location, Boot partition and other related hardware information.
Four, manually install the virtual machine
Proceed as follows:
Five, virtual machine management commands
virt-viewer test #Display test virtual machine
virt-manager #Open the virtual controller
virsh list #List the running virtual machines
virsh list --all
#List all the virtual machines vish start test #Open the virtual machine
virsh shutdown test #Shut down the virtual machine normally (may be blocked)
virsh destory test #Power off the virtual machine test (will not be blocked)
Six, the virtual machine is transmitted in the linux system
Move the hardware information and hard disk of the virtual machine from their respective locations to another unrelated place (simulate two hosts, move the hardware information and hard disk of the virtual machine of host 1 to another host without virtual machine 2 Above)
Then move the hardware information and hard disk to /var/lib/libvirt/images (simulate in host 2, move the hardware information and hard disk to this directory), you
can open the hardware information through virsh create xxxx.xml Virtual machine, but after destruction, the virtual machine disappears
You can use virsh define xxx.xml to open the virtual machine by opening the hardware information. After the destruction, the virtual machine will not disappear
If you want to delete a virtual machine, enter virsh underfine xxxxx to delete only the hardware information of the virtual machine. You should also delete the hard disk with rm -fr /var/lib/libvirt/qemu/xxxx.qcow2, so that the virtual machine can be completely deleted.
Seven, virtual machine snapshot
qemu-img create #take a snapshot
Give test a snapshot of node1
8. Build a network source to install a virtual machine
dnf install httpd -y
Turn off the firewall and turn on the httpd service
systemctl disable --now firewalld
systemctl enable --now httpd
mkdir /var/www/html/westos
umount /dev/sr0 #
mount /dev/sr0 /var/www/html/westos/ #Mount to the specified shared directory
At this time, you can see the mounted warehouse on the web page:
Edit the network configuration file
nm-connection-editor
nmcli connection reload
nmcli connection down xxxx
nmcli connection up xxxx
ifconfig
cd /var/lib/libvirt/images/
chown qemu.qemu /var/lib/libvirt/images/ #Change all groups to enable the creation of virtual machines (this is the path to the virtual machine hard disk)
virt-manager
is installed via network:
But when using virtualization in Linux, there is no bridged network by default, and the default is NAT mode.
If there is a bridge:
How to build a network bridge to install a virtual machine:
On the real host:
cd /etc/sysconfig/network-scripts/
ls
ifcfg-ens160
cp ifcfg-ens160 ifcfg-br0
vim ifcfg-br0
vim ifcfg -ens160
nmcli connection reload
nmcli connection down xxxx
nmcli connection up xxxx
Check the bridge:
bridge link
At this time, the virtual machine is established and there is a bridging option
But after the bridge is established, the newly created virtual machine cannot get the IP,
so you need to build dhcp on the real host (for details, please review the chapter on network configuration under linux)
dnf install dhcp-server -y
There is no problem during installation at this time
Nine, use the command line to install the virtual machine
Virtual machine installation command line
virt-insatll
Edit the execution document
vim westos.sh
#!/bin/bash #Script specifies the interpreter
virt-install \
#Virtual machine installation command --location http://192.168.1.216/westos \ #Specify installation resources
--os-variant rhel8.2 \
#Specify the installation system version --vcpus 1 \ #cpu core number --memory
2048 \
#Memory size --disk /var/lib/libvirt/images/$1.qcow2,size=8,bus=virtio \ #hard disk location, hard disk size , Hard disk working stack
-name $1 \
#virtual machine name -network bridge=br0,model=virtio & #Virtual network card is a bridge, the working mode of the network card is virtual input and output
$1 #The first string of characters followed by the script
\ #Newline character, there must be a space before each newline character, representing division, do not add anything after the newline character, including spaces
& # can be followed by & to indicate that it will run in the background
Run sh.westos.sh lee
to create a virtual machine named lee