[Kimchi-devel] [project-kimchi][PATCHv2 1/2] utils: Add nfs prevalication

lvroyce at linux.vnet.ibm.com lvroyce at linux.vnet.ibm.com
Wed Dec 18 09:34:44 UTC 2013


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

Abstract a helper function to parse cmd result.
Usage:
    (1)get cmd result with subprocess.call or subprocess.Popen
    (2) call pass_cmd_output to get formated outputs
Example:
    blkid = subprocess.Popen(["cat", "/proc/mounts"],
                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    outs = blkid.communicate()[0]
    output_items= ['path', 'mnt_point', 'type', 'option']
    utils.parse_cmd_output(outs, output_items)
Sample output:
    [{'path': '/dev/sda8', 'type': 'ext4',
      'option': 'rw,relatime,data=ordered', 'mnt_point': '/home'},
     {'path': 'localhost:/home/royce/isorepo', 'type': 'nfs4',
      'option': 'rw...addr=127.0.0.1', 'mnt_point': '/mnt'}]

To prevent future mount of nfs path will hang,
we will try nfs path with a quick mount,
if this check fails, report warning to user.

Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
 src/kimchi/utils.py | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py
index f7eda93..14b9c8a 100644
--- a/src/kimchi/utils.py
+++ b/src/kimchi/utils.py
@@ -22,6 +22,9 @@
 #
 
 import cherrypy
+import subprocess
+import tempfile
+import threading
 import os
 
 
@@ -84,3 +87,38 @@ def import_class(class_path):
 
 def import_module(module_name):
     return __import__(module_name, fromlist=[''])
+
+def parse_cmd_output(output, output_items):
+    res = []
+    for line in output.split("\n"):
+        res.append(dict(zip(output_items, line.split())))
+
+    return res
+
+def check_nfs_export(export_path):
+    res = False
+    outputs = None
+    mnt_point = tempfile.mkdtemp(dir='/tmp')
+    cmd = ["mount", "-o", 'soft,timeo=100,retrans=3,retry=0',
+           export_path, mnt_point]
+    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                            stderr=subprocess.PIPE)
+    thread = threading.Thread(target = proc.communicate)
+    thread.start()
+    thread.join(30)
+
+    if thread.is_alive():
+        proc.kill()
+        thread.join()
+
+    with open("/proc/mounts" , "rb") as f:
+        outputs = f.read()
+    output_items = ['dev_path', 'mnt_point', 'type']
+    mounts = parse_cmd_output(outputs, output_items)
+    for item in mounts:
+        if 'dev_path' in item and item['dev_path'] == export_path:
+            res =  True
+    cmd = ["umount", "-f", export_path]
+    subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+    return res
-- 
1.8.1.2

-- 
project-kimchi mailing list <project-kimchi at googlegroups.com>
https://groups.google.com/forum/#!forum/project-kimchi
--- 
You received this message because you are subscribed to the Google Groups "project-kimchi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-kimchi+unsubscribe at googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



More information about the Kimchi-devel mailing list