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

Last change on this file since 836 was 834, checked in by y_z, 16 years ago

use invirt config in sipb-xen-lvm and to generate /etc/remctl/acl/remote

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/env python
2
3import sys
4import os.path
5from subprocess import call, PIPE, Popen
6from invirt.config import structs as config
7
8def check(b):
9    if not b:
10        exit(1)
11
12vg = "xenvg"
13prefix = "d_"
14
15subcommand = sys.argv[1]
16
17def ensureoff(machine):
18    # Make sure the machine is off, but we don't care about errors if it is already off.
19    rv = call(["/usr/sbin/xm", "destroy", prefix + machine],
20              stderr=PIPE)
21
22if subcommand == "lvcreate-all":
23    from sipb_xen_database import *
24    import re
25    connect(config.db.uri)
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)
38else:
39    machine = sys.argv[2]
40    disk = sys.argv[3]
41    lvname = prefix + machine + "_" + disk
42    lvpath = "/dev/" + vg + "/" + lvname
43if subcommand == "lvremove":
44    rv = call(["/sbin/lvremove", "--force", lvpath])
45    ensureoff(machine)
46    if rv != 0:
47        print >>sys.stderr, "Error removing LV %s\n" %(lvname,)
48        sys.exit(1)
49elif subcommand == "lvresize":
50    size = sys.argv[4]
51    ensureoff(machine)
52    p = Popen(["/sbin/lvresize", "-L", size + "M", lvpath],
53              stdin=PIPE, stderr=PIPE)
54    print >> p.stdin, 'y'
55    err = p.stderr.read()
56    if p.wait() != 0 and 'matches existing size' not in err:
57        print >> sys.stderr, "Error resizing LV %s:\n" %(lvname,)
58        print >> sys.stderr, err
59        sys.exit(1)
60    print >> sys.stderr, err
61elif subcommand == "lvrename":
62    newmachine = sys.argv[4]
63    newlvname = prefix + newmachine + "_" + disk
64    ensureoff(machine)
65    ensureoff(newmachine)   
66    rv = call(["/sbin/lvrename", vg, lvname, newlvname])
67    if rv != 0:
68        print >>sys.stderr, "Error renaming LV %s\n" %(lvname,)
69        sys.exit(1)
70elif subcommand == "lvcreate":
71    size = sys.argv[4]
72    rv = call(["/sbin/lvcreate", "-L", size + "M", "-n", lvname, vg])
73    if rv != 0:
74        print >>sys.stderr, "Error creating LV %s\n" %(lvname,)
75        sys.exit(1)
76   
77
Note: See TracBrowser for help on using the repository browser.