2020-01-30 19:32:25 +03:00
|
|
|
#!/usr/bin/env python3
|
2012-04-19 02:37:04 +04:00
|
|
|
##
|
|
|
|
# QEMU Object Model test tools
|
|
|
|
#
|
|
|
|
# Copyright IBM, Corp. 2012
|
2020-07-23 17:27:37 +03:00
|
|
|
# Copyright (C) 2020 Red Hat, Inc.
|
2012-04-19 02:37:04 +04:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Anthony Liguori <aliguori@us.ibm.com>
|
2020-07-23 17:27:37 +03:00
|
|
|
# Markus Armbruster <armbru@redhat.com>
|
2012-04-19 02:37:04 +04:00
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2 or later. See
|
|
|
|
# the COPYING file in the top-level directory.
|
|
|
|
##
|
|
|
|
|
|
|
|
import fuse, stat
|
2020-07-23 17:27:37 +03:00
|
|
|
from fuse import FUSE, FuseOSError, Operations
|
2020-07-23 17:27:36 +03:00
|
|
|
import os, posix, sys
|
2012-04-19 02:37:04 +04:00
|
|
|
from errno import *
|
2020-05-29 01:21:26 +03:00
|
|
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
|
|
|
|
from qemu.qmp import QEMUMonitorProtocol
|
2012-04-19 02:37:04 +04:00
|
|
|
|
|
|
|
fuse.fuse_python_api = (0, 2)
|
|
|
|
|
2020-07-23 17:27:37 +03:00
|
|
|
class QOMFS(Operations):
|
|
|
|
def __init__(self, qmp):
|
2012-04-19 02:37:04 +04:00
|
|
|
self.qmp = qmp
|
|
|
|
self.qmp.connect()
|
|
|
|
self.ino_map = {}
|
|
|
|
self.ino_count = 1
|
|
|
|
|
|
|
|
def get_ino(self, path):
|
2018-06-08 15:29:46 +03:00
|
|
|
if path in self.ino_map:
|
2012-04-19 02:37:04 +04:00
|
|
|
return self.ino_map[path]
|
|
|
|
self.ino_map[path] = self.ino_count
|
|
|
|
self.ino_count += 1
|
|
|
|
return self.ino_map[path]
|
|
|
|
|
|
|
|
def is_object(self, path):
|
|
|
|
try:
|
|
|
|
items = self.qmp.command('qom-list', path=path)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_property(self, path):
|
2020-07-23 17:27:38 +03:00
|
|
|
path, prop = path.rsplit('/', 1)
|
|
|
|
if path == '':
|
|
|
|
path = '/'
|
2012-04-19 02:37:04 +04:00
|
|
|
try:
|
|
|
|
for item in self.qmp.command('qom-list', path=path):
|
|
|
|
if item['name'] == prop:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_link(self, path):
|
2020-07-23 17:27:38 +03:00
|
|
|
path, prop = path.rsplit('/', 1)
|
|
|
|
if path == '':
|
|
|
|
path = '/'
|
2012-04-19 02:37:04 +04:00
|
|
|
try:
|
|
|
|
for item in self.qmp.command('qom-list', path=path):
|
|
|
|
if item['name'] == prop:
|
|
|
|
if item['type'].startswith('link<'):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2020-07-23 17:27:37 +03:00
|
|
|
def read(self, path, length, offset, fh):
|
2012-04-19 02:37:04 +04:00
|
|
|
if not self.is_property(path):
|
|
|
|
return -ENOENT
|
|
|
|
|
|
|
|
path, prop = path.rsplit('/', 1)
|
2020-07-23 17:27:38 +03:00
|
|
|
if path == '':
|
|
|
|
path = '/'
|
2012-04-19 02:37:04 +04:00
|
|
|
try:
|
2020-07-23 17:27:37 +03:00
|
|
|
data = self.qmp.command('qom-get', path=path, property=prop)
|
2012-04-19 02:37:04 +04:00
|
|
|
data += '\n' # make values shell friendly
|
|
|
|
except:
|
2020-07-23 17:27:37 +03:00
|
|
|
raise FuseOSError(EPERM)
|
2012-04-19 02:37:04 +04:00
|
|
|
|
|
|
|
if offset > len(data):
|
|
|
|
return ''
|
|
|
|
|
2020-07-23 17:27:37 +03:00
|
|
|
return bytes(data[offset:][:length], encoding='utf-8')
|
2012-04-19 02:37:04 +04:00
|
|
|
|
|
|
|
def readlink(self, path):
|
|
|
|
if not self.is_link(path):
|
|
|
|
return False
|
|
|
|
path, prop = path.rsplit('/', 1)
|
|
|
|
prefix = '/'.join(['..'] * (len(path.split('/')) - 1))
|
|
|
|
return prefix + str(self.qmp.command('qom-get', path=path,
|
|
|
|
property=prop))
|
|
|
|
|
2020-07-23 17:27:37 +03:00
|
|
|
def getattr(self, path, fh=None):
|
2012-04-19 02:37:04 +04:00
|
|
|
if self.is_link(path):
|
2020-07-23 17:27:37 +03:00
|
|
|
value = { 'st_mode': 0o755 | stat.S_IFLNK,
|
|
|
|
'st_ino': self.get_ino(path),
|
|
|
|
'st_dev': 0,
|
|
|
|
'st_nlink': 2,
|
|
|
|
'st_uid': 1000,
|
|
|
|
'st_gid': 1000,
|
|
|
|
'st_size': 4096,
|
|
|
|
'st_atime': 0,
|
|
|
|
'st_mtime': 0,
|
|
|
|
'st_ctime': 0 }
|
2012-04-19 02:37:04 +04:00
|
|
|
elif self.is_object(path):
|
2020-07-23 17:27:37 +03:00
|
|
|
value = { 'st_mode': 0o755 | stat.S_IFDIR,
|
|
|
|
'st_ino': self.get_ino(path),
|
|
|
|
'st_dev': 0,
|
|
|
|
'st_nlink': 2,
|
|
|
|
'st_uid': 1000,
|
|
|
|
'st_gid': 1000,
|
|
|
|
'st_size': 4096,
|
|
|
|
'st_atime': 0,
|
|
|
|
'st_mtime': 0,
|
|
|
|
'st_ctime': 0 }
|
2012-04-19 02:37:04 +04:00
|
|
|
elif self.is_property(path):
|
2020-07-23 17:27:37 +03:00
|
|
|
value = { 'st_mode': 0o644 | stat.S_IFREG,
|
|
|
|
'st_ino': self.get_ino(path),
|
|
|
|
'st_dev': 0,
|
|
|
|
'st_nlink': 1,
|
|
|
|
'st_uid': 1000,
|
|
|
|
'st_gid': 1000,
|
|
|
|
'st_size': 4096,
|
|
|
|
'st_atime': 0,
|
|
|
|
'st_mtime': 0,
|
|
|
|
'st_ctime': 0 }
|
2012-04-19 02:37:04 +04:00
|
|
|
else:
|
2020-07-23 17:27:37 +03:00
|
|
|
raise FuseOSError(ENOENT)
|
2012-04-19 02:37:04 +04:00
|
|
|
return value
|
|
|
|
|
2020-07-23 17:27:37 +03:00
|
|
|
def readdir(self, path, fh):
|
|
|
|
yield '.'
|
|
|
|
yield '..'
|
2012-04-19 02:37:04 +04:00
|
|
|
for item in self.qmp.command('qom-list', path=path):
|
2020-07-23 17:27:37 +03:00
|
|
|
yield str(item['name'])
|
2012-04-19 02:37:04 +04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-07-23 17:27:36 +03:00
|
|
|
import os
|
2012-04-19 02:37:04 +04:00
|
|
|
|
2020-07-23 17:27:37 +03:00
|
|
|
fuse = FUSE(QOMFS(QEMUMonitorProtocol(os.environ['QMP_SOCKET'])),
|
|
|
|
sys.argv[1], foreground=True)
|