From c37147a07f9830cc6c6084142cbdae53741a2913 Mon Sep 17 00:00:00 2001 From: Mürsel Türk Date: Wed, 14 Sep 2022 23:12:36 +0200 Subject: Add support for alpine linux --- inspect.py | 6 +++++- tools/inspect_apps.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/inspect.py b/inspect.py index 8b41fbf..aba1731 100755 --- a/inspect.py +++ b/inspect.py @@ -7,6 +7,7 @@ import sys from tools import libvslvm, lklfuse, nbdfuse, subfiles, unmount, rmdir from tools.inspect_apps import ( + list_applications_apk, list_applications_dpkg, list_applications_pacman, list_applications_rpm, @@ -18,6 +19,7 @@ from tools.pyparted import list_partitions fmt = "{asctime}, {name}:{lineno}:{funcName}(), {levelname}, {message}" logging.basicConfig(level=logging.DEBUG, format=fmt, style="{") +APK = re.compile(r"^(Alpine).*$") DEB = re.compile(r"^(Debian|Ubuntu|Linux\sMint|LMDE).*$") PACMAN = re.compile(r"^(Arch|Manjaro).*$") RPM = re.compile(r"^(CentOS|AlmaLinux|Scientific|Rocky|Oracle|openSUSE|Fedora).*$") # noqa @@ -66,7 +68,9 @@ def main(vmdk_path): os_name = os_info.get("name", "") for fspath, _ in fs_mps: - if DEB.match(os_name): + if APK.match(os_name): + apps = list_applications_apk(fspath) + elif DEB.match(os_name): apps = list_applications_dpkg(fspath) elif PACMAN.match(os_name): apps = list_applications_pacman(fspath) diff --git a/tools/inspect_apps.py b/tools/inspect_apps.py index 7031a0a..613649b 100644 --- a/tools/inspect_apps.py +++ b/tools/inspect_apps.py @@ -6,6 +6,7 @@ import tempfile from . import log, subdirs __all__ = [ + "list_applications_apk", "list_applications_dpkg", "list_applications_pacman", "list_applications_rpm", @@ -33,6 +34,60 @@ except ModuleNotFoundError: raise +@log +def list_applications_apk(path): + """Find all packages installed on the linux distribution Alpine Linux. + + See also: + https://wiki.alpinelinux.org/wiki/Alpine_Package_Keeper + + Args: + path (str): Path to the mounted filesystem. + + Returns: + List of packages. For example: + [{'name': 'musl', 'version': '1.2.3-r0'}, ...] + """ + apk_db = None + + location = "lib/apk/db/installed" + + db = os.path.join(path, location) + if os.path.exists(db): + apk_db = db + + if apk_db is None: + for dir in subdirs(path): + new_path = os.path.join(path, dir) + db = os.path.join(new_path, location) + if os.path.exists(db): + apk_db = db + break + + if apk_db is None: + L.debug("apk database not found") + return [] + + pkgs = [] + with open(apk_db) as f: + name = version = "" + for line in f: + line = line.strip() + if not line: + if name and version: + pkgs.append({ + "name": name, + "version": version + }) + name = version = "" + elif line.startswith("P:"): + name = line[2:] + elif line.startswith("V:"): + version = line[2:] + + return pkgs + + @log def list_applications_dpkg(path): """Find all packages installed on a debian-based linux distribution. -- cgit v1.2.3-55-g7522