]>
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 | ||
14cd493f | 11 | |
ee227ea1 | 12 | class Disk(): |
14cd493f | 13 | def __init__(self, disk): |
1ed8ca9f JS |
14 | self.log = logger.getChild(os.path.basename(disk)) |
15 | self.log.debug("Initiated a disk class for {}".format(disk)) | |
14cd493f JS |
16 | self.con = guestfs.GuestFS(python_return_dict=True) |
17 | self.con.add_drive_opts(disk, format="qcow2") | |
18 | ||
19 | def mount(self, uuid, path): | |
cfd70f97 | 20 | self.log.info("Trying to mount the partion with uuid: {} under {}".format(uuid, path)) |
14cd493f JS |
21 | self.con.launch() |
22 | part = self.con.findfs_uuid(uuid) | |
23 | self.con.mount(part, path) | |
24 | ||
25 | def copy_in(self, fr, to): | |
cfd70f97 | 26 | self.log.info("Going to copy some files into the image.") |
14cd493f JS |
27 | tmp = tempfile.mkstemp() |
28 | tmp = tmp[1] + ".tar" | |
29 | with tarfile.open(tmp, "w") as tar: | |
30 | for file in fr: | |
1ed8ca9f | 31 | self.log.debug("Adding {} to be copied into the image".format(file)) |
14cd493f | 32 | tar.add(file, arcname=os.path.basename(file)) |
1ed8ca9f | 33 | |
cfd70f97 | 34 | self.log.info("Going to copy the files into the image") |
14cd493f JS |
35 | self.con.tar_in_opts(tmp, to) |
36 | ||
37 | def umount(self, path): | |
cfd70f97 | 38 | self.log.info("Unmounting the image") |
14cd493f JS |
39 | self.con.umount_opts(path) |
40 | ||
41 | def close(self): | |
cfd70f97 | 42 | self.log.info("Flush the image and closing the connection") |
14cd493f JS |
43 | self.con.shutdown() |
44 | self.con.close() | |
45 | ||
46 | # test = disk("/var/lib/libvirt/images/alice.qcow2") | |
47 | # test.mount("45598e92-3487-4a1b-961d-79aa3dd42a7d", "/") | |
48 | # test.copy_in("/home/jonatan/nitsi/libguestfs-test", "/root/") | |
49 | # test.umount("/") | |
6632e137 | 50 | # test.close() |