mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 13:45:38 +00:00
openvzDomainSetNetwork: use virCommand
Currently, the openvzDomainSetNetwork function constructs an array of strings representing a command line for VZCTL binary. This is a overkill since our virCommand APIs can cover all the functionality. Moreover, the function is not following our structure where return value is set to -1 initially, and after all operations succeeded then it is set to zero.
This commit is contained in:
parent
3a6204cbbd
commit
d8b3130664
@ -88,15 +88,6 @@ static void openvzDriverUnlock(struct openvz_driver *driver)
|
|||||||
|
|
||||||
struct openvz_driver ovz_driver;
|
struct openvz_driver ovz_driver;
|
||||||
|
|
||||||
static void cmdExecFree(const char *cmdExec[])
|
|
||||||
{
|
|
||||||
int i=-1;
|
|
||||||
while (cmdExec[++i]) {
|
|
||||||
VIR_FREE(cmdExec[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
openvzDomainDefPostParse(virDomainDefPtr def,
|
openvzDomainDefPostParse(virDomainDefPtr def,
|
||||||
virCapsPtr caps ATTRIBUTE_UNUSED,
|
virCapsPtr caps ATTRIBUTE_UNUSED,
|
||||||
@ -827,22 +818,12 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
|
|||||||
virDomainNetDefPtr net,
|
virDomainNetDefPtr net,
|
||||||
virBufferPtr configBuf)
|
virBufferPtr configBuf)
|
||||||
{
|
{
|
||||||
int rc = 0, narg;
|
int rc = -1;
|
||||||
const char *prog[OPENVZ_MAX_ARG];
|
|
||||||
char macaddr[VIR_MAC_STRING_BUFLEN];
|
char macaddr[VIR_MAC_STRING_BUFLEN];
|
||||||
virMacAddr host_mac;
|
virMacAddr host_mac;
|
||||||
char host_macaddr[VIR_MAC_STRING_BUFLEN];
|
char host_macaddr[VIR_MAC_STRING_BUFLEN];
|
||||||
struct openvz_driver *driver = conn->privateData;
|
struct openvz_driver *driver = conn->privateData;
|
||||||
char *opt = NULL;
|
virCommandPtr cmd = NULL;
|
||||||
|
|
||||||
#define ADD_ARG_LIT(thisarg) \
|
|
||||||
do { \
|
|
||||||
if (narg >= OPENVZ_MAX_ARG) \
|
|
||||||
goto no_memory; \
|
|
||||||
if ((prog[narg++] = strdup(thisarg)) == NULL) \
|
|
||||||
goto no_memory; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
|
|
||||||
if (net == NULL)
|
if (net == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
@ -852,18 +833,11 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (narg = 0; narg < OPENVZ_MAX_ARG; narg++)
|
if (net->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
|
||||||
prog[narg] = NULL;
|
net->type != VIR_DOMAIN_NET_TYPE_ETHERNET)
|
||||||
|
return 0;
|
||||||
|
|
||||||
narg = 0;
|
cmd = virCommandNewArgList(VZCTL, "--quiet", "set", vpsid, NULL);
|
||||||
|
|
||||||
if (net->type == VIR_DOMAIN_NET_TYPE_BRIDGE ||
|
|
||||||
net->type == VIR_DOMAIN_NET_TYPE_ETHERNET) {
|
|
||||||
ADD_ARG_LIT(VZCTL);
|
|
||||||
ADD_ARG_LIT("--quiet");
|
|
||||||
ADD_ARG_LIT("set");
|
|
||||||
ADD_ARG_LIT(vpsid);
|
|
||||||
}
|
|
||||||
|
|
||||||
virMacAddrFormat(&net->mac, macaddr);
|
virMacAddrFormat(&net->mac, macaddr);
|
||||||
virDomainNetGenerateMAC(driver->xmlopt, &host_mac);
|
virDomainNetGenerateMAC(driver->xmlopt, &host_mac);
|
||||||
@ -875,9 +849,6 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
|
|||||||
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
||||||
int veid = openvzGetVEID(vpsid);
|
int veid = openvzGetVEID(vpsid);
|
||||||
|
|
||||||
/* --netif_add ifname[,mac,host_ifname,host_mac] */
|
|
||||||
ADD_ARG_LIT("--netif_add") ;
|
|
||||||
|
|
||||||
/* if user doesn't specify guest interface name,
|
/* if user doesn't specify guest interface name,
|
||||||
* then we need to generate it */
|
* then we need to generate it */
|
||||||
if (net->data.ethernet.dev == NULL) {
|
if (net->data.ethernet.dev == NULL) {
|
||||||
@ -885,8 +856,7 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
|
|||||||
if (net->data.ethernet.dev == NULL) {
|
if (net->data.ethernet.dev == NULL) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("Could not generate eth name for container"));
|
_("Could not generate eth name for container"));
|
||||||
rc = -1;
|
goto cleanup;
|
||||||
goto exit;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -897,8 +867,7 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
|
|||||||
if (net->ifname == NULL) {
|
if (net->ifname == NULL) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
_("Could not generate veth name"));
|
_("Could not generate veth name"));
|
||||||
rc = -1;
|
goto cleanup;
|
||||||
goto exit;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -919,40 +888,23 @@ openvzDomainSetNetwork(virConnectPtr conn, const char *vpsid,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(opt = virBufferContentAndReset(&buf)))
|
/* --netif_add ifname[,mac,host_ifname,host_mac] */
|
||||||
goto no_memory;
|
virCommandAddArg(cmd, "--netif_add");
|
||||||
|
virCommandAddArgBuffer(cmd, &buf);
|
||||||
ADD_ARG_LIT(opt) ;
|
|
||||||
VIR_FREE(opt);
|
|
||||||
} else if (net->type == VIR_DOMAIN_NET_TYPE_ETHERNET &&
|
} else if (net->type == VIR_DOMAIN_NET_TYPE_ETHERNET &&
|
||||||
net->data.ethernet.ipaddr != NULL) {
|
net->data.ethernet.ipaddr != NULL) {
|
||||||
/* --ipadd ip */
|
/* --ipadd ip */
|
||||||
ADD_ARG_LIT("--ipadd") ;
|
virCommandAddArgList(cmd, "--ipadd", net->data.ethernet.ipaddr, NULL);
|
||||||
ADD_ARG_LIT(net->data.ethernet.ipaddr) ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: processing NAT and physical device */
|
/* TODO: processing NAT and physical device */
|
||||||
|
|
||||||
if (prog[0] != NULL) {
|
virCommandAddArg(cmd, "--save");
|
||||||
ADD_ARG_LIT("--save");
|
rc = virCommandRun(cmd, NULL);
|
||||||
if (virRun(prog, NULL) < 0) {
|
|
||||||
rc = -1;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
exit:
|
cleanup:
|
||||||
cmdExecFree(prog);
|
virCommandFree(cmd);
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
no_memory:
|
|
||||||
VIR_FREE(opt);
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
_("Could not put argument to %s"), VZCTL);
|
|
||||||
cmdExecFree(prog);
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
#undef ADD_ARG_LIT
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user