Hi This patch adds virsh attach-device/detach-device. Currently it supports a definition of a device by XML(proposal 2). I plan to submit command option version (proposal 1) later. Any comments are welcome! Signed-off-by: Masayuki Sunou <fj1826dm@xxxxxxxxxxxxxxxxx> Thanks, Masayuki Sunou. -------------------------------------------------------------------------------- Index: src/virsh.c =================================================================== RCS file: /data/cvs/libvirt/src/virsh.c,v retrieving revision 1.75 diff -u -p -r1.75 virsh.c --- src/virsh.c 3 May 2007 16:03:02 -0000 1.75 +++ src/virsh.c 16 May 2007 00:56:21 -0000 @@ -2403,6 +2403,139 @@ cmdVNCDisplay(vshControl * ctl, vshCmd * return ret; } +/* + * "attach-device" command + */ +static vshCmdInfo info_attach_device[] = { + {"syntax", "attach-device <domain> <file> "}, + {"help", gettext_noop("attach device from an XML file")}, + {"desc", gettext_noop("Attach device from an XML <file>.")}, + {NULL, NULL} +}; + +static vshCmdOptDef opts_attach_device[] = { + {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("domain name, id or uuid")}, + {"file", VSH_OT_DATA, 0, gettext_noop("XML file")}, + {NULL, 0, 0, NULL} +}; + +static int +cmdAttachDevice(vshControl * ctl, vshCmd * cmd) +{ + virDomainPtr dom; + char *from; + char buffer[BUFSIZ]; + int ret = TRUE; + int found; + int fd, l; + + if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) + return FALSE; + + if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL))) + return FALSE; + + from = vshCommandOptString(cmd, "file", &found); + if (!found) { + virDomainFree(dom); + return FALSE; + } + + fd = open(from, O_RDONLY); + if (fd < 0) { + vshError(ctl, FALSE, _("Failed to read description file %s"), from); + virDomainFree(dom); + return FALSE; + } + l = read(fd, &buffer[0], sizeof(buffer)); + if ((l <= 0) || (l >= (int) sizeof(buffer))) { + vshError(ctl, FALSE, _("Failed to read description file %s"), from); + close(fd); + virDomainFree(dom); + return FALSE; + } + buffer[l] = 0; + + ret = virDomainAttachDevice(dom, &buffer[0]); + if (ret < 0) { + vshError(ctl, FALSE, _("Failed to attach device from %s"), from); + close(fd); + virDomainFree(dom); + return FALSE; + } + + close(fd); + virDomainFree(dom); + return TRUE; +} + + +/* + * "detach-device" command + */ +static vshCmdInfo info_detach_device[] = { + {"syntax", "detach-device <domain> <file> "}, + {"help", gettext_noop("detach device from an XML file")}, + {"desc", gettext_noop("Detach device from an XML <file>")}, + {NULL, NULL} +}; + +static vshCmdOptDef opts_detach_device[] = { + {"domain", VSH_OT_DATA, VSH_OFLAG_REQ, gettext_noop("domain name, id or uuid")}, + {"file", VSH_OT_DATA, 0, gettext_noop("XML file")}, + {NULL, 0, 0, NULL} +}; + +static int +cmdDetachDevice(vshControl * ctl, vshCmd * cmd) +{ + virDomainPtr dom; + char *from; + char buffer[BUFSIZ]; + int ret = TRUE; + int found; + int fd, l; + + if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) + return FALSE; + + if (!(dom = vshCommandOptDomain(ctl, cmd, "domain", NULL))) + return FALSE; + + from = vshCommandOptString(cmd, "file", &found); + if (!found) { + virDomainFree(dom); + return FALSE; + } + + fd = open(from, O_RDONLY); + if (fd < 0) { + vshError(ctl, FALSE, _("Failed to read description file %s"), from); + virDomainFree(dom); + return FALSE; + } + l = read(fd, &buffer[0], sizeof(buffer)); + if ((l <= 0) || (l >= (int) sizeof(buffer))) { + vshError(ctl, FALSE, _("Failed to read description file %s"), from); + close(fd); + virDomainFree(dom); + return FALSE; + } + buffer[l] = 0; + + ret = virDomainDetachDevice(dom, &buffer[0]); + if (ret < 0) { + vshError(ctl, FALSE, _("Failed to detach device from %s"), from); + close(fd); + virDomainFree(dom); + return FALSE; + } + + close(fd); + virDomainFree(dom); + return TRUE; +} + /* * "quit" command @@ -2467,6 +2600,8 @@ static vshCmdDef commands[] = { {"vcpupin", cmdVcpupin, opts_vcpupin, info_vcpupin}, {"version", cmdVersion, NULL, info_version}, {"vncdisplay", cmdVNCDisplay, opts_vncdisplay, info_vncdisplay}, + {"attach-device", cmdAttachDevice, opts_attach_device, info_attach_device}, + {"detach-device", cmdDetachDevice, opts_detach_device, info_detach_device}, {NULL, NULL, NULL, NULL} }; -------------------------------------------------------------------------------- In message <200705111842.IJH95853.739E2KNG@xxxxxxxxxxxxxxxxx> "Re: [RFC] Device attach/detach on virsh" "Masayuki Sunou <fj1826dm@xxxxxxxxxxxxxxxxx>" wrote: > Hi > > I understood as follows. > > * There is no problem in a proposal 1 and a proposal 2 > * It is better that the user can choose use of XML and use of command-option > by the situation > > Therefore, I want to add both proposal 1 and proposal 2 to virsh. > There is no problem in this opinion? > > And, I am going to correct about naming. > vif --> interface > vbd --> disk >