Whamcloud - gitweb
- initial version
[fs/lustre-release.git] / lustre / utils / lmc
1 #!/usr/bin/env python
2
3 import sys, getopt
4 import xml.dom.minidom
5
6 #
7 # Example of walking the tree
8 #
9 def handleLustre(lustre):
10     handleHost(lustre.getElementsByTagName("node"))
11
12 def handleHost(hosts):
13     for host in hosts:
14         name = host.getAttribute("name")
15         uuid = host.getAttribute("uuid")
16
17 #
18 # manage names and uuids
19 # need to initialize this by walking tree to ensure
20 # no duplicate names or uuids are created.
21 # this are just place holders for now.
22 name_ctr = 1
23 def new_name(base):
24     global name_ctr
25     name = "%s_%d" % (base, name_ctr)
26     name_ctr += 1
27     return name
28
29 def get_uuid(name):
30     return "%s_UUID" % (name)
31
32 #
33 # Create a new empty lustre document 
34 def new_Lustre():
35     str = """<lustre>
36 </lustre>"""
37     dom = xml.dom.minidom.parseString(str)
38     return dom
39
40 #
41 # Create a new object the "correct" way by using the DOM api.
42 #
43 def new_OBD(dom, name, fs, devname, format, dev_size=0, dev_file=""):
44     uuid = get_uuid(name)
45     
46     obd = dom.createElement("obd")
47     obd.setAttribute("name", name)
48     obd.setAttribute("uuid", uuid)
49
50     fstype = dom.createElement("fstype")
51     txt= dom.createTextNode(fs)
52     fstype.appendChild(txt)
53     obd.appendChild(fstype)
54     
55     dev = dom.createElement("device")
56     if (dev_size):
57         dev.setAttribute("size", "%s" % (dev_size))
58     txt = dom.createTextNode(devname)
59     dev.appendChild(txt)
60     obd.appendChild(dev)
61
62     fmt = dom.createElement("autoformat")
63     txt = dom.createTextNode(format)
64     fmt.appendChild(txt)
65     obd.appendChild(fmt)
66
67     return obd
68
69 #
70 # Create new object the fast and easy way
71 # (note: dom is not needed for this way)
72 def new_OSC(dom, osc, obd):
73     osc_uuid = get_uuid(osc)
74     obd_uuid = get_uuid(obd)
75
76     osc_str ="""
77 <osc name="%s" uuid="%s">
78    <service_id num="1" name="%s" uuid="%s"/>
79 </osc> """ % (osc, osc_uuid, obd, obd_uuid)
80     osc = xml.dom.minidom.parseString(osc_str)
81     return osc.getElementsByTagName("osc")[0]
82
83 #
84 # Create new object the fast and easy way
85 #
86 def new_OST(dom, ost, host, port, obd):
87     ost_uuid = get_uuid(ost)
88     obd_uuid = get_uuid(obd)
89
90     str ="""
91 <ost name="%s" uuid="%s">
92    <network type="tcp">
93       <server>%s</server>
94       <port>%d</port>
95    </network>
96    <server_id num="1" name="%s" uuid="%s"/>
97 </ost> """ % (ost, ost_uuid, host, port, obd, obd_uuid)
98     node = xml.dom.minidom.parseString(str)
99     return node.getElementsByTagName("ost")[0]
100
101 #
102 # Create a new obd, osc, and ost. Add them to the DOM.
103 #
104 def add_OST(dom, options, args):
105     # XXX need some error checking
106     devname = args[0]
107     host = args[1]
108
109     obdname = new_name("obd")
110     oscname = new_name("osc")
111     ostname = new_name("ost")
112     
113     obd = new_OBD(dom, obdname, "extN", devname, "no")
114     osc = new_OSC(dom, oscname, obdname)
115     ost = new_OST(dom, ostname, host, 2020, obdname)
116     
117     dom.getElementsByTagName("lustre")[0].appendChild(obd)
118     dom.getElementsByTagName("lustre")[0].appendChild(osc)
119     dom.getElementsByTagName("lustre")[0].appendChild(ost)
120
121
122 #
123 # Command line processing
124 #
125 def usage():
126     print """usage: lmc [--ost | --mtpt | --lov] cmd args
127 Commands:
128 --ost "device" "host"
129    Creates an OBD/OST/OSC configuration triplet for a new device.
130    When used on "host", the device will be initialized and the OST
131    will be enabled. On client nodes, the OSC will be avaiable.
132
133 --mtpt "mds" "ost/lov-name" /mnt/point
134    Creates a client mount point.
135
136 --lov "mds" "lov name < "all-ost.xml"
137    Produces a logical volum striped over the OSTs found in all-ost.xml.
138    (Not sure how all-ost.xml is created, exactly.)
139
140 Options:
141 --merge="xml file"  Add the new objects to an existing file
142 --format            Format the partitions if unformated
143 --reformat          Reformat partitions (this should be an lconf arg,
144                     I think)
145 (SCRIPT STILL UNDER DEVELOPMENT, MOST COMMANDS/OPTIONS UNIMPLEMENTED)
146 """
147
148 def cmdline(argv):
149     short_opts = "ho:"
150     long_opts = ["ost", "mtpt", "lov",
151                  "merge=", "format", "reformat", "output=",
152                  "help"]
153     opts = []
154     args = []
155     options = {}
156     try:
157         opts, args = getopt.getopt(argv, short_opts, long_opts)
158     except getopt.GetoptError:
159         print "invalid opt"
160         usage()
161         sys.exit(2)
162
163     for o, a in opts:
164         if o in ("-h", "--help"):
165             usage()
166             sys.exit()
167         if o in ("-o", "--output"):
168             options['output'] = a
169         if o == "--ost":
170             options['ost'] = 1
171         if o == "--merge":
172             options['merge'] = a
173         if o == "--format":
174             options['format'] = 1
175         if o  == "--reformat":
176             options['reformat'] = 1
177             
178     return options, args
179
180 def main():
181     options, args = cmdline(sys.argv[1:])
182
183     if options.has_key('merge'):
184         outFile = options['merge']
185         dom = xml.dom.minidom.parse(outFile)
186     else:
187         if options.has_key('output'):
188             outFile = options['output']
189         else:
190             outFile = '-'
191         dom = new_Lustre()
192
193     if options.has_key('ost'):
194         add_OST(dom, options, args)
195     elif options.has_key('mtpt'):
196         print "--mtpt not implemented"
197     elif options.has_key('lov'):
198         print "--mtpt not implemented"
199     else:
200         print "Missing command"
201         usage()
202         sys.exit(1)
203         
204
205     if outFile == '-':
206         print dom.toxml()
207     else:
208         dom.writexml(open(outFile,"w"))
209     
210 if __name__ == "__main__":
211     main()