背景:当虚拟机的资源耗尽无响应,磁盘只读,虚机如何自救呢?自动重启就好了。虚机怎么检测到资源耗尽和磁盘只读呢?这就借用了Kvm模拟设备的能力,模拟出来一个看门狗设备,虚拟机在资源充足前提下,会不断喂狗,但是资源不足,看门狗奕无法喂狗则被KVM重启。

KVM模拟设备

KVM作为软件实现了许多虚拟机的核心设备。这些模拟硬件设备对于虚拟化操作系统至关重要。仿真设备是完全以软件存在的虚拟设备。

另外,KVM提供了仿真驱动程序。这些形成了虚拟机和Linux内核(管理源设备)之间的转换层。设备级别的指令完全由KVM管理程序翻译。任何可以被Linux内核识别的相同类型的设备(存储设备,网络设备,键盘或鼠标)都可以用作仿真驱动程序的后备源设备。

当机器过载或无响应时,可以使用看门狗来自动重启虚拟机。红帽企业版Linux 7提供了以下仿真看门狗设备:i6300esb,一个仿真的英特尔6300 ESB PCI看门狗设备。它在客户机操作系统Red Hat Enterprise Linux 6.0及以上版本中受支持,并且是推荐使用的设备。ib700,一个仿真的iBase 700 ISA看门狗设备。该ib700监视设备仅在使用红帽企业版Linux 6.2及以上。在来宾操作系统Red Hat Enterprise Linux 6.2及以上版本的i386和x86_64体系结构中,两种看门狗设备均受支持。

一般看门狗的原理

当看门狗启动后,计数器开始自动计数,经过一定时间,如果没有被复位,计数器溢出就会对CPU产生一个复位信号使系统重启(俗称“被狗咬”)。系统正常运行时,需要在看门狗允许的时间间隔内对看门狗计数器清零(俗称“喂狗”),不让复位信号产生。

看门狗一般有软件看门狗和硬件看门狗。对于使用CPU内置的软件看门狗,只有在看门狗守护进程启动后,由守护进程打开看门狗,并根据配置文件监控其他进程的状态来开始喂狗。对于独立的硬件看门狗,系统一上电就必须不断喂狗。

Libvirt watchdong
Libvirt has the ability to configure a watchdog device for KVM / QEMU guests. This device can be used by the guest OS to automatically trigger some action when the guest OS hangs/crashes. There are a variety of actions supported by libvirt / KVM

‘reset’ — default, forcefully reset the guest

‘shutdown’ — gracefully shutdown the guest

‘poweroff’ — forcefully power off the guest

‘pause’ — pause the guest

‘none’ — do nothing

‘dump’ — automatically dump core of the guest。

It is thought that initially the instance flavours should define whether a watchdog device is provided for a guest or not. A new attribute against the flavour object is imagined

watchdog=

where is one of

Flavour configuration

It is thought that initially the instance flavours should define whether a watchdog device is provided for a guest or not. A new attribute against the flavour object is imagined

watchdog=

where is one of

Image properties
Although flavours would define the default watchdog behaviour, it may be desirable to allow this to be overridden on a per-image basis using a property in glance.

glance image-update   --property watchdog=poweroff \

    f16-x86_64-openstack-sda

参考资料
https://www.sysctl.me/2017/11/11/Virtualization/

http://m.blog.chinaunix.net/uid-26611973-id-3380174.html

https://wiki.openstack.org/wiki/LibvirtWatchdog