Virtualization basics and an introduction to KVM

 Basic management and operation of KVM virtual machine under Linux system

1. Check the local environment

1. Check the system version

[root@serer1 ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

2. Check firewall status

[root@serer1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead) since Wed 2022-10-19 00:09:43 CST; 7s ago
     Docs: man:firewalld(1)
 Main PID: 642 (code=exited, status=0/SUCCESS)

3. Check selinux

[root@serer01 ~]# getenforce 
Disabled

3. Check libvirtd service status

[root@server ~]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2022-10-16 17:53:24 CST; 2 days ago
     Docs: man:libvirtd(8)
           https://libvirt.org
 Main PID: 29472 (libvirtd)
    Tasks: 18 (limit: 32768)
   Memory: 34.8M
   CGroup: /system.slice/libvirtd.service
           └─29472 /usr/sbin/libvirtd --listen

4. Check the kvm installation results

[root@server ~]# lsmod|grep kvm
kvm_intel             183621  4 
kvm                   586948  1 kvm_intel
irqbypass              13503  3 kvm

5. Check the kvm virtual machine status

[root@server ~]# virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------
 9     kvm01                          running

6. Check virsh version

[root@server ~]# virsh --version
4.5.0
[root@server ~]# virt-install --version
1.5.0

Second, virsh common commands

1. List virtual machines

[root@server ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 9     kvm01                          running

[root@server ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 9     kvm01                          running

2. Virtual machine on/off operation

virsh start kvm01    # Virtual Machine Boot
virsh shutdown kvm01  # Close the virtual machine
virsh destroy kvm01  # Force stopping a virtual machine

3. Delete the virtual machine

virsh undefine kvm01  # Destroying the virtual machine completely will delete the virtual machine configuration file, but not the virtual disk

4. Set the virtual machine to start automatically when the host machine is turned on

virsh autostart kvm01    # Self-start when the host is turned on
virsh autostart --disable  kvm01   #  Take the power-on self-start

5. Suspend the virtual machine

virsh suspend  kvm01    # Hanging a virtual machine
virsh resume  kvm01       #  Recovering a hung virtual machine 

6. View the configuration file of the virtual machine

[root@server ~]# virsh dumpxml kvm01 
<domain type='kvm' id='9'>
  <name>kvm01</name>
  <uuid>65438c9d-81cd-cd83-01de-77a5ed5af051</uuid>
  <description>None</description>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
    <boot dev='cdrom'/>
    <bootmenu enable='yes'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <cpu mode='custom' match='exact' check='full'>
    <model fallback='forbid'>IvyBridge-IBRS</model>
    <vendor>Intel</vendor>
    <feature policy='disable' name='ds'/>
    <feature policy='disable' name='acpi'/>
    <feature policy='require' name='ss'/>
    <feature policy='disable' name='ht'/>
    <feature policy='disable' name='tm'/>
    <feature policy='disable' name='pbe'/>
    <feature policy='disable' name='dtes64'/>
    <feature policy='disable' name='monitor'/>
    <feature policy='disable' name='ds_cpl'/>
    <feature policy='disable' name='vmx'/>
    <feature policy='disable' name='est'/>
    <feature policy='disable' name='tm2'/>
    <feature policy='disable' name='xtpr'/>
    <feature policy='disable' name='pdcm'/>
    <feature policy='require' name='movbe'/>
    <feature policy='disable' name='osxsave'/>
    <feature policy='disable' name='arat'/>
    <feature policy='disable' name='tsc_adjust'/>
    <feature policy='require' name='rdseed'/>
    <feature policy='require' name='smap'/>
    <feature policy='require' name='clflushopt'/>
    <feature policy='require' name='clwb'/>
    <feature policy='disable' name='intel-pt'/>
    <feature policy='require' name='sha-ni'/>
    <feature policy='require' name='umip'/>
    <feature policy='require' name='gfni'/>
    <feature policy='disable' name='md-clear'/>
    <feature policy='require' name='stibp'/>
    <feature policy='require' name='ssbd'/>
    <feature policy='require' name='xsaveopt'/>
    <feature policy='require' name='xsavec'/>
    <feature policy='require' name='xgetbv1'/>
    <feature policy='require' name='3dnowprefetch'/>
    <feature policy='disable' name='avx'/>
    <feature policy='disable' name='f16c'/>
    <feature policy='require' name='hypervisor'/>
  </cpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/nas/kvm/centos_kvm.img'/>
      <backingStore/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/storage/kvm/CentOS-7-x86_64-Minimal-1511.iso'/>
      <backingStore/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <alias name='ide0-1-1'/>
      <address type='drive' controller='0' bus='1' target='0' unit='1'/>
    </disk>
    <controller type='usb' index='0' model='piix3-uhci'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
      <alias name='pci.0'/>
    </controller>
    <controller type='ide' index='0'>
      <alias name='ide'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:6c:cb:10'/>
      <source network='br0' bridge='br0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/0'/>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/0'>
      <source path='/dev/pts/0'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='mouse' bus='ps2'>
      <alias name='input0'/>
    </input>
    <input type='tablet' bus='usb'>
      <alias name='input1'/>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='keyboard' bus='ps2'>
      <alias name='input2'/>
    </input>
    <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='dac' relabel='yes'>
    <label>+107:+107</label>
    <imagelabel>+107:+107</imagelabel>
  </seclabel>
</domain>

7. Operation on the virtual machine configuration file

virsh edit vm-name  # Modify the configuration file of the virtual machine
virsh define file-name.xml  # Define the virtual machine according to the configuration file

8. View KVM virtual machine related configuration files

[root@server ~]# ls /etc/libvirt/qemu/
autostart  kvm01.xml  networks
[root@server ~]# ls /etc/libvirt/qemu/networks/
autostart  br0.xml
[root@server ~]# ls /etc/libvirt/qemu/networks/autostart/
br0.xml

3. Disk management of KVM virtual machine

1. Check the disk format

[root@server ~]# qemu-img info /nas/kvm/centos_kvm.img 
image: /nas/kvm/centos_kvm.img
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 2.2G
cluster_size: 65536
Format specific information:
    compat: 0.10

2. Convert the raw format disk file to qcow2


$ qemu-img convert -f raw -O qcow2  /data/centos7.raw  /data/centos7.qcow
# qemu-img covert -f Source format -O Destination format Source disk file Destination disk file

3. Create an empty disk file in qcow2 format

[root@server kvm]# qemu-img create -f qcow2 /nas/kvm/test.qcow2 5G
Formatting '/nas/kvm/test.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@server kvm]# qemu-img info /nas/kvm/test.qcow2 
image: /nas/kvm/test.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

4. Modify the image file size


[root@server kvm]# qemu-img resize ./test.qcow2 +7G
Image resized.
[root@server kvm]# qemu-img info /nas/kvm/test.qcow2 
image: /nas/kvm/test.qcow2
file format: qcow2
virtual size: 12G (12884901888 bytes)
disk size: 200K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

5. Add a new hard disk to the virtual machine - take effect temporarily

virsh attach-disk kvm01 /nas/kvm/test.qcow2 vdb --subdriver=qcow2
# virsh  attach-disk  <virtual machine name> <addition disk path> <device name> <disk format type>

6. Add a new hard disk to the virtual machine - permanent effect

virsh attach-disk kvm01 /nas/kvm/test.qcow2 vdb --subdriver=qcow2  --config

7. Unmount the hard drive

virsh detach-disk kvm01 /nas/kvm/test.qcow2    #卸载临时生效
virsh detach-disk kvm01  /nas/kvm/test.qcow2  --config      # 卸载永久生效

8. Check the virtual machine hardware

[root@server kvm]# virsh dumpxml kvm01 |grep  dev
    <boot dev='hd'/>
    <boot dev='cdrom'/>
  <devices>
    <disk type='file' device='disk'>
      <target dev='vda' bus='virtio'/>
    <disk type='file' device='disk'>
      <target dev='vdb' bus='virtio'/>
    <disk type='file' device='cdrom'>
      <target dev='hda' bus='ide'/>
      <target dev='vnet0'/>
      <source path='/dev/pts/0'/>
    <console type='pty' tty='/dev/pts/0'>
      <source path='/dev/pts/0'/>
  </devices>

9. View new virtual machine hard disks on WebVirtMgr



Four, KVM virtual machine network card management

1. Check the network card

[root@server kvm]# virsh domiflist kvm01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:6c:cb:10

2. Add network card

[root@server kvm]# virsh attach-interface kvm01 --type bridge --source br0 
Interface attached successfully

3. Make the new network card take effect

[root@server kvm]# virsh dumpxml kvm01 > /etc/libvirt/qemu/kvm01.xml 
[root@server kvm]# virsh define /etc/libvirt/qemu/kvm01.xml 
Domain kvm01 defined from /etc/libvirt/qemu/kvm01.xml

4. View the newly added network card in the virtual machine

[root@server kvm]# ssh root@192.168.3.169
The authenticity of host '192.168.3.169 (192.168.3.169)' can't be established.
ECDSA key fingerprint is SHA256:SMUWtN77SI+zUIX2bBR7/hY176zJLiWvY7KpIUlK0dw.
ECDSA key fingerprint is MD5:df:58:7e:61:73:82:b5:56:03:25:60:f1:80:a6:b1:0b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.3.169' (ECDSA) to the list of known hosts.
root@192.168.3.169's password: 
Last login: Tue Oct 18 23:19:15 2022 from 192.168.3.11
[root@serer01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:6c:cb:10 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.169/24 brd 192.168.3.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe6c:cb10/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:5d:4a:c4:9f brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:5dff:fe4a:c49f/64 scope link 
       valid_lft forever preferred_lft forever
5: veth0604e80@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether be:7f:bd:57:88:71 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::bc7f:bdff:fe57:8871/64 scope link 
       valid_lft forever preferred_lft forever
6: ens7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 52:54:00:45:1b:53 brd ff:ff:ff:ff:ff:ff

5. Delete the network card

You need to shut down the virtual machine and then delete the network card

[root@server kvm]# virsh detach-interface kvm01 --type bridge --mac 52:54:00:45:1b:53 --config
Interface detached successfully

[root@server kvm]# virsh dumpxml kvm01 > /etc/libvirt/qemu/kvm01.xml
[root@server kvm]# virsh define /etc/libvirt/qemu/kvm01.xml
Domain kvm01 defined from /etc/libvirt/qemu/kvm01.xml

[root@server kvm]# virsh domiflist kvm01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
-          bridge     br0        virtio      52:54:00:6c:cb:10

5. Snapshot management of KVM virtual machine

1. Create a snapshot

[root@server kvm]# virsh snapshot-create kvm01
Domain snapshot 1666155063 created

2. View the snapshot list

[root@server kvm]# virsh snapshot-list kvm01
 Name                 Creation Time             State
------------------------------------------------------------
 1666155063           2022-10-19 12:51:03 +0800 shutoff

3. View snapshot information

[root@server kvm]# virsh snapshot-info kvm01 --current 
Name:           1666155063
Domain:         kvm01
Current:        yes
State:          shutoff
Location:       internal
Parent:         -
Children:       0
Descendants:    0
Metadata:       yes

4. View snapshot directory

[root@server kvm]#  ll /var/lib/libvirt/qemu/snapshot/
total 0
drwxr-xr-x 2 root root 28 Oct 19 12:51 kvm01
[root@server kvm]#  ll /var/lib/libvirt/qemu/snapshot/kvm01/
total 8
-rw------- 1 root root 5550 Oct 19 12:51 1666155063.xml

5. Delete snapshots

[root@server kvm]#  virsh snapshot-delete kvm01 1666155063
Domain snapshot 1666155063 deleted

6. Restoring a Snapshot

virsh snapshot-revert kvm01  1666155063  --running

7. snapshot-revert usage

snapshot-revert用法:snapshot-revert <domain> [<snapshotname>] [–current] [–running] [–paused] [–force]

Options.
     [--domain] <string> : Specify the domain name, id or uuid
     [--snapshotname] <string> : Specify the snapshot name
     --current : Restore to the current snapshot
     --running : Automatically run the virtual machine after resuming the snapshot
     --paused : Automatically suspend the virtual machine after resuming the snapshot
     --force : force snapshot, risky recovery option
Post a Comment (0)
Previous Post Next Post