def list(path=None): """Return a list of all the tapdisks on a system (if path == None) or only those which have opened a file in a particular directory tree (if path == "/some/tree").""" cmd = [TAP_CTL, "list"] output = util.ioretry(lambda: util.pread2(cmd), errlist=[errno.EPROTO, errno.ENOENT]) results = [] for line in output.split("\n"): # FIXME: tap-ctl writes error messages to stdout and # confuses this parser if line == "blktap kernel module not installed\n": # This isn't pretty but (a) neither is confusing stdout/stderr # and at least causes the error to describe the fix raise Exception, "blktap kernel module not installed: try 'modprobe blktap'" minor = None pid = None file = None # pid=16775 minor=0 state=0 args=vhd:/foo/data/108.vhd # line = [minor=1] for field in line.rstrip().split(" "): bits = field.split("=") if len(bits) == 2: key, val = field.split("=") if key == "pid": pid = int(val, 10) elif key == "minor": minor = int(val, 10) elif key == "args": x = val.split(":", 2) file = (x[0], x[1]) else: log("Ignoring unexpected tap-ctl output: %s" % repr(field)) if minor or pid: if not path or (file and file[1].startswith(path)): c = Tapdisk(minor=minor, pid=pid, file=file) results.append(c) return results
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import os import util, errno, os.path from xcp import log, MissingDependency MOUNT_NFS = "/sbin/mount.nfs" UMOUNT = "/bin/umount" MTAB = "/etc/mtab" MKDIR = "/bin/mkdir" if not(os.path.exists(MOUNT_NFS)): log("%s does not exist: do you need to install nfs-utils?" % MOUNT_NFS) raise MissingDependency(MOUNT_NFS) if not(os.path.exists(UMOUNT)): log("%s does not exist: do you need to install util-linux?" % UMOUNT) raise MissingDependency(UMOUNT) if not(os.path.exists(MTAB)): log("%s does not exist: this environment is too strange?" % MTAB) raise MissingDependency(MTAB) if not(os.path.exists(MKDIR)): log("%s does not exist: do you need to install coreutils?" % MKDIR) raise MissingDependency(MKDIR) class Mount:
# # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import util, errno, os.path from xcp import log, MissingDependency TAP_CTL = "/usr/sbin/tap-ctl" if not (os.path.exists(TAP_CTL)): log("%s does not exist: do you need to install tapdisk/blktap?" % TAP_CTL) raise MissingDependency(TAP_CTL) class Tapdisk: def __init__(self, minor=None, pid=None, file=None): if minor == None: minor = self._allocate() if pid == None: pid = self._spawn() self._attach(minor, pid) self.minor = minor self.pid = pid self.file = file self.mirror = None