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

Last change on this file since 155 was 155, checked in by tabbott, 17 years ago

fix bug in the lvcreate-all script.

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