Debian Codegraph @

examples/domstart.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """
3 Check that the domain described by DOMAIN.XML is running.
4 If the domain is not running, create it.
5 """
6
7 import libvirt
8 import libxml2
9 from argparse import ArgumentParser
10
11
12 # Parse the XML description of domU from FNAME
13 # and return a tuple (name, xmldesc) where NAME
14 # is the name of the domain, and xmldesc is the contetn of FNAME
15 def read_domain(fname: str) -> tuple[str, str]:
16 fp = open(fname, "r")
17 xmldesc = fp.read()
18 fp.close()
19
20 doc = libxml2.parseDoc(xmldesc)
21 name = doc.xpathNewContext().xpathEval("/domain/name")[0].content
22 return (name, xmldesc)
23
24
25 parser = ArgumentParser(description=__doc__)
26 parser.add_argument("file", metavar="DOMAIN.XML", help="XML configuration of the domain in libvirt's XML format")
27 args = parser.parse_args()
28
29 name, xmldesc = read_domain(args.file)
30
31 try:
32 conn = libvirt.open(None)
33 except libvirt.libvirtError:
34 print('Failed to open connection to the hypervisor')
35 exit(1)
36
37 try:
38 dom = conn.lookupByName(name)
39 except libvirt.libvirtError:
40 print("Starting domain %s ... " % name)
41 dom = conn.createLinux(xmldesc, 0)
42 if dom is None:
43 print("failed")
44 exit(1)
45 else:
46 print("done")