]>
git.ipfire.org Git - nitsi.git/blob - machine.py
3 import xml
.etree
.ElementTree
as ET
7 from serial_connection
import serial_connection
14 def __init__(self
, libvirt_con
, vm_xml_file
, snapshot_xml_file
, image
, root_uid
, username
, password
):
16 self
.con
= libvirt_con
18 with
open(vm_xml_file
) as fobj
:
19 self
.vm_xml
= fobj
.read()
20 except FileNotFoundError
as error
:
21 self
.log
.error("No such file: {}".format(vm_xml_file
))
24 with
open(snapshot_xml_file
) as fobj
:
25 self
.snapshot_xml
= fobj
.read()
26 except FileNotFoundError
as error
:
27 self
.log
.error("No such file: {}".format(snapshot_xml_file
))
31 if not os
.path
.isfile(self
.image
):
32 self
.log
.error("No such file: {}".format(self
.image
))
34 self
.root_uid
= root_uid
35 self
.disk
= disk(image
)
37 self
.username
= username
38 self
.password
= password
41 self
.dom
= self
.con
.defineXML(self
.vm_xml
)
43 self
.log
.error("Could not define VM")
47 if self
.dom
.create() < 0:
48 self
.log
.error("Could not start VM")
53 if self
.dom
.shutdown() < 0:
54 self
.log
.error("Could not shutdown VM")
57 self
.log
.error("Domain is not running")
62 def create_snapshot(self
):
64 self
.snapshot
= self
.dom
.snapshotCreateXML(self
.snapshot_xml
)
67 self
.log
.error("Could not create snapshot")
70 def revert_snapshot(self
):
71 self
.dom
.revertToSnapshot(self
.snapshot
)
72 self
.snapshot
.delete()
76 state
, reason
= self
.dom
.state()
78 if state
== libvirt
.VIR_DOMAIN_RUNNING
:
83 def get_serial_device(self
):
85 if not self
.is_running():
88 xml_root
= ET
.fromstring(self
.dom
.XMLDesc(0))
90 elem
= xml_root
.find("./devices/serial/source")
91 return elem
.get("path")
93 def check_is_booted_up(self
):
94 serial_con
= serial_connection(self
.get_serial_device())
96 serial_con
.write("\n")
97 # This will block till the domain is booted up
104 self
.serial_con
= serial_connection(self
.get_serial_device(), username
=self
.username
)
105 self
.serial_con
.login(self
.password
)
106 except BaseException
as e
:
107 self
.log
.error("Could not connect to the domain via serial console")
110 return self
.serial_con
.command(cmd
)
112 def copy_in(self
, fr
, to
):
114 self
.disk
.mount(self
.root_uid
, "/")
115 self
.disk
.copy_in(fr
, to
)
116 except BaseException
as e
:
119 self
.disk
.umount("/")