You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

14959 lines
421 KiB

  1. /*
  2. * virsh-domain.c: Commands to manage domain
  3. *
  4. * Copyright (C) 2005, 2007-2016 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include "virsh-domain.h"
  22. #include "virsh-util.h"
  23. #include <fcntl.h>
  24. #include <signal.h>
  25. #include <sys/time.h>
  26. #include <libxml/parser.h>
  27. #include <libxml/tree.h>
  28. #include <libxml/xpath.h>
  29. #include <libxml/xmlsave.h>
  30. #include "internal.h"
  31. #include "virbitmap.h"
  32. #include "virbuffer.h"
  33. #include "conf/domain_conf.h"
  34. #include "viralloc.h"
  35. #include "vircommand.h"
  36. #include "virfile.h"
  37. #include "virjson.h"
  38. #include "virkeycode.h"
  39. #include "virmacaddr.h"
  40. #include "virnetdevbandwidth.h"
  41. #include "virprocess.h"
  42. #include "virstring.h"
  43. #include "virsh-console.h"
  44. #include "virsh-domain-monitor.h"
  45. #include "virerror.h"
  46. #include "virtime.h"
  47. #include "virtypedparam.h"
  48. #include "virxml.h"
  49. #include "virsh-nodedev.h"
  50. #include "viruri.h"
  51. #include "vsh-table.h"
  52. #include "virenum.h"
  53. #include "virutil.h"
  54. #define VIRSH_COMMON_OPT_DOMAIN_PERSISTENT \
  55. {.name = "persistent", \
  56. .type = VSH_OT_BOOL, \
  57. .help = N_("make live change persistent") \
  58. }
  59. #define VIRSH_COMMON_OPT_DOMAIN_CONFIG \
  60. VIRSH_COMMON_OPT_CONFIG(N_("affect next boot"))
  61. #define VIRSH_COMMON_OPT_DOMAIN_LIVE \
  62. VIRSH_COMMON_OPT_LIVE(N_("affect running domain"))
  63. #define VIRSH_COMMON_OPT_DOMAIN_CURRENT \
  64. VIRSH_COMMON_OPT_CURRENT(N_("affect current domain"))
  65. static virDomainPtr
  66. virshDomainDefine(virConnectPtr conn, const char *xml, unsigned int flags)
  67. {
  68. virDomainPtr dom;
  69. if (flags) {
  70. dom = virDomainDefineXMLFlags(conn, xml, flags);
  71. /* If validate is the only flag, just drop it and
  72. * try again.
  73. */
  74. if (!dom) {
  75. if ((virGetLastErrorCode() == VIR_ERR_NO_SUPPORT) &&
  76. (flags == VIR_DOMAIN_DEFINE_VALIDATE))
  77. dom = virDomainDefineXML(conn, xml);
  78. }
  79. } else {
  80. dom = virDomainDefineXML(conn, xml);
  81. }
  82. return dom;
  83. }
  84. VIR_ENUM_DECL(virshDomainVcpuState);
  85. VIR_ENUM_IMPL(virshDomainVcpuState,
  86. VIR_VCPU_LAST,
  87. N_("offline"),
  88. N_("running"),
  89. N_("blocked"));
  90. static const char *
  91. virshDomainVcpuStateToString(int state)
  92. {
  93. const char *str = virshDomainVcpuStateTypeToString(state);
  94. return str ? _(str) : _("no state");
  95. }
  96. /*
  97. * Determine number of CPU nodes present by trying
  98. * virNodeGetCPUMap and falling back to virNodeGetInfo
  99. * if needed.
  100. */
  101. static int
  102. virshNodeGetCPUCount(virConnectPtr conn)
  103. {
  104. int ret;
  105. virNodeInfo nodeinfo;
  106. if ((ret = virNodeGetCPUMap(conn, NULL, NULL, 0)) < 0) {
  107. /* fall back to nodeinfo */
  108. vshResetLibvirtError();
  109. if (virNodeGetInfo(conn, &nodeinfo) == 0)
  110. ret = VIR_NODEINFO_MAXCPUS(nodeinfo);
  111. }
  112. return ret;
  113. }
  114. /*
  115. * "attach-device" command
  116. */
  117. static const vshCmdInfo info_attach_device[] = {
  118. {.name = "help",
  119. .data = N_("attach device from an XML file")
  120. },
  121. {.name = "desc",
  122. .data = N_("Attach device from an XML <file>.")
  123. },
  124. {.name = NULL}
  125. };
  126. static const vshCmdOptDef opts_attach_device[] = {
  127. VIRSH_COMMON_OPT_DOMAIN_FULL(0),
  128. VIRSH_COMMON_OPT_FILE(N_("XML file")),
  129. VIRSH_COMMON_OPT_DOMAIN_PERSISTENT,
  130. VIRSH_COMMON_OPT_DOMAIN_CONFIG,
  131. VIRSH_COMMON_OPT_DOMAIN_LIVE,
  132. VIRSH_COMMON_OPT_DOMAIN_CURRENT,
  133. {.name = NULL}
  134. };
  135. static bool
  136. cmdAttachDevice(vshControl *ctl, const vshCmd *cmd)
  137. {
  138. virDomainPtr dom;
  139. const char *from = NULL;
  140. char *buffer;
  141. int rv;
  142. bool ret = false;
  143. unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
  144. bool current = vshCommandOptBool(cmd, "current");
  145. bool config = vshCommandOptBool(cmd, "config");
  146. bool live = vshCommandOptBool(cmd, "live");
  147. bool persistent = vshCommandOptBool(cmd, "persistent");
  148. VSH_EXCLUSIVE_OPTIONS_VAR(persistent, current);
  149. VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
  150. VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
  151. if (config || persistent)
  152. flags |= VIR_DOMAIN_AFFECT_CONFIG;
  153. if (live)
  154. flags |= VIR_DOMAIN_AFFECT_LIVE;
  155. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  156. return false;
  157. if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
  158. goto cleanup;
  159. if (persistent &&
  160. virDomainIsActive(dom) == 1)
  161. flags |= VIR_DOMAIN_AFFECT_LIVE;
  162. if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) {
  163. vshReportError(ctl);
  164. goto cleanup;
  165. }
  166. if (flags || current)
  167. rv = virDomainAttachDeviceFlags(dom, buffer, flags);
  168. else
  169. rv = virDomainAttachDevice(dom, buffer);
  170. VIR_FREE(buffer);
  171. if (rv < 0) {
  172. vshError(ctl, _("Failed to attach device from %s"), from);
  173. goto cleanup;
  174. }
  175. vshPrintExtra(ctl, "%s", _("Device attached successfully\n"));
  176. ret = true;
  177. cleanup:
  178. virshDomainFree(dom);
  179. return ret;
  180. }
  181. /*
  182. * "attach-disk" command
  183. */
  184. static const vshCmdInfo info_attach_disk[] = {
  185. {.name = "help",
  186. .data = N_("attach disk device")
  187. },
  188. {.name = "desc",
  189. .data = N_("Attach new disk device.")
  190. },
  191. {.name = NULL}
  192. };
  193. static const vshCmdOptDef opts_attach_disk[] = {
  194. VIRSH_COMMON_OPT_DOMAIN_FULL(0),
  195. {.name = "source",
  196. .type = VSH_OT_DATA,
  197. .flags = VSH_OFLAG_REQ | VSH_OFLAG_EMPTY_OK,
  198. .help = N_("source of disk device")
  199. },
  200. {.name = "target",
  201. .type = VSH_OT_DATA,
  202. .flags = VSH_OFLAG_REQ,
  203. .help = N_("target of disk device")
  204. },
  205. {.name = "targetbus",
  206. .type = VSH_OT_STRING,
  207. .help = N_("target bus of disk device")
  208. },
  209. {.name = "driver",
  210. .type = VSH_OT_STRING,
  211. .help = N_("driver of disk device")
  212. },
  213. {.name = "subdriver",
  214. .type = VSH_OT_STRING,
  215. .help = N_("subdriver of disk device")
  216. },
  217. {.name = "iothread",
  218. .type = VSH_OT_STRING,
  219. .help = N_("IOThread to be used by supported device")
  220. },
  221. {.name = "cache",
  222. .type = VSH_OT_STRING,
  223. .help = N_("cache mode of disk device")
  224. },
  225. {.name = "io",
  226. .type = VSH_OT_STRING,
  227. .help = N_("io policy of disk device")
  228. },
  229. {.name = "type",
  230. .type = VSH_OT_STRING,
  231. .help = N_("target device type")
  232. },
  233. {.name = "shareable",
  234. .type = VSH_OT_ALIAS,
  235. .help = "mode=shareable"
  236. },
  237. {.name = "mode",
  238. .type = VSH_OT_STRING,
  239. .help = N_("mode of device reading and writing")
  240. },
  241. {.name = "sourcetype",
  242. .type = VSH_OT_STRING,
  243. .help = N_("type of source (block|file)")
  244. },
  245. {.name = "serial",
  246. .type = VSH_OT_STRING,
  247. .help = N_("serial of disk device")
  248. },
  249. {.name = "wwn",
  250. .type = VSH_OT_STRING,
  251. .help = N_("wwn of disk device")
  252. },
  253. {.name = "alias",
  254. .type = VSH_OT_STRING,
  255. .help = N_("custom alias name of disk device")
  256. },
  257. {.name = "rawio",
  258. .type = VSH_OT_BOOL,
  259. .help = N_("needs rawio capability")
  260. },
  261. {.name = "address",
  262. .type = VSH_OT_STRING,
  263. .help = N_("address of disk device")
  264. },
  265. {.name = "multifunction",
  266. .type = VSH_OT_BOOL,
  267. .help = N_("use multifunction pci under specified address")
  268. },
  269. {.name = "print-xml",
  270. .type = VSH_OT_BOOL,
  271. .help = N_("print XML document rather than attach the disk")
  272. },
  273. VIRSH_COMMON_OPT_DOMAIN_PERSISTENT,
  274. VIRSH_COMMON_OPT_DOMAIN_CONFIG,
  275. VIRSH_COMMON_OPT_DOMAIN_LIVE,
  276. VIRSH_COMMON_OPT_DOMAIN_CURRENT,
  277. {.name = NULL}
  278. };
  279. enum {
  280. DISK_ADDR_TYPE_INVALID,
  281. DISK_ADDR_TYPE_PCI,
  282. DISK_ADDR_TYPE_SCSI,
  283. DISK_ADDR_TYPE_IDE,
  284. DISK_ADDR_TYPE_CCW,
  285. DISK_ADDR_TYPE_USB,
  286. DISK_ADDR_TYPE_SATA,
  287. };
  288. struct PCIAddress {
  289. unsigned int domain;
  290. unsigned int bus;
  291. unsigned int slot;
  292. unsigned int function;
  293. };
  294. struct SCSIAddress {
  295. unsigned int controller;
  296. unsigned int bus;
  297. unsigned long long unit;
  298. };
  299. struct IDEAddress {
  300. unsigned int controller;
  301. unsigned int bus;
  302. unsigned int unit;
  303. };
  304. struct CCWAddress {
  305. unsigned int cssid;
  306. unsigned int ssid;
  307. unsigned int devno;
  308. };
  309. struct USBAddress {
  310. unsigned int bus;
  311. unsigned int port;
  312. };
  313. struct SATAAddress {
  314. unsigned int controller;
  315. unsigned int bus;
  316. unsigned long long unit;
  317. };
  318. struct DiskAddress {
  319. int type;
  320. union {
  321. struct PCIAddress pci;
  322. struct SCSIAddress scsi;
  323. struct IDEAddress ide;
  324. struct CCWAddress ccw;
  325. struct USBAddress usb;
  326. struct SATAAddress sata;
  327. } addr;
  328. };
  329. static int str2PCIAddress(const char *str, struct PCIAddress *pciAddr)
  330. {
  331. char *domain, *bus, *slot, *function;
  332. if (!pciAddr)
  333. return -1;
  334. if (!str)
  335. return -1;
  336. domain = (char *)str;
  337. if (virStrToLong_uip(domain, &bus, 16, &pciAddr->domain) != 0)
  338. return -1;
  339. bus++;
  340. if (virStrToLong_uip(bus, &slot, 16, &pciAddr->bus) != 0)
  341. return -1;
  342. slot++;
  343. if (virStrToLong_uip(slot, &function, 16, &pciAddr->slot) != 0)
  344. return -1;
  345. function++;
  346. if (virStrToLong_uip(function, NULL, 16, &pciAddr->function) != 0)
  347. return -1;
  348. return 0;
  349. }
  350. static int str2SCSIAddress(const char *str, struct SCSIAddress *scsiAddr)
  351. {
  352. char *controller, *bus, *unit;
  353. if (!scsiAddr)
  354. return -1;
  355. if (!str)
  356. return -1;
  357. controller = (char *)str;
  358. if (virStrToLong_uip(controller, &bus, 10, &scsiAddr->controller) != 0)
  359. return -1;
  360. bus++;
  361. if (virStrToLong_uip(bus, &unit, 10, &scsiAddr->bus) != 0)
  362. return -1;
  363. unit++;
  364. if (virStrToLong_ullp(unit, NULL, 10, &scsiAddr->unit) != 0)
  365. return -1;
  366. return 0;
  367. }
  368. static int str2IDEAddress(const char *str, struct IDEAddress *ideAddr)
  369. {
  370. char *controller, *bus, *unit;
  371. if (!ideAddr)
  372. return -1;
  373. if (!str)
  374. return -1;
  375. controller = (char *)str;
  376. if (virStrToLong_uip(controller, &bus, 10, &ideAddr->controller) != 0)
  377. return -1;
  378. bus++;
  379. if (virStrToLong_uip(bus, &unit, 10, &ideAddr->bus) != 0)
  380. return -1;
  381. unit++;
  382. if (virStrToLong_uip(unit, NULL, 10, &ideAddr->unit) != 0)
  383. return -1;
  384. return 0;
  385. }
  386. static int str2CCWAddress(const char *str, struct CCWAddress *ccwAddr)
  387. {
  388. char *cssid, *ssid, *devno;
  389. if (!ccwAddr)
  390. return -1;
  391. if (!str)
  392. return -1;
  393. cssid = (char *)str;
  394. if (virStrToLong_uip(cssid, &ssid, 16, &ccwAddr->cssid) != 0)
  395. return -1;
  396. ssid++;
  397. if (virStrToLong_uip(ssid, &devno, 16, &ccwAddr->ssid) != 0)
  398. return -1;
  399. devno++;
  400. if (virStrToLong_uip(devno, NULL, 16, &ccwAddr->devno) != 0)
  401. return -1;
  402. return 0;
  403. }
  404. static int str2USBAddress(const char *str, struct USBAddress *usbAddr)
  405. {
  406. char *bus, *port;
  407. if (!usbAddr)
  408. return -1;
  409. if (!str)
  410. return -1;
  411. bus = (char *)str;
  412. if (virStrToLong_uip(bus, &port, 10, &usbAddr->bus) != 0)
  413. return -1;
  414. port++;
  415. if (virStrToLong_uip(port, NULL, 10, &usbAddr->port) != 0)
  416. return -1;
  417. return 0;
  418. }
  419. static int str2SATAAddress(const char *str, struct SATAAddress *sataAddr)
  420. {
  421. char *controller, *bus, *unit;
  422. if (!sataAddr)
  423. return -1;
  424. if (!str)
  425. return -1;
  426. controller = (char *)str;
  427. if (virStrToLong_uip(controller, &bus, 10, &sataAddr->controller) != 0)
  428. return -1;
  429. bus++;
  430. if (virStrToLong_uip(bus, &unit, 10, &sataAddr->bus) != 0)
  431. return -1;
  432. unit++;
  433. if (virStrToLong_ullp(unit, NULL, 10, &sataAddr->unit) != 0)
  434. return -1;
  435. return 0;
  436. }
  437. /* pci address pci:0000.00.0x0a.0 (domain:bus:slot:function)
  438. * ide disk address: ide:00.00.0 (controller:bus:unit)
  439. * scsi disk address: scsi:00.00.0 (controller:bus:unit)
  440. * ccw disk address: ccw:0xfe.0.0000 (cssid:ssid:devno)
  441. * usb disk address: usb:00.00 (bus:port)
  442. * sata disk address: sata:00.00.0 (controller:bus:unit)
  443. */
  444. static int str2DiskAddress(const char *str, struct DiskAddress *diskAddr)
  445. {
  446. char *type, *addr;
  447. if (!diskAddr)
  448. return -1;
  449. if (!str)
  450. return -1;
  451. type = (char *)str;
  452. addr = strchr(type, ':');
  453. if (!addr)
  454. return -1;
  455. if (STREQLEN(type, "pci", addr - type)) {
  456. diskAddr->type = DISK_ADDR_TYPE_PCI;
  457. return str2PCIAddress(addr + 1, &diskAddr->addr.pci);
  458. } else if (STREQLEN(type, "scsi", addr - type)) {
  459. diskAddr->type = DISK_ADDR_TYPE_SCSI;
  460. return str2SCSIAddress(addr + 1, &diskAddr->addr.scsi);
  461. } else if (STREQLEN(type, "ide", addr - type)) {
  462. diskAddr->type = DISK_ADDR_TYPE_IDE;
  463. return str2IDEAddress(addr + 1, &diskAddr->addr.ide);
  464. } else if (STREQLEN(type, "ccw", addr - type)) {
  465. diskAddr->type = DISK_ADDR_TYPE_CCW;
  466. return str2CCWAddress(addr + 1, &diskAddr->addr.ccw);
  467. } else if (STREQLEN(type, "usb", addr - type)) {
  468. diskAddr->type = DISK_ADDR_TYPE_USB;
  469. return str2USBAddress(addr + 1, &diskAddr->addr.usb);
  470. } else if (STREQLEN(type, "sata", addr - type)) {
  471. diskAddr->type = DISK_ADDR_TYPE_SATA;
  472. return str2SATAAddress(addr + 1, &diskAddr->addr.sata);
  473. }
  474. return -1;
  475. }
  476. static bool
  477. cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
  478. {
  479. virDomainPtr dom = NULL;
  480. const char *source = NULL, *target = NULL, *driver = NULL,
  481. *subdriver = NULL, *type = NULL, *mode = NULL,
  482. *iothread = NULL, *cache = NULL, *io = NULL,
  483. *serial = NULL, *straddr = NULL, *wwn = NULL,
  484. *targetbus = NULL, *alias = NULL;
  485. struct DiskAddress diskAddr;
  486. bool isFile = false, functionReturn = false;
  487. int ret;
  488. unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
  489. const char *stype = NULL;
  490. virBuffer buf = VIR_BUFFER_INITIALIZER;
  491. char *xml = NULL;
  492. struct stat st;
  493. bool current = vshCommandOptBool(cmd, "current");
  494. bool config = vshCommandOptBool(cmd, "config");
  495. bool live = vshCommandOptBool(cmd, "live");
  496. bool persistent = vshCommandOptBool(cmd, "persistent");
  497. VSH_EXCLUSIVE_OPTIONS_VAR(persistent, current);
  498. VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
  499. VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
  500. if (config || persistent)
  501. flags |= VIR_DOMAIN_AFFECT_CONFIG;
  502. if (live)
  503. flags |= VIR_DOMAIN_AFFECT_LIVE;
  504. if (vshCommandOptStringReq(ctl, cmd, "source", &source) < 0 ||
  505. vshCommandOptStringReq(ctl, cmd, "target", &target) < 0 ||
  506. vshCommandOptStringReq(ctl, cmd, "driver", &driver) < 0 ||
  507. vshCommandOptStringReq(ctl, cmd, "subdriver", &subdriver) < 0 ||
  508. vshCommandOptStringReq(ctl, cmd, "type", &type) < 0 ||
  509. vshCommandOptStringReq(ctl, cmd, "mode", &mode) < 0 ||
  510. vshCommandOptStringReq(ctl, cmd, "iothread", &iothread) < 0 ||
  511. vshCommandOptStringReq(ctl, cmd, "cache", &cache) < 0 ||
  512. vshCommandOptStringReq(ctl, cmd, "io", &io) < 0 ||
  513. vshCommandOptStringReq(ctl, cmd, "serial", &serial) < 0 ||
  514. vshCommandOptStringReq(ctl, cmd, "wwn", &wwn) < 0 ||
  515. vshCommandOptStringReq(ctl, cmd, "address", &straddr) < 0 ||
  516. vshCommandOptStringReq(ctl, cmd, "targetbus", &targetbus) < 0 ||
  517. vshCommandOptStringReq(ctl, cmd, "alias", &alias) < 0 ||
  518. vshCommandOptStringReq(ctl, cmd, "sourcetype", &stype) < 0)
  519. goto cleanup;
  520. if (!stype) {
  521. if (driver && (STREQ(driver, "file") || STREQ(driver, "tap"))) {
  522. isFile = true;
  523. } else {
  524. if (source && !stat(source, &st))
  525. isFile = S_ISREG(st.st_mode) ? true : false;
  526. }
  527. } else if (STREQ(stype, "file")) {
  528. isFile = true;
  529. } else if (STRNEQ(stype, "block")) {
  530. vshError(ctl, _("Unknown source type: '%s'"), stype);
  531. goto cleanup;
  532. }
  533. if (mode) {
  534. if (STRNEQ(mode, "readonly") && STRNEQ(mode, "shareable")) {
  535. vshError(ctl, _("No support for %s in command 'attach-disk'"),
  536. mode);
  537. goto cleanup;
  538. }
  539. }
  540. if (wwn && !virValidateWWN(wwn))
  541. goto cleanup;
  542. /* Make XML of disk */
  543. virBufferAsprintf(&buf, "<disk type='%s'",
  544. isFile ? "file" : "block");
  545. if (type)
  546. virBufferAsprintf(&buf, " device='%s'", type);
  547. if (vshCommandOptBool(cmd, "rawio"))
  548. virBufferAddLit(&buf, " rawio='yes'");
  549. virBufferAddLit(&buf, ">\n");
  550. virBufferAdjustIndent(&buf, 2);
  551. if (driver || subdriver || iothread || cache || io) {
  552. virBufferAddLit(&buf, "<driver");
  553. if (driver)
  554. virBufferAsprintf(&buf, " name='%s'", driver);
  555. if (subdriver)
  556. virBufferAsprintf(&buf, " type='%s'", subdriver);
  557. if (iothread)
  558. virBufferAsprintf(&buf, " iothread='%s'", iothread);
  559. if (cache)
  560. virBufferAsprintf(&buf, " cache='%s'", cache);
  561. if (io)
  562. virBufferAsprintf(&buf, " io='%s'", io);
  563. virBufferAddLit(&buf, "/>\n");
  564. }
  565. if (source)
  566. virBufferAsprintf(&buf, "<source %s='%s'/>\n",
  567. isFile ? "file" : "dev", source);
  568. virBufferAsprintf(&buf, "<target dev='%s'", target);
  569. if (targetbus)
  570. virBufferAsprintf(&buf, " bus='%s'", targetbus);
  571. virBufferAddLit(&buf, "/>\n");
  572. if (mode)
  573. virBufferAsprintf(&buf, "<%s/>\n", mode);
  574. if (serial)
  575. virBufferAsprintf(&buf, "<serial>%s</serial>\n", serial);
  576. if (alias)
  577. virBufferAsprintf(&buf, "<alias name='%s'/>\n", alias);
  578. if (wwn)
  579. virBufferAsprintf(&buf, "<wwn>%s</wwn>\n", wwn);
  580. if (straddr) {
  581. if (str2DiskAddress(straddr, &diskAddr) != 0) {
  582. vshError(ctl, _("Invalid address."));
  583. goto cleanup;
  584. }
  585. if (STRPREFIX((const char *)target, "vd")) {
  586. if (diskAddr.type == DISK_ADDR_TYPE_PCI) {
  587. virBufferAsprintf(&buf,
  588. "<address type='pci' domain='0x%04x'"
  589. " bus ='0x%02x' slot='0x%02x' function='0x%0x'",
  590. diskAddr.addr.pci.domain, diskAddr.addr.pci.bus,
  591. diskAddr.addr.pci.slot, diskAddr.addr.pci.function);
  592. if (vshCommandOptBool(cmd, "multifunction"))
  593. virBufferAddLit(&buf, " multifunction='on'");
  594. virBufferAddLit(&buf, "/>\n");
  595. } else if (diskAddr.type == DISK_ADDR_TYPE_CCW) {
  596. virBufferAsprintf(&buf,
  597. "<address type='ccw' cssid='0x%02x'"
  598. " ssid='0x%01x' devno='0x%04x' />\n",
  599. diskAddr.addr.ccw.cssid, diskAddr.addr.ccw.ssid,
  600. diskAddr.addr.ccw.devno);
  601. } else {
  602. vshError(ctl, "%s",
  603. _("expecting a pci:0000.00.00.00 or ccw:00.0.0000 address."));
  604. goto cleanup;
  605. }
  606. } else if (STRPREFIX((const char *)target, "sd")) {
  607. if (diskAddr.type == DISK_ADDR_TYPE_SCSI) {
  608. virBufferAsprintf(&buf,
  609. "<address type='drive' controller='%u'"
  610. " bus='%u' unit='%llu' />\n",
  611. diskAddr.addr.scsi.controller, diskAddr.addr.scsi.bus,
  612. diskAddr.addr.scsi.unit);
  613. } else if (diskAddr.type == DISK_ADDR_TYPE_USB) {
  614. virBufferAsprintf(&buf,
  615. "<address type='usb' bus='%u' port='%u' />\n",
  616. diskAddr.addr.usb.bus, diskAddr.addr.usb.port);
  617. } else if (diskAddr.type == DISK_ADDR_TYPE_SATA) {
  618. virBufferAsprintf(&buf,
  619. "<address type='drive' controller='%u'"
  620. " bus='%u' unit='%llu' />\n",
  621. diskAddr.addr.sata.controller, diskAddr.addr.sata.bus,
  622. diskAddr.addr.sata.unit);
  623. } else {
  624. vshError(ctl, "%s",
  625. _("expecting a scsi:00.00.00 or usb:00.00 or sata:00.00.00 address."));
  626. goto cleanup;
  627. }
  628. } else if (STRPREFIX((const char *)target, "hd")) {
  629. if (diskAddr.type == DISK_ADDR_TYPE_IDE) {
  630. virBufferAsprintf(&buf,
  631. "<address type='drive' controller='%u'"
  632. " bus='%u' unit='%u' />\n",
  633. diskAddr.addr.ide.controller, diskAddr.addr.ide.bus,
  634. diskAddr.addr.ide.unit);
  635. } else {
  636. vshError(ctl, "%s", _("expecting an ide:00.00.00 address."));
  637. goto cleanup;
  638. }
  639. }
  640. }
  641. virBufferAdjustIndent(&buf, -2);
  642. virBufferAddLit(&buf, "</disk>\n");
  643. xml = virBufferContentAndReset(&buf);
  644. if (vshCommandOptBool(cmd, "print-xml")) {
  645. vshPrint(ctl, "%s", xml);
  646. functionReturn = true;
  647. goto cleanup;
  648. }
  649. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  650. goto cleanup;
  651. if (persistent &&
  652. virDomainIsActive(dom) == 1)
  653. flags |= VIR_DOMAIN_AFFECT_LIVE;
  654. if (flags || current)
  655. ret = virDomainAttachDeviceFlags(dom, xml, flags);
  656. else
  657. ret = virDomainAttachDevice(dom, xml);
  658. if (ret != 0) {
  659. vshError(ctl, "%s", _("Failed to attach disk"));
  660. } else {
  661. vshPrintExtra(ctl, "%s", _("Disk attached successfully\n"));
  662. functionReturn = true;
  663. }
  664. cleanup:
  665. VIR_FREE(xml);
  666. virshDomainFree(dom);
  667. virBufferFreeAndReset(&buf);
  668. return functionReturn;
  669. }
  670. /*
  671. * "attach-interface" command
  672. */
  673. static const vshCmdInfo info_attach_interface[] = {
  674. {.name = "help",
  675. .data = N_("attach network interface")
  676. },
  677. {.name = "desc",
  678. .data = N_("Attach new network interface.")
  679. },
  680. {.name = NULL}
  681. };
  682. static const vshCmdOptDef opts_attach_interface[] = {
  683. VIRSH_COMMON_OPT_DOMAIN_FULL(0),
  684. {.name = "type",
  685. .type = VSH_OT_DATA,
  686. .flags = VSH_OFLAG_REQ,
  687. .help = N_("network interface type")
  688. },
  689. {.name = "source",
  690. .type = VSH_OT_DATA,
  691. .flags = VSH_OFLAG_REQ,
  692. .help = N_("source of network interface")
  693. },
  694. {.name = "target",
  695. .type = VSH_OT_STRING,
  696. .help = N_("target network name")
  697. },
  698. {.name = "mac",
  699. .type = VSH_OT_STRING,
  700. .help = N_("MAC address")
  701. },
  702. {.name = "script",
  703. .type = VSH_OT_STRING,
  704. .help = N_("script used to bridge network interface")
  705. },
  706. {.name = "model",
  707. .type = VSH_OT_STRING,
  708. .help = N_("model type")
  709. },
  710. {.name = "alias",
  711. .type = VSH_OT_STRING,
  712. .help = N_("custom alias name of interface device")
  713. },
  714. {.name = "inbound",
  715. .type = VSH_OT_STRING,
  716. .help = N_("control domain's incoming traffics")
  717. },
  718. {.name = "outbound",
  719. .type = VSH_OT_STRING,
  720. .help = N_("control domain's outgoing traffics")
  721. },
  722. VIRSH_COMMON_OPT_DOMAIN_PERSISTENT,
  723. VIRSH_COMMON_OPT_DOMAIN_CONFIG,
  724. VIRSH_COMMON_OPT_DOMAIN_LIVE,
  725. VIRSH_COMMON_OPT_DOMAIN_CURRENT,
  726. {.name = "print-xml",
  727. .type = VSH_OT_BOOL,
  728. .help = N_("print XML document rather than attach the interface")
  729. },
  730. {.name = "managed",
  731. .type = VSH_OT_BOOL,
  732. .help = N_("libvirt will automatically detach/attach the device from/to host")
  733. },
  734. {.name = NULL}
  735. };
  736. /* parse inbound and outbound which are in the format of
  737. * 'average,peak,burst,floor', in which peak and burst are optional,
  738. * thus 'average,,burst' and 'average,peak' are also legal. */
  739. #define VIRSH_PARSE_RATE_FIELD(index, name) \
  740. do { \
  741. if (index < ntok && \
  742. *tok[index] != '\0' && \
  743. virStrToLong_ullp(tok[index], NULL, 10, &rate->name) < 0) { \
  744. vshError(ctl, _("field '%s' is malformed"), #name); \
  745. goto cleanup; \
  746. } \
  747. } while (0)
  748. static int
  749. virshParseRateStr(vshControl *ctl,
  750. const char *rateStr,
  751. virNetDevBandwidthRatePtr rate)
  752. {
  753. char **tok = NULL;
  754. size_t ntok;
  755. int ret = -1;
  756. if (!(tok = virStringSplitCount(rateStr, ",", 0, &ntok)))
  757. return -1;
  758. if (ntok > 4) {
  759. vshError(ctl, _("Rate string '%s' has too many fields"), rateStr);
  760. goto cleanup;
  761. }
  762. VIRSH_PARSE_RATE_FIELD(0, average);
  763. VIRSH_PARSE_RATE_FIELD(1, peak);
  764. VIRSH_PARSE_RATE_FIELD(2, burst);
  765. VIRSH_PARSE_RATE_FIELD(3, floor);
  766. ret = 0;
  767. cleanup:
  768. virStringListFree(tok);
  769. return ret;
  770. }
  771. #undef VIRSH_PARSE_RATE_FIELD
  772. static bool
  773. cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
  774. {
  775. virDomainPtr dom = NULL;
  776. const char *mac = NULL, *target = NULL, *script = NULL,
  777. *type = NULL, *source = NULL, *model = NULL,
  778. *inboundStr = NULL, *outboundStr = NULL, *alias = NULL;
  779. virNetDevBandwidthRate inbound, outbound;
  780. virDomainNetType typ;
  781. int ret;
  782. bool functionReturn = false;
  783. virBuffer buf = VIR_BUFFER_INITIALIZER;
  784. char *xml = NULL;
  785. unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
  786. bool current = vshCommandOptBool(cmd, "current");
  787. bool config = vshCommandOptBool(cmd, "config");
  788. bool live = vshCommandOptBool(cmd, "live");
  789. bool persistent = vshCommandOptBool(cmd, "persistent");
  790. bool managed = vshCommandOptBool(cmd, "managed");
  791. VSH_EXCLUSIVE_OPTIONS_VAR(persistent, current);
  792. VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
  793. VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
  794. if (config || persistent)
  795. flags |= VIR_DOMAIN_AFFECT_CONFIG;
  796. if (live)
  797. flags |= VIR_DOMAIN_AFFECT_LIVE;
  798. if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0 ||
  799. vshCommandOptStringReq(ctl, cmd, "source", &source) < 0 ||
  800. vshCommandOptStringReq(ctl, cmd, "target", &target) < 0 ||
  801. vshCommandOptStringReq(ctl, cmd, "mac", &mac) < 0 ||
  802. vshCommandOptStringReq(ctl, cmd, "script", &script) < 0 ||
  803. vshCommandOptStringReq(ctl, cmd, "model", &model) < 0 ||
  804. vshCommandOptStringReq(ctl, cmd, "alias", &alias) < 0 ||
  805. vshCommandOptStringReq(ctl, cmd, "inbound", &inboundStr) < 0 ||
  806. vshCommandOptStringReq(ctl, cmd, "outbound", &outboundStr) < 0)
  807. goto cleanup;
  808. /* check interface type */
  809. if ((int)(typ = virDomainNetTypeFromString(type)) < 0) {
  810. vshError(ctl, _("No support for %s in command 'attach-interface'"),
  811. type);
  812. goto cleanup;
  813. }
  814. if (inboundStr) {
  815. memset(&inbound, 0, sizeof(inbound));
  816. if (virshParseRateStr(ctl, inboundStr, &inbound) < 0)
  817. goto cleanup;
  818. if (!inbound.average && !inbound.floor) {
  819. vshError(ctl, _("either inbound average or floor is mandatory"));
  820. goto cleanup;
  821. }
  822. }
  823. if (outboundStr) {
  824. memset(&outbound, 0, sizeof(outbound));
  825. if (virshParseRateStr(ctl, outboundStr, &outbound) < 0)
  826. goto cleanup;
  827. if (outbound.average == 0) {
  828. vshError(ctl, _("outbound average is mandatory"));
  829. goto cleanup;
  830. }
  831. if (outbound.floor) {
  832. vshError(ctl, _("outbound floor is unsupported yet"));
  833. goto cleanup;
  834. }
  835. }
  836. /* Make XML of interface */
  837. virBufferAsprintf(&buf, "<interface type='%s'", type);
  838. if (managed)
  839. virBufferAddLit(&buf, " managed='yes'>\n");
  840. else
  841. virBufferAddLit(&buf, ">\n");
  842. virBufferAdjustIndent(&buf, 2);
  843. switch (typ) {
  844. case VIR_DOMAIN_NET_TYPE_NETWORK:
  845. case VIR_DOMAIN_NET_TYPE_BRIDGE:
  846. virBufferAsprintf(&buf, "<source %s='%s'/>\n",
  847. virDomainNetTypeToString(typ), source);
  848. break;
  849. case VIR_DOMAIN_NET_TYPE_DIRECT:
  850. virBufferAsprintf(&buf, "<source dev='%s'/>\n", source);
  851. break;
  852. case VIR_DOMAIN_NET_TYPE_HOSTDEV:
  853. {
  854. struct PCIAddress pciAddr = {0, 0, 0, 0};
  855. if (str2PCIAddress(source, &pciAddr) < 0) {
  856. vshError(ctl, _("cannot parse pci address '%s' for network "
  857. "interface"), source);
  858. goto cleanup;
  859. }
  860. virBufferAddLit(&buf, "<source>\n");
  861. virBufferAdjustIndent(&buf, 2);
  862. virBufferAsprintf(&buf, "<address type='pci' domain='0x%04x'"
  863. " bus='0x%02x' slot='0x%02x' function='0x%d'/>\n",
  864. pciAddr.domain, pciAddr.bus,
  865. pciAddr.slot, pciAddr.function);
  866. virBufferAdjustIndent(&buf, -2);
  867. virBufferAddLit(&buf, "</source>\n");
  868. break;
  869. }
  870. case VIR_DOMAIN_NET_TYPE_USER:
  871. case VIR_DOMAIN_NET_TYPE_ETHERNET:
  872. case VIR_DOMAIN_NET_TYPE_VHOSTUSER:
  873. case VIR_DOMAIN_NET_TYPE_SERVER:
  874. case VIR_DOMAIN_NET_TYPE_CLIENT:
  875. case VIR_DOMAIN_NET_TYPE_MCAST:
  876. case VIR_DOMAIN_NET_TYPE_UDP:
  877. case VIR_DOMAIN_NET_TYPE_INTERNAL:
  878. case VIR_DOMAIN_NET_TYPE_LAST:
  879. vshError(ctl, _("No support for %s in command 'attach-interface'"),
  880. type);
  881. goto cleanup;
  882. break;
  883. }
  884. if (target != NULL)
  885. virBufferAsprintf(&buf, "<target dev='%s'/>\n", target);
  886. if (mac != NULL)
  887. virBufferAsprintf(&buf, "<mac address='%s'/>\n", mac);
  888. if (script != NULL)
  889. virBufferAsprintf(&buf, "<script path='%s'/>\n", script);
  890. if (model != NULL)
  891. virBufferAsprintf(&buf, "<model type='%s'/>\n", model);
  892. if (alias != NULL)
  893. virBufferAsprintf(&buf, "<alias name='%s'/>\n", alias);
  894. if (inboundStr || outboundStr) {
  895. virBufferAddLit(&buf, "<bandwidth>\n");
  896. virBufferAdjustIndent(&buf, 2);
  897. if (inboundStr && (inbound.average || inbound.floor)) {
  898. virBufferAddLit(&buf, "<inbound");
  899. if (inbound.average > 0)
  900. virBufferAsprintf(&buf, " average='%llu'", inbound.average);
  901. if (inbound.peak > 0)
  902. virBufferAsprintf(&buf, " peak='%llu'", inbound.peak);
  903. if (inbound.burst > 0)
  904. virBufferAsprintf(&buf, " burst='%llu'", inbound.burst);
  905. if (inbound.floor > 0)
  906. virBufferAsprintf(&buf, " floor='%llu'", inbound.floor);
  907. virBufferAddLit(&buf, "/>\n");
  908. }
  909. if (outboundStr && outbound.average > 0) {
  910. virBufferAsprintf(&buf, "<outbound average='%llu'", outbound.average);
  911. if (outbound.peak > 0)
  912. virBufferAsprintf(&buf, " peak='%llu'", outbound.peak);
  913. if (outbound.burst > 0)
  914. virBufferAsprintf(&buf, " burst='%llu'", outbound.burst);
  915. virBufferAddLit(&buf, "/>\n");
  916. }
  917. virBufferAdjustIndent(&buf, -2);
  918. virBufferAddLit(&buf, "</bandwidth>\n");
  919. }
  920. virBufferAdjustIndent(&buf, -2);
  921. virBufferAddLit(&buf, "</interface>\n");
  922. xml = virBufferContentAndReset(&buf);
  923. if (vshCommandOptBool(cmd, "print-xml")) {
  924. vshPrint(ctl, "%s", xml);
  925. functionReturn = true;
  926. goto cleanup;
  927. }
  928. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  929. goto cleanup;
  930. if (persistent &&
  931. virDomainIsActive(dom) == 1)
  932. flags |= VIR_DOMAIN_AFFECT_LIVE;
  933. if (flags || current)
  934. ret = virDomainAttachDeviceFlags(dom, xml, flags);
  935. else
  936. ret = virDomainAttachDevice(dom, xml);
  937. if (ret != 0) {
  938. vshError(ctl, "%s", _("Failed to attach interface"));
  939. } else {
  940. vshPrintExtra(ctl, "%s", _("Interface attached successfully\n"));
  941. functionReturn = true;
  942. }
  943. cleanup:
  944. VIR_FREE(xml);
  945. virshDomainFree(dom);
  946. virBufferFreeAndReset(&buf);
  947. return functionReturn;
  948. }
  949. /*
  950. * "autostart" command
  951. */
  952. static const vshCmdInfo info_autostart[] = {
  953. {.name = "help",
  954. .data = N_("autostart a domain")
  955. },
  956. {.name = "desc",
  957. .data = N_("Configure a domain to be automatically started at boot.")
  958. },
  959. {.name = NULL}
  960. };
  961. static const vshCmdOptDef opts_autostart[] = {
  962. VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_PERSISTENT),
  963. {.name = "disable",
  964. .type = VSH_OT_BOOL,
  965. .help = N_("disable autostarting")
  966. },
  967. {.name = NULL}
  968. };
  969. static bool
  970. cmdAutostart(vshControl *ctl, const vshCmd *cmd)
  971. {
  972. virDomainPtr dom;
  973. const char *name;
  974. int autostart;
  975. if (!(dom = virshCommandOptDomain(ctl, cmd, &name)))
  976. return false;
  977. autostart = !vshCommandOptBool(cmd, "disable");
  978. if (virDomainSetAutostart(dom, autostart) < 0) {
  979. if (autostart)
  980. vshError(ctl, _("Failed to mark domain %s as autostarted"), name);
  981. else
  982. vshError(ctl, _("Failed to unmark domain %s as autostarted"), name);
  983. virshDomainFree(dom);
  984. return false;
  985. }
  986. if (autostart)
  987. vshPrintExtra(ctl, _("Domain %s marked as autostarted\n"), name);
  988. else
  989. vshPrintExtra(ctl, _("Domain %s unmarked as autostarted\n"), name);
  990. virshDomainFree(dom);
  991. return true;
  992. }
  993. /*
  994. * "blkdeviotune" command
  995. */
  996. static const vshCmdInfo info_blkdeviotune[] = {
  997. {.name = "help",
  998. .data = N_("Set or query a block device I/O tuning parameters.")
  999. },
  1000. {.name = "desc",
  1001. .data = N_("Set or query disk I/O parameters such as block throttling.")
  1002. },
  1003. {.name = NULL}
  1004. };
  1005. static const vshCmdOptDef opts_blkdeviotune[] = {
  1006. VIRSH_COMMON_OPT_DOMAIN_FULL(0),
  1007. {.name = "device",
  1008. .type = VSH_OT_DATA,
  1009. .flags = VSH_OFLAG_REQ,
  1010. .completer = virshDomainDiskTargetCompleter,
  1011. .help = N_("block device")
  1012. },
  1013. {.name = "total_bytes_sec",
  1014. .type = VSH_OT_ALIAS,
  1015. .help = "total-bytes-sec"
  1016. },
  1017. {.name = "total-bytes-sec",
  1018. .type = VSH_OT_INT,
  1019. .help = N_("total throughput limit, as scaled integer (default bytes)")
  1020. },
  1021. {.name = "read_bytes_sec",
  1022. .type = VSH_OT_ALIAS,
  1023. .help = "read-bytes-sec"
  1024. },
  1025. {.name = "read-bytes-sec",
  1026. .type = VSH_OT_INT,
  1027. .help = N_("read throughput limit, as scaled integer (default bytes)")
  1028. },
  1029. {.name = "write_bytes_sec",
  1030. .type = VSH_OT_ALIAS,
  1031. .help = "write-bytes-sec"
  1032. },
  1033. {.name = "write-bytes-sec",
  1034. .type = VSH_OT_INT,
  1035. .help = N_("write throughput limit, as scaled integer (default bytes)")
  1036. },
  1037. {.name = "total_iops_sec",
  1038. .type = VSH_OT_ALIAS,
  1039. .help = "total-iops-sec"
  1040. },
  1041. {.name = "total-iops-sec",
  1042. .type = VSH_OT_INT,
  1043. .help = N_("total I/O operations limit per second")
  1044. },
  1045. {.name = "read_iops_sec",
  1046. .type = VSH_OT_ALIAS,
  1047. .help = "read-iops-sec"
  1048. },
  1049. {.name = "read-iops-sec",
  1050. .type = VSH_OT_INT,
  1051. .help = N_("read I/O operations limit per second")
  1052. },
  1053. {.name = "write_iops_sec",
  1054. .type = VSH_OT_ALIAS,
  1055. .help = "write-iops-sec"
  1056. },
  1057. {.name = "write-iops-sec",
  1058. .type = VSH_OT_INT,
  1059. .help = N_("write I/O operations limit per second")
  1060. },
  1061. {.name = "total_bytes_sec_max",
  1062. .type = VSH_OT_ALIAS,
  1063. .help = "total-bytes-sec-max"
  1064. },
  1065. {.name = "total-bytes-sec-max",
  1066. .type = VSH_OT_INT,
  1067. .help = N_("total max, as scaled integer (default bytes)")
  1068. },
  1069. {.name = "read_bytes_sec_max",
  1070. .type = VSH_OT_ALIAS,
  1071. .help = "read-bytes-sec-max"
  1072. },
  1073. {.name = "read-bytes-sec-max",
  1074. .type = VSH_OT_INT,
  1075. .help = N_("read max, as scaled integer (default bytes)")
  1076. },
  1077. {.name = "write_bytes_sec_max",
  1078. .type = VSH_OT_ALIAS,
  1079. .help = "write-bytes-sec-max"
  1080. },
  1081. {.name = "write-bytes-sec-max",
  1082. .type = VSH_OT_INT,
  1083. .help = N_("write max, as scaled integer (default bytes)")
  1084. },
  1085. {.name = "total_iops_sec_max",
  1086. .type = VSH_OT_ALIAS,
  1087. .help = "total-iops-sec-max"
  1088. },
  1089. {.name = "total-iops-sec-max",
  1090. .type = VSH_OT_INT,
  1091. .help = N_("total I/O operations max")
  1092. },
  1093. {.name = "read_iops_sec_max",
  1094. .type = VSH_OT_ALIAS,
  1095. .help = "read-iops-sec-max"
  1096. },
  1097. {.name = "read-iops-sec-max",
  1098. .type = VSH_OT_INT,
  1099. .help = N_("read I/O operations max")
  1100. },
  1101. {.name = "write_iops_sec_max",
  1102. .type = VSH_OT_ALIAS,
  1103. .help = "write-iops-sec-max"
  1104. },
  1105. {.name = "write-iops-sec-max",
  1106. .type = VSH_OT_INT,
  1107. .help = N_("write I/O operations max")
  1108. },
  1109. {.name = "size_iops_sec",
  1110. .type = VSH_OT_ALIAS,
  1111. .help = "size-iops-sec"
  1112. },
  1113. {.name = "size-iops-sec",
  1114. .type = VSH_OT_INT,
  1115. .help = N_("I/O size in bytes")
  1116. },
  1117. {.name = "group_name",
  1118. .type = VSH_OT_ALIAS,
  1119. .help = "group-name"
  1120. },
  1121. {.name = "group-name",
  1122. .type = VSH_OT_STRING,
  1123. .help = N_("group name to share I/O quota between multiple drives")
  1124. },
  1125. {.name = "total_bytes_sec_max_length",
  1126. .type = VSH_OT_ALIAS,
  1127. .help = "total-bytes-sec-max-length"
  1128. },
  1129. {.name = "total-bytes-sec-max-length",
  1130. .type = VSH_OT_INT,
  1131. .help = N_("duration in seconds to allow total max bytes")
  1132. },
  1133. {.name = "read_bytes_sec_max_length",
  1134. .type = VSH_OT_ALIAS,
  1135. .help = "read-bytes-sec-max-length"
  1136. },
  1137. {.name = "read-bytes-sec-max-length",
  1138. .type = VSH_OT_INT,
  1139. .help = N_("duration in seconds to allow read max bytes")
  1140. },
  1141. {.name = "write_bytes_sec_max_length",
  1142. .type = VSH_OT_ALIAS,
  1143. .help = "write-bytes-sec-max-length"
  1144. },
  1145. {.name = "write-bytes-sec-max-length",
  1146. .type = VSH_OT_INT,
  1147. .help = N_("duration in seconds to allow write max bytes")
  1148. },
  1149. {.name = "total_iops_sec_max_length",
  1150. .type = VSH_OT_ALIAS,
  1151. .help = "total-iops-sec-max-length"
  1152. },
  1153. {.name = "total-iops-sec-max-length",
  1154. .type = VSH_OT_INT,
  1155. .help = N_("duration in seconds to allow total I/O operations max")
  1156. },
  1157. {.name = "read_iops_sec_max_length",
  1158. .type = VSH_OT_ALIAS,
  1159. .help = "read-iops-sec-max-length"
  1160. },
  1161. {.name = "read-iops-sec-max-length",
  1162. .type = VSH_OT_INT,
  1163. .help = N_("duration in seconds to allow read I/O operations max")
  1164. },
  1165. {.name = "write_iops_sec_max_length",
  1166. .type = VSH_OT_ALIAS,
  1167. .help = "write-iops-sec-max-length"
  1168. },
  1169. {.name = "write-iops-sec-max-length",
  1170. .type = VSH_OT_INT,
  1171. .help = N_("duration in seconds to allow write I/O operations max")
  1172. },
  1173. VIRSH_COMMON_OPT_DOMAIN_CONFIG,
  1174. VIRSH_COMMON_OPT_DOMAIN_LIVE,
  1175. VIRSH_COMMON_OPT_DOMAIN_CURRENT,
  1176. {.name = NULL}
  1177. };
  1178. static bool
  1179. cmdBlkdeviotune(vshControl *ctl, const vshCmd *cmd)
  1180. {
  1181. virDomainPtr dom = NULL;
  1182. const char *name, *disk;
  1183. const char *group_name = NULL;
  1184. unsigned long long value;
  1185. int nparams = 0;
  1186. int maxparams = 0;
  1187. virTypedParameterPtr params = NULL;
  1188. unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
  1189. size_t i;
  1190. int rv = 0;
  1191. bool current = vshCommandOptBool(cmd, "current");
  1192. bool config = vshCommandOptBool(cmd, "config");
  1193. bool live = vshCommandOptBool(cmd, "live");
  1194. bool ret = false;
  1195. VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
  1196. VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
  1197. if (config)
  1198. flags |= VIR_DOMAIN_AFFECT_CONFIG;
  1199. if (live)
  1200. flags |= VIR_DOMAIN_AFFECT_LIVE;
  1201. if (!(dom = virshCommandOptDomain(ctl, cmd, &name)))
  1202. goto cleanup;
  1203. if (vshCommandOptStringReq(ctl, cmd, "device", &disk) < 0)
  1204. goto cleanup;
  1205. #define VSH_ADD_IOTUNE_SCALED(PARAM, CONST) \
  1206. if ((rv = vshCommandOptScaledInt(ctl, cmd, #PARAM, &value, \
  1207. 1, ULLONG_MAX)) < 0) { \
  1208. goto interror; \
  1209. } else if (rv > 0) { \
  1210. if (virTypedParamsAddULLong(&params, &nparams, &maxparams, \
  1211. VIR_DOMAIN_BLOCK_IOTUNE_##CONST, \
  1212. value) < 0) \
  1213. goto save_error; \
  1214. }
  1215. VSH_ADD_IOTUNE_SCALED(total-bytes-sec, TOTAL_BYTES_SEC);
  1216. VSH_ADD_IOTUNE_SCALED(read-bytes-sec, READ_BYTES_SEC);
  1217. VSH_ADD_IOTUNE_SCALED(write-bytes-sec, WRITE_BYTES_SEC);
  1218. VSH_ADD_IOTUNE_SCALED(total-bytes-sec-max, TOTAL_BYTES_SEC_MAX);
  1219. VSH_ADD_IOTUNE_SCALED(read-bytes-sec-max, READ_BYTES_SEC_MAX);
  1220. VSH_ADD_IOTUNE_SCALED(write-bytes-sec-max, WRITE_BYTES_SEC_MAX);
  1221. #undef VSH_ADD_IOTUNE_SCALED
  1222. #define VSH_ADD_IOTUNE(PARAM, CONST) \
  1223. if ((rv = vshCommandOptULongLong(ctl, cmd, #PARAM, &value)) < 0) { \
  1224. goto interror; \
  1225. } else if (rv > 0) { \
  1226. if (virTypedParamsAddULLong(&params, &nparams, &maxparams, \
  1227. VIR_DOMAIN_BLOCK_IOTUNE_##CONST, \
  1228. value) < 0) \
  1229. goto save_error; \
  1230. }
  1231. VSH_ADD_IOTUNE(total-iops-sec, TOTAL_IOPS_SEC);
  1232. VSH_ADD_IOTUNE(read-iops-sec, READ_IOPS_SEC);
  1233. VSH_ADD_IOTUNE(write-iops-sec, WRITE_IOPS_SEC);
  1234. VSH_ADD_IOTUNE(total-iops-sec-max, TOTAL_IOPS_SEC_MAX);
  1235. VSH_ADD_IOTUNE(read-iops-sec-max, READ_IOPS_SEC_MAX);
  1236. VSH_ADD_IOTUNE(write-iops-sec-max, WRITE_IOPS_SEC_MAX);
  1237. VSH_ADD_IOTUNE(size-iops-sec, SIZE_IOPS_SEC);
  1238. VSH_ADD_IOTUNE(total-bytes-sec-max-length, TOTAL_BYTES_SEC_MAX_LENGTH);
  1239. VSH_ADD_IOTUNE(read-bytes-sec-max-length, READ_BYTES_SEC_MAX_LENGTH);
  1240. VSH_ADD_IOTUNE(write-bytes-sec-max-length, WRITE_BYTES_SEC_MAX_LENGTH);
  1241. VSH_ADD_IOTUNE(total-iops-sec-max-length, TOTAL_IOPS_SEC_MAX_LENGTH);
  1242. VSH_ADD_IOTUNE(read-iops-sec-max-length, READ_IOPS_SEC_MAX_LENGTH);
  1243. VSH_ADD_IOTUNE(write-iops-sec-max-length, WRITE_IOPS_SEC_MAX_LENGTH);
  1244. #undef VSH_ADD_IOTUNE
  1245. if (vshCommandOptStringReq(ctl, cmd, "group-name", &group_name) < 0) {
  1246. vshError(ctl, "%s", _("Unable to parse group-name parameter"));
  1247. goto cleanup;
  1248. }
  1249. if (group_name) {
  1250. if (virTypedParamsAddString(&params, &nparams, &maxparams,
  1251. VIR_DOMAIN_BLOCK_IOTUNE_GROUP_NAME,
  1252. group_name) < 0)
  1253. goto save_error;
  1254. }
  1255. if (nparams == 0) {
  1256. if (virDomainGetBlockIoTune(dom, NULL, NULL, &nparams, flags) != 0) {
  1257. vshError(ctl, "%s",
  1258. _("Unable to get number of block I/O throttle parameters"));
  1259. goto cleanup;
  1260. }
  1261. if (nparams == 0) {
  1262. ret = true;
  1263. goto cleanup;
  1264. }
  1265. params = vshCalloc(ctl, nparams, sizeof(*params));
  1266. if (virDomainGetBlockIoTune(dom, disk, params, &nparams, flags) != 0) {
  1267. vshError(ctl, "%s",
  1268. _("Unable to get block I/O throttle parameters"));
  1269. goto cleanup;
  1270. }
  1271. for (i = 0; i < nparams; i++) {
  1272. char *str = vshGetTypedParamValue(ctl, &params[i]);
  1273. vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
  1274. VIR_FREE(str);
  1275. }
  1276. } else {
  1277. if (virDomainSetBlockIoTune(dom, disk, params, nparams, flags) < 0)
  1278. goto error;
  1279. }
  1280. ret = true;
  1281. cleanup:
  1282. virTypedParamsFree(params, nparams);
  1283. virshDomainFree(dom);
  1284. return ret;
  1285. save_error:
  1286. vshSaveLibvirtError();
  1287. error:
  1288. vshError(ctl, "%s", _("Unable to change block I/O throttle"));
  1289. goto cleanup;
  1290. interror:
  1291. vshError(ctl, "%s", _("Unable to parse integer parameter"));
  1292. goto cleanup;
  1293. }
  1294. /*
  1295. * "blkiotune" command
  1296. */
  1297. static const vshCmdInfo info_blkiotune[] = {
  1298. {.name = "help",
  1299. .data = N_("Get or set blkio parameters")
  1300. },
  1301. {.name = "desc",
  1302. .data = N_("Get or set the current blkio parameters for a guest"
  1303. " domain.\n"
  1304. " To get the blkio parameters use following command: \n\n"
  1305. " virsh # blkiotune <domain>")
  1306. },
  1307. {.name = NULL}
  1308. };
  1309. static const vshCmdOptDef opts_blkiotune[] = {
  1310. VIRSH_COMMON_OPT_DOMAIN_FULL(0),
  1311. {.name = "weight",
  1312. .type = VSH_OT_INT,
  1313. .help = N_("IO Weight")
  1314. },
  1315. {.name = "device-weights",
  1316. .type = VSH_OT_STRING,
  1317. .help = N_("per-device IO Weights, in the form of /path/to/device,weight,...")
  1318. },
  1319. {.name = "device-read-iops-sec",
  1320. .type = VSH_OT_STRING,
  1321. .help = N_("per-device read I/O limit per second, in the form of /path/to/device,read_iops_sec,...")
  1322. },
  1323. {.name = "device-write-iops-sec",
  1324. .type = VSH_OT_STRING,
  1325. .help = N_("per-device write I/O limit per second, in the form of /path/to/device,write_iops_sec,...")
  1326. },
  1327. {.name = "device-read-bytes-sec",
  1328. .type = VSH_OT_STRING,
  1329. .help = N_("per-device bytes read per second, in the form of /path/to/device,read_bytes_sec,...")
  1330. },
  1331. {.name = "device-write-bytes-sec",
  1332. .type = VSH_OT_STRING,
  1333. .help = N_("per-device bytes wrote per second, in the form of /path/to/device,write_bytes_sec,...")
  1334. },
  1335. VIRSH_COMMON_OPT_DOMAIN_CONFIG,
  1336. VIRSH_COMMON_OPT_DOMAIN_LIVE,
  1337. VIRSH_COMMON_OPT_DOMAIN_CURRENT,
  1338. {.name = NULL}
  1339. };
  1340. static bool
  1341. cmdBlkiotune(vshControl * ctl, const vshCmd * cmd)
  1342. {
  1343. virDomainPtr dom;
  1344. const char *device_weight = NULL;
  1345. const char *device_riops = NULL;
  1346. const char *device_wiops = NULL;
  1347. const char *device_rbps = NULL;
  1348. const char *device_wbps = NULL;
  1349. int weight = 0;
  1350. int nparams = 0;
  1351. int maxparams = 0;
  1352. int rv = 0;
  1353. size_t i;
  1354. virTypedParameterPtr params = NULL;
  1355. bool ret = false;
  1356. unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
  1357. bool current = vshCommandOptBool(cmd, "current");
  1358. bool config = vshCommandOptBool(cmd, "config");
  1359. bool live = vshCommandOptBool(cmd, "live");
  1360. VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
  1361. VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
  1362. if (config)
  1363. flags |= VIR_DOMAIN_AFFECT_CONFIG;
  1364. if (live)
  1365. flags |= VIR_DOMAIN_AFFECT_LIVE;
  1366. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  1367. return false;
  1368. if ((rv = vshCommandOptInt(ctl, cmd, "weight", &weight)) < 0) {
  1369. goto cleanup;
  1370. } else if (rv > 0) {
  1371. if (weight <= 0) {
  1372. vshError(ctl, _("Invalid value of %d for I/O weight"), weight);
  1373. goto cleanup;
  1374. }
  1375. if (virTypedParamsAddUInt(&params, &nparams, &maxparams,
  1376. VIR_DOMAIN_BLKIO_WEIGHT, weight) < 0)
  1377. goto save_error;
  1378. }
  1379. rv = vshCommandOptStringQuiet(ctl, cmd, "device-weights", &device_weight);
  1380. if (rv < 0) {
  1381. vshError(ctl, "%s", _("Unable to parse string parameter"));
  1382. goto cleanup;
  1383. } else if (rv > 0) {
  1384. if (virTypedParamsAddString(&params, &nparams, &maxparams,
  1385. VIR_DOMAIN_BLKIO_DEVICE_WEIGHT,
  1386. device_weight) < 0)
  1387. goto save_error;
  1388. }
  1389. rv = vshCommandOptStringQuiet(ctl, cmd, "device-read-iops-sec", &device_riops);
  1390. if (rv < 0) {
  1391. vshError(ctl, "%s", _("Unable to parse string parameter"));
  1392. goto cleanup;
  1393. } else if (rv > 0) {
  1394. if (virTypedParamsAddString(&params, &nparams, &maxparams,
  1395. VIR_DOMAIN_BLKIO_DEVICE_READ_IOPS,
  1396. device_riops) < 0)
  1397. goto save_error;
  1398. }
  1399. rv = vshCommandOptStringQuiet(ctl, cmd, "device-write-iops-sec", &device_wiops);
  1400. if (rv < 0) {
  1401. vshError(ctl, "%s", _("Unable to parse string parameter"));
  1402. goto cleanup;
  1403. } else if (rv > 0) {
  1404. if (virTypedParamsAddString(&params, &nparams, &maxparams,
  1405. VIR_DOMAIN_BLKIO_DEVICE_WRITE_IOPS,
  1406. device_wiops) < 0)
  1407. goto save_error;
  1408. }
  1409. rv = vshCommandOptStringQuiet(ctl, cmd, "device-read-bytes-sec", &device_rbps);
  1410. if (rv < 0) {
  1411. vshError(ctl, "%s", _("Unable to parse string parameter"));
  1412. goto cleanup;
  1413. } else if (rv > 0) {
  1414. if (virTypedParamsAddString(&params, &nparams, &maxparams,
  1415. VIR_DOMAIN_BLKIO_DEVICE_READ_BPS,
  1416. device_rbps) < 0)
  1417. goto save_error;
  1418. }
  1419. rv = vshCommandOptStringQuiet(ctl, cmd, "device-write-bytes-sec", &device_wbps);
  1420. if (rv < 0) {
  1421. vshError(ctl, "%s", _("Unable to parse string parameter"));
  1422. goto cleanup;
  1423. } else if (rv > 0) {
  1424. if (virTypedParamsAddString(&params, &nparams, &maxparams,
  1425. VIR_DOMAIN_BLKIO_DEVICE_WRITE_BPS,
  1426. device_wbps) < 0)
  1427. goto save_error;
  1428. }
  1429. if (nparams == 0) {
  1430. /* get the number of blkio parameters */
  1431. if (virDomainGetBlkioParameters(dom, NULL, &nparams, flags) != 0) {
  1432. vshError(ctl, "%s",
  1433. _("Unable to get number of blkio parameters"));
  1434. goto cleanup;
  1435. }
  1436. if (nparams == 0) {
  1437. /* nothing to output */
  1438. ret = true;
  1439. goto cleanup;
  1440. }
  1441. /* now go get all the blkio parameters */
  1442. params = vshCalloc(ctl, nparams, sizeof(*params));
  1443. if (virDomainGetBlkioParameters(dom, params, &nparams, flags) != 0) {
  1444. vshError(ctl, "%s", _("Unable to get blkio parameters"));
  1445. goto cleanup;
  1446. }
  1447. for (i = 0; i < nparams; i++) {
  1448. char *str = vshGetTypedParamValue(ctl, &params[i]);
  1449. vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
  1450. VIR_FREE(str);
  1451. }
  1452. } else {
  1453. /* set the blkio parameters */
  1454. if (virDomainSetBlkioParameters(dom, params, nparams, flags) < 0)
  1455. goto error;
  1456. }
  1457. ret = true;
  1458. cleanup:
  1459. virTypedParamsFree(params, nparams);
  1460. virshDomainFree(dom);
  1461. return ret;
  1462. save_error:
  1463. vshSaveLibvirtError();
  1464. error:
  1465. vshError(ctl, "%s", _("Unable to change blkio parameters"));
  1466. goto cleanup;
  1467. }
  1468. static void
  1469. virshPrintJobProgress(const char *label, unsigned long long remaining,
  1470. unsigned long long total)
  1471. {
  1472. int progress;
  1473. if (remaining == 0) {
  1474. /* migration has completed */
  1475. progress = 100;
  1476. } else {
  1477. /* use float to avoid overflow */
  1478. progress = (int)(100.0 - remaining * 100.0 / total);
  1479. if (progress >= 100) {
  1480. /* migration has not completed, do not print [100 %] */
  1481. progress = 99;
  1482. }
  1483. }
  1484. /* see comments in vshError about why we must flush */
  1485. fflush(stdout);
  1486. fprintf(stderr, "\r%s: [%3d %%]", label, progress);
  1487. fflush(stderr);
  1488. }
  1489. static volatile sig_atomic_t intCaught;
  1490. #ifndef WIN32
  1491. static void virshCatchInt(int sig G_GNUC_UNUSED,
  1492. siginfo_t *siginfo G_GNUC_UNUSED,
  1493. void *context G_GNUC_UNUSED)
  1494. {
  1495. intCaught = 1;
  1496. }
  1497. #endif /* !WIN32 */
  1498. typedef struct _virshBlockJobWaitData virshBlockJobWaitData;
  1499. typedef virshBlockJobWaitData *virshBlockJobWaitDataPtr;
  1500. struct _virshBlockJobWaitData {
  1501. vshControl *ctl;
  1502. virDomainPtr dom;
  1503. const char *dev;
  1504. const char *job_name;
  1505. bool verbose;
  1506. unsigned int timeout;
  1507. bool async_abort;
  1508. int cb_id;
  1509. int cb_id2;
  1510. int status;
  1511. };
  1512. static void
  1513. virshBlockJobStatusHandler(virConnectPtr conn G_GNUC_UNUSED,
  1514. virDomainPtr dom G_GNUC_UNUSED,
  1515. const char *disk,
  1516. int type G_GNUC_UNUSED,
  1517. int status,
  1518. void *opaque)
  1519. {
  1520. virshBlockJobWaitDataPtr data = opaque;
  1521. if (STREQ_NULLABLE(disk, data->dev))
  1522. data->status = status;
  1523. }
  1524. /**
  1525. * virshBlockJobWaitInit:
  1526. * @ctl: vsh control structure
  1527. * @dom: domain object
  1528. * @dev: block device name to wait for
  1529. * @job_name: block job name to display in user-facing messages
  1530. * @verbose: enable progress reporting
  1531. * @timeout: number of milliseconds to wait before aborting the job
  1532. * @async_abort: abort the job asynchronously
  1533. *
  1534. * Prepares virsh for waiting for completion of a block job. This function
  1535. * registers event handlers for block job events and prepares the data structures
  1536. * for them. A call to virshBlockJobWait then waits for completion of the given
  1537. * block job. This function should be tolerant to different versions of daemon
  1538. * and the reporting capabilities of those.
  1539. *
  1540. * Returns the data structure that holds data needed for block job waiting or
  1541. * NULL in case of error.
  1542. */
  1543. static virshBlockJobWaitDataPtr
  1544. virshBlockJobWaitInit(vshControl *ctl,
  1545. virDomainPtr dom,
  1546. const char *dev,
  1547. const char *job_name,
  1548. bool verbose,
  1549. unsigned int timeout,
  1550. bool async_abort)
  1551. {
  1552. virshBlockJobWaitDataPtr ret;
  1553. virshControlPtr priv = ctl->privData;
  1554. if (VIR_ALLOC(ret) < 0)
  1555. return NULL;
  1556. ret->ctl = ctl;
  1557. ret->dom = dom;
  1558. ret->dev = dev;
  1559. ret->job_name = job_name;
  1560. ret->async_abort = async_abort;
  1561. ret->timeout = timeout;
  1562. ret->verbose = verbose;
  1563. ret->status = -1;
  1564. virConnectDomainEventGenericCallback cb =
  1565. VIR_DOMAIN_EVENT_CALLBACK(virshBlockJobStatusHandler);
  1566. if ((ret->cb_id = virConnectDomainEventRegisterAny(priv->conn, dom,
  1567. VIR_DOMAIN_EVENT_ID_BLOCK_JOB,
  1568. cb, ret, NULL)) < 0)
  1569. vshResetLibvirtError();
  1570. if ((ret->cb_id2 = virConnectDomainEventRegisterAny(priv->conn, dom,
  1571. VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2,
  1572. cb, ret, NULL)) < 0)
  1573. vshResetLibvirtError();
  1574. return ret;
  1575. }
  1576. static void
  1577. virshBlockJobWaitFree(virshBlockJobWaitDataPtr data)
  1578. {
  1579. virshControlPtr priv = NULL;
  1580. if (!data)
  1581. return;
  1582. priv = data->ctl->privData;
  1583. if (data->cb_id >= 0)
  1584. virConnectDomainEventDeregisterAny(priv->conn, data->cb_id);
  1585. if (data->cb_id2 >= 0)
  1586. virConnectDomainEventDeregisterAny(priv->conn, data->cb_id2);
  1587. VIR_FREE(data);
  1588. }
  1589. /**
  1590. * virshBlockJobWait:
  1591. * @data: private data initialized by virshBlockJobWaitInit
  1592. *
  1593. * Waits for the block job to complete. This function prefers to wait for a
  1594. * matching VIR_DOMAIN_EVENT_ID_BLOCK_JOB or VIR_DOMAIN_EVENT_ID_BLOCK_JOB_2
  1595. * event from libvirt; however, it has a fallback mode should either of these
  1596. * events not be available.
  1597. *
  1598. * This function returns values from the virConnectDomainEventBlockJobStatus
  1599. * enum or -1 in case of an internal error.
  1600. *
  1601. * If the fallback mode is activated the returned event is
  1602. * VIR_DOMAIN_BLOCK_JOB_COMPLETED if the block job vanishes or
  1603. * VIR_DOMAIN_BLOCK_JOB_READY if the block job reaches 100%.
  1604. */
  1605. static int
  1606. virshBlockJobWait(virshBlockJobWaitDataPtr data)
  1607. {
  1608. /* For two phase jobs like active commit or block copy, the marker reaches
  1609. * 100% and an event fires. In case where virsh would not be able to match
  1610. * the event to the given block job we will wait for the number of retries
  1611. * before claiming that we entered synchronised phase */
  1612. unsigned int retries = 5;
  1613. #ifndef WIN32
  1614. struct sigaction sig_action;
  1615. struct sigaction old_sig_action;
  1616. sigset_t sigmask, oldsigmask;
  1617. #endif /* !WIN32 */
  1618. unsigned long long start = 0;
  1619. unsigned long long curr = 0;
  1620. unsigned int abort_flags = 0;
  1621. int ret = -1;
  1622. virDomainBlockJobInfo info, last;
  1623. int result;
  1624. if (!data)
  1625. return 0;
  1626. if (data->async_abort)
  1627. abort_flags |= VIR_DOMAIN_BLOCK_JOB_ABORT_ASYNC;
  1628. #ifndef WIN32
  1629. sigemptyset(&sigmask);
  1630. sigaddset(&sigmask, SIGINT);
  1631. intCaught = 0;
  1632. sig_action.sa_sigaction = virshCatchInt;
  1633. sig_action.sa_flags = SA_SIGINFO;
  1634. sigemptyset(&sig_action.sa_mask);
  1635. sigaction(SIGINT, &sig_action, &old_sig_action);
  1636. #endif /* !WIN32 */
  1637. if (data->timeout && virTimeMillisNow(&start) < 0) {
  1638. vshSaveLibvirtError();
  1639. goto cleanup;
  1640. }
  1641. last.cur = last.end = 0;
  1642. while (true) {
  1643. #ifndef WIN32
  1644. pthread_sigmask(SIG_BLOCK, &sigmask, &oldsigmask);
  1645. #endif /* !WIN32 */
  1646. result = virDomainGetBlockJobInfo(data->dom, data->dev, &info, 0);
  1647. #ifndef WIN32
  1648. pthread_sigmask(SIG_SETMASK, &oldsigmask, NULL);
  1649. #endif /* !WIN32 */
  1650. if (result < 0) {
  1651. vshError(data->ctl, _("failed to query job for disk %s"), data->dev);
  1652. goto cleanup;
  1653. }
  1654. /* If either callback could be registered and we've got an event, we can
  1655. * can end the waiting loop */
  1656. if ((data->cb_id >= 0 || data->cb_id2 >= 0) && data->status != -1) {
  1657. ret = data->status;
  1658. break;
  1659. }
  1660. /* Fallback behaviour is only needed if one or both callbacks could not
  1661. * be registered */
  1662. if (data->cb_id < 0 || data->cb_id2 < 0) {
  1663. /* If the block job vanishes, synthesize a COMPLETED event */
  1664. if (result == 0) {
  1665. ret = VIR_DOMAIN_BLOCK_JOB_COMPLETED;
  1666. break;
  1667. }
  1668. /* If the block job hits 100%, wait a little while for a possible
  1669. * event from libvirt unless both callbacks could not be registered
  1670. * in order to synthesize our own READY event */
  1671. if (info.end == info.cur &&
  1672. ((data->cb_id < 0 && data->cb_id2 < 0) || --retries == 0)) {
  1673. ret = VIR_DOMAIN_BLOCK_JOB_READY;
  1674. break;
  1675. }
  1676. }
  1677. if (data->verbose && (info.cur != last.cur || info.end != last.end))
  1678. virshPrintJobProgress(data->job_name, info.end - info.cur,
  1679. info.end);
  1680. last = info;
  1681. if (data->timeout && virTimeMillisNow(&curr) < 0) {
  1682. vshSaveLibvirtError();
  1683. goto cleanup;
  1684. }
  1685. if (intCaught || (data->timeout && (curr - start > data->timeout))) {
  1686. if (virDomainBlockJobAbort(data->dom, data->dev, abort_flags) < 0) {
  1687. vshError(data->ctl, _("failed to abort job for disk '%s'"),
  1688. data->dev);
  1689. goto cleanup;
  1690. }
  1691. ret = VIR_DOMAIN_BLOCK_JOB_CANCELED;
  1692. break;
  1693. }
  1694. g_usleep(500 * 1000);
  1695. }
  1696. /* print 100% completed */
  1697. if (data->verbose &&
  1698. (ret == VIR_DOMAIN_BLOCK_JOB_COMPLETED ||
  1699. ret == VIR_DOMAIN_BLOCK_JOB_READY))
  1700. virshPrintJobProgress(data->job_name, 0, 1);
  1701. cleanup:
  1702. #ifndef WIN32
  1703. sigaction(SIGINT, &old_sig_action, NULL);
  1704. #endif /* !WIN32 */
  1705. return ret;
  1706. }
  1707. /*
  1708. * "blockcommit" command
  1709. */
  1710. static const vshCmdInfo info_blockcommit[] = {
  1711. {.name = "help",
  1712. .data = N_("Start a block commit operation.")
  1713. },
  1714. {.name = "desc",
  1715. .data = N_("Commit changes from a snapshot down to its backing image.")
  1716. },
  1717. {.name = NULL}
  1718. };
  1719. static const vshCmdOptDef opts_blockcommit[] = {
  1720. VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
  1721. {.name = "path",
  1722. .type = VSH_OT_DATA,
  1723. .flags = VSH_OFLAG_REQ,
  1724. .completer = virshDomainDiskTargetCompleter,
  1725. .help = N_("fully-qualified path of disk")
  1726. },
  1727. {.name = "bandwidth",
  1728. .type = VSH_OT_INT,
  1729. .help = N_("bandwidth limit in MiB/s")
  1730. },
  1731. {.name = "base",
  1732. .type = VSH_OT_STRING,
  1733. .help = N_("path of base file to commit into (default bottom of chain)")
  1734. },
  1735. {.name = "shallow",
  1736. .type = VSH_OT_BOOL,
  1737. .help = N_("use backing file of top as base")
  1738. },
  1739. {.name = "top",
  1740. .type = VSH_OT_STRING,
  1741. .help = N_("path of top file to commit from (default top of chain)")
  1742. },
  1743. {.name = "active",
  1744. .type = VSH_OT_BOOL,
  1745. .help = N_("trigger two-stage active commit of top file")
  1746. },
  1747. {.name = "delete",
  1748. .type = VSH_OT_BOOL,
  1749. .help = N_("delete files that were successfully committed")
  1750. },
  1751. {.name = "wait",
  1752. .type = VSH_OT_BOOL,
  1753. .help = N_("wait for job to complete "
  1754. "(with --active, wait for job to sync)")
  1755. },
  1756. {.name = "verbose",
  1757. .type = VSH_OT_BOOL,
  1758. .help = N_("with --wait, display the progress")
  1759. },
  1760. {.name = "timeout",
  1761. .type = VSH_OT_INT,
  1762. .help = N_("implies --wait, abort if copy exceeds timeout (in seconds)")
  1763. },
  1764. {.name = "pivot",
  1765. .type = VSH_OT_BOOL,
  1766. .help = N_("implies --active --wait, pivot when commit is synced")
  1767. },
  1768. {.name = "keep-overlay",
  1769. .type = VSH_OT_BOOL,
  1770. .help = N_("implies --active --wait, quit when commit is synced")
  1771. },
  1772. {.name = "async",
  1773. .type = VSH_OT_BOOL,
  1774. .help = N_("with --wait, don't wait for cancel to finish")
  1775. },
  1776. {.name = "keep-relative",
  1777. .type = VSH_OT_BOOL,
  1778. .help = N_("keep the backing chain relatively referenced")
  1779. },
  1780. {.name = "bytes",
  1781. .type = VSH_OT_BOOL,
  1782. .help = N_("the bandwidth limit is in bytes/s rather than MiB/s")
  1783. },
  1784. {.name = NULL}
  1785. };
  1786. static bool
  1787. cmdBlockcommit(vshControl *ctl, const vshCmd *cmd)
  1788. {
  1789. virDomainPtr dom = NULL;
  1790. bool ret = false;
  1791. bool verbose = vshCommandOptBool(cmd, "verbose");
  1792. bool pivot = vshCommandOptBool(cmd, "pivot");
  1793. bool finish = vshCommandOptBool(cmd, "keep-overlay");
  1794. bool active = vshCommandOptBool(cmd, "active") || pivot || finish;
  1795. bool blocking = vshCommandOptBool(cmd, "wait") || pivot || finish;
  1796. bool async = vshCommandOptBool(cmd, "async");
  1797. bool bytes = vshCommandOptBool(cmd, "bytes");
  1798. int timeout = 0;
  1799. const char *path = NULL;
  1800. const char *base = NULL;
  1801. const char *top = NULL;
  1802. int abort_flags = 0;
  1803. unsigned int flags = 0;
  1804. unsigned long bandwidth = 0;
  1805. virshBlockJobWaitDataPtr bjWait = NULL;
  1806. VSH_EXCLUSIVE_OPTIONS("pivot", "keep-overlay");
  1807. if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0)
  1808. return false;
  1809. if (vshCommandOptStringReq(ctl, cmd, "base", &base) < 0)
  1810. return false;
  1811. if (vshCommandOptStringReq(ctl, cmd, "top", &top) < 0)
  1812. return false;
  1813. if (vshBlockJobOptionBandwidth(ctl, cmd, bytes, &bandwidth) < 0)
  1814. return false;
  1815. if (bytes)
  1816. flags |= VIR_DOMAIN_BLOCK_COMMIT_BANDWIDTH_BYTES;
  1817. if (vshCommandOptBool(cmd, "shallow"))
  1818. flags |= VIR_DOMAIN_BLOCK_COMMIT_SHALLOW;
  1819. if (vshCommandOptBool(cmd, "delete"))
  1820. flags |= VIR_DOMAIN_BLOCK_COMMIT_DELETE;
  1821. if (active)
  1822. flags |= VIR_DOMAIN_BLOCK_COMMIT_ACTIVE;
  1823. if (vshCommandOptBool(cmd, "keep-relative"))
  1824. flags |= VIR_DOMAIN_BLOCK_COMMIT_RELATIVE;
  1825. if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
  1826. return false;
  1827. if (timeout)
  1828. blocking = true;
  1829. if (!blocking) {
  1830. if (verbose) {
  1831. vshError(ctl, "%s", _("--verbose requires at least one of --timeout, "
  1832. "--wait, --pivot, or --keep-overlay"));
  1833. return false;
  1834. }
  1835. if (async) {
  1836. vshError(ctl, "%s", _("--async requires at least one of --timeout, "
  1837. "--wait, --pivot, or --keep-overlay"));
  1838. return false;
  1839. }
  1840. }
  1841. if (async)
  1842. abort_flags |= VIR_DOMAIN_BLOCK_JOB_ABORT_ASYNC;
  1843. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  1844. return false;
  1845. if (blocking &&
  1846. !(bjWait = virshBlockJobWaitInit(ctl, dom, path, _("Block commit"),
  1847. verbose, timeout, async)))
  1848. goto cleanup;
  1849. if (virDomainBlockCommit(dom, path, base, top, bandwidth, flags) < 0)
  1850. goto cleanup;
  1851. if (!blocking) {
  1852. if (active)
  1853. vshPrintExtra(ctl, "%s", _("Active Block Commit started"));
  1854. else
  1855. vshPrintExtra(ctl, "%s", _("Block Commit started"));
  1856. ret = true;
  1857. goto cleanup;
  1858. }
  1859. /* Execution continues here only if --wait or friends were specified */
  1860. switch (virshBlockJobWait(bjWait)) {
  1861. case -1:
  1862. goto cleanup;
  1863. case VIR_DOMAIN_BLOCK_JOB_CANCELED:
  1864. vshPrintExtra(ctl, "\n%s", _("Commit aborted"));
  1865. goto cleanup;
  1866. break;
  1867. case VIR_DOMAIN_BLOCK_JOB_FAILED:
  1868. vshPrintExtra(ctl, "\n%s", _("Commit failed"));
  1869. goto cleanup;
  1870. break;
  1871. case VIR_DOMAIN_BLOCK_JOB_READY:
  1872. case VIR_DOMAIN_BLOCK_JOB_COMPLETED:
  1873. break;
  1874. }
  1875. if (active) {
  1876. if (pivot) {
  1877. abort_flags |= VIR_DOMAIN_BLOCK_JOB_ABORT_PIVOT;
  1878. if (virDomainBlockJobAbort(dom, path, abort_flags) < 0) {
  1879. vshError(ctl, _("failed to pivot job for disk %s"), path);
  1880. goto cleanup;
  1881. }
  1882. vshPrintExtra(ctl, "\n%s", _("Successfully pivoted"));
  1883. } else if (finish) {
  1884. if (virDomainBlockJobAbort(dom, path, abort_flags) < 0) {
  1885. vshError(ctl, _("failed to finish job for disk %s"), path);
  1886. goto cleanup;
  1887. }
  1888. vshPrintExtra(ctl, "\n%s", _("Commit complete, overlay "
  1889. "image kept"));
  1890. } else {
  1891. vshPrintExtra(ctl, "\n%s", _("Now in synchronized phase"));
  1892. }
  1893. } else {
  1894. vshPrintExtra(ctl, "\n%s", _("Commit complete"));
  1895. }
  1896. ret = true;
  1897. cleanup:
  1898. virshDomainFree(dom);
  1899. virshBlockJobWaitFree(bjWait);
  1900. return ret;
  1901. }
  1902. /*
  1903. * "blockcopy" command
  1904. */
  1905. static const vshCmdInfo info_blockcopy[] = {
  1906. {.name = "help",
  1907. .data = N_("Start a block copy operation.")
  1908. },
  1909. {.name = "desc",
  1910. .data = N_("Copy a disk backing image chain to dest.")
  1911. },
  1912. {.name = NULL}
  1913. };
  1914. static const vshCmdOptDef opts_blockcopy[] = {
  1915. VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
  1916. {.name = "path",
  1917. .type = VSH_OT_DATA,
  1918. .flags = VSH_OFLAG_REQ,
  1919. .completer = virshDomainDiskTargetCompleter,
  1920. .help = N_("fully-qualified path of source disk")
  1921. },
  1922. {.name = "dest",
  1923. .type = VSH_OT_STRING,
  1924. .help = N_("path of the copy to create")
  1925. },
  1926. {.name = "bandwidth",
  1927. .type = VSH_OT_INT,
  1928. .help = N_("bandwidth limit in MiB/s")
  1929. },
  1930. {.name = "shallow",
  1931. .type = VSH_OT_BOOL,
  1932. .help = N_("make the copy share a backing chain")
  1933. },
  1934. {.name = "reuse-external",
  1935. .type = VSH_OT_BOOL,
  1936. .help = N_("reuse existing destination")
  1937. },
  1938. {.name = "raw",
  1939. .type = VSH_OT_ALIAS,
  1940. .help = "format=raw"
  1941. },
  1942. {.name = "blockdev",
  1943. .type = VSH_OT_BOOL,
  1944. .help = N_("copy destination is block device instead of regular file")
  1945. },
  1946. {.name = "wait",
  1947. .type = VSH_OT_BOOL,
  1948. .help = N_("wait for job to reach mirroring phase")
  1949. },
  1950. {.name = "verbose",
  1951. .type = VSH_OT_BOOL,
  1952. .help = N_("with --wait, display the progress")
  1953. },
  1954. {.name = "timeout",
  1955. .type = VSH_OT_INT,
  1956. .help = N_("implies --wait, abort if copy exceeds timeout (in seconds)")
  1957. },
  1958. {.name = "pivot",
  1959. .type = VSH_OT_BOOL,
  1960. .help = N_("implies --wait, pivot when mirroring starts")
  1961. },
  1962. {.name = "finish",
  1963. .type = VSH_OT_BOOL,
  1964. .help = N_("implies --wait, quit when mirroring starts")
  1965. },
  1966. {.name = "async",
  1967. .type = VSH_OT_BOOL,
  1968. .help = N_("with --wait, don't wait for cancel to finish")
  1969. },
  1970. {.name = "xml",
  1971. .type = VSH_OT_STRING,
  1972. .help = N_("filename containing XML description of the copy destination")
  1973. },
  1974. {.name = "format",
  1975. .type = VSH_OT_STRING,
  1976. .help = N_("format of the destination file")
  1977. },
  1978. {.name = "granularity",
  1979. .type = VSH_OT_INT,
  1980. .help = N_("power-of-two granularity to use during the copy")
  1981. },
  1982. {.name = "buf-size",
  1983. .type = VSH_OT_INT,
  1984. .help = N_("maximum amount of in-flight data during the copy")
  1985. },
  1986. {.name = "bytes",
  1987. .type = VSH_OT_BOOL,
  1988. .help = N_("the bandwidth limit is in bytes/s rather than MiB/s")
  1989. },
  1990. {.name = "transient-job",
  1991. .type = VSH_OT_BOOL,
  1992. .help = N_("the copy job is not persisted if VM is turned off")
  1993. },
  1994. {.name = NULL}
  1995. };
  1996. static bool
  1997. cmdBlockcopy(vshControl *ctl, const vshCmd *cmd)
  1998. {
  1999. virDomainPtr dom = NULL;
  2000. const char *dest = NULL;
  2001. const char *format = NULL;
  2002. unsigned long bandwidth = 0;
  2003. unsigned int granularity = 0;
  2004. unsigned long long buf_size = 0;
  2005. unsigned int flags = 0;
  2006. bool ret = false;
  2007. bool verbose = vshCommandOptBool(cmd, "verbose");
  2008. bool pivot = vshCommandOptBool(cmd, "pivot");
  2009. bool finish = vshCommandOptBool(cmd, "finish");
  2010. bool blockdev = vshCommandOptBool(cmd, "blockdev");
  2011. bool blocking = vshCommandOptBool(cmd, "wait") || finish || pivot;
  2012. bool async = vshCommandOptBool(cmd, "async");
  2013. bool bytes = vshCommandOptBool(cmd, "bytes");
  2014. bool transientjob = vshCommandOptBool(cmd, "transient-job");
  2015. int timeout = 0;
  2016. const char *path = NULL;
  2017. int abort_flags = 0;
  2018. const char *xml = NULL;
  2019. char *xmlstr = NULL;
  2020. virTypedParameterPtr params = NULL;
  2021. virshBlockJobWaitDataPtr bjWait = NULL;
  2022. int nparams = 0;
  2023. if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0)
  2024. return false;
  2025. if (vshCommandOptStringReq(ctl, cmd, "dest", &dest) < 0)
  2026. return false;
  2027. if (vshCommandOptStringReq(ctl, cmd, "xml", &xml) < 0)
  2028. return false;
  2029. if (vshCommandOptStringReq(ctl, cmd, "format", &format) < 0)
  2030. return false;
  2031. if (vshBlockJobOptionBandwidth(ctl, cmd, bytes, &bandwidth) < 0)
  2032. return false;
  2033. if (vshCommandOptUInt(ctl, cmd, "granularity", &granularity) < 0)
  2034. return false;
  2035. if (vshCommandOptULongLong(ctl, cmd, "buf-size", &buf_size) < 0)
  2036. return false;
  2037. /* Exploit that some VIR_DOMAIN_BLOCK_REBASE_* and
  2038. * VIR_DOMAIN_BLOCK_COPY_* flags have the same values. */
  2039. if (vshCommandOptBool(cmd, "shallow"))
  2040. flags |= VIR_DOMAIN_BLOCK_REBASE_SHALLOW;
  2041. if (vshCommandOptBool(cmd, "reuse-external"))
  2042. flags |= VIR_DOMAIN_BLOCK_REBASE_REUSE_EXT;
  2043. if (transientjob)
  2044. flags |= VIR_DOMAIN_BLOCK_COPY_TRANSIENT_JOB;
  2045. if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
  2046. return false;
  2047. if (timeout)
  2048. blocking = true;
  2049. if (async)
  2050. abort_flags |= VIR_DOMAIN_BLOCK_JOB_ABORT_ASYNC;
  2051. VSH_EXCLUSIVE_OPTIONS_VAR(dest, xml);
  2052. VSH_EXCLUSIVE_OPTIONS_VAR(format, xml);
  2053. VSH_EXCLUSIVE_OPTIONS_VAR(blockdev, xml);
  2054. VSH_EXCLUSIVE_OPTIONS_VAR(pivot, finish);
  2055. if (!dest && !xml) {
  2056. vshError(ctl, "%s", _("need either --dest or --xml"));
  2057. return false;
  2058. }
  2059. if (!blocking) {
  2060. if (verbose) {
  2061. vshError(ctl, "%s", _("--verbose requires at least one of --timeout, "
  2062. "--wait, --pivot, or --finish"));
  2063. return false;
  2064. }
  2065. if (async) {
  2066. vshError(ctl, "%s", _("--async requires at least one of --timeout, "
  2067. "--wait, --pivot, or --finish"));
  2068. return false;
  2069. }
  2070. }
  2071. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  2072. goto cleanup;
  2073. if (blocking &&
  2074. !(bjWait = virshBlockJobWaitInit(ctl, dom, path, _("Block Copy"),
  2075. verbose, timeout, async)))
  2076. goto cleanup;
  2077. if (xml) {
  2078. if (virFileReadAll(xml, VSH_MAX_XML_FILE, &xmlstr) < 0) {
  2079. vshReportError(ctl);
  2080. goto cleanup;
  2081. }
  2082. }
  2083. if (granularity || buf_size || (format && STRNEQ(format, "raw")) || xml ||
  2084. transientjob) {
  2085. /* New API */
  2086. if (bandwidth || granularity || buf_size) {
  2087. params = vshCalloc(ctl, 3, sizeof(*params));
  2088. if (bandwidth) {
  2089. if (!bytes) {
  2090. /* bandwidth is ulong MiB/s, but the typed parameter is
  2091. * ullong bytes/s; make sure we don't overflow */
  2092. unsigned long long limit = MIN(ULONG_MAX, ULLONG_MAX >> 20);
  2093. if (bandwidth > limit) {
  2094. vshError(ctl, _("bandwidth must be less than %llu"), limit);
  2095. goto cleanup;
  2096. }
  2097. bandwidth <<= 20ULL;
  2098. }
  2099. if (virTypedParameterAssign(&params[nparams++],
  2100. VIR_DOMAIN_BLOCK_COPY_BANDWIDTH,
  2101. VIR_TYPED_PARAM_ULLONG,
  2102. bandwidth) < 0)
  2103. goto cleanup;
  2104. }
  2105. if (granularity &&
  2106. virTypedParameterAssign(&params[nparams++],
  2107. VIR_DOMAIN_BLOCK_COPY_GRANULARITY,
  2108. VIR_TYPED_PARAM_UINT,
  2109. granularity) < 0)
  2110. goto cleanup;
  2111. if (buf_size &&
  2112. virTypedParameterAssign(&params[nparams++],
  2113. VIR_DOMAIN_BLOCK_COPY_BUF_SIZE,
  2114. VIR_TYPED_PARAM_ULLONG,
  2115. buf_size) < 0)
  2116. goto cleanup;
  2117. }
  2118. if (!xmlstr) {
  2119. virBuffer buf = VIR_BUFFER_INITIALIZER;
  2120. virBufferAsprintf(&buf, "<disk type='%s'>\n",
  2121. blockdev ? "block" : "file");
  2122. virBufferAdjustIndent(&buf, 2);
  2123. virBufferAsprintf(&buf, "<source %s", blockdev ? "dev" : "file");
  2124. virBufferEscapeString(&buf, "='%s'/>\n", dest);
  2125. virBufferEscapeString(&buf, "<driver type='%s'/>\n", format);
  2126. virBufferAdjustIndent(&buf, -2);
  2127. virBufferAddLit(&buf, "</disk>\n");
  2128. xmlstr = virBufferContentAndReset(&buf);
  2129. }
  2130. if (virDomainBlockCopy(dom, path, xmlstr, params, nparams, flags) < 0)
  2131. goto cleanup;
  2132. } else {
  2133. /* Old API */
  2134. flags |= VIR_DOMAIN_BLOCK_REBASE_COPY;
  2135. if (blockdev)
  2136. flags |= VIR_DOMAIN_BLOCK_REBASE_COPY_DEV;
  2137. if (STREQ_NULLABLE(format, "raw"))
  2138. flags |= VIR_DOMAIN_BLOCK_REBASE_COPY_RAW;
  2139. if (bytes)
  2140. flags |= VIR_DOMAIN_BLOCK_REBASE_BANDWIDTH_BYTES;
  2141. if (virDomainBlockRebase(dom, path, dest, bandwidth, flags) < 0)
  2142. goto cleanup;
  2143. }
  2144. if (!blocking) {
  2145. vshPrintExtra(ctl, "%s", _("Block Copy started"));
  2146. ret = true;
  2147. goto cleanup;
  2148. }
  2149. /* Execution continues here only if --wait or friends were specified */
  2150. switch (virshBlockJobWait(bjWait)) {
  2151. case -1:
  2152. goto cleanup;
  2153. case VIR_DOMAIN_BLOCK_JOB_CANCELED:
  2154. vshPrintExtra(ctl, "\n%s", _("Copy aborted"));
  2155. goto cleanup;
  2156. break;
  2157. case VIR_DOMAIN_BLOCK_JOB_FAILED:
  2158. vshPrintExtra(ctl, "\n%s", _("Copy failed"));
  2159. goto cleanup;
  2160. break;
  2161. case VIR_DOMAIN_BLOCK_JOB_READY:
  2162. case VIR_DOMAIN_BLOCK_JOB_COMPLETED:
  2163. break;
  2164. }
  2165. if (pivot) {
  2166. abort_flags |= VIR_DOMAIN_BLOCK_JOB_ABORT_PIVOT;
  2167. if (virDomainBlockJobAbort(dom, path, abort_flags) < 0) {
  2168. vshError(ctl, _("failed to pivot job for disk %s"), path);
  2169. goto cleanup;
  2170. }
  2171. vshPrintExtra(ctl, "\n%s", _("Successfully pivoted"));
  2172. } else if (finish) {
  2173. if (virDomainBlockJobAbort(dom, path, abort_flags) < 0) {
  2174. vshError(ctl, _("failed to finish job for disk %s"), path);
  2175. goto cleanup;
  2176. }
  2177. vshPrintExtra(ctl, "\n%s", _("Successfully copied"));
  2178. } else {
  2179. vshPrintExtra(ctl, "\n%s", _("Now in mirroring phase"));
  2180. }
  2181. ret = true;
  2182. cleanup:
  2183. VIR_FREE(xmlstr);
  2184. virTypedParamsFree(params, nparams);
  2185. virshDomainFree(dom);
  2186. virshBlockJobWaitFree(bjWait);
  2187. return ret;
  2188. }
  2189. /*
  2190. * "blockjob" command
  2191. */
  2192. static const vshCmdInfo info_blockjob[] = {
  2193. {.name = "help",
  2194. .data = N_("Manage active block operations")
  2195. },
  2196. {.name = "desc",
  2197. .data = N_("Query, adjust speed, or cancel active block operations.")
  2198. },
  2199. {.name = NULL}
  2200. };
  2201. static const vshCmdOptDef opts_blockjob[] = {
  2202. VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
  2203. {.name = "path",
  2204. .type = VSH_OT_DATA,
  2205. .flags = VSH_OFLAG_REQ,
  2206. .completer = virshDomainDiskTargetCompleter,
  2207. .help = N_("fully-qualified path of disk")
  2208. },
  2209. {.name = "abort",
  2210. .type = VSH_OT_BOOL,
  2211. .help = N_("abort the active job on the specified disk")
  2212. },
  2213. {.name = "async",
  2214. .type = VSH_OT_BOOL,
  2215. .help = N_("implies --abort; request but don't wait for job end")
  2216. },
  2217. {.name = "pivot",
  2218. .type = VSH_OT_BOOL,
  2219. .help = N_("implies --abort; conclude and pivot a copy or commit job")
  2220. },
  2221. {.name = "info",
  2222. .type = VSH_OT_BOOL,
  2223. .help = N_("get active job information for the specified disk")
  2224. },
  2225. {.name = "bytes",
  2226. .type = VSH_OT_BOOL,
  2227. .help = N_("get/set bandwidth in bytes rather than MiB/s")
  2228. },
  2229. {.name = "raw",
  2230. .type = VSH_OT_BOOL,
  2231. .help = N_("implies --info; output details rather than human summary")
  2232. },
  2233. {.name = "bandwidth",
  2234. .type = VSH_OT_INT,
  2235. .help = N_("set the bandwidth limit in MiB/s")
  2236. },
  2237. {.name = NULL}
  2238. };
  2239. VIR_ENUM_DECL(virshDomainBlockJob);
  2240. VIR_ENUM_IMPL(virshDomainBlockJob,
  2241. VIR_DOMAIN_BLOCK_JOB_TYPE_LAST,
  2242. N_("Unknown job"),
  2243. N_("Block Pull"),
  2244. N_("Block Copy"),
  2245. N_("Block Commit"),
  2246. N_("Active Block Commit"),
  2247. N_("Backup"),
  2248. );
  2249. static const char *
  2250. virshDomainBlockJobToString(int type)
  2251. {
  2252. const char *str = virshDomainBlockJobTypeToString(type);
  2253. return str ? _(str) : _("Unknown job");
  2254. }
  2255. static bool
  2256. virshBlockJobInfo(vshControl *ctl,
  2257. virDomainPtr dom,
  2258. const char *path,
  2259. bool raw,
  2260. bool bytes)
  2261. {
  2262. virDomainBlockJobInfo info;
  2263. virshControlPtr priv = ctl->privData;
  2264. unsigned long long speed;
  2265. unsigned int flags = 0;
  2266. int rc = -1;
  2267. /* If bytes were requested, or if raw mode is not forcing a MiB/s
  2268. * query and cache can't prove failure, then query bytes/sec. */
  2269. if (bytes || !(raw || priv->blockJobNoBytes)) {
  2270. flags |= VIR_DOMAIN_BLOCK_JOB_INFO_BANDWIDTH_BYTES;
  2271. rc = virDomainGetBlockJobInfo(dom, path, &info, flags);
  2272. if (rc < 0) {
  2273. /* Check for particular errors, let all the rest be fatal. */
  2274. switch (last_error->code) {
  2275. case VIR_ERR_INVALID_ARG:
  2276. priv->blockJobNoBytes = true;
  2277. G_GNUC_FALLTHROUGH;
  2278. case VIR_ERR_OVERFLOW:
  2279. if (!bytes && !raw) {
  2280. /* try again with MiB/s, unless forcing bytes */
  2281. vshResetLibvirtError();
  2282. break;
  2283. }
  2284. G_GNUC_FALLTHROUGH;
  2285. default:
  2286. return false;
  2287. }
  2288. }
  2289. speed = info.bandwidth;
  2290. }
  2291. /* If we don't already have a query result, query for MiB/s */
  2292. if (rc < 0) {
  2293. flags &= ~VIR_DOMAIN_BLOCK_JOB_INFO_BANDWIDTH_BYTES;
  2294. if ((rc = virDomainGetBlockJobInfo(dom, path, &info, flags)) < 0)
  2295. return false;
  2296. speed = info.bandwidth;
  2297. /* Scale to bytes/s unless in raw mode */
  2298. if (!raw) {
  2299. speed <<= 20;
  2300. if (speed >> 20 != info.bandwidth) {
  2301. vshError(ctl, _("overflow in converting %ld MiB/s to bytes\n"),
  2302. info.bandwidth);
  2303. return false;
  2304. }
  2305. }
  2306. }
  2307. if (rc == 0) {
  2308. if (!raw)
  2309. vshPrintExtra(ctl, _("No current block job for %s"), path);
  2310. return true;
  2311. }
  2312. if (raw) {
  2313. vshPrint(ctl, _(" type=%s\n bandwidth=%lu\n cur=%llu\n end=%llu\n"),
  2314. virshDomainBlockJobTypeToString(info.type),
  2315. info.bandwidth, info.cur, info.end);
  2316. } else {
  2317. virshPrintJobProgress(virshDomainBlockJobToString(info.type),
  2318. info.end - info.cur, info.end);
  2319. if (speed) {
  2320. const char *unit;
  2321. double val = vshPrettyCapacity(speed, &unit);
  2322. vshPrint(ctl, _(" Bandwidth limit: %llu bytes/s (%-.3lf %s/s)"),
  2323. speed, val, unit);
  2324. }
  2325. vshPrint(ctl, "\n");
  2326. }
  2327. return true;
  2328. }
  2329. static bool
  2330. virshBlockJobSetSpeed(vshControl *ctl,
  2331. const vshCmd *cmd,
  2332. virDomainPtr dom,
  2333. const char *path,
  2334. bool bytes)
  2335. {
  2336. unsigned long bandwidth;
  2337. unsigned int flags = 0;
  2338. if (bytes)
  2339. flags |= VIR_DOMAIN_BLOCK_JOB_SPEED_BANDWIDTH_BYTES;
  2340. if (vshBlockJobOptionBandwidth(ctl, cmd, bytes, &bandwidth) < 0)
  2341. return false;
  2342. if (virDomainBlockJobSetSpeed(dom, path, bandwidth, flags) < 0)
  2343. return false;
  2344. return true;
  2345. }
  2346. static bool
  2347. virshBlockJobAbort(virDomainPtr dom,
  2348. const char *path,
  2349. bool pivot,
  2350. bool async)
  2351. {
  2352. unsigned int flags = 0;
  2353. if (async)
  2354. flags |= VIR_DOMAIN_BLOCK_JOB_ABORT_ASYNC;
  2355. if (pivot)
  2356. flags |= VIR_DOMAIN_BLOCK_JOB_ABORT_PIVOT;
  2357. if (virDomainBlockJobAbort(dom, path, flags) < 0)
  2358. return false;
  2359. return true;
  2360. }
  2361. static bool
  2362. cmdBlockjob(vshControl *ctl, const vshCmd *cmd)
  2363. {
  2364. bool ret = false;
  2365. bool raw = vshCommandOptBool(cmd, "raw");
  2366. bool bytes = vshCommandOptBool(cmd, "bytes");
  2367. bool abortMode = vshCommandOptBool(cmd, "abort");
  2368. bool pivot = vshCommandOptBool(cmd, "pivot");
  2369. bool async = vshCommandOptBool(cmd, "async");
  2370. bool info = vshCommandOptBool(cmd, "info");
  2371. bool bandwidth = vshCommandOptBool(cmd, "bandwidth");
  2372. virDomainPtr dom = NULL;
  2373. const char *path;
  2374. VSH_EXCLUSIVE_OPTIONS("raw", "abort");
  2375. VSH_EXCLUSIVE_OPTIONS_VAR(raw, pivot);
  2376. VSH_EXCLUSIVE_OPTIONS_VAR(raw, async);
  2377. VSH_EXCLUSIVE_OPTIONS_VAR(raw, bandwidth);
  2378. VSH_EXCLUSIVE_OPTIONS("info", "abort");
  2379. VSH_EXCLUSIVE_OPTIONS_VAR(info, pivot);
  2380. VSH_EXCLUSIVE_OPTIONS_VAR(info, async);
  2381. VSH_EXCLUSIVE_OPTIONS_VAR(info, bandwidth);
  2382. VSH_EXCLUSIVE_OPTIONS("bytes", "abort");
  2383. VSH_EXCLUSIVE_OPTIONS_VAR(bytes, pivot);
  2384. VSH_EXCLUSIVE_OPTIONS_VAR(bytes, async);
  2385. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  2386. goto cleanup;
  2387. /* XXX Allow path to be optional to list info on all devices at once */
  2388. if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0)
  2389. goto cleanup;
  2390. if (bandwidth)
  2391. ret = virshBlockJobSetSpeed(ctl, cmd, dom, path, bytes);
  2392. else if (abortMode || pivot || async)
  2393. ret = virshBlockJobAbort(dom, path, pivot, async);
  2394. else
  2395. ret = virshBlockJobInfo(ctl, dom, path, raw, bytes);
  2396. cleanup:
  2397. virshDomainFree(dom);
  2398. return ret;
  2399. }
  2400. /*
  2401. * "blockpull" command
  2402. */
  2403. static const vshCmdInfo info_blockpull[] = {
  2404. {.name = "help",
  2405. .data = N_("Populate a disk from its backing image.")
  2406. },
  2407. {.name = "desc",
  2408. .data = N_("Populate a disk from its backing image.")
  2409. },
  2410. {.name = NULL}
  2411. };
  2412. static const vshCmdOptDef opts_blockpull[] = {
  2413. VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
  2414. {.name = "path",
  2415. .type = VSH_OT_DATA,
  2416. .flags = VSH_OFLAG_REQ,
  2417. .completer = virshDomainDiskTargetCompleter,
  2418. .help = N_("fully-qualified path of disk")
  2419. },
  2420. {.name = "bandwidth",
  2421. .type = VSH_OT_INT,
  2422. .help = N_("bandwidth limit in MiB/s")
  2423. },
  2424. {.name = "base",
  2425. .type = VSH_OT_STRING,
  2426. .help = N_("path of backing file in chain for a partial pull")
  2427. },
  2428. {.name = "wait",
  2429. .type = VSH_OT_BOOL,
  2430. .help = N_("wait for job to finish")
  2431. },
  2432. {.name = "verbose",
  2433. .type = VSH_OT_BOOL,
  2434. .help = N_("with --wait, display the progress")
  2435. },
  2436. {.name = "timeout",
  2437. .type = VSH_OT_INT,
  2438. .help = N_("with --wait, abort if pull exceeds timeout (in seconds)")
  2439. },
  2440. {.name = "async",
  2441. .type = VSH_OT_BOOL,
  2442. .help = N_("with --wait, don't wait for cancel to finish")
  2443. },
  2444. {.name = "keep-relative",
  2445. .type = VSH_OT_BOOL,
  2446. .help = N_("keep the backing chain relatively referenced")
  2447. },
  2448. {.name = "bytes",
  2449. .type = VSH_OT_BOOL,
  2450. .help = N_("the bandwidth limit is in bytes/s rather than MiB/s")
  2451. },
  2452. {.name = NULL}
  2453. };
  2454. static bool
  2455. cmdBlockpull(vshControl *ctl, const vshCmd *cmd)
  2456. {
  2457. virDomainPtr dom = NULL;
  2458. bool ret = false;
  2459. bool blocking = vshCommandOptBool(cmd, "wait");
  2460. bool verbose = vshCommandOptBool(cmd, "verbose");
  2461. bool async = vshCommandOptBool(cmd, "async");
  2462. bool bytes = vshCommandOptBool(cmd, "bytes");
  2463. int timeout = 0;
  2464. const char *path = NULL;
  2465. const char *base = NULL;
  2466. unsigned long bandwidth = 0;
  2467. unsigned int flags = 0;
  2468. virshBlockJobWaitDataPtr bjWait = NULL;
  2469. VSH_REQUIRE_OPTION("verbose", "wait");
  2470. VSH_REQUIRE_OPTION("async", "wait");
  2471. if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0)
  2472. return false;
  2473. if (vshCommandOptStringReq(ctl, cmd, "base", &base) < 0)
  2474. return false;
  2475. if (vshBlockJobOptionBandwidth(ctl, cmd, bytes, &bandwidth) < 0)
  2476. return false;
  2477. if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
  2478. return false;
  2479. if (vshCommandOptBool(cmd, "keep-relative"))
  2480. flags |= VIR_DOMAIN_BLOCK_REBASE_RELATIVE;
  2481. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  2482. return false;
  2483. if (blocking &&
  2484. !(bjWait = virshBlockJobWaitInit(ctl, dom, path, _("Block Pull"),
  2485. verbose, timeout, async)))
  2486. goto cleanup;
  2487. if (base || flags) {
  2488. if (bytes)
  2489. flags |= VIR_DOMAIN_BLOCK_REBASE_BANDWIDTH_BYTES;
  2490. if (virDomainBlockRebase(dom, path, base, bandwidth, flags) < 0)
  2491. goto cleanup;
  2492. } else {
  2493. if (bytes)
  2494. flags |= VIR_DOMAIN_BLOCK_PULL_BANDWIDTH_BYTES;
  2495. if (virDomainBlockPull(dom, path, bandwidth, flags) < 0)
  2496. goto cleanup;
  2497. }
  2498. if (!blocking) {
  2499. vshPrintExtra(ctl, "%s", _("Block Pull started"));
  2500. ret = true;
  2501. goto cleanup;
  2502. }
  2503. /* Execution continues here only if --wait or friends were specified */
  2504. switch (virshBlockJobWait(bjWait)) {
  2505. case -1:
  2506. goto cleanup;
  2507. case VIR_DOMAIN_BLOCK_JOB_CANCELED:
  2508. vshPrintExtra(ctl, "\n%s", _("Pull aborted"));
  2509. goto cleanup;
  2510. break;
  2511. case VIR_DOMAIN_BLOCK_JOB_FAILED:
  2512. vshPrintExtra(ctl, "\n%s", _("Pull failed"));
  2513. goto cleanup;
  2514. break;
  2515. case VIR_DOMAIN_BLOCK_JOB_READY:
  2516. case VIR_DOMAIN_BLOCK_JOB_COMPLETED:
  2517. vshPrintExtra(ctl, "\n%s", _("Pull complete"));
  2518. break;
  2519. }
  2520. ret = true;
  2521. cleanup:
  2522. virshDomainFree(dom);
  2523. virshBlockJobWaitFree(bjWait);
  2524. return ret;
  2525. }
  2526. /*
  2527. * "blockresize" command
  2528. */
  2529. static const vshCmdInfo info_blockresize[] = {
  2530. {.name = "help",
  2531. .data = N_("Resize block device of domain.")
  2532. },
  2533. {.name = "desc",
  2534. .data = N_("Resize block device of domain.")
  2535. },
  2536. {.name = NULL}
  2537. };
  2538. static const vshCmdOptDef opts_blockresize[] = {
  2539. VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
  2540. {.name = "path",
  2541. .type = VSH_OT_DATA,
  2542. .flags = VSH_OFLAG_REQ,
  2543. .completer = virshDomainDiskTargetCompleter,
  2544. .help = N_("Fully-qualified path of block device")
  2545. },
  2546. {.name = "size",
  2547. .type = VSH_OT_INT,
  2548. .flags = VSH_OFLAG_REQ,
  2549. .help = N_("New size of the block device, as scaled integer (default KiB)")
  2550. },
  2551. {.name = NULL}
  2552. };
  2553. static bool
  2554. cmdBlockresize(vshControl *ctl, const vshCmd *cmd)
  2555. {
  2556. virDomainPtr dom;
  2557. const char *path = NULL;
  2558. unsigned long long size = 0;
  2559. unsigned int flags = 0;
  2560. bool ret = false;
  2561. if (vshCommandOptStringReq(ctl, cmd, "path", (const char **) &path) < 0)
  2562. return false;
  2563. if (vshCommandOptScaledInt(ctl, cmd, "size", &size, 1024, ULLONG_MAX) < 0)
  2564. return false;
  2565. /* Prefer the older interface of KiB. */
  2566. if (size % 1024 == 0)
  2567. size /= 1024;
  2568. else
  2569. flags |= VIR_DOMAIN_BLOCK_RESIZE_BYTES;
  2570. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  2571. return false;
  2572. if (virDomainBlockResize(dom, path, size, flags) < 0) {
  2573. vshError(ctl, _("Failed to resize block device '%s'"), path);
  2574. } else {
  2575. vshPrintExtra(ctl, _("Block device '%s' is resized"), path);
  2576. ret = true;
  2577. }
  2578. virshDomainFree(dom);
  2579. return ret;
  2580. }
  2581. #ifndef WIN32
  2582. /*
  2583. * "console" command
  2584. */
  2585. static const vshCmdInfo info_console[] = {
  2586. {.name = "help",
  2587. .data = N_("connect to the guest console")
  2588. },
  2589. {.name = "desc",
  2590. .data = N_("Connect the virtual serial console for the guest")
  2591. },
  2592. {.name = NULL}
  2593. };
  2594. static const vshCmdOptDef opts_console[] = {
  2595. VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
  2596. {.name = "devname", /* sc_prohibit_devname */
  2597. .type = VSH_OT_STRING,
  2598. .help = N_("character device name")
  2599. },
  2600. {.name = "force",
  2601. .type = VSH_OT_BOOL,
  2602. .help = N_("force console connection (disconnect already connected sessions)")
  2603. },
  2604. {.name = "safe",
  2605. .type = VSH_OT_BOOL,
  2606. .help = N_("only connect if safe console handling is supported")
  2607. },
  2608. {.name = NULL}
  2609. };
  2610. static bool
  2611. cmdRunConsole(vshControl *ctl, virDomainPtr dom,
  2612. const char *name,
  2613. unsigned int flags)
  2614. {
  2615. int state;
  2616. virshControlPtr priv = ctl->privData;
  2617. if ((state = virshDomainState(ctl, dom, NULL)) < 0) {
  2618. vshError(ctl, "%s", _("Unable to get domain status"));
  2619. return false;
  2620. }
  2621. if (state == VIR_DOMAIN_SHUTOFF) {
  2622. vshError(ctl, "%s", _("The domain is not running"));
  2623. return false;
  2624. }
  2625. if (!isatty(STDIN_FILENO)) {
  2626. vshError(ctl, "%s", _("Cannot run interactive console without a controlling TTY"));
  2627. return false;
  2628. }
  2629. vshPrintExtra(ctl, _("Connected to domain %s\n"), virDomainGetName(dom));
  2630. vshPrintExtra(ctl, _("Escape character is %s"), priv->escapeChar);
  2631. if (priv->escapeChar[0] == '^')
  2632. vshPrintExtra(ctl, " (Ctrl + %c)", priv->escapeChar[1]);
  2633. vshPrintExtra(ctl, "\n");
  2634. fflush(stdout);
  2635. if (virshRunConsole(ctl, dom, name, flags) == 0)
  2636. return true;
  2637. return false;
  2638. }
  2639. static bool
  2640. cmdConsole(vshControl *ctl, const vshCmd *cmd)
  2641. {
  2642. virDomainPtr dom;
  2643. bool ret = false;
  2644. bool force = vshCommandOptBool(cmd, "force");
  2645. bool safe = vshCommandOptBool(cmd, "safe");
  2646. unsigned int flags = 0;
  2647. const char *name = NULL;
  2648. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  2649. return false;
  2650. if (vshCommandOptStringReq(ctl, cmd, "devname", &name) < 0) /* sc_prohibit_devname */
  2651. goto cleanup;
  2652. if (force)
  2653. flags |= VIR_DOMAIN_CONSOLE_FORCE;
  2654. if (safe)
  2655. flags |= VIR_DOMAIN_CONSOLE_SAFE;
  2656. ret = cmdRunConsole(ctl, dom, name, flags);
  2657. cleanup:
  2658. virshDomainFree(dom);
  2659. return ret;
  2660. }
  2661. #endif /* WIN32 */
  2662. /* "domif-setlink" command
  2663. */
  2664. static const vshCmdInfo info_domif_setlink[] = {
  2665. {.name = "help",
  2666. .data = N_("set link state of a virtual interface")
  2667. },
  2668. {.name = "desc",
  2669. .data = N_("Set link state of a domain's virtual interface. This command "
  2670. "wraps usage of update-device command.")
  2671. },
  2672. {.name = NULL}
  2673. };
  2674. static const vshCmdOptDef opts_domif_setlink[] = {
  2675. VIRSH_COMMON_OPT_DOMAIN_FULL(0),
  2676. {.name = "interface",
  2677. .type = VSH_OT_DATA,
  2678. .flags = VSH_OFLAG_REQ,
  2679. .completer = virshDomainInterfaceCompleter,
  2680. .help = N_("interface device (MAC Address)")
  2681. },
  2682. {.name = "state",
  2683. .type = VSH_OT_DATA,
  2684. .flags = VSH_OFLAG_REQ,
  2685. .completer = virshDomainInterfaceStateCompleter,
  2686. .help = N_("new state of the device")
  2687. },
  2688. {.name = "persistent",
  2689. .type = VSH_OT_ALIAS,
  2690. .help = "config"
  2691. },
  2692. VIRSH_COMMON_OPT_DOMAIN_CONFIG,
  2693. {.name = NULL}
  2694. };
  2695. static bool
  2696. cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd)
  2697. {
  2698. virDomainPtr dom;
  2699. const char *iface;
  2700. const char *state;
  2701. char *value;
  2702. virMacAddr macaddr;
  2703. const char *element;
  2704. const char *attr;
  2705. bool config;
  2706. bool ret = false;
  2707. unsigned int flags = 0;
  2708. unsigned int xmlflags = 0;
  2709. size_t i;
  2710. xmlDocPtr xml = NULL;
  2711. xmlXPathContextPtr ctxt = NULL;
  2712. xmlXPathObjectPtr obj = NULL;
  2713. xmlNodePtr cur = NULL;
  2714. char *xml_buf = NULL;
  2715. if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
  2716. return false;
  2717. if (vshCommandOptStringReq(ctl, cmd, "interface", &iface) < 0 ||
  2718. vshCommandOptStringReq(ctl, cmd, "state", &state) < 0)
  2719. goto cleanup;
  2720. config = vshCommandOptBool(cmd, "config");
  2721. if (STRNEQ(state, "up") && STRNEQ(state, "down")) {
  2722. vshError(ctl, _("invalid link state '%s'"), state);
  2723. goto cleanup;
  2724. }
  2725. if (config) {
  2726. flags = VIR_DOMAIN_AFFECT_CONFIG;
  2727. xmlflags |= VIR_DOMAIN_XML_INACTIVE;
  2728. } else {
  2729. flags = VIR_DOMAIN_AFFECT_LIVE;
  2730. }
  2731. if (virDomainIsActive(dom) == 0)
  2732. flags = VIR_DOMAIN_AFFECT_CONFIG;
  2733. if (virshDomainGetXMLFromDom(ctl, dom, xmlflags, &xml, &ctxt) < 0)
  2734. goto cleanup;
  2735. obj = xmlXPathEval(BAD_CAST "/domain/devices/interface", ctxt);
  2736. if (obj == NULL || obj->type != XPATH_NODESET ||
  2737. obj->nodesetval == NULL || obj->nodesetval->nodeNr == 0) {
  2738. vshError(ctl, _("Failed to extract interface information or no interfaces found"));
  2739. goto cleanup;
  2740. }
  2741. if (virMacAddrParse(iface, &macaddr) == 0) {
  2742. element = "mac";
  2743. attr = "address";
  2744. } else {
  2745. element = "target";
  2746. attr = "dev";
  2747. }
  2748. /* find interface with matching mac addr */
  2749. for (i = 0; i < obj->nodesetval->nodeNr; i++) {
  2750. cur = obj->nodesetval->nodeTab[i]->children;
  2751. while (cur) {
  2752. if (cur->type == XML_ELEMENT_NODE &&
  2753. virXMLNodeNameEqual(cur, element)) {
  2754. value = virXMLPropString(cur, attr);
  2755. if (STRCASEEQ(value, iface)) {
  2756. VIR_FREE(value);
  2757. goto hit;
  2758. }
  2759. VIR_FREE(value);
  2760. }
  2761. cur = cur->next;
  2762. }
  2763. }
  2764. vshError(ctl, _("interface (%s: %s) not found"), element, iface);
  2765. goto cleanup;
  2766. hit:
  2767. /* find and modify/add link state node */
  2768. /* try to find <link> element */
  2769. cur = obj->nodesetval->nodeTab[i]->children;
  2770. while (cur) {
  2771. if (cur->type == XML_ELEMENT_NODE &&
  2772. virXMLNodeNameEqual(cur, "link")) {
  2773. /* found, just modify the property */
  2774. xmlSetProp(cur, BAD_CAST "state", BAD_CAST state);
  2775. break;
  2776. }
  2777. cur = cur->next;
  2778. }
  2779. if (!cur) {
  2780. /* element <link> not found, add one */
  2781. cur = xmlNewChild(obj->nodesetval->nodeTab[i],
  2782. NULL,
  2783. BAD_CAST "link",
  2784. NULL);
  2785. if (!cur)
  2786. goto cleanup;
  2787. if (xmlNewProp(cur, BAD_CAST "state", BAD_CAST state) == NULL)
  2788. goto cleanup;
  2789. }
  2790. if (!(xml_buf = virXMLNodeToString(xml, obj->nodesetval->nodeTab[i]))) {
  2791. vshSaveLibvirtError();
  2792. vshError(ctl, _("Failed to create XML"));
  2793. goto cleanup;
  2794. }
  2795. if (virDomainUpdateDeviceFlags(dom, xml_buf, flags) < 0) {
  2796. vshError(ctl, _("Failed to update interface link state"));
  2797. goto cleanup;
  2798. } else {
  2799. vshPrintExtra(ctl, "%s", _("Device updated successfully\n"));
  2800. ret = true;
  2801. }
  2802. cleanup:
  2803. xmlXPathFreeObject(obj);
  2804. xmlXPathFreeContext(ctxt);
  2805. xmlFreeDoc(xml);
  2806. VIR_FREE(xml_buf);
  2807. virshDomainFree(dom);
  2808. return ret;
  2809. }
  2810. /* "domiftune" command
  2811. */
  2812. static const vshCmdInfo info_domiftune[] = {
  2813. {.name = "help",
  2814. .data = N_("get/set parameters of a virtual interface")
  2815. },
  2816. {.name = "desc",
  2817. .data = N_("Get/set parameters of a domain's virtual interface.")
  2818. },
  2819. {.name = NULL}
  2820. };
  2821. static const vshCmdOptDef opts_domiftune[] = {
  2822. VIRSH_COMMON_OPT_DOMAIN_FULL(0),
  2823. {.name = "interface",
  2824. .type = VSH_OT_DATA,
  2825. .flags = VSH_OFLAG_REQ,
  2826. .completer = virshDomainInterfaceCompleter,
  2827. .help = N_("interface device (MAC Address)")
  2828. },
  2829. {.name = "inbound",
  2830. .type = VSH_OT_STRING,
  2831. .help = N_("control domain's incoming traffics")
  2832. },
  2833. {.name = "outbound",
  2834. .type = VSH_OT_STRING,
  2835. .help = N_("control domain's outgoing traffics")
  2836. },
  2837. VIRSH_COMMON_OPT_DOMAIN_CONFIG,
  2838. VIRSH_COMMON_OPT_DOMAIN_LIVE,
  2839. VIRSH_COMMON_OPT_DOMAIN_CURRENT,
  2840. {.name = NULL}
  2841. };
  2842. static bool
  2843. cmdDomIftune(vshControl *ctl, const vshCmd *cmd)
  2844. {
  2845. virDomainPtr dom;
  2846. const char *name = NULL, *device = NULL,
  2847. *inboundStr = NULL, *outboundStr = NULL;
  2848. unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
  2849. int nparams = 0;
  2850. int maxparams = 0;
  2851. virTypedParameterPtr params = NULL;
  2852. bool ret = false;
  2853. bool current = vshCommandOptBool(cmd, "current");
  2854. bool config = vshCommandOptBool(cmd, "config");
  2855. bool live = vshCommandOptBool(cmd, "live");
  2856. virNetDevBandwidthRate inbound, outbound;
  2857. size_t i;
  2858. VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
  2859. VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
  2860. if (config)
  2861. flags |= VIR_DOMAIN_AFFECT_CONFIG;
  2862. if (live)
  2863. flags |= VIR_DOMAIN_AFFECT_LIVE;
  2864. if (!(dom = virshCommandOptDomain(ctl, cmd, &name)))
  2865. return false;
  2866. if (vshCommandOptStringReq(ctl, cmd, "interface", &device) < 0)
  2867. goto cleanup;
  2868. if (vshCommandOptStringReq(ctl, cmd, "inbound", &inboundStr) < 0 ||
  2869. vshCommandOptStringReq(ctl, cmd, "outbound", &outboundStr) < 0)
  2870. goto cleanup;
  2871. memset(&inbound, 0, sizeof(inbound));
  2872. memset(&outbound, 0, sizeof(outbound));
  2873. if (inboundStr) {
  2874. if (virshParseRateStr(ctl, inboundStr, &inbound) < 0)
  2875. goto cleanup;
  2876. /* we parse the rate as unsigned long long, but the API
  2877. * only accepts UINT */
  2878. if (inbound.average > UINT_MAX || inbound.peak > UINT_MAX ||
  2879. inbound.burst > UINT_MAX) {
  2880. vshError(ctl, _("inbound rate larger than maximum %u"),
  2881. UINT_MAX);
  2882. goto cleanup;
  2883. }
  2884. if ((!inbound.average && (inbound.burst || inbound.peak)) &&
  2885. !inbound.floor) {
  2886. vshError(ctl, _("either inbound average or floor is mandatory"));
  2887. goto cleanup;
  2888. }
  2889. if (virTypedParamsAddUInt(&params, &nparams, &maxparams,
  2890. VIR_DOMAIN_BANDWIDTH_IN_AVERAGE,
  2891. inbound.average) < 0)
  2892. goto save_error;
  2893. if (inbound.peak &&
  2894. virTypedParamsAddUInt(&params, &nparams, &maxparams,
  2895. VIR_DOMAIN_BANDWIDTH_IN_PEAK,
  2896. inbound.peak) < 0)
  2897. goto save_error;
  2898. if (inbound.burst &&
  2899. virTypedParamsAddUInt(&params, &nparams, &maxparams,
  2900. VIR_DOMAIN_BANDWIDTH_IN_BURST,
  2901. inbound.burst) < 0)
  2902. goto save_error;
  2903. if (inbound.floor &&
  2904. virTypedParamsAddUInt(&params, &nparams, &maxparams,
  2905. VIR_DOMAIN_BANDWIDTH_IN_FLOOR,
  2906. inbound.floor) < 0)
  2907. goto save_error;
  2908. }
  2909. if (outboundStr) {
  2910. if (virshParseRateStr(ctl, outboundStr, &outbound) < 0)
  2911. goto cleanup;
  2912. if (outbound.average > UINT_MAX || outbound.peak > UINT_MAX ||
  2913. outbound.burst > UINT_MAX) {
  2914. vshError(ctl, _("outbound rate larger than maximum %u"),
  2915. UINT_MAX);
  2916. goto cleanup;
  2917. }
  2918. if (outbound.average == 0 && (outbound.burst || outbound.peak)) {
  2919. vshError(ctl, _("outbound average is mandatory"));
  2920. goto cleanup;
  2921. }
  2922. if (outbound.floor) {
  2923. vshError(ctl, _("outbound floor is unsupported yet"));
  2924. goto cleanup;
  2925. }
  2926. if (virTypedParamsAddUInt(&params, &nparams, &maxparams,
  2927. VIR_DOMAIN_BANDWIDTH_OUT_AVERAGE,
  2928. outbound.average) < 0)
  2929. goto save_error;
  2930. if (outbound.peak &&
  2931. virTypedParamsAddUInt(&params, &nparams, &maxparams,
  2932. VIR_DOMAIN_BANDWIDTH_OUT_PEAK,
  2933. outbound.peak) < 0)
  2934. goto save_error;
  2935. if (outbound.burst &&
  2936. virTypedParamsAddUInt(&params, &nparams, &maxparams,
  2937. VIR_DOMAIN_BANDWIDTH_OUT_BURST,
  2938. outbound.burst) < 0)
  2939. goto save_error;
  2940. }
  2941. if (nparams == 0) {
  2942. /* get the number of interface parameters */
  2943. if (virDomainGetInterfaceParameters(dom, device, NULL, &nparams, flags) != 0) {
  2944. vshError(ctl, "%s",
  2945. _("Unable to get number of interface parameters"));
  2946. goto cleanup;
  2947. }
  2948. if (nparams == 0) {
  2949. /* nothing to output */
  2950. ret = true;
  2951. goto cleanup;
  2952. }
  2953. /* get all interface parameters */
  2954. params = vshCalloc(ctl, nparams, sizeof(*params));
  2955. if (virDomainGetInterfaceParameters(dom, device, params, &nparams, flags) != 0) {
  2956. vshError(ctl, "%s", _("Unable to get interface parameters"));
  2957. goto cleanup;
  2958. }
  2959. for (i = 0; i < nparams; i++) {
  2960. char *str = vshGetTypedParamValue(ctl, &params[i]);
  2961. vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
  2962. VIR_FREE(str);
  2963. }
  2964. } else {
  2965. if (virDomainSetInterfaceParameters(dom, device, params,
  2966. nparams, flags) != 0)
  2967. goto error;
  2968. }
  2969. ret = true;
  2970. cleanup:
  2971. virTypedPa