1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os.path |
---|
5 | from subprocess import call, PIPE, Popen |
---|
6 | |
---|
7 | def check(b): |
---|
8 | if not b: |
---|
9 | exit(1) |
---|
10 | |
---|
11 | vg = "xenvg" |
---|
12 | prefix = "d_" |
---|
13 | |
---|
14 | subcommand = sys.argv[1] |
---|
15 | |
---|
16 | def ensureoff(machine): |
---|
17 | # Make sure the machine is off, but we don't care about errors if it is already off. |
---|
18 | rv = call(["/usr/sbin/xm", "destroy", prefix + machine], |
---|
19 | stderr=PIPE) |
---|
20 | |
---|
21 | if subcommand == "lvcreate-all": |
---|
22 | from sipb_xen_database import * |
---|
23 | import re |
---|
24 | connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen') |
---|
25 | for d in Disk.select(): |
---|
26 | check(re.match('^[A-Za-z0-9]+$', d.guest_device_name)) |
---|
27 | machine = Machine.get(d.machine_id) |
---|
28 | check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name)) |
---|
29 | lvname = prefix + machine.name + "_" + d.guest_device_name |
---|
30 | if not os.path.exists("/dev/%s/%s" % (vg, lvname)): |
---|
31 | # LV doesn't exist |
---|
32 | print >>sys.stderr, "Creating LV %s..." % (lvname,) |
---|
33 | rv = call(["/sbin/lvcreate", "-L", str(d.size) + "M", "-n", lvname, vg]) |
---|
34 | if rv != 0: |
---|
35 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
36 | sys.exit(1) |
---|
37 | else: |
---|
38 | machine = sys.argv[2] |
---|
39 | disk = sys.argv[3] |
---|
40 | lvname = prefix + machine + "_" + disk |
---|
41 | lvpath = "/dev/" + vg + "/" + lvname |
---|
42 | if subcommand == "lvremove": |
---|
43 | rv = call(["/sbin/lvremove", "--force", lvpath]) |
---|
44 | ensureoff(machine) |
---|
45 | if rv != 0: |
---|
46 | print >>sys.stderr, "Error removing LV %s\n" %(lvname,) |
---|
47 | sys.exit(1) |
---|
48 | elif subcommand == "lvresize": |
---|
49 | size = sys.argv[4] |
---|
50 | ensureoff(machine) |
---|
51 | p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath], |
---|
52 | stdin=PIPE, stderr=PIPE) |
---|
53 | print >> p.stdin, 'y' |
---|
54 | err = p.stderr.read() |
---|
55 | if p.wait() != 0 and 'matches existing size' not in err: |
---|
56 | print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,) |
---|
57 | print >> sys.stderr, err |
---|
58 | sys.exit(1) |
---|
59 | print >> sys.stderr, err |
---|
60 | elif subcommand == "lvrename": |
---|
61 | newmachine = sys.argv[4] |
---|
62 | newlvname = prefix + newmachine + "_" + disk |
---|
63 | ensureoff(machine) |
---|
64 | ensureoff(newmachine) |
---|
65 | rv = call(["/sbin/lvrename", vg, lvname, newlvname]) |
---|
66 | if rv != 0: |
---|
67 | print >>sys.stderr, "Error renaming LV %s\n" %(lvname,) |
---|
68 | sys.exit(1) |
---|
69 | elif subcommand == "lvcreate": |
---|
70 | size = sys.argv[4] |
---|
71 | rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg]) |
---|
72 | if rv != 0: |
---|
73 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
74 | sys.exit(1) |
---|
75 | |
---|
76 | |
---|