5 from subprocess import call, PIPE, Popen
6 from invirt.config import structs as config
15 subcommand = sys.argv[1]
17 def ensureoff(machine):
18 # Make sure the machine is off, but we don't care about errors if it is already off.
19 rv = call(["/usr/sbin/xm", "destroy", prefix + machine],
22 if subcommand == "lvcreate-all":
23 from invirt import database
26 for d in Disk.select():
27 check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
28 machine = Machine.get(d.machine_id)
29 check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name))
30 lvname = prefix + machine.name + "_" + d.guest_device_name
31 if not os.path.exists("/dev/%s/%s" % (vg, lvname)):
33 print >>sys.stderr, "Creating LV %s..." % (lvname,)
34 rv = call(["/sbin/lvcreate", "-L", str(d.size) + "M", "-n", lvname, vg])
36 print >>sys.stderr, "Error creating LV %s\n" %(lvname,)
41 lvname = prefix + machine + "_" + disk
42 lvpath = "/dev/" + vg + "/" + lvname
43 if subcommand == "lvremove":
44 rv = call(["/sbin/lvremove", "--force", lvpath])
47 print >>sys.stderr, "Error removing LV %s\n" %(lvname,)
49 elif subcommand == "lvresize":
52 p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath],
53 stdin=PIPE, stderr=PIPE)
56 if p.wait() != 0 and 'matches existing size' not in err:
57 print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,)
58 print >> sys.stderr, err
60 print >> sys.stderr, err
61 elif subcommand == "lvrename":
62 newmachine = sys.argv[4]
63 newlvname = prefix + newmachine + "_" + disk
66 rv = call(["/sbin/lvrename", vg, lvname, newlvname])
68 print >>sys.stderr, "Error renaming LV %s\n" %(lvname,)
70 elif subcommand == "lvcreate":
72 rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg])
74 print >>sys.stderr, "Error creating LV %s\n" %(lvname,)