When we allow multiple instances of the driver for the same user
account, using a separate root directory, we need to ensure mutual
exclusion. Use a pidfile to guarantee this.
In privileged libvirtd this ends up locking
/var/run/libvirt/bhyve/driver.pid
In unprivileged libvirtd this ends up locking
/run/user/$UID/libvirt/bhyve/run/driver.pid
NB, the latter can vary depending on $XDG_RUNTIME_DIR
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/bhyve/bhyve_driver.c | 9 +++++++++
src/bhyve/bhyve_utils.h | 3 +++
2 files changed, 12 insertions(+)
diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c
index 4ce9ef0b95..cfcf4e1fba 100644
--- a/src/bhyve/bhyve_driver.c
+++ b/src/bhyve/bhyve_driver.c
@@ -43,6 +43,7 @@
#include "virthread.h"
#include "virlog.h"
#include "virfile.h"
+#include "virpidfile.h"
#include "virtypedparam.h"
#include "virrandom.h"
#include "virstring.h"
@@ -1203,6 +1204,9 @@ bhyveStateCleanup(void)
virObjectUnref(bhyve_driver->config);
virPortAllocatorRangeFree(bhyve_driver->remotePorts);
+ if (bhyve_driver->lockFD != -1)
+ virPidFileRelease(BHYVE_STATE_DIR, "driver", bhyve_driver->lockFD);
+
virMutexDestroy(&bhyve_driver->lock);
VIR_FREE(bhyve_driver);
@@ -1222,6 +1226,7 @@ bhyveStateInitialize(bool privileged,
if (VIR_ALLOC(bhyve_driver) < 0)
return -1;
+ bhyve_driver->lockFD = -1;
if (virMutexInit(&bhyve_driver->lock) < 0) {
VIR_FREE(bhyve_driver);
return -1;
@@ -1274,6 +1279,10 @@ bhyveStateInitialize(bool privileged,
goto cleanup;
}
+ if ((bhyve_driver->lockFD =
+ virPidFileAcquire(BHYVE_STATE_DIR, "driver", true, getpid())) < 0)
+ goto cleanup;
+
if (virDomainObjListLoadAllConfigs(bhyve_driver->domains,
BHYVE_STATE_DIR,
NULL, true,
diff --git a/src/bhyve/bhyve_utils.h b/src/bhyve/bhyve_utils.h
index 26956d7d21..3d212e3ccf 100644
--- a/src/bhyve/bhyve_utils.h
+++ b/src/bhyve/bhyve_utils.h
@@ -48,6 +48,9 @@ struct _bhyveConn {
virBhyveDriverConfigPtr config;
+ /* pid file FD, ensures two copies of the driver can't use the same root */
+ int lockFD;
+
virDomainObjListPtr domains;
virCapsPtr caps;
virDomainXMLOptionPtr xmlopt;
--
2.21.0