add archipelago-dbg package
[archipelago] / xseg / tools / vlmc
index 1a3df38..e15ca47 100755 (executable)
@@ -2,19 +2,24 @@
 #
 # vlmc tool
 
-import os, sys, subprocess, argparse
+import os, sys, subprocess, argparse, time
 from subprocess import call, check_call
 
 def vlmc_create(args):
     name = args.name[0]
-    size = args.size << 20
+    size = args.size 
     snap = args.snap
 
     if size == None and snap == None:
         print >> sys.stderr, "At least one of the size/snap args must be provided"
         sys.exit(-1)
 
-    cmd = [XSEG_HOME + "/peers/user/vlmc-xseg", "%s" % SPEC, "create", "--name", "%s" % name]
+    if size:
+        size = size << 20
+    else:
+        size = 0
+
+    cmd = ["vlmc-xseg", "%s" % SPEC, "create", "--name", "%s" % name]
     if snap != None:
             cmd.extend(["--snap", "%s" % snap])
     if size != None:
@@ -23,7 +28,7 @@ def vlmc_create(args):
     cmd.extend(["-p", "%s" % VTOOL])
 
     try:
-        check_call(cmd, stderr=subprocess.STDOUT, shell=False);
+        check_call(cmd, shell=False, env=os.environ);
     except Exception:
             sys.stderr.write("vlmc creation failed\n")
             sys.exit(-1)
@@ -39,12 +44,14 @@ def vlmc_list(args):
 
 def vlmc_remove(args):
     name = args.name[0]
-    cmd = [XSEG_HOME + "/peers/user/vlmc-xseg", "%s" % SPEC, "remove", "--name", "%s" % name]
+    cmd = ["vlmc-xseg", "%s" % SPEC, "remove", "--name", "%s" % name]
     cmd.extend(["-mp", "%s" % MPORT])
     cmd.extend(["-p", "%s" % VTOOL])
-    result = utils.RunCmd(cmd)
-    if result.failed:
-            sys.stderr.write("vlmc creation failed\n")
+
+    try:
+        check_call(cmd, shell=False, env=os.environ);
+    except Exception:
+            sys.stderr.write("vlmc removal failed\n")
             sys.exit(-1)
 
 
@@ -55,6 +62,25 @@ def xsegbd_loaded():
         print >> sys.stderr, reason
         sys.exit(-1)
 
+def exclusive(fn):
+    def exclusive_args(args):
+        file = "/tmp/vlmc_map.lock"
+        while True:
+            try:
+                fd = os.open(file, os.O_CREAT|os.O_EXCL|os.O_WRONLY)
+                break;
+            except Exception, reason:
+                print >> sys.stderr, reason
+                time.sleep(0.05)
+        try:
+            fn(args)
+        finally:
+            os.close(fd)
+            os.unlink(file)
+
+    return exclusive_args
+
+@exclusive
 def vlmc_map(args):
     xsegbd_loaded()
     name = args.name[0]
@@ -71,12 +97,14 @@ def vlmc_map(args):
 
         port = prev + 1
         fd = os.open(XSEGBD_SYSFS + "add", os.O_WRONLY)
-        os.write(fd, "%s %d:%d:%d" % (name, port, VPORT, REQS))
+        print >> sys.stderr, "write to %s : %s %d:%d:%d" %( XSEGBD_SYSFS + "add", name, port, port + VPORT_START -4, REQS )
+        os.write(fd, "%s %d:%d:%d" % (name, port, port + VPORT_START-4, REQS))
         os.close(fd)
     except Exception, reason:
         print >> sys.stderr, reason
         sys.exit(-1)
 
+@exclusive
 def vlmc_unmap(args):
     xsegbd_loaded()
     device = args.name[0]
@@ -96,6 +124,7 @@ def vlmc_unmap(args):
         print >> sys.stderr, reason
         sys.exit(-1)
 
+@exclusive
 def vlmc_showmapped(args):
     xsegbd_loaded()
     print "id\tpool\timage\tsnap\tdevice"
@@ -136,7 +165,7 @@ def loadrc(rc):
     #FIXME
     try:
         if rc == None:
-            execfile(os.path.expanduser("~/.xsegrc"), globals())
+            execfile(os.path.expanduser("/etc/default/archipelago"), globals())
         else:
             execfile(rc, globals())
     except: