Move recipe class into an own file
[nitsi.git] / disk.py
1 #!/usr/bin/python3
2
3 import guestfs
4
5 import tempfile
6 import tarfile
7 import os
8
9
10 class disk():
11     def __init__(self, disk):
12         self.con = guestfs.GuestFS(python_return_dict=True)
13         self.con.add_drive_opts(disk, format="qcow2")
14
15     def mount(self, uuid, path):
16         self.con.launch()
17         part = self.con.findfs_uuid(uuid)
18         self.con.mount(part, path)
19
20     def copy_in(self, fr, to):
21         tmp = tempfile.mkstemp()
22         tmp = tmp[1] + ".tar"
23         with tarfile.open(tmp, "w") as tar:
24             for file in fr:
25                 tar.add(file, arcname=os.path.basename(file))
26         self.con.tar_in_opts(tmp, to)
27
28     def umount(self, path):
29         self.con.umount_opts(path)
30
31     def close(self):
32         self.con.shutdown()
33         self.con.close()
34
35 # test  = disk("/var/lib/libvirt/images/alice.qcow2")
36 # test.mount("45598e92-3487-4a1b-961d-79aa3dd42a7d", "/")
37 # test.copy_in("/home/jonatan/nitsi/libguestfs-test", "/root/")
38 # test.umount("/")
39 # test.close()