qemu-monitor-command & qemu monitor in openstack




instance-name.monitor的用途
首先我们注意到openstack中创建的每台KVM虚拟机,都会在/var/lib/libvirt/qemu/目录下生成一个instance-name.monitor socket文件
这个文件的用途是什么?
libvirt.xml里面以及用virsh dumpxml命令都看不到这个文件的配置,它是怎么创建出来的?
wangpan@xxyyzz8:~$ sudo ls /var/lib/libvirt/qemu/ l
srwxrxrx 1 libvirtqemu kvm             0 Aug 30  2012 instance000000a4.monitor
srwxrxrx 1 libvirtqemu kvm             0 Sep 17  2012 instance000001ec.monitor
srwxrxrx 1 libvirtqemu kvm             0 Oct  9  2012 instance00000217.monitor
用途:用来查看qemu运行状态以及与qemu进行交互,管理虚拟机,提供类似qemu命令行方式启动虚拟机的与虚拟机的交互操作
怎么创建的:可以查看libvirt的运行时配置文件/var/run/libvirt/qemu/instance-name.xml,打开可以看到如下内容:
<monitor path=’/var/lib/libvirt/qemu/instance-00000611.monitor’ json=’1′ type=’unix’/>
但是我们在openstack生成的libvirt.xml中又看不到这项配置,那它到底是怎么生成的?
普通列表项目看了libvirt的源码,在创建虚拟机的qemuProcessstart()的时候会先准备qemu监控字符设备‘MonitorChr’:
VIR_DEBUG(“Preparing monitor state”);
if (qemuProcessPrepareMonitorChr(driver, priv->monConfig, vm->def->name) < 0)
    goto cleanup;
这里就会增加monitor字符设备,类型为unix domain socket,文件路径为libvirt libdir+vm-name+.monitor,具体代码如下:
int
qemuProcessPrepareMonitorChr(struct qemud_driver *driver,
                             virDomainChrSourceDefPtr monConfig,
                             const char *vm)
{
    monConfig->type = VIR_DOMAIN_CHR_TYPE_UNIX;
    monConfig->data.nix.listen = true;
    if (virAsprintf(&monConfig->data.nix.path, “%s/%s.monitor”,
                    driver->libDir, vm) < 0) {
        virReportOOMError();
        return 1;
    }
    return 0;
}
最后libvirt生成qemu命令行参数的时候会把这个配置加进去:
VIR_DEBUG(“Building emulator command line”);
if (!(cmd = qemuBuildCommandLine(conn, driver, vm->def, priv->monConfig,
                                 priv->monJSON != 0, priv->qemuCaps,
                                 migrateFrom, stdin_fd, snapshot, vmop)))
    goto cleanup;
qemuBuildCommandLine():
……
if (monitor_chr) { /*monitor_chr就是priv->monConfig,所以这里走if分支*/
      char *chrdev;
      /* Use -chardev if it’s available */
      if (qemuCapsGet(qemuCaps, QEMU_CAPS_CHARDEV)) {
          virCommandAddArg(cmd, “-chardev”);
          if (!(chrdev = qemuBuildChrChardevStr(monitor_chr, “monitor”,
                                                qemuCaps)))
              goto error;
          virCommandAddArg(cmd, chrdev);
          VIR_FREE(chrdev);
          virCommandAddArg(cmd, “-mon”);
          virCommandAddArgFormat(cmd,
                                 “chardev=charmonitor,id=monitor,mode=%s”,
                                 monitor_json ? “control” : “readline”);
      } else {
          const char *prefix = NULL;
          if (monitor_json)
              prefix = “control,”;
          virCommandAddArg(cmd, “-monitor”);
          if (!(chrdev = qemuBuildChrArgStr(monitor_chr, prefix)))
              goto error;
          virCommandAddArg(cmd, chrdev);
          VIR_FREE(chrdev);
      }
  }
最终生成的qemu命令行信息为:
-chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/instance-00000611.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control
使用qemu-monitor-command查询、管理虚拟机
libvirt提供了qemu-monitor-command命令行及API,下面以命令行方式为例进行介绍:
$ sudo virsh help qemumonitorcommand
NAME
  qemumonitorcommand QEMU Monitor Command
SYNOPSIS
  qemumonitorcommand <domain> [–hmp] {[–cmd] <string>}…
DESCRIPTION
  QEMU Monitor Command
OPTIONS
  [–domain] <string>  domain name, id or uuid
  hmp            command is in human monitor protocol
  [–cmd] <string>  command
$ sudo virsh qemumonitorcommand ${instancename} hmp ‘info commands’
info balloon   show balloon information
info block   show the block devices
info blockjobs   show progress of ongoing block device operations
info blockstats   show block device statistics
info capture   show capture information
info chardev   show the character devices
info cpus   show infos for each CPU
info history   show the command line history
info irq   show the interrupts statistics (if available)
info jit   show dynamic compiler info
info kvm   show KVM information
info mem   show the active virtual memory mappings
info mice   show which guest mouse is receiving events
info migrate   show migration status
info mtree   show memory tree
info name   show the current VM name
info network   show the network state
info numa   show NUMA information
info pci   show PCI info
info pcmcia   show guest PCMCIA status
info pic   show i8259 (PIC) state
info profile   show profiling information
info qdm   show qdev device model list
info qtree   show device tree
info registers   show the cpu registers
info roms   show roms
info snapshots   show the currently saved VM snapshots
info spice   show the spice server status
info status   show the current VM status (running|paused)
info tlb   show virtual to physical memory mappings
info traceevents   show available traceevents & their state
info usb   show guest USB devices
info usbhost   show host USB devices
info usernet   show user network stack connection states
info uuid   show the current VM UUID
info version   show the version of QEMU
info vnc   show the vnc server status
$ sudo virsh qemumonitorcommand instance00000611 hmp ‘info vnc’
Server:
     address: 114.113.199.8:5900
        auth: none
Client: none
还有一些其他的管理类的操作,这里不一一列举,可参考:
另外在openstack Folsom版本中没有看到有用到这个monitor,没有看到这个Libvirt API被调用,不知道后续会不会有用处?个人感觉应该不会用到,因为这个monitor是libvirt自己生成的,跟openstack其实没有关系。