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

Posted 江湖有缘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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=以上是关于Linux系统下KVM虚拟机的基本管理和操作的主要内容,如果未能解决你的问题,请参考以下文章

虚拟机数据恢复Linux系统下误删除KVM虚拟机的数据恢复案例

Linux之部署KVM虚拟化平台

linux下kvm虚拟机的搭建

Linux系统之虚拟化实战PVE下KVM虚拟机的嵌套安装方法

实现将VirtualBox 虚拟机转换为KVM虚拟机的步骤

KVM虚拟化之基本命令管理虚拟机