From 2ff5fbe692bd474cb7a4e1b8a36e828b457c43b5 Mon Sep 17 00:00:00 2001 From: Mürsel Türk Date: Thu, 25 Aug 2022 22:41:17 +0200 Subject: Add wrappers for the tools libvmdk and libvslvm --- tools/libvmdk.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/libvslvm.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 tools/libvmdk.py create mode 100644 tools/libvslvm.py diff --git a/tools/libvmdk.py b/tools/libvmdk.py new file mode 100644 index 0000000..ca67161 --- /dev/null +++ b/tools/libvmdk.py @@ -0,0 +1,54 @@ +import logging +import os +import tempfile + +from subprocess import run +from . import log, rmdir + +__all__ = ["mount"] + +L = logging.getLogger(__name__) + + +@log +def mount(path): + """Mount a VMware Virtual Machine Disk (VMDK) file as a RAW image in the + local filesystem with read-only support using `libvmdk`. + + See also: + https://github.com/libyal/libvmdk/wiki/Building + https://github.com/libyal/libvmdk/wiki/Mounting + + Make sure you have downloaded the latest source distribution package from + https://github.com/libyal/libvmdk/releases, e.g. + libvmdk-alpha-20210807.tar.gz and built it as follows: + $ sudo apt install build-essential libfuse-dev + $ tar xfv libvmdk-alpha-20210807.tar.gz + $ cd libvmdk-20210807/ + $ ./configure + $ make + $ sudo make install + $ sudo ldconfig + + Args: + path (str): Path to the VMDK file. + + Returns: + Path to the directory containing a single virtual file named `vmdk1`. + """ + mp = tempfile.mkdtemp() + cmd = ["vmdkmount", path, mp] + try: + p = run(cmd, capture_output=True, check=False, text=True) + except Exception as e: + L.error("failed to execute command %s: %r", cmd, e) + rmdir(mp) + return "" + + out, err = p.stdout.strip(), p.stderr.strip() + if p.returncode or not os.path.ismount(mp): + L.error("retcode: %d, stdout: %s, stderr: %s", p.returncode, out, err) + rmdir(mp) + return "" + + return mp diff --git a/tools/libvslvm.py b/tools/libvslvm.py new file mode 100644 index 0000000..ec1fdbb --- /dev/null +++ b/tools/libvslvm.py @@ -0,0 +1,56 @@ +import logging +import os +import tempfile + +from subprocess import run +from . import log, rmdir + +__all__ = ["mount"] + +L = logging.getLogger(__name__) + + +@log +def mount(path, offset): + """Mount a Linux Logical Volume Manager (LVM) volume system using + `libvslvm`. + + See also: + https://github.com/libyal/libvslvm/wiki/Mounting + https://github.com/libyal/libvslvm/wiki/Building + + Make sure you have downloaded the latest source distribution package from + https://github.com/libyal/libvslvm/releases, e.g. + libvslvm-experimental-20210807.tar.gz and built it as follows: + $ sudo apt install build-essential libfuse-dev + $ tar xfv libvslvm-experimental-20210807.tar.gz + $ cd libvslvm-20210807/ + $ ./configure + $ make + $ sudo make install + $ sudo ldconfig + + Args: + path (str): Path to the RAW image file containing LVM volume system. + offset (str|int): Value in bytes where the LVM volume system starts. + + Returns: + Path to the directory containing volumes as a virtual file named + `lvm1`, `lvm2`, etc. + """ + mp = tempfile.mkdtemp() + cmd = ["vslvmmount", "-o", str(offset), path, mp] + try: + p = run(cmd, capture_output=True, check=False, text=True) + except Exception as e: + L.error("failed to execute command %s: %r", cmd, e) + rmdir(mp) + return "" + + out, err = p.stdout.strip(), p.stderr.strip() + if p.returncode or not os.path.ismount(mp): + L.error("retcode: %d, stdout: %s, stderr: %s", p.returncode, out, err) + rmdir(mp) + return "" + + return mp -- cgit v1.2.3-55-g7522