[146] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
| 4 | import os.path |
---|
[175] | 5 | from subprocess import call, PIPE, Popen |
---|
[834] | 6 | from invirt.config import structs as config |
---|
[146] | 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. |
---|
[175] | 19 | rv = call(["/usr/sbin/xm", "destroy", prefix + machine], |
---|
| 20 | stderr=PIPE) |
---|
[146] | 21 | |
---|
[156] | 22 | if subcommand == "lvcreate-all": |
---|
[837] | 23 | from invirt import database |
---|
[156] | 24 | import re |
---|
[837] | 25 | database.connect() |
---|
[156] | 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 |
---|
[146] | 43 | if subcommand == "lvremove": |
---|
[1066] | 44 | def error(): |
---|
| 45 | print >>sys.stderr, "Error removing LV %s\n" % lvname |
---|
| 46 | sys.exit(1) |
---|
[1716] | 47 | # I know this is the wrong answer, but sometimes the first |
---|
| 48 | # lvchange -a n fails for no particularly good reason, so this is |
---|
| 49 | # a pretty good workaround |
---|
| 50 | call(["/sbin/lvchange", "-a", "n", lvpath]) |
---|
[1066] | 51 | rv = call(["/sbin/lvchange", "-a", "n", lvpath]) |
---|
| 52 | if rv != 0: |
---|
| 53 | error() |
---|
| 54 | rv = call(["/sbin/lvchange", "-a", "ey", lvpath]) |
---|
| 55 | if rv != 0: |
---|
| 56 | error() |
---|
[146] | 57 | rv = call(["/sbin/lvremove", "--force", lvpath]) |
---|
[1066] | 58 | if rv != 0: |
---|
| 59 | error() |
---|
[146] | 60 | ensureoff(machine) |
---|
[149] | 61 | elif subcommand == "lvresize": |
---|
[146] | 62 | size = sys.argv[4] |
---|
| 63 | ensureoff(machine) |
---|
[175] | 64 | p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath], |
---|
| 65 | stdin=PIPE, stderr=PIPE) |
---|
| 66 | print >> p.stdin, 'y' |
---|
| 67 | err = p.stderr.read() |
---|
| 68 | if p.wait() != 0 and 'matches existing size' not in err: |
---|
| 69 | print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,) |
---|
| 70 | print >> sys.stderr, err |
---|
[146] | 71 | sys.exit(1) |
---|
[175] | 72 | print >> sys.stderr, err |
---|
[149] | 73 | elif subcommand == "lvrename": |
---|
[146] | 74 | newmachine = sys.argv[4] |
---|
| 75 | newlvname = prefix + newmachine + "_" + disk |
---|
| 76 | ensureoff(machine) |
---|
| 77 | ensureoff(newmachine) |
---|
| 78 | rv = call(["/sbin/lvrename", vg, lvname, newlvname]) |
---|
| 79 | if rv != 0: |
---|
| 80 | print >>sys.stderr, "Error renaming LV %s\n" %(lvname,) |
---|
| 81 | sys.exit(1) |
---|
[149] | 82 | elif subcommand == "lvcreate": |
---|
[146] | 83 | size = sys.argv[4] |
---|
| 84 | rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg]) |
---|
| 85 | if rv != 0: |
---|
| 86 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
| 87 | sys.exit(1) |
---|
| 88 | |
---|
| 89 | |
---|