[146] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
[1987] | 4 | import time |
---|
| 5 | import os |
---|
| 6 | import random |
---|
| 7 | import string |
---|
[175] | 8 | from subprocess import call, PIPE, Popen |
---|
[834] | 9 | from invirt.config import structs as config |
---|
[146] | 10 | |
---|
| 11 | def check(b): |
---|
| 12 | if not b: |
---|
| 13 | exit(1) |
---|
| 14 | |
---|
| 15 | vg = "xenvg" |
---|
| 16 | prefix = "d_" |
---|
| 17 | |
---|
| 18 | subcommand = sys.argv[1] |
---|
| 19 | |
---|
| 20 | def ensureoff(machine): |
---|
| 21 | # Make sure the machine is off, but we don't care about errors if it is already off. |
---|
[175] | 22 | rv = call(["/usr/sbin/xm", "destroy", prefix + machine], |
---|
| 23 | stderr=PIPE) |
---|
[146] | 24 | |
---|
[156] | 25 | if subcommand == "lvcreate-all": |
---|
[837] | 26 | from invirt import database |
---|
[156] | 27 | import re |
---|
[837] | 28 | database.connect() |
---|
[156] | 29 | for d in Disk.select(): |
---|
| 30 | check(re.match('^[A-Za-z0-9]+$', d.guest_device_name)) |
---|
| 31 | machine = Machine.get(d.machine_id) |
---|
| 32 | check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name)) |
---|
| 33 | lvname = prefix + machine.name + "_" + d.guest_device_name |
---|
| 34 | if not os.path.exists("/dev/%s/%s" % (vg, lvname)): |
---|
| 35 | # LV doesn't exist |
---|
| 36 | print >>sys.stderr, "Creating LV %s..." % (lvname,) |
---|
| 37 | rv = call(["/sbin/lvcreate", "-L", str(d.size) + "M", "-n", lvname, vg]) |
---|
| 38 | if rv != 0: |
---|
| 39 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
| 40 | sys.exit(1) |
---|
| 41 | else: |
---|
| 42 | machine = sys.argv[2] |
---|
| 43 | disk = sys.argv[3] |
---|
| 44 | lvname = prefix + machine + "_" + disk |
---|
| 45 | lvpath = "/dev/" + vg + "/" + lvname |
---|
[146] | 46 | if subcommand == "lvremove": |
---|
[1066] | 47 | def error(): |
---|
| 48 | print >>sys.stderr, "Error removing LV %s\n" % lvname |
---|
| 49 | sys.exit(1) |
---|
[1987] | 50 | |
---|
| 51 | # Rename the LV to something else so we can wipe it before reusing |
---|
| 52 | # the space |
---|
| 53 | while True: |
---|
| 54 | new_lvname = "old_%s_%s" % (lvname, ''.join(random.choice(string.ascii_letters) for i in xrange(6))) |
---|
| 55 | new_lvpath = "/dev/%s/%s" % (vg, new_lvname) |
---|
| 56 | p = Popen(["/sbin/lvrename", lvpath, new_lvpath], stdout=PIPE, stderr=PIPE) |
---|
| 57 | rv = p.wait() |
---|
| 58 | if rv == 5 and 'already exists in volume group' in p.stderr.read(): |
---|
| 59 | continue |
---|
| 60 | elif rv != 0: |
---|
| 61 | error() |
---|
| 62 | else: |
---|
| 63 | break |
---|
[146] | 64 | ensureoff(machine) |
---|
[1987] | 65 | |
---|
[2436] | 66 | # Touch a file corresponding to the new name of the LV; a separate |
---|
| 67 | # daemon will handle wiping and deleting it. |
---|
| 68 | open(os.path.join('/var/lib/invirt-remote/cleanup', new_lvname), 'w') |
---|
[149] | 69 | elif subcommand == "lvresize": |
---|
[146] | 70 | size = sys.argv[4] |
---|
| 71 | ensureoff(machine) |
---|
[175] | 72 | p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath], |
---|
| 73 | stdin=PIPE, stderr=PIPE) |
---|
| 74 | print >> p.stdin, 'y' |
---|
| 75 | err = p.stderr.read() |
---|
| 76 | if p.wait() != 0 and 'matches existing size' not in err: |
---|
| 77 | print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,) |
---|
| 78 | print >> sys.stderr, err |
---|
[146] | 79 | sys.exit(1) |
---|
[175] | 80 | print >> sys.stderr, err |
---|
[149] | 81 | elif subcommand == "lvrename": |
---|
[146] | 82 | newmachine = sys.argv[4] |
---|
| 83 | newlvname = prefix + newmachine + "_" + disk |
---|
| 84 | ensureoff(machine) |
---|
| 85 | ensureoff(newmachine) |
---|
| 86 | rv = call(["/sbin/lvrename", vg, lvname, newlvname]) |
---|
| 87 | if rv != 0: |
---|
| 88 | print >>sys.stderr, "Error renaming LV %s\n" %(lvname,) |
---|
| 89 | sys.exit(1) |
---|
[149] | 90 | elif subcommand == "lvcreate": |
---|
[146] | 91 | size = sys.argv[4] |
---|
| 92 | rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg]) |
---|
| 93 | if rv != 0: |
---|
| 94 | print >>sys.stderr, "Error creating LV %s\n" %(lvname,) |
---|
| 95 | sys.exit(1) |
---|
| 96 | |
---|