Add configure.ac and Makefile.am and make them work
[nitsi.git] / src / nitsi / disk.py
CommitLineData
14cd493f
JS
1#!/usr/bin/python3
2
3import guestfs
4
5import tempfile
6import tarfile
7import os
8
1ed8ca9f
JS
9import logging
10
11logger = logging.getLogger("nitsi.disk")
12
14cd493f
JS
13
14class disk():
15 def __init__(self, disk):
1ed8ca9f
JS
16 self.log = logger.getChild(os.path.basename(disk))
17 self.log.debug("Initiated a disk class for {}".format(disk))
14cd493f
JS
18 self.con = guestfs.GuestFS(python_return_dict=True)
19 self.con.add_drive_opts(disk, format="qcow2")
20
21 def mount(self, uuid, path):
1ed8ca9f 22 self.log.debug("Trying to mount the partion with uuid: {} under {}".format(uuid, path))
14cd493f
JS
23 self.con.launch()
24 part = self.con.findfs_uuid(uuid)
25 self.con.mount(part, path)
26
27 def copy_in(self, fr, to):
1ed8ca9f 28 self.log.debug("Going to copy some files into the image.")
14cd493f
JS
29 tmp = tempfile.mkstemp()
30 tmp = tmp[1] + ".tar"
31 with tarfile.open(tmp, "w") as tar:
32 for file in fr:
1ed8ca9f 33 self.log.debug("Adding {} to be copied into the image".format(file))
14cd493f 34 tar.add(file, arcname=os.path.basename(file))
1ed8ca9f
JS
35
36 self.log.debug("Going to copy the files into the image")
14cd493f
JS
37 self.con.tar_in_opts(tmp, to)
38
39 def umount(self, path):
1ed8ca9f 40 self.log.debug("Unmounting the image")
14cd493f
JS
41 self.con.umount_opts(path)
42
43 def close(self):
1ed8ca9f 44 self.log.debug("Flush the image and closing the connection")
14cd493f
JS
45 self.con.shutdown()
46 self.con.close()
47
48# test = disk("/var/lib/libvirt/images/alice.qcow2")
49# test.mount("45598e92-3487-4a1b-961d-79aa3dd42a7d", "/")
50# test.copy_in("/home/jonatan/nitsi/libguestfs-test", "/root/")
51# test.umount("/")
52# test.close()