]> git.ipfire.org Git - nitsi.git/blob - src/nitsi/disk.py
Add new DiskException class
[nitsi.git] / src / nitsi / disk.py
1 #!/usr/bin/python3
2
3 import guestfs
4 import logging
5 import os
6 import tarfile
7 import tempfile
8
9 logger = logging.getLogger("nitsi.disk")
10
11 class DiskExeption(Exception):
12 def __init__(self, message):
13 self.message = message
14
15 class Disk():
16 def __init__(self, disk):
17 self.log = logger.getChild(os.path.basename(disk))
18 self.log.debug("Initiated a disk class for {}".format(disk))
19 self.con = guestfs.GuestFS(python_return_dict=True)
20 self.con.add_drive_opts(disk, format="qcow2")
21
22 def mount(self, uuid, path):
23 self.log.info("Trying to mount the partion with uuid: {} under {}".format(uuid, path))
24 self.con.launch()
25 part = self.con.findfs_uuid(uuid)
26 self.con.mount(part, path)
27
28 def copy_in(self, fr, to):
29 self.log.info("Going to copy some files into the image.")
30 tmp = tempfile.mkstemp()
31 tmp = tmp[1] + ".tar"
32 self.log.debug("Path of tarfile is: {}".format(tmp))
33 with tarfile.open(tmp, "w") as tar:
34 for file in fr:
35 self.log.debug("Adding {} to be copied into the image".format(file))
36 tar.add(file, arcname=os.path.basename(file))
37
38 self.log.info("Going to copy the files into the image")
39 self.con.tar_in_opts(tmp, to)
40 self.log.debug(self.con.ls(to))
41
42 def umount(self, path):
43 self.log.info("Unmounting the image")
44 self.con.umount_opts(path)
45
46 def close(self):
47 self.log.info("Flush the image and closing the connection")
48 self.con.shutdown()
49 self.con.close()
50
51 # test = Disk("/var/lib/libvirt/images/ipfire-bob.qcow2")
52 # test.mount("1efb5389-0949-46bb-b688-5246acba9f6d", "/")
53 # test.copy_in("/home/jonatan/nitsi/libguestfs-test", "/root/")
54 # test.umount("/")
55 # test.close()