]>
git.ipfire.org Git - nitsi.git/blob - machine.py
3 import xml
.etree
.ElementTree
as ET
7 from serial_connection
import serial_connection
9 from test
import libvirt_con
16 def __init__(self
, vm_xml_file
, snapshot_xml_file
, image
, root_uid
, username
, password
):
18 self
.con
= libvirt_con("qemu:///system")
20 with
open(vm_xml_file
) as fobj
:
21 self
.vm_xml
= fobj
.read()
22 except FileNotFoundError
as error
:
23 self
.log
.error("No such file: {}".format(vm_xml_file
))
26 with
open(snapshot_xml_file
) as fobj
:
27 self
.snapshot_xml
= fobj
.read()
28 except FileNotFoundError
as error
:
29 self
.log
.error("No such file: {}".format(snapshot_xml_file
))
33 if not os
.path
.isfile(self
.image
):
34 self
.log
.error("No such file: {}".format(self
.image
))
36 self
.root_uid
= root_uid
37 self
.disk
= disk(image
)
39 self
.username
= username
40 self
.password
= password
43 self
.dom
= self
.con
.con
.defineXML(self
.vm_xml
)
45 self
.log
.error("Could not define VM")
49 if self
.dom
.create() < 0:
50 self
.log
.error("Could not start VM")
55 if self
.dom
.shutdown() < 0:
56 self
.log
.error("Could not shutdown VM")
59 self
.log
.error("Domain is not running")
64 def create_snapshot(self
):
66 self
.snapshot
= self
.dom
.snapshotCreateXML(self
.snapshot_xml
)
69 self
.log
.error("Could not create snapshot")
72 def revert_snapshot(self
):
73 self
.dom
.revertToSnapshot(self
.snapshot
)
74 self
.snapshot
.delete()
78 state
, reason
= self
.dom
.state()
80 if state
== libvirt
.VIR_DOMAIN_RUNNING
:
85 def get_serial_device(self
):
87 if not self
.is_running():
90 xml_root
= ET
.fromstring(self
.dom
.XMLDesc(0))
92 elem
= xml_root
.find("./devices/serial/source")
93 return elem
.get("path")
95 def check_is_booted_up(self
):
96 serial_con
= serial_connection(self
.get_serial_device())
98 serial_con
.write("\n")
99 # This will block till the domain is booted up
106 self
.serial_con
= serial_connection(self
.get_serial_device(), username
=self
.username
)
107 self
.serial_con
.login(self
.password
)
108 except BaseException
as e
:
109 self
.log
.error("Could not connect to the domain via serial console")
112 return self
.serial_con
.command(cmd
)
114 def copy_in(self
, fr
, to
):
116 self
.disk
.mount(self
.root_uid
, "/")
117 self
.disk
.copy_in(fr
, to
)
118 except BaseException
as e
:
121 self
.disk
.umount("/")