3 # QEMU Object Model test tools
5 # Copyright IBM, Corp. 2012
6 # Copyright (C) 2020 Red Hat, Inc.
9 # Anthony Liguori <aliguori@us.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
12 # This work is licensed under the terms of the GNU GPL, version 2 or later. See
13 # the COPYING file in the top-level directory.
17 from fuse import FUSE, FuseOSError, Operations
21 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
22 from qemu.qmp import QEMUMonitorProtocol
24 fuse.fuse_python_api = (0, 2)
26 class QOMFS(Operations):
27 def __init__(self, qmp):
33 def get_ino(self, path):
34 if path in self.ino_map:
35 return self.ino_map[path]
36 self.ino_map[path] = self.ino_count
38 return self.ino_map[path]
40 def is_object(self, path):
42 items = self.qmp.command('qom-list', path=path)
47 def is_property(self, path):
48 path, prop = path.rsplit('/', 1)
52 for item in self.qmp.command('qom-list', path=path):
53 if item['name'] == prop:
59 def is_link(self, path):
60 path, prop = path.rsplit('/', 1)
64 for item in self.qmp.command('qom-list', path=path):
65 if item['name'] == prop:
66 if item['type'].startswith('link<'):
73 def read(self, path, length, offset, fh):
74 if not self.is_property(path):
77 path, prop = path.rsplit('/', 1)
81 data = self.qmp.command('qom-get', path=path, property=prop)
82 data += '\n' # make values shell friendly
84 raise FuseOSError(EPERM)
86 if offset > len(data):
89 return bytes(data[offset:][:length], encoding='utf-8')
91 def readlink(self, path):
92 if not self.is_link(path):
94 path, prop = path.rsplit('/', 1)
95 prefix = '/'.join(['..'] * (len(path.split('/')) - 1))
96 return prefix + str(self.qmp.command('qom-get', path=path,
99 def getattr(self, path, fh=None):
100 if self.is_link(path):
101 value = { 'st_mode': 0o755 | stat.S_IFLNK,
102 'st_ino': self.get_ino(path),
111 elif self.is_object(path):
112 value = { 'st_mode': 0o755 | stat.S_IFDIR,
113 'st_ino': self.get_ino(path),
122 elif self.is_property(path):
123 value = { 'st_mode': 0o644 | stat.S_IFREG,
124 'st_ino': self.get_ino(path),
134 raise FuseOSError(ENOENT)
137 def readdir(self, path, fh):
140 for item in self.qmp.command('qom-list', path=path):
141 yield str(item['name'])
143 if __name__ == '__main__':
146 fuse = FUSE(QOMFS(QEMUMonitorProtocol(os.environ['QMP_SOCKET'])),
147 sys.argv[1], foreground=True)