Parse os-release in chroot

To parse the right os-release file case of absolute symlinks
This commit is contained in:
Jip-Hop 2024-06-24 10:17:33 +02:00
parent 49d65c1073
commit 51884e215c
1 changed files with 26 additions and 11 deletions

View File

@ -125,6 +125,8 @@ SCRIPT_PATH = os.path.realpath(__file__)
SCRIPT_NAME = os.path.basename(SCRIPT_PATH) SCRIPT_NAME = os.path.basename(SCRIPT_PATH)
SCRIPT_DIR_PATH = os.path.dirname(SCRIPT_PATH) SCRIPT_DIR_PATH = os.path.dirname(SCRIPT_PATH)
COMMAND_NAME = os.path.basename(__file__) COMMAND_NAME = os.path.basename(__file__)
INITIAL_ROOT = os.open("/", os.O_PATH)
CWD_BEFORE_CHROOT = None
SHORTNAME = "jlmkr" SHORTNAME = "jlmkr"
# Only set a color if we have an interactive tty # Only set a color if we have an interactive tty
@ -294,6 +296,20 @@ def fail(*args, **kwargs):
sys.exit(1) sys.exit(1)
def enter_chroot(new_root):
global CWD_BEFORE_CHROOT
CWD_BEFORE_CHROOT = os.path.abspath(os.getcwd())
os.chdir(new_root)
os.chroot(".")
# https://stackoverflow.com/a/61533559
def exit_chroot():
os.chdir(INITIAL_ROOT)
os.chroot(".")
os.chdir(CWD_BEFORE_CHROOT)
def get_jail_path(jail_name): def get_jail_path(jail_name):
return os.path.join(JAILS_DIR_PATH, jail_name) return os.path.join(JAILS_DIR_PATH, jail_name)
@ -1627,16 +1643,21 @@ def get_all_jail_names():
return jail_names return jail_names
def parse_os_release(candidates): def parse_os_release(new_rootfs):
for candidate in candidates: enter_chroot(new_rootfs)
result = {}
for candidate in ["/etc/os-release", "/usr/lib/os-release"]:
try: try:
with open(candidate, encoding="utf-8") as f: with open(candidate, encoding="utf-8") as f:
# TODO: can I create a solution which not depends on the internal _parse_os_release method? # TODO: can I create a solution which not depends on the internal _parse_os_release method?
return platform._parse_os_release(f) result = platform._parse_os_release(f)
break
except OSError: except OSError:
# Silently ignore failing to read os release info # Silently ignore failing to read os release info
pass pass
return {}
exit_chroot()
return result
def list_jails(): def list_jails():
@ -1691,13 +1712,7 @@ def list_jails():
jail["addresses"] += "" jail["addresses"] += ""
else: else:
# Parse os-release info ourselves # Parse os-release info ourselves
jail_platform = parse_os_release( jail_platform = parse_os_release(jail_rootfs_path)
(
os.path.join(jail_rootfs_path, "etc/os-release"),
os.path.join(jail_rootfs_path, "usr/lib/os-release"),
)
)
jail["os"] = jail_platform.get("ID") jail["os"] = jail_platform.get("ID")
jail["version"] = jail_platform.get("VERSION_ID") or jail_platform.get( jail["version"] = jail_platform.get("VERSION_ID") or jail_platform.get(
"VERSION_CODENAME" "VERSION_CODENAME"