X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-remote.git/blobdiff_plain/ccdd1009d00397d0f81fa9a95aa96faf448b9e94..967a3566b99b42fe71a52c2b6e5be99bfb24718f:/host/usr/sbin/invirt-lvm?ds=sidebyside diff --git a/host/usr/sbin/invirt-lvm b/host/usr/sbin/invirt-lvm index 6367216..6860d9c 100755 --- a/host/usr/sbin/invirt-lvm +++ b/host/usr/sbin/invirt-lvm @@ -1,7 +1,10 @@ #!/usr/bin/env python import sys -import os.path +import time +import os +import random +import string from subprocess import call, PIPE, Popen from invirt.config import structs as config @@ -19,13 +22,21 @@ def ensureoff(machine): rv = call(["/usr/sbin/xm", "destroy", prefix + machine], stderr=PIPE) +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)): @@ -35,29 +46,29 @@ 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) - # I know this is the wrong answer, but sometimes the first - # lvchange -a n fails for no particularly good reason, so this is - # a pretty good workaround - call(["/sbin/lvchange", "-a", "n", lvpath]) - rv = call(["/sbin/lvchange", "-a", "n", lvpath]) - if rv != 0: - error() - rv = call(["/sbin/lvchange", "-a", "ey", lvpath]) - if rv != 0: - error() - rv = call(["/sbin/lvremove", "--force", lvpath]) - if rv != 0: - error() + + # Rename the LV to something else so we can wipe it before reusing + # the space + 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) + p = Popen(["/sbin/lvrename", lvpath, new_lvpath], stdout=PIPE, stderr=PIPE) + rv = p.wait() + if rv == 5 and 'already exists in volume group' in p.stderr.read(): + continue + elif rv != 0: + error() + else: + break ensureoff(machine) + + # 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) @@ -85,5 +96,23 @@ elif subcommand == "lvcreate": 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)