source: trunk/packages/sipb-xen-remctl-auto/files/usr/sbin/sipb-xen-lvm @ 738

Last change on this file since 738 was 175, checked in by ecprice, 17 years ago

Fix errors on lvresize to equal or smaller value.

  • Property svn:executable set to *
File size: 2.4 KB
Line 
1#!/usr/bin/env python
2
3import sys
4import os.path
5from subprocess import call, PIPE, Popen
6
7def check(b):
8    if not b:
9        exit(1)
10
11vg = "xenvg"
12prefix = "d_"
13
14subcommand = sys.argv[1]
15
16def ensureoff(machine):
17    # Make sure the machine is off, but we don't care about errors if it is already off.
18    rv = call(["/usr/sbin/xm", "destroy", prefix + machine],
19              stderr=PIPE)
20
21if subcommand == "lvcreate-all":
22    from sipb_xen_database import *
23    import re
24    connect('postgres://sipb-xen@sipb-xen-dev.mit.edu/sipb_xen')
25    for d in Disk.select():
26        check(re.match('^[A-Za-z0-9]+$', d.guest_device_name))
27        machine = Machine.get(d.machine_id)
28        check(re.match('^[A-Za-z0-9][A-Za-z0-9._-]*$', machine.name))
29        lvname = prefix + machine.name + "_" + d.guest_device_name
30        if not os.path.exists("/dev/%s/%s" % (vg, lvname)):
31            # LV doesn't exist
32            print >>sys.stderr, "Creating LV %s..." % (lvname,)
33            rv = call(["/sbin/lvcreate", "-L", str(d.size) + "M", "-n", lvname, vg])
34            if rv != 0:
35                print >>sys.stderr, "Error creating LV %s\n" %(lvname,)
36                sys.exit(1)
37else:
38    machine = sys.argv[2]
39    disk = sys.argv[3]
40    lvname = prefix + machine + "_" + disk
41    lvpath = "/dev/" + vg + "/" + lvname
42if subcommand == "lvremove":
43    rv = call(["/sbin/lvremove", "--force", lvpath])
44    ensureoff(machine)
45    if rv != 0:
46        print >>sys.stderr, "Error removing LV %s\n" %(lvname,)
47        sys.exit(1)
48elif subcommand == "lvresize":
49    size = sys.argv[4]
50    ensureoff(machine)
51    p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath],
52              stdin=PIPE, stderr=PIPE)
53    print >> p.stdin, 'y'
54    err = p.stderr.read()
55    if p.wait() != 0 and 'matches existing size' not in err:
56        print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,)
57        print >> sys.stderr, err
58        sys.exit(1)
59    print >> sys.stderr, err
60elif subcommand == "lvrename":
61    newmachine = sys.argv[4]
62    newlvname = prefix + newmachine + "_" + disk
63    ensureoff(machine)
64    ensureoff(newmachine)   
65    rv = call(["/sbin/lvrename", vg, lvname, newlvname])
66    if rv != 0:
67        print >>sys.stderr, "Error renaming LV %s\n" %(lvname,)
68        sys.exit(1)
69elif subcommand == "lvcreate":
70    size = sys.argv[4]
71    rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg])
72    if rv != 0:
73        print >>sys.stderr, "Error creating LV %s\n" %(lvname,)
74        sys.exit(1)
75   
76
Note: See TracBrowser for help on using the repository browser.