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/storage/driver.pid
In unprivileged libvirtd this ends up locking
/run/user/$UID/libvirt/storage/run/driver.pid
NB, the latter can vary depending on $XDG_RUNTIME_DIR
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/conf/virstorageobj.h | 3 +++
src/storage/storage_driver.c | 11 +++++++++++
2 files changed, 14 insertions(+)
diff --git a/src/conf/virstorageobj.h b/src/conf/virstorageobj.h
index 92d229f9b4..4547a0df9b 100644
--- a/src/conf/virstorageobj.h
+++ b/src/conf/virstorageobj.h
@@ -37,6 +37,9 @@ typedef virStorageDriverState *virStorageDriverStatePtr;
struct _virStorageDriverState {
virMutex lock;
+ /* pid file FD, ensures two copies of the driver can't use the same root */
+ int lockFD;
+
virStoragePoolObjListPtr pools;
char *configDir;
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 496d51b1e0..03ac6a6845 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -43,6 +43,7 @@
#include "virlog.h"
#include "virfile.h"
#include "virfdstream.h"
+#include "virpidfile.h"
#include "configmake.h"
#include "virsecret.h"
#include "virstring.h"
@@ -256,6 +257,7 @@ storageStateInitialize(bool privileged,
if (VIR_ALLOC(driver) < 0)
return -1;
+ driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0) {
VIR_FREE(driver);
return -1;
@@ -296,6 +298,11 @@ storageStateInitialize(bool privileged,
goto error;
}
+ if ((driver->lockFD =
+ virPidFileAcquire(driver->stateDir, "driver",
+ true, getpid())) < 0)
+ goto error;
+
if (virStoragePoolObjLoadAllState(driver->pools,
driver->stateDir) < 0)
goto error;
@@ -371,6 +378,10 @@ storageStateCleanup(void)
/* free inactive pools */
virObjectUnref(driver->pools);
+ if (driver->lockFD != -1)
+ virPidFileRelease(driver->stateDir, "driver",
+ driver->lockFD);
+
VIR_FREE(driver->configDir);
VIR_FREE(driver->autostartDir);
VIR_FREE(driver->stateDir);
--
2.21.0