mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-20 07:59:00 +00:00
Starts dnsmasq from libvirtd with --dhcp-hostsfile option
This patch makes libvirtd start the dnsmasq daemon with a --dhcp-hostsfile option instead of --dhcp-host options for each '//ip/dhcp/host' entries defined in network xml file. the dnsmasq host file is stored into /var/lib/libvirt/network * src/network/bridge_driver.c: define the directory for the hostfiles and save/delete them to be used by dnsmasq
This commit is contained in:
parent
51d203c676
commit
8fa9c22142
@ -57,10 +57,13 @@
|
|||||||
#include "iptables.h"
|
#include "iptables.h"
|
||||||
#include "bridge.h"
|
#include "bridge.h"
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
#include "dnsmasq.h"
|
||||||
|
|
||||||
#define NETWORK_PID_DIR LOCAL_STATE_DIR "/run/libvirt/network"
|
#define NETWORK_PID_DIR LOCAL_STATE_DIR "/run/libvirt/network"
|
||||||
#define NETWORK_STATE_DIR LOCAL_STATE_DIR "/lib/libvirt/network"
|
#define NETWORK_STATE_DIR LOCAL_STATE_DIR "/lib/libvirt/network"
|
||||||
|
|
||||||
|
#define DNSMASQ_STATE_DIR LOCAL_STATE_DIR "/lib/libvirt/network"
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_NETWORK
|
#define VIR_FROM_THIS VIR_FROM_NETWORK
|
||||||
|
|
||||||
#define networkReportError(code, ...) \
|
#define networkReportError(code, ...) \
|
||||||
@ -360,6 +363,29 @@ networkShutdown(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
networkSaveDnsmasqHostsfile(virNetworkObjPtr network,
|
||||||
|
dnsmasqContext *dctx,
|
||||||
|
bool force)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
if (! force && virFileExists(dctx->hostsfile->path))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
for (i = 0 ; i < network->def->nhosts ; i++) {
|
||||||
|
virNetworkDHCPHostDefPtr host = &(network->def->hosts[i]);
|
||||||
|
if ((host->mac) && (host->ip))
|
||||||
|
dnsmasqAddDhcpHost(dctx, host->mac, host->ip, host->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dnsmasqSave(dctx) < 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
networkBuildDnsmasqArgv(virNetworkObjPtr network,
|
networkBuildDnsmasqArgv(virNetworkObjPtr network,
|
||||||
const char *pidfile,
|
const char *pidfile,
|
||||||
@ -401,8 +427,8 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
|
|||||||
(2 * network->def->nranges) + /* --dhcp-range 10.0.0.2,10.0.0.254 */
|
(2 * network->def->nranges) + /* --dhcp-range 10.0.0.2,10.0.0.254 */
|
||||||
/* --dhcp-lease-max=xxx if needed */
|
/* --dhcp-lease-max=xxx if needed */
|
||||||
(network->def->nranges ? 0 : 1) +
|
(network->def->nranges ? 0 : 1) +
|
||||||
/* --dhcp-host 01:23:45:67:89:0a,hostname,10.0.0.3 */
|
/* --dhcp-hostsfile=/var/lib/dnsmasq/$NAME.hostsfile */
|
||||||
(2 * network->def->nhosts) +
|
(network->def->nhosts > 0 ? 1 : 0) +
|
||||||
/* --enable-tftp --tftp-root /srv/tftp */
|
/* --enable-tftp --tftp-root /srv/tftp */
|
||||||
(network->def->tftproot ? 3 : 0) +
|
(network->def->tftproot ? 3 : 0) +
|
||||||
/* --dhcp-boot pxeboot.img[,,12.34.56.78] */
|
/* --dhcp-boot pxeboot.img[,,12.34.56.78] */
|
||||||
@ -473,22 +499,22 @@ networkBuildDnsmasqArgv(virNetworkObjPtr network,
|
|||||||
APPEND_ARG(*argv, i++, buf);
|
APPEND_ARG(*argv, i++, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (r = 0 ; r < network->def->nhosts ; r++) {
|
if (network->def->nhosts > 0) {
|
||||||
virNetworkDHCPHostDefPtr host = &(network->def->hosts[r]);
|
dnsmasqContext *dctx = dnsmasqContextNew(network->def->name, DNSMASQ_STATE_DIR);
|
||||||
if ((host->mac) && (host->name)) {
|
char *hostsfileArg;
|
||||||
snprintf(buf, sizeof(buf), "%s,%s,%s",
|
|
||||||
host->mac, host->name, host->ip);
|
|
||||||
} else if (host->mac) {
|
|
||||||
snprintf(buf, sizeof(buf), "%s,%s",
|
|
||||||
host->mac, host->ip);
|
|
||||||
} else if (host->name) {
|
|
||||||
snprintf(buf, sizeof(buf), "%s,%s",
|
|
||||||
host->name, host->ip);
|
|
||||||
} else
|
|
||||||
continue;
|
|
||||||
|
|
||||||
APPEND_ARG(*argv, i++, "--dhcp-host");
|
if (dctx == NULL)
|
||||||
APPEND_ARG(*argv, i++, buf);
|
goto no_memory;
|
||||||
|
|
||||||
|
if (networkSaveDnsmasqHostsfile(network, dctx, false)) {
|
||||||
|
if (virAsprintf(&hostsfileArg, "--dhcp-hostsfile=%s", dctx->hostsfile->path) < 0) {
|
||||||
|
dnsmasqContextFree(dctx);
|
||||||
|
goto no_memory;
|
||||||
|
}
|
||||||
|
APPEND_ARG_LIT(*argv, i++, hostsfileArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
dnsmasqContextFree(dctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (network->def->tftproot) {
|
if (network->def->tftproot) {
|
||||||
@ -1294,6 +1320,15 @@ static virNetworkPtr networkDefine(virConnectPtr conn, const char *xml) {
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (network->def->nhosts > 0) {
|
||||||
|
dnsmasqContext *dctx = dnsmasqContextNew(network->def->name, DNSMASQ_STATE_DIR);
|
||||||
|
if (dctx == NULL)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
networkSaveDnsmasqHostsfile(network, dctx, true);
|
||||||
|
dnsmasqContextFree(dctx);
|
||||||
|
}
|
||||||
|
|
||||||
ret = virGetNetwork(conn, network->def->name, network->def->uuid);
|
ret = virGetNetwork(conn, network->def->name, network->def->uuid);
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
@ -1329,6 +1364,15 @@ static int networkUndefine(virNetworkPtr net) {
|
|||||||
network) < 0)
|
network) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
|
if (network->def->nhosts > 0) {
|
||||||
|
dnsmasqContext *dctx = dnsmasqContextNew(network->def->name, DNSMASQ_STATE_DIR);
|
||||||
|
if (dctx == NULL)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
dnsmasqDelete(dctx);
|
||||||
|
dnsmasqContextFree(dctx);
|
||||||
|
}
|
||||||
|
|
||||||
virNetworkRemoveInactive(&driver->networks,
|
virNetworkRemoveInactive(&driver->networks,
|
||||||
network);
|
network);
|
||||||
network = NULL;
|
network = NULL;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user