1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os.path |
---|
5 | from subprocess import call |
---|
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 | machine = sys.argv[2] |
---|
16 | disk = sys.argv[3] |
---|
17 | lvname = prefix + machine + "_" + disk |
---|
18 | lvpath = "/dev/" + vg + "/" + lvname |
---|
19 | |
---|
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]) |
---|
23 | |
---|
24 | if subcommand == "lvremove": |
---|
25 | rv = call(["/sbin/lvremove", "--force", lvpath]) |
---|
26 | ensureoff(machine) |
---|
27 | if rv != 0: |
---|
28 | print >>sys.stderr, "Error removing LV %s\n" %(lvname,) |
---|
29 | sys.exit(1) |
---|
30 | elif subcommand == "lvresize": |
---|
31 | size = sys.argv[4] |
---|
32 | ensureoff(machine) |
---|
33 | rv = call(["/sbin/lvresize", "-L", size + "M", lvpath]) |
---|
34 | if rv != 0: |
---|
35 | print >>sys.stderr, "Error resizing LV %s\n" %(lvname,) |
---|
36 | sys.exit(1) |
---|
37 | elif subcommand == "lvrename": |
---|
38 | newmachine = sys.argv[4] |
---|
39 | newlvname = prefix + newmachine + "_" + disk |
---|
40 | ensureoff(machine) |
---|
41 | ensureoff(newmachine) |
---|
42 | rv = call(["/sbin/lvrename", vg, lvname, newlvname]) |
---|
43 | if rv != 0: |
---|
44 | print >>sys.stderr, "Error renaming LV %s\n" %(lvname,) |
---|
45 | sys.exit(1) |
---|
46 | elif subcommand == "lvcreate": |
---|
47 | size = sys.argv[4] |
---|
48 | rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg]) |
---|
49 | if rv != 0: |
---|
50 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
51 | sys.exit(1) |
---|
52 | |
---|
53 | elif subcommand == "lvcreate-all": |
---|
54 | from sipb_xen_database import * |
---|
55 | import re |
---|
56 | connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen') |
---|
57 | for d in Disk.select(): |
---|
58 | check(re.match('^[A-Za-z0-9]+$', d.guest_device_name)) |
---|
59 | machine = Machine.get(d.machine_id) |
---|
60 | check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name)) |
---|
61 | lvname = prefix + machine.name + "_" + d.guest_device_name |
---|
62 | if not os.path.exists("/dev/%s/%s" % (vg, lvname)): |
---|
63 | # LV doesn't exist |
---|
64 | print >>sys.stderr, "Creating LV %s..." % (lvname,) |
---|
65 | rv = call(["/sbin/lvcreate", "-L", str(d.size) + "M", "-n", lvname, vg]) |
---|
66 | if rv != 0: |
---|
67 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
68 | sys.exit(1) |
---|
69 | |
---|