[libvirt] [sandbox PATCH v3 06/22] Image: Add check_writable and runtime resolver

Eren Yagdiran erenyagdiran at gmail.com
Tue Aug 18 06:53:27 UTC 2015


These helper functions are for selecting right directories according
to running user privileges
---
 virt-sandbox-image/virt-sandbox-image.py | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/virt-sandbox-image/virt-sandbox-image.py b/virt-sandbox-image/virt-sandbox-image.py
index 9e98bf2..5917dd6 100755
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -32,6 +32,8 @@ import sys
 import urllib2
 import subprocess
 
+template_dir = None
+storage_dir = None
 default_privileged_template_dir = "/var/lib/libvirt/templates"
 default_home_dir = os.environ['HOME']
 default_unprivileged_template_dir = default_home_dir + "/.local/share/libvirt/templates"
@@ -40,6 +42,29 @@ default_unprivileged_storage_dir = default_unprivileged_template_dir + "/storage
 debug = False
 verbose = False
 
+def check_dir_writable(path):
+    if not os.access(path,os.W_OK):
+        return False
+    return True
+
+def runtime_dir_resolver():
+    global default_privileged_template_dir
+    global default_privileged_storage_dir
+    global default_unprivileged_template_dir
+    global default_unprivileged_storage_dir
+    global template_dir
+    global storage_dir
+    if(check_dir_writable(default_privileged_template_dir)):
+        template_dir = default_privileged_template_dir
+        storage_dir = default_privileged_storage_dir
+        return
+    template_dir = default_unprivileged_template_dir
+    storage_dir = default_unprivileged_storage_dir
+    if not os.path.exists(template_dir):
+        os.makedirs(template_dir)
+    if not os.path.exists(storage_dir):
+        os.makedirs(storage_dir)
+
 sys.dont_write_byte_code = True
 
 import importlib
@@ -380,8 +405,8 @@ def gen_create_args(subparser):
     parser.set_defaults(func=create)
 
 if __name__ == '__main__':
+    runtime_dir_resolver()
     parser = argparse.ArgumentParser(description='Sandbox Container Image Tool')
-
     subparser = parser.add_subparsers(help=_("commands"))
     gen_download_args(subparser)
     gen_delete_args(subparser)
-- 
2.1.0




More information about the libvir-list mailing list