3 # Copyright (C) 2002 Cluster File Systems, Inc.
4 # Author: Robert Read <rread@clusterfs.com>
6 # This file is part of Lustre, http://www.lustre.org.
8 # Lustre is free software; you can redistribute it and/or
9 # modify it under the terms of version 2 of the GNU General Public
10 # License as published by the Free Software Foundation.
12 # Lustre is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Lustre; if not, write to the Free Software
19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 lmc - lustre configurtion data manager
25 Basic plan for lmc usage:
27 ./lmc --output config.xml --node server --net server1 tcp
28 ./lmc --merge config.xml --node client --net client1 tcp
32 ./lmc --merge config.xml --node server --mds mds1 /tmp/mds1 50000
35 ./lmc --merge config.xml --lov lov1 mds1 65536 0 0
36 ./lmc --merge config.xml --node server --lov lov1 --ost /tmp/ost1 100000
37 ./lmc --merge config.xml --node server --lov lov1 --ost /tmp/ost2 100000
39 # create client config
40 ./lmc --merge config.xml --node client --mtpt /mnt/lustre mds1 lov1
44 import sys, getopt, string
45 import xml.dom.minidom
46 from xml.dom.ext import PrettyPrint
49 DEFAULT_PORT = 988 # XXX What is the right default acceptor port to use?
52 print """usage: lmc [--node --ost | --mtpt | --lov] args
55 Node_name by itself it will create a new node. When used with other
56 commands it specifies the node to modify
58 --net hostname nettype [port, recv_buf, send_buf]
59 Nettype is either tcp, elan, or gm.
60 Requires a node argument
63 Create a MDS using the device
66 --lov lov_name [mdc_name stripe_sz stripe_off pattern]
67 Creates a logical volume
68 When used with other commands, it specifics the lov to modify
71 Configures a MDC for a node.
75 Creates an OBD/OST/OSC configuration triplet for a new device.
76 When used on "host", the device will be initialized and the OST
77 will be enabled. On client nodes, the OSC will be avaiable.
79 If --lov lov_name is used, this device is added to lov.
81 --mtpt /mnt/point mds_name lov_name|osc_name
82 Creates a client mount point.
86 --merge="xml file" Add the new objects to an existing file
87 --format Format the partitions if unformated
88 --reformat Reformat partitions (this should be an lconf arg,
90 --obdtype="obdtype" Specifiy obdtype: valid ones are obdecho and obdfilter.
91 This is only useful for the --ost command.
92 The device parameters are ignored for the obdecho type.
97 msg = string.join(map(str,args))
102 # manage names and uuids
103 # need to initialize this by walking tree to ensure
104 # no duplicate names or uuids are created.
105 # this are just place holders for now.
106 # consider changing this to be like OBD-dev-host
110 while names.has_key(ret):
111 ret = "%s_%d" % (base, ctr)
117 return "%s_UUID" % (name)
120 ldlm_uuid = 'ldlm_UUID'
122 """Create a new empty lustre document"""
123 # adding ldlm here is a bit of a hack, but one is enough.
124 str = """<lustre> <ldlm name="%s" uuid="%s"/> </lustre>""" % (ldlm_name, ldlm_uuid)
125 return dom.parseString(str)
131 """initialize auto-name generation tables"""
133 # get all elements that contain a name attribute
134 for n in doc.childNodes:
135 if n.nodeType == n.ELEMENT_NODE:
137 names[getName(n)] = 1
138 uuids[getUUID(n)] = 1
141 def get_format_flag(options):
142 if options.has_key('format'):
143 if options['format']:
150 def __init__(self, doc):
153 def ref(self, type, uuid):
154 """ generate <[type]_ref uuidref="[uuid]"/> """
155 tag = "%s_ref" % (type)
156 ref = self.doc.createElement(tag)
157 ref.setAttribute("uuidref", uuid)
160 def newService(self, tag, name, uuid):
161 """ create a new service elmement, which requires name and uuid attributes """
162 new = self.doc.createElement(tag)
163 new.setAttribute("name", name);
164 new.setAttribute("uuid", uuid);
167 def addText(self, node, str):
168 txt = self.doc.createTextNode(str)
169 node.appendChild(txt)
171 def addElement(self, node, tag, str=None):
172 """ create a new element and add it as a child to node. If str is passed,
173 a text node is created for the new element"""
174 new = self.doc.createElement(tag)
176 self.addText(new, str)
177 node.appendChild(new)
180 def network(self, name, uuid, hostname, net, port=0):
181 """create <network> node"""
182 network = self.newService("network", name, uuid)
183 network.setAttribute("type", net);
184 self.addElement(network, "server", hostname)
186 self.addElement(network, "port", "%d" %(port))
189 def node(self, name, uuid):
190 """ create a host """
191 node = self.newService("node", name, uuid)
192 self.addElement(node, 'profile')
195 def ldlm(self, name, uuid):
196 """ create a ldlm """
197 ldlm = self.newService("ldlm", name, uuid)
200 def obd(self, name, uuid, fs, obdtype, devname, format, dev_size=0):
201 obd = self.newService("obd", name, uuid)
202 obd.setAttribute('type', obdtype)
204 self.addElement(obd, "fstype", fs)
206 dev = self.addElement(obd, "device", devname)
208 dev.setAttribute("size", "%s" % (dev_size))
209 self.addElement(obd, "autoformat", format)
212 def osc(self, name, uuid, obd_uuid, net_uuid):
213 osc = self.newService("osc", name, uuid)
214 osc.appendChild(self.ref("ost", net_uuid))
215 osc.appendChild(self.ref("obd", obd_uuid))
218 def ost(self, name, uuid, obd_uuid, net_uuid):
219 ost = self.newService("ost", name, uuid)
220 ost.appendChild(self.ref("network", net_uuid))
221 ost.appendChild(self.ref("obd", obd_uuid))
224 def lov(self, name, uuid, mds_uuid, stripe_sz, stripe_off, pattern):
225 lov = self.newService("lov", name, uuid)
226 lov.appendChild(self.ref("mds", mds_uuid))
227 devs = self.addElement(lov, "devices" )
228 devs.setAttribute("stripesize", stripe_sz)
229 devs.setAttribute("stripeoffset", stripe_off)
230 devs.setAttribute("pattern", pattern)
233 def mds(self, name, uuid, fs, devname, format, net_uuid, node_uuid,
234 failover_uuid = "", dev_size=0 ):
235 mds = self.newService("mds", name, uuid)
236 self.addElement(mds, "fstype", fs)
237 dev = self.addElement(mds, "device", devname)
239 dev.setAttribute("size", "%s" % (dev_size))
240 self.addElement(mds, "autoformat", format)
241 mds.appendChild(self.ref("network", net_uuid))
242 mds.appendChild(self.ref("node", node_uuid))
244 mds.appendChild(self.ref("failover", failover_uuid))
247 def mdc(self, name, uuid, mds_uuid):
248 mdc = self.newService("mdc", name, uuid)
249 mdc.appendChild(self.ref("mds", mds_uuid))
252 def mountpoint(self, name, uuid, mdc_uuid, osc_uuid, path):
253 mtpt = self.newService("mountpoint", name, uuid)
254 mtpt.appendChild(self.ref("mdc", mdc_uuid))
255 mtpt.appendChild(self.ref("osc", osc_uuid))
256 self.addElement(mtpt, "path", path)
260 return n.getAttribute('name')
263 return node.getAttribute('uuid')
266 def findByName(lustre, name, tag = ""):
267 for n in lustre.childNodes:
268 if n.nodeType == n.ELEMENT_NODE:
269 if tag and n.nodeName != tag:
271 if getName(n) == name:
274 n = findByName(n, name)
278 def lookup(node, uuid):
279 for n in node.childNodes:
280 if n.nodeType == n.ELEMENT_NODE:
281 if getUUID(n) == uuid:
289 def mds2node(lustre, mds_name):
290 """ Find the node a MDS is configured on """
291 mds = findByName(lustre, mds_name, 'mds')
292 ref = mds.getElementsByTagName('node_ref')
294 error("no node found for:", mds_name)
295 node_uuid = ref[0].getAttribute('uuidref')
296 node = lookup(lustre, node_uuid)
298 error("no node found for :", mds_name)
301 def name2uuid(lustre, name, tag="", fatal=1):
302 ret = findByName(lustre, name, tag)
305 error('name2uuid:', name, "not found.")
310 # XXX: assumes only one network element per node. will fix this
311 # as soon as support for routers is added
312 def get_net_uuid(lustre, node_name):
313 """ get a network uuid for a node_name """
314 node = findByName(lustre, node_name, "node")
316 error ("node not found:", node_name)
317 net = node.getElementsByTagName('network')
319 return getUUID(net[0])
322 def lov_add_osc(gen, lov, osc_uuid):
323 devs = lov.getElementsByTagName('devices')
325 devs[0].appendChild(gen.ref("osc", osc_uuid))
327 error("No devices element found for LOV:", lov)
329 def node_add_profile(gen, node, ref, uuid):
330 ret = node.getElementsByTagName('profile')
332 error('node has no profile:', node)
333 ret[0].appendChild(gen.ref(ref, uuid))
336 # Create a new obd, osc, and ost. Add them to the DOM.
338 def add_ost(gen, lustre, options, args):
340 obdtype = 'obdfilter'
348 if options.has_key('node'):
349 node_name = options['node']
351 error("--ost requires a --node argument")
353 if options.has_key('lov'):
354 lovname = options['lov']
356 if options.has_key('obdtype'):
357 obdtype = options['obdtype']
359 if obdtype == 'obdecho':
366 obdname = new_name('OBD_'+ node_name)
367 oscname = new_name('OSC_'+ node_name)
368 ostname = new_name('OST_'+ node_name)
369 obd_uuid = get_uuid(obdname)
370 ost_uuid = get_uuid(ostname)
371 osc_uuid = get_uuid(oscname)
373 net_uuid = get_net_uuid(lustre, node_name)
375 error("NODE: ", node_name, "not found")
377 obd = gen.obd(obdname, obd_uuid, fstype, obdtype, devname, get_format_flag(options), size)
378 ost = gen.ost(ostname, ost_uuid, obd_uuid, net_uuid)
379 osc = gen.osc(oscname, osc_uuid, obd_uuid, ost_uuid)
382 lov = findByName(lustre, lovname, "lov")
384 error("LOV:", lovname, "not found.")
385 lov_add_osc(gen, lov, osc_uuid)
387 node = findByName(lustre, node_name, "node")
388 node_add_profile(gen, node, 'obd', obd_uuid)
389 node_add_profile(gen, node, 'ost', ost_uuid)
391 lustre.appendChild(obd)
392 lustre.appendChild(osc)
393 lustre.appendChild(ost)
395 def add_net(gen, lustre, options, args):
396 """ create a node with a network config """
400 node_name = options['node']
404 if net_type == 'tcp':
409 # add send, recv buffer size here
410 elif net_type in ('elan', 'gm'):
413 print "Unknown net_type: ", net_type
416 ret = findByName(lustre, node_name, "node")
418 node = do_add_node(gen, lustre, node_name)
421 net_name = new_name('NET_'+ node_name +'_'+ net_type)
422 net_uuid = get_uuid(net_name)
423 node.appendChild(gen.network(net_name, net_uuid, nid, net_type, port))
424 node_add_profile(gen, node, "network", net_uuid)
426 def do_add_node(gen, lustre, node_name):
427 uuid = get_uuid(node_name)
428 node = gen.node(node_name, uuid)
429 node_add_profile(gen, node, 'ldlm', ldlm_uuid)
430 lustre.appendChild(node)
433 def add_node(gen, lustre, options, args):
434 """ create a node with a network config """
438 node_name = options['node']
440 ret = findByName(lustre, node_name, "node")
442 print "Node:", node_name, "exists."
444 do_add_node(gen, lustre, node_name)
447 def add_lov(gen, lustre, options, args):
452 name = options['lov']
457 uuid = get_uuid(name)
459 ret = findByName(lustre, name, "lov")
461 error("LOV: ", name, " already exists.")
463 mds_uuid = name2uuid(lustre, mds_name)
465 node = mds2node(lustre, mds_name)
466 node_add_profile(gen, node, "lov", uuid)
467 lov = gen.lov(name, uuid, mds_uuid, stripe_sz, stripe_off, pattern)
468 lustre.appendChild(lov)
470 def add_mtpt(gen, lustre, options, args):
471 """ create mtpt on a node """
475 if options.has_key('node'):
476 node_name = options['node']
478 error("--mtpt requires a --node argument")
483 mdc_name = 'MDC_' + mds_name
485 name = new_name('MNT_'+ node_name)
487 ret = findByName(lustre, name, "mountpoint")
489 error("MOUNTPOINT: ", name, " already exists.")
491 mdc_uuid = name2uuid(lustre, mdc_name)
492 lov_uuid = name2uuid(lustre, lov_name, tag='lov', fatal=0)
494 lov_uuid = name2uuid(lustre, lov_name, tag='osc', fatal=1)
496 uuid = get_uuid(name)
497 mtpt = gen.mountpoint(name, uuid, mdc_uuid, lov_uuid, path)
498 node = findByName(lustre, node_name, "node")
500 error('node:', node_name, "not found.")
501 node_add_profile(gen, node, "mountpoint", uuid)
502 node_add_profile(gen, node, "mdc", mdc_uuid)
503 lustre.appendChild(mtpt)
505 def add_mdc(gen, lustre, options, args):
506 """ create mtpt on a node """
510 if options.has_key('node'):
511 node_name = options['node']
513 error("--mdc requires a --node argument")
516 mdc_uuid = name2uuid(lustre, mdc_name)
518 node = findByName(lustre, node_name, "node")
520 error('node:', node_name, "not found.")
521 node_add_profile(gen, node, "mdc", mdc_uuid)
523 def add_mds(gen, lustre, options, args):
527 if options.has_key('node'):
528 node_name = options['node']
530 error("--mds requires a --node argument")
532 mds_name = new_name(options['mds'])
539 mdc_name = 'MDC_' + mds_name
540 mds_uuid = get_uuid(mds_name)
541 mdc_uuid = get_uuid(mdc_name)
543 node_uuid = name2uuid(lustre, node_name)
545 node = findByName(lustre, node_name, "node")
546 node_add_profile(gen, node, "mds", mds_uuid)
547 net_uuid = get_net_uuid(lustre, node_name)
549 error("NODE: ", node_name, "not found")
552 mds = gen.mds(mds_name, mds_uuid, "extN", devname, get_format_flag(options),
553 net_uuid, node_uuid, dev_size=size)
554 mdc = gen.mdc(mdc_name, mdc_uuid, mds_uuid)
555 lustre.appendChild(mds)
556 lustre.appendChild(mdc)
560 # Command line processing
563 def parse_cmdline(argv):
564 short_opts = "ho:i:m:"
565 long_opts = ["ost", "mtpt", "lov=", "node=", "mds=", "net",
566 "mdc", "merge=", "format", "reformat", "output=",
567 "obdtype=", "in=", "help"]
572 opts, args = getopt.getopt(argv, short_opts, long_opts)
578 if o in ("-h", "--help"):
580 if o in ("-o", "--output"):
581 options['output'] = a
596 if o in ("-m", "--merge"):
599 options['obdtype'] = a
601 options['format'] = 1
602 if o == "--reformat":
603 options['reformat'] = 1
604 if o in ("--in" , "-i"):
616 self._start = time.time()
617 def stop(self, msg=''):
618 self._stop = time.time()
622 return self._stop - self._start
623 def display(self, msg):
625 str = '%s: %g secs' % (msg, d)
630 options, args = parse_cmdline(sys.argv[1:])
633 if options.has_key('merge'):
634 outFile = options['merge']
635 doc = xml.dom.minidom.parse(outFile)
636 elif options.has_key('in'):
637 doc = xml.dom.minidom.parse(options['in'])
639 doc = new_lustre(xml.dom.minidom)
641 if options.has_key('output'):
642 outFile = options['output']
644 lustre = doc.documentElement
646 if lustre.tagName != "lustre":
647 print "Existing config not valid."
651 if options.has_key('ost'):
652 add_ost(gen, lustre, options, args)
653 elif options.has_key('mtpt'):
654 add_mtpt(gen, lustre, options, args)
655 elif options.has_key('mds'):
656 add_mds(gen, lustre, options, args)
657 elif options.has_key('mdc'):
658 add_mdc(gen, lustre, options, args)
659 elif options.has_key('net'):
660 add_net(gen, lustre, options, args)
661 elif options.has_key('lov'):
662 add_lov(gen, lustre, options, args)
663 elif options.has_key('node'):
664 add_node(gen, lustre, options, args)
666 print "Missing command"
672 PrettyPrint(doc, open(outFile,"w"))
674 if __name__ == "__main__":