[PATCHv2 07/33] storage: Add support for access to files using provided uid/gid

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To allow using the storage driver APIs to access files on various
storage sources in an universal fashion possibly on storage such as nfs
with root squash we'll need to store the desired uid/gid in the
metadata.

Add new initialisation API that will store the desired uid/gid and a
wrapper for the current use. Additionally add docs for the two APIs.
---
 src/storage/storage_backend.h |  3 +++
 src/storage/storage_driver.c  | 39 ++++++++++++++++++++++++++++++++++++++-
 src/storage/storage_driver.h  |  5 +++--
 3 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/src/storage/storage_backend.h b/src/storage/storage_backend.h
index 456b9d7..fcbb6da 100644
--- a/src/storage/storage_backend.h
+++ b/src/storage/storage_backend.h
@@ -169,6 +169,9 @@ typedef virStorageFileBackend *virStorageFileBackendPtr;
 struct _virStorageDriverData {
     virStorageFileBackendPtr backend;
     void *priv;
+
+    uid_t uid;
+    gid_t gid;
 };

 typedef int
diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c
index 455a2ef..5e740f9 100644
--- a/src/storage/storage_driver.c
+++ b/src/storage/storage_driver.c
@@ -2801,13 +2801,37 @@ virStorageFileDeinit(virStorageSourcePtr src)
 }


+/**
+ * virStorageFileInitAs:
+ *
+ * @src: storage source definition
+ * @uid: uid to access the file as, -1 for current uid
+ * @gid: gid to access the file as, -1 for current gid
+ *
+ * Initialize a storage source to be used with storage driver. Use the provided
+ * uid and gid if possible for the operations.
+ *
+ * Returns 0 if the storage file was successfully initialized, -1 if the
+ * initialization failed. Libvirt error is reported.
+ */
 int
-virStorageFileInit(virStorageSourcePtr src)
+virStorageFileInitAs(virStorageSourcePtr src,
+                     uid_t uid, gid_t gid)
 {
     int actualType = virStorageSourceGetActualType(src);
     if (VIR_ALLOC(src->drv) < 0)
         return -1;

+    if (uid == (uid_t) -1)
+        src->drv->uid = geteuid();
+    else
+        src->drv->uid = uid;
+
+    if (gid == (gid_t) -1)
+        src->drv->gid = getegid();
+    else
+        src->drv->gid = gid;
+
     if (!(src->drv->backend = virStorageFileBackendForType(actualType,
                                                            src->protocol)))
         goto error;
@@ -2825,6 +2849,19 @@ virStorageFileInit(virStorageSourcePtr src)


 /**
+ * virStorageFileInit:
+ *
+ * See virStorageFileInitAs. The file is initialized to be accessed by the
+ * current user.
+ */
+int
+virStorageFileInit(virStorageSourcePtr src)
+{
+    return virStorageFileInitAs(src, (uid_t) -1, (gid_t) -1);
+}
+
+
+/**
  * virStorageFileCreate: Creates an empty storage file via storage driver
  *
  * @src: file structure pointing to the file
diff --git a/src/storage/storage_driver.h b/src/storage/storage_driver.h
index fb03870..49be999 100644
--- a/src/storage/storage_driver.h
+++ b/src/storage/storage_driver.h
@@ -29,8 +29,9 @@
 # include "storage_conf.h"
 # include "virstoragefile.h"

-int
-virStorageFileInit(virStorageSourcePtr src);
+int virStorageFileInit(virStorageSourcePtr src);
+int virStorageFileInitAs(virStorageSourcePtr src,
+                         uid_t uid, gid_t gid);
 void virStorageFileDeinit(virStorageSourcePtr src);

 int virStorageFileCreate(virStorageSourcePtr src);
-- 
1.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]