8 from subprocess import call, PIPE, Popen
9 from invirt.config import structs as config
18 subcommand = sys.argv[1]
20 def ensureoff(machine):
21 # Make sure the machine is off, but we don't care about errors if it is already off.
22 rv = call(["/usr/sbin/xm", "destroy", prefix + machine],
25 def lvm_activation(path, mode):
26 p = Popen(["/sbin/lvchange", "-a%s" % (mode,), path], stdout=PIPE, stderr=PIPE)
30 machine_specific = subcommand not in ['lvcreate-all', 'vgcapacity']
35 lvname = prefix + machine + "_" + disk
36 lvpath = "/dev/" + vg + "/" + lvname
38 if subcommand == "lvcreate-all":
39 from invirt.database import models, connect
42 for d in models.Disk.query().all():
43 check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
44 machine = models.Machine.query().filter_by(machine_id=d.machine_id).one()
45 check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name))
46 lvname = prefix + machine.name + "_" + d.guest_device_name
47 if not os.path.exists("/dev/%s/%s" % (vg, lvname)):
49 print >>sys.stderr, "Creating LV %s..." % (lvname,)
50 rv = call(["/sbin/lvcreate", "-L", str(d.size) + "M", "-n", lvname, vg])
52 print >>sys.stderr, "Error creating LV %s\n" %(lvname,)
54 elif subcommand == "lvremove":
56 print >>sys.stderr, "Error removing LV %s\n" % lvname
59 # Rename the LV to something else so we can wipe it before reusing
61 if lvm_activation(lvpath, 'n') != 0:
62 print >>sys.stderr, "Could not deactivate LV %s", % (lvname,)
65 new_lvname = "old_%s_%s" % (lvname, ''.join(random.choice(string.ascii_letters) for i in xrange(6)))
66 new_lvpath = "/dev/%s/%s" % (vg, new_lvname)
67 p = Popen(["/sbin/lvrename", lvpath, new_lvpath], stdout=PIPE, stderr=PIPE)
69 if rv == 5 and 'already exists in volume group' in p.stderr.read():
74 if lvm_activation(new_lvpath, 'y') != 0:
75 print >> sys.stderr, "Could not reactivate renamed LV %s" % (lvname,)
80 # Touch a file corresponding to the new name of the LV; a separate
81 # daemon will handle wiping and deleting it.
82 open(os.path.join('/var/lib/invirt-remote/cleanup', new_lvname), 'w')
83 elif subcommand == "lvresize":
86 if lvm_activation(lvpath, 'n') != 0:
87 print >>sys.stderr, "Could not deactivate LV %s", % (lvname,)
89 p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath],
90 stdin=PIPE, stderr=PIPE)
93 if p.wait() != 0 and 'matches existing size' not in err:
94 print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,)
95 print >> sys.stderr, err
97 if lvm_activation(lvpath, 'y') != 0:
98 print >> sys.stderr, "Could not reactivate resized LV %s" % (lvname,)
100 print >> sys.stderr, err
101 elif subcommand == "lvrename":
102 newmachine = sys.argv[4]
103 newlvname = prefix + newmachine + "_" + disk
105 ensureoff(newmachine)
106 lvpath = "/dev/" + vg + "/" + lvname
107 new_lvpath = "/dev/" + vg + "/" + newlvname
108 if lvm_activation(lvpath, 'n') != 0:
109 print >>sys.stderr, "Could not deactivate LV %s", % (lvname,)
111 rv = call(["/sbin/lvrename", vg, lvname, newlvname])
113 print >>sys.stderr, "Error renaming LV %s\n" %(lvname,)
115 if lvm_activation(new_lvpath, 'y') != 0:
116 print >> sys.stderr, "Could not reactivate renamed LV %s" % (lvname,)
118 elif subcommand == "lvcreate":
120 rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg])
122 print >>sys.stderr, "Error creating LV %s\n" %(lvname,)
124 elif subcommand == "vgcapacity":
125 p = Popen(["/sbin/vgs", "-o", "vg_extent_size,vg_extent_count,vg_free_count",
126 "--noheadings", "--units", "k", "--nosuffix", "--separator", ":",
128 stdout=PIPE, stderr=PIPE)
129 out,err = p.communicate()
132 fields = out.strip().split(':')
133 extent_size = float(fields[0]) # in kibibytes
134 extent_count = int(fields[1])
135 free_count = int(fields[2])
136 total_space_TiB = extent_size * extent_count / 1024.**3
137 free_space_TiB = extent_size * free_count / 1024.**3
138 print >>sys.stdout, "Total: %.3f TiB" % (total_space_TiB,)
139 print >>sys.stdout, "Free: %.3f TiB" % (free_space_TiB,)
141 print >>sys.stderr, "Error obtaining vg capacity:\n%s\n" % (err,)