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