Deactivate and reactivate LVs
[invirt/packages/invirt-remote.git] / host / usr / sbin / invirt-lvm
index a2a6cc9..58e6c1c 100755 (executable)
@@ -22,13 +22,26 @@ def ensureoff(machine):
     rv = call(["/usr/sbin/xm", "destroy", prefix + machine],
               stderr=PIPE)
 
+def lvm_activation(path, mode):
+    p = Popen(["/sbin/lvchange", "-a%s" % (mode,), path], stdout=PIPE, stderr=PIPE)
+    rv = p.wait()
+    return rv
+
+machine_specific = subcommand not in ['lvcreate-all', 'vgcapacity']
+
+if machine_specific:
+    machine = sys.argv[2]
+    disk = sys.argv[3]
+    lvname = prefix + machine + "_" + disk
+    lvpath = "/dev/" + vg + "/" + lvname
+
 if subcommand == "lvcreate-all":
-    from invirt import database
+    from invirt.database import models, connect
     import re
-    database.connect()
-    for d in Disk.select():
+    connect()
+    for d in models.Disk.query().all():
         check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
-        machine = Machine.get(d.machine_id)
+        machine = models.Machine.query().filter_by(machine_id=d.machine_id).one()
         check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name))
         lvname = prefix + machine.name + "_" + d.guest_device_name
         if not os.path.exists("/dev/%s/%s" % (vg, lvname)):
@@ -38,18 +51,16 @@ if subcommand == "lvcreate-all":
             if rv != 0:
                 print >>sys.stderr, "Error creating LV %s\n" %(lvname,)
                 sys.exit(1)
-else:
-    machine = sys.argv[2]
-    disk = sys.argv[3]
-    lvname = prefix + machine + "_" + disk
-    lvpath = "/dev/" + vg + "/" + lvname
-if subcommand == "lvremove":
+elif subcommand == "lvremove":
     def error():
         print >>sys.stderr, "Error removing LV %s\n" % lvname
         sys.exit(1)
     
     # Rename the LV to something else so we can wipe it before reusing
     # the space
+    if lvm_activation(lvpath, 'n') != 0:
+        print >>sys.stderr, "Could not deactivate LV %s", % (lvname,)
+        sys.exit(1)
     while True:
         new_lvname = "old_%s_%s" % (lvname, ''.join(random.choice(string.ascii_letters) for i in xrange(6)))
         new_lvpath = "/dev/%s/%s" % (vg, new_lvname)
@@ -60,21 +71,21 @@ if subcommand == "lvremove":
         elif rv != 0:
             error()
         else:
+            if lvm_activation(new_lvpath, 'y') != 0:
+                print >> sys.stderr, "Could not reactivate renamed LV %s" % (lvname,)
+                sys.exit(1)
             break
     ensureoff(machine)
     
-    # Fork. The child process wipes the LV and then deletes
-    # it. There's not really anything sane to do with errors (since
-    # this is running non-interactively), so let's just drop them on
-    # the floor for now.
-    if os.fork() == 0:
-        call(["/usr/bin/ionice", "-c", "2", "-n", "7", "/bin/dd", "if=/dev/zero", "of=%s" % new_lvpath])
-        call(["/sbin/lvchange", "-a", "n", new_lvpath])
-        call(["/sbin/lvchange", "-a", "ey", new_lvpath])
-        call(["/sbin/lvremove", "--force", new_lvpath])
+    # Touch a file corresponding to the new name of the LV; a separate
+    # daemon will handle wiping and deleting it.
+    open(os.path.join('/var/lib/invirt-remote/cleanup', new_lvname), 'w')
 elif subcommand == "lvresize":
     size = sys.argv[4]
     ensureoff(machine)
+    if lvm_activation(lvpath, 'n') != 0:
+        print >>sys.stderr, "Could not deactivate LV %s", % (lvname,)
+        sys.exit(1)
     p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath],
               stdin=PIPE, stderr=PIPE)
     print >> p.stdin, 'y'
@@ -83,20 +94,50 @@ elif subcommand == "lvresize":
         print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,)
         print >> sys.stderr, err
         sys.exit(1)
+    if lvm_activation(lvpath, 'y') != 0:
+        print >> sys.stderr, "Could not reactivate resized LV %s" % (lvname,)
+        sys.exit(1)
     print >> sys.stderr, err
 elif subcommand == "lvrename":
     newmachine = sys.argv[4]
     newlvname = prefix + newmachine + "_" + disk
     ensureoff(machine)
-    ensureoff(newmachine)    
+    ensureoff(newmachine)
+    lvpath = "/dev/" + vg + "/" + lvname
+    new_lvpath = "/dev/" + vg + "/" + newlvname
+    if lvm_activation(lvpath, 'n') != 0:
+        print >>sys.stderr, "Could not deactivate LV %s", % (lvname,)
+        sys.exit(1)
     rv = call(["/sbin/lvrename", vg, lvname, newlvname])
     if rv != 0:
         print >>sys.stderr, "Error renaming LV %s\n" %(lvname,)
         sys.exit(1)
+    if lvm_activation(new_lvpath, 'y') != 0:
+        print >> sys.stderr, "Could not reactivate renamed LV %s" % (lvname,)
+        sys.exit(1)
 elif subcommand == "lvcreate":
     size = sys.argv[4]
     rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg])
     if rv != 0:
         print >>sys.stderr, "Error creating LV %s\n" %(lvname,)
         sys.exit(1)
+elif subcommand == "vgcapacity":
+    p = Popen(["/sbin/vgs", "-o", "vg_extent_size,vg_extent_count,vg_free_count", 
+                            "--noheadings", "--units", "k", "--nosuffix", "--separator", ":", 
+                            vg],
+              stdout=PIPE, stderr=PIPE)
+    out,err = p.communicate()
+    
+    try:
+        fields = out.strip().split(':')
+        extent_size = float(fields[0]) # in kibibytes
+        extent_count = int(fields[1])
+        free_count = int(fields[2])
+        total_space_TiB = extent_size * extent_count / 1024.**3
+        free_space_TiB = extent_size * free_count / 1024.**3
+        print >>sys.stdout, "Total: %.3f TiB" % (total_space_TiB,)
+        print >>sys.stdout, "Free: %.3f TiB" % (free_space_TiB,)
+    except:
+        print >>sys.stderr, "Error obtaining vg capacity:\n%s\n" % (err,)
+        sys.exit(1)