Linux系统下KVM虚拟机的基本管理和操作

举报
江湖有缘 发表于 2022/10/19 13:07:23 2022/10/19
【摘要】 Linux系统下KVM虚拟机的基本管理和操作

Linux系统下KVM虚拟机的基本管理和操作

一、检查本地环境

1.检查系统版本

[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.检查防火墙状态

[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.检查selinux

[root@serer01 ~]# getenforce 
Disabled

3.检查libvirtd服务状态

[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.检查kvm安装结果

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

5.检查kvm虚拟机状态

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

6.检查virsh版本

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

二、virsh常用命令

1.列出虚拟机

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

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

2.虚拟机开关机操作

virsh start kvm01    #虚拟机开机
virsh shutdown kvm01  #关闭虚拟机
virsh destroy kvm01  # 强制停止虚拟机

3.删除虚拟机

virsh undefine kvm01  #彻底销毁虚拟机,会删除虚拟机配置文件,但不会删除虚拟磁盘

4.设置虚拟机在宿主机开机时自启

virsh autostart kvm01    # 宿主机开启时自启
virsh autostart --disable  kvm01   #  取开机自启

5.挂起虚拟机

virsh suspend  kvm01    # 挂起虚拟机
virsh resume  kvm01       #   恢复挂起的虚拟机

6.查看虚拟机的配置文件

[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.对虚拟机配置文件操作

virsh edit vm-name  #修改虚拟机的配置文件
virsh define file-name.xml  #根据配置文件定义虚拟机

8.查看KVM虚拟机相关配置文件

[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

三、KVM虚拟机的磁盘管理

1.查看磁盘格式

[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.将raw格式磁盘文件转化为qcow2


$ qemu-img convert -f raw -O qcow2  /data/centos7.raw  /data/centos7.qcow
# qemu-img covert -f 源格式 -O 目标格式   源磁盘文件   目标磁盘文件

3.创建空的qcow2格式的磁盘文件

[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.修改镜像文件大小


[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.给虚拟机新增硬盘——临时生效

virsh attach-disk kvm01 /nas/kvm/test.qcow2 vdb --subdriver=qcow2
# virsh  attach-disk  <虚拟机名称>  <新增磁盘路径>  <设备名>  <磁盘格式类型>

6.给虚拟机新增硬盘——永久生效

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

7.卸载硬盘

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

8.查看虚拟机硬件情况

[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.在WebVirtMgr上查看虚拟机硬盘新增情况

image.png

四、KVM虚拟机的网卡管理

1.查看网卡

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

2.新增网卡

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

3.使新增网卡生效

[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.虚拟机查看新增网卡

[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.删除网卡

需要将虚拟机关机再进行删除网卡操作

[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

五、KVM虚拟机的快照管理

1.创建快照

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

2.查看快照列表

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

3.查看快照信息

[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.查看快照目录

[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.删除快照

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

6.恢复快照

virsh snapshot-revert kvm01  1666155063  --running

7.snapshot-revert用法

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

选项:
     [--domain] <string> :指定domain名称,id或者uuid
     [--snapshotname] <string> : 指定快照名称
     --current  :恢复到当前快照
     --running :在恢复快照后自动运行虚拟机
     --paused  :在恢复快照后自动暂停虚拟机
     --force  :强制执行快照,有风险的恢复选项
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。