]> xenbits.xensource.com Git - libvirt.git/commitdiff
storage: Add support for access to files using provided uid/gid
authorPeter Krempa <pkrempa@redhat.com>
Fri, 25 Apr 2014 11:45:48 +0000 (13:45 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 23 May 2014 08:48:48 +0000 (10:48 +0200)
To allow using the storage driver APIs to access files on various
storage sources in a 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
src/storage/storage_driver.c
src/storage/storage_driver.h

index 456b9d764e12f9cbb99e37b44bcbbb132fd6d8b0..fcbb6da5460094244534e03c3d1f7e8066071445 100644 (file)
@@ -169,6 +169,9 @@ typedef virStorageFileBackend *virStorageFileBackendPtr;
 struct _virStorageDriverData {
     virStorageFileBackendPtr backend;
     void *priv;
+
+    uid_t uid;
+    gid_t gid;
 };
 
 typedef int
index 455a2efcc0e752d397820fb4895324e5364dcdc4..6d29067fca5cd8b935388ebd84fc9cb5b3c2417c 100644 (file)
@@ -2801,13 +2801,37 @@ virStorageFileDeinit(virStorageSourcePtr src)
 }
 
 
+/**
+ * virStorageFileInitAs:
+ *
+ * @src: storage source definition
+ * @uid: uid used to access the file, or -1 for current uid
+ * @gid: gid used to access the file, or -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;
@@ -2824,6 +2848,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, -1, -1);
+}
+
+
 /**
  * virStorageFileCreate: Creates an empty storage file via storage driver
  *
index fb03870df5491ab40cdc5e27b1d897bd58670ff9..49be9995c9dab47c0d1280ceedea3249090b0a5f 100644 (file)
@@ -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);