1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os.path |
---|
5 | from subprocess import call, PIPE, Popen |
---|
6 | from invirt.config import structs as config |
---|
7 | |
---|
8 | def check(b): |
---|
9 | if not b: |
---|
10 | exit(1) |
---|
11 | |
---|
12 | vg = "xenvg" |
---|
13 | prefix = "d_" |
---|
14 | |
---|
15 | subcommand = sys.argv[1] |
---|
16 | |
---|
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], |
---|
20 | stderr=PIPE) |
---|
21 | |
---|
22 | if subcommand == "lvcreate-all": |
---|
23 | from invirt import database |
---|
24 | import re |
---|
25 | database.connect() |
---|
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)): |
---|
32 | # LV doesn't exist |
---|
33 | print >>sys.stderr, "Creating LV %s..." % (lvname,) |
---|
34 | rv = call(["/sbin/lvcreate", "-L", str(d.size) + "M", "-n", lvname, vg]) |
---|
35 | if rv != 0: |
---|
36 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
37 | sys.exit(1) |
---|
38 | else: |
---|
39 | machine = sys.argv[2] |
---|
40 | disk = sys.argv[3] |
---|
41 | lvname = prefix + machine + "_" + disk |
---|
42 | lvpath = "/dev/" + vg + "/" + lvname |
---|
43 | if subcommand == "lvremove": |
---|
44 | def error(): |
---|
45 | print >>sys.stderr, "Error removing LV %s\n" % lvname |
---|
46 | sys.exit(1) |
---|
47 | rv = call(["/sbin/lvchange", "-a", "n", lvpath]) |
---|
48 | if rv != 0: |
---|
49 | error() |
---|
50 | rv = call(["/sbin/lvchange", "-a", "ey", lvpath]) |
---|
51 | if rv != 0: |
---|
52 | error() |
---|
53 | rv = call(["/sbin/lvremove", "--force", lvpath]) |
---|
54 | if rv != 0: |
---|
55 | error() |
---|
56 | ensureoff(machine) |
---|
57 | elif subcommand == "lvresize": |
---|
58 | size = sys.argv[4] |
---|
59 | ensureoff(machine) |
---|
60 | p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath], |
---|
61 | stdin=PIPE, stderr=PIPE) |
---|
62 | print >> p.stdin, 'y' |
---|
63 | err = p.stderr.read() |
---|
64 | if p.wait() != 0 and 'matches existing size' not in err: |
---|
65 | print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,) |
---|
66 | print >> sys.stderr, err |
---|
67 | sys.exit(1) |
---|
68 | print >> sys.stderr, err |
---|
69 | elif subcommand == "lvrename": |
---|
70 | newmachine = sys.argv[4] |
---|
71 | newlvname = prefix + newmachine + "_" + disk |
---|
72 | ensureoff(machine) |
---|
73 | ensureoff(newmachine) |
---|
74 | rv = call(["/sbin/lvrename", vg, lvname, newlvname]) |
---|
75 | if rv != 0: |
---|
76 | print >>sys.stderr, "Error renaming LV %s\n" %(lvname,) |
---|
77 | sys.exit(1) |
---|
78 | elif subcommand == "lvcreate": |
---|
79 | size = sys.argv[4] |
---|
80 | rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg]) |
---|
81 | if rv != 0: |
---|
82 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
83 | sys.exit(1) |
---|
84 | |
---|
85 | |
---|