3 # Render Qemu Block Graph
5 # Copyright (c) 2018 Virtuozzo International GmbH. All rights reserved.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
25 from graphviz
import Digraph
27 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), '..', 'python'))
28 from qemu
.aqmp
import QMPError
29 from qemu
.aqmp
.legacy
import QEMUMonitorProtocol
33 s
= 'w' if 'write' in arr
else '_'
34 s
+= 'r' if 'consistent-read' in arr
else '_'
35 s
+= 'u' if 'write-unchanged' in arr
else '_'
36 s
+= 's' if 'resize' in arr
else '_'
40 def render_block_graph(qmp
, filename
, format
='png'):
42 Render graph in text (dot) representation into "@filename" and
43 representation in @format into "@filename.@format"
46 bds_nodes
= qmp
.command('query-named-block-nodes')
47 bds_nodes
= {n
['node-name']: n
for n
in bds_nodes
}
49 job_nodes
= qmp
.command('query-block-jobs')
50 job_nodes
= {n
['device']: n
for n
in job_nodes
}
52 block_graph
= qmp
.command('x-debug-query-block-graph')
54 graph
= Digraph(comment
='Block Nodes Graph')
56 graph
.node('permission symbols:\l'
58 ' r - consistent-Read\l'
59 ' u - write - Unchanged\l'
62 'edge label scheme:\l'
65 ' <shared_perm>\l', shape
='none')
67 for n
in block_graph
['nodes']:
68 if n
['type'] == 'block-driver':
69 info
= bds_nodes
[n
['name']]
70 label
= n
['name'] + ' [' + info
['drv'] + ']'
71 if info
['drv'] == 'file':
72 label
+= '\n' + os
.path
.basename(info
['file'])
74 elif n
['type'] == 'block-job':
75 info
= job_nodes
[n
['name']]
76 label
= info
['type'] + ' job (' + n
['name'] + ')'
79 assert n
['type'] == 'block-backend'
80 label
= n
['name'] if n
['name'] else 'unnamed blk'
83 graph
.node(str(n
['id']), label
, shape
=shape
)
85 for e
in block_graph
['edges']:
86 label
= '%s\l%s\l%s\l' % (e
['name'], perm(e
['perm']),
87 perm(e
['shared-perm']))
88 graph
.edge(str(e
['parent']), str(e
['child']), label
=label
)
90 graph
.render(filename
)
94 def __init__(self
, name
):
97 def command(self
, cmd
):
98 # only supports qmp commands without parameters
100 ar
= ['virsh', 'qemu-monitor-command', self
.name
, json
.dumps(m
)]
102 reply
= json
.loads(subprocess
.check_output(ar
))
105 raise QMPError(reply
)
107 return reply
['return']
110 if __name__
== '__main__':
114 if os
.path
.exists(obj
):
116 qmp
= QEMUMonitorProtocol(obj
)
119 # assume libvirt guest name
120 qmp
= LibvirtGuest(obj
)
122 render_block_graph(qmp
, out
)