KVM虚拟化(五)虚拟机日常管理

与虚拟机状态相关的命令

1.查看所有虚拟机状态

[root@kvm ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     vm2                            running
 -     vm1                            shut off

2.开启虚拟机

[root@kvm ~]# virsh start vm2
Domain vm2 started

3.关闭虚拟机

[root@kvm ~]# virsh shutdown vm2
Domain vm2 is being shutdown

3.强制关闭电源

[root@kvm ~]# virsh destroy vm1
Domain vm1 destroyed

4.删除虚拟机–只的配置文件

[root@kvm ~]# virsh undefine vm1
Domain vm1 has been undefined
  1. 编辑虚拟机配置文件

    [root@kvm ~]# virsh edit vm1

    <domain type='kvm'>
      <name>vm1</name>
      <uuid>8a906ecf-f1d5-6200-8d2c-cf80f0cf1d82</uuid>
      <memory unit='KiB'>1048576</memory>
      <currentMemory unit='KiB'>1048576</currentMemory>
      <vcpu placement='static'>2</vcpu>
      <os>
        <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
        <boot dev='hd'/>

故障排查

1.selinux报错

[root@kvm ~]# virsh start vm1
error: Failed to start domain vm1
error: unsupported configuration: Unable to find security driver for label selinux

编辑vm1的配置文件查看是否有SELinux的相关配置

[root@kvm ~]# virsh edit vm1
<domain type='kvm'>
  <name>vm1</name>
  <uuid>8a906ecf-f1d5-6200-8d2c-cf80f0cf1d82</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='rhel6.6.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <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='raw' cache='none'/>
      <source file='/home/kvm/images/vm1.img'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:d3:7f:be'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5910' autoport='no' passwd='123456'/>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
</domain>
~                   

我这里发现没有类似这样的配置

<seclabel type='dynamic' model='selinux' relabel='yes'>
    <label>system_u:system_r:svirt_t:s0:c625,c859</label>
    <imagelabel>system_u:object_r:svirt_image_t:s0:c625,c859</imagelabel>
</seclabel>

则可能已经保存在vm状态中,将原有的状态删除即可(对应路径/var/lib/libvirt/qemu/save),如下:

[root@kvm ~]# virsh  managedsave-remove vm1
Removed managedsave image for domain vm1

2.内存不足,无法启动

[root@kvm ~]# virsh start vm1
error: Failed to start domain vm1
error: Unable to read from monitor: Connection reset by peer