]>
Commit | Line | Data |
---|---|---|
14cd493f JS |
1 | #!/usr/bin/python3 |
2 | ||
3 | import guestfs | |
1ed8ca9f | 4 | import logging |
6632e137 JS |
5 | import os |
6 | import tarfile | |
7 | import tempfile | |
1ed8ca9f JS |
8 | |
9 | logger = logging.getLogger("nitsi.disk") | |
10 | ||
0285784e JS |
11 | class DiskExeption(Exception): |
12 | def __init__(self, message): | |
13 | self.message = message | |
14cd493f | 14 | |
ee227ea1 | 15 | class Disk(): |
14cd493f | 16 | def __init__(self, disk): |
1ed8ca9f JS |
17 | self.log = logger.getChild(os.path.basename(disk)) |
18 | self.log.debug("Initiated a disk class for {}".format(disk)) | |
14cd493f JS |
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): | |
cfd70f97 | 23 | self.log.info("Trying to mount the partion with uuid: {} under {}".format(uuid, path)) |
14cd493f JS |
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): | |
cfd70f97 | 29 | self.log.info("Going to copy some files into the image.") |
14cd493f JS |
30 | tmp = tempfile.mkstemp() |
31 | tmp = tmp[1] + ".tar" | |
b7e02750 | 32 | self.log.debug("Path of tarfile is: {}".format(tmp)) |
14cd493f JS |
33 | with tarfile.open(tmp, "w") as tar: |
34 | for file in fr: | |
1ed8ca9f | 35 | self.log.debug("Adding {} to be copied into the image".format(file)) |
14cd493f | 36 | tar.add(file, arcname=os.path.basename(file)) |
1ed8ca9f | 37 | |
cfd70f97 | 38 | self.log.info("Going to copy the files into the image") |
14cd493f | 39 | self.con.tar_in_opts(tmp, to) |
b7e02750 | 40 | self.log.debug(self.con.ls(to)) |
14cd493f JS |
41 | |
42 | def umount(self, path): | |
cfd70f97 | 43 | self.log.info("Unmounting the image") |
14cd493f JS |
44 | self.con.umount_opts(path) |
45 | ||
46 | def close(self): | |
cfd70f97 | 47 | self.log.info("Flush the image and closing the connection") |
14cd493f JS |
48 | self.con.shutdown() |
49 | self.con.close() | |
50 | ||
b7e02750 JS |
51 | # test = Disk("/var/lib/libvirt/images/ipfire-bob.qcow2") |
52 | # test.mount("1efb5389-0949-46bb-b688-5246acba9f6d", "/") | |
14cd493f JS |
53 | # test.copy_in("/home/jonatan/nitsi/libguestfs-test", "/root/") |
54 | # test.umount("/") | |
6632e137 | 55 | # test.close() |