From eb7be6a6063936041de4a65154e35858e99b3ae7 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Thu, 5 May 2011 17:32:07 +0100 Subject: [PATCH] Add a new 'virsh qemu-attach' command. This command allows libvirt to attach to an existing QEMU instance. $ qemu-kvm -cdrom ~/demo.iso \ -monitor unix:/tmp/demo,server,nowait \ -name foo \ -uuid cece4f9f-dff0-575d-0e8e-01fe380f12ea & $ QEMUPID=$! $ virsh qemu-attach $QEMUPID --- tools/virsh.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++-- tools/virsh.pod | 26 ++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index 3cdf043946..cd17f4249f 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -11552,8 +11552,8 @@ cleanup: * "qemu-monitor-command" command */ static const vshCmdInfo info_qemu_monitor_command[] = { - {"help", N_("Qemu Monitor Command")}, - {"desc", N_("Qemu Monitor Command")}, + {"help", N_("QEMU Monitor Command")}, + {"desc", N_("QEMU Monitor Command")}, {NULL, NULL} }; @@ -11603,6 +11603,52 @@ cleanup: return ret; } +/* + * "qemu-attach" command + */ +static const vshCmdInfo info_qemu_attach[] = { + {"help", N_("QEMU Attach")}, + {"desc", N_("QEMU Attach")}, + {NULL, NULL} +}; + +static const vshCmdOptDef opts_qemu_attach[] = { + {"pid", VSH_OT_DATA, VSH_OFLAG_REQ, N_("pid")}, + {NULL, 0, 0, NULL} +}; + +static bool +cmdQemuAttach(vshControl *ctl, const vshCmd *cmd) +{ + virDomainPtr dom = NULL; + bool ret = false; + unsigned int flags = 0; + unsigned int pid; + + if (!vshConnectionUsability(ctl, ctl->conn)) + goto cleanup; + + if (vshCommandOptUInt(cmd, "pid", &pid) <= 0) { + vshError(ctl, "%s", _("missing pid value")); + goto cleanup; + } + + if (!(dom = virDomainQemuAttach(ctl->conn, pid, flags))) + goto cleanup; + + if (dom != NULL) { + vshPrint(ctl, _("Domain %s attached to pid %u\n"), + virDomainGetName(dom), pid); + virDomainFree(dom); + ret = true; + } else { + vshError(ctl, _("Failed to attach to pid %u"), pid); + } + +cleanup: + return ret; +} + static const vshCmdDef domManagementCmds[] = { {"attach-device", cmdAttachDevice, opts_attach_device, info_attach_device, 0}, @@ -11866,6 +11912,7 @@ static const vshCmdDef hostAndHypervisorCmds[] = { {"nodecpustats", cmdNodeCpuStats, opts_node_cpustats, info_nodecpustats, 0}, {"nodeinfo", cmdNodeinfo, NULL, info_nodeinfo, 0}, {"nodememstats", cmdNodeMemStats, opts_node_memstats, info_nodememstats, 0}, + {"qemu-attach", cmdQemuAttach, opts_qemu_attach, info_qemu_attach}, {"qemu-monitor-command", cmdQemuMonitorCommand, opts_qemu_monitor_command, info_qemu_monitor_command, 0}, {"sysinfo", cmdSysinfo, NULL, info_sysinfo, 0}, diff --git a/tools/virsh.pod b/tools/virsh.pod index d1c285f167..52f1549221 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -131,7 +131,8 @@ group as an option. For example: connect (re)connect to hypervisor freecell NUMA free memory hostname print the hypervisor hostname - qemu-monitor-command Qemu Monitor Command + qemu-attach Attach to existing QEMU process + qemu-monitor-command QEMU Monitor Command sysinfo print the hypervisor sysinfo uri print the hypervisor canonical URI @@ -1519,6 +1520,29 @@ problems to the libvirt developers; the reports will be ignored. =over 4 +=item B I + +Attach an externally launched QEMU process to the libvirt QEMU driver. +The QEMU process must have been created with a monitor connection +using the UNIX driver. Ideally the process will also have had the +'-name' argument specified. + +=over 4 + + $ qemu-kvm -cdrom ~/demo.iso \ + -monitor unix:/tmp/demo,server,nowait \ + -name foo \ + -uuid cece4f9f-dff0-575d-0e8e-01fe380f12ea & + $ QEMUPID=$! + $ virsh qemu-attach $QEMUPID + +=back + +Not all functions of libvirt are expected to work reliably after +attaching to an externally launched QEMU process. There may be +issues with the guest ABI changing upon migration, and hotunplug +may not work. + =item B I I optional I<--hmp> Send an arbitrary monitor command I to domain I through the