+# handle lctl interface
+class LCTLInterface:
+ """
+ Manage communication with lctl
+ """
+
+ def __init__(self, cmd):
+ """
+ Initialize close by finding the lctl binary.
+ """
+ syspath = string.split(os.environ['PATH'], ':')
+ syspath.insert(0, "../utils");
+ self.lctlcmd = None
+ for d in syspath:
+ lctl = os.path.join(d,cmd)
+ if os.access(lctl, os.X_OK):
+ self.lctl = lctl
+ break
+ if not self.lctl:
+ raise RuntimeError, "unable to find lctl binary."
+
+ def run(self, cmds):
+ """
+ run lctl
+ the cmds are written to stdin of lctl
+ lctl doesn't return errors when run in script mode, so
+ stderr is checked
+ should modify command line to accept multiple commands, or
+ create complex command line options
+ """
+ debug("+", self.lctl, cmds)
+ if isnotouch(): return ([], 0)
+ p = popen2.Popen3(self.lctl, 1)
+ p.tochild.write(cmds + "\n")
+ p.tochild.close()
+ out = p.fromchild.readlines()
+ ret = p.poll()
+ err = p.childerr.readlines()
+ if ret or len(err):
+ log (self.lctl, "error:", ret)
+ logall(err)
+ raise CommandError, err
+ return ret, out
+
+
+ # create a new device with lctl
+ def network(self, net, nid):
+ cmds = """
+ network %s
+ mynid %s
+ quit""" % (net, nid)
+ self.run(cmds)
+
+ # create a new connection
+ def connect(self, net, nid, port, servuuid):
+ cmds = """
+ network %s
+ connect %s %d
+ add_uuid %s %s
+ quit""" % (net, nid, port, servuuid, nid)
+ self.run(cmds)
+
+ # create a new device with lctl
+ def disconnect(self, net, nid, port, servuuid):
+ cmds = """
+ network %s
+ disconnect %s
+ quit""" % (net, nid)
+ self.run(cmds)
+
+ # create a new device with lctl
+ def newdev(self, attach, setup):
+ cmds = """
+ newdev
+ attach %s
+ setup %s
+ quit""" % (attach, setup)
+ self.run(cmds)
+
+ # cleanup a device
+ def cleanup(self, name, uuid):
+ cmds = """
+ device $%s
+ cleanup
+ detach
+ quit""" % (name)
+ self.run(cmds)
+
+ # create an lov
+ def lovconfig(self, uuid, mdcuuid, stripe_cnt, stripe_sz, pattern, devlist):
+ cmds = """
+ device $%s
+ probe
+ lovconfig %s %d %d %s %s
+ quit""" % (mdcuuid, uuid, stripe_cnt, stripe_sz, pattern, devlist)
+ self.run(cmds)
+
+# ============================================================