fix(fex): the x86 runtime is built from files inside it so FEX finds its libraries (#2484)

* fix(fex): the x86 runtime is built from files inside it so FEX finds its libraries

* fix(fex): the x86 runtime keeps the permissions of the directories it is built from

* fix(fex): rebuilding the x86 runtime refreshes the Steam client links and removes read-only trees

* fix(fex): rebuilding the x86 runtime removes old trees that contain unreadable directories

* fix(fex): isolate generated rootfs files
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-15 10:08:31 -05:00
1 parent ab7e17a09d
commit a62933d023
2 files changed
+113 -14

No files matched your search

@@ -8,6 +8,7 @@ import os
import re
import runpy
import shutil
import stat
import struct
import subprocess
import sys
@@ -37,7 +38,9 @@ RUNTIMES = {SNIPER_APPID: ("sniper_platform_", "Steam Linux Runtime 3.0 (sniper"
SLR4_APPID: ("steamrt4_platform_", "Steam Linux Runtime 4.0 (steamrt4")}
MARKER = ".droiddeck-fex"
LAYOUT = 1
ROOTFS_LAYOUT = 2
ROOTFS_LAYOUT = 4
BLOB_DIRS = re.compile(r"^(?:\.ref|[0-9a-f]{2})$")
ROOTFS_LIBRARY_DIRS = ("lib", "lib64")
ARCH_DIRS = ("x86_64-linux-gnu", "i386-linux-gnu")
INTERPRETERS = ("ld-linux-x86-64.so.2", "ld-linux.so.2")
PROVIDED = frozenset((
@@ -310,9 +313,38 @@ def provides_newer(base, provider):
return True
def mirror(source, provider, name, target):
def clone(source, target, parents=()):
real = os.path.realpath(source)
if real in parents or not os.path.lexists(real):
return
if os.path.isdir(real):
os.makedirs(target, exist_ok=True)
for entry in os.listdir(real):
clone(os.path.join(real, entry), os.path.join(target, entry), parents + (real,))
shutil.copystat(real, target)
return
shutil.copy2(real, target)
def remove_tree(path):
if os.path.islink(path) or not os.path.isdir(path):
with contextlib.suppress(FileNotFoundError):
os.unlink(path)
return
os.chmod(path, os.lstat(path).st_mode | stat.S_IRWXU)
for entry in os.listdir(path):
remove_tree(os.path.join(path, entry))
os.rmdir(path)
def stays_in_libraries(root, target, link):
resolved = os.path.normpath(os.path.join(os.path.relpath(target, root), link))
return resolved.split(os.sep)[0] in ROOTFS_LIBRARY_DIRS
def mirror(source, provider, name, target, root):
if provider and name in PROVIDED and os.path.lexists(os.path.join(provider, name)):
os.symlink(os.path.realpath(os.path.join(provider, name)), os.path.join(target, name))
clone(os.path.join(provider, name), os.path.join(target, name))
return
path = os.path.join(source, name)
link = os.readlink(path) if os.path.islink(path) else None
@@ -322,10 +354,10 @@ def mirror(source, provider, name, target):
link = os.path.join(os.path.dirname(link), name)
if provider and GLIBC_BUILD.match(name):
return
if link and not link.startswith("/"):
if link and not link.startswith("/") and stays_in_libraries(root, target, link):
os.symlink(link, os.path.join(target, name))
else:
os.symlink(os.path.realpath(path), os.path.join(target, name))
clone(path, os.path.join(target, name))
def build_rootfs(base, provider):
@@ -337,12 +369,12 @@ def build_rootfs(base, provider):
return False
for entry in os.listdir(STATE):
if entry.startswith("rootfs."):
shutil.rmtree(os.path.join(STATE, entry), ignore_errors=True)
remove_tree(os.path.join(STATE, entry))
tmp = "%s.%d.tmp" % (rootfs, os.getpid())
os.makedirs(tmp)
for entry in os.listdir(base_files):
if entry not in ("lib", "lib64", "usr", MARKER):
os.symlink(os.path.realpath(os.path.join(base_files, entry)), os.path.join(tmp, entry))
if entry not in ("lib", "lib64", "usr", MARKER) and not BLOB_DIRS.match(entry):
clone(os.path.join(base_files, entry), os.path.join(tmp, entry))
os.symlink(".", os.path.join(tmp, "usr"))
for top in ("lib", "lib64"):
source = os.path.join(base_files, top)
@@ -353,15 +385,15 @@ def build_rootfs(base, provider):
provided = os.path.join(provider_files, top, entry)
os.makedirs(os.path.join(tmp, top, entry))
for name in set(os.listdir(path)) | {name for name in PROVIDED if os.path.lexists(os.path.join(provided, name))}:
mirror(path, provided, name, os.path.join(tmp, top, entry))
mirror(path, provided, name, os.path.join(tmp, top, entry), tmp)
else:
mirror(source, None, entry, os.path.join(tmp, top))
mirror(source, None, entry, os.path.join(tmp, top), tmp)
write_if_changed(os.path.join(tmp, MARKER), stamp)
old = "%s.%d.old" % (rootfs, os.getpid())
if os.path.lexists(rootfs):
os.rename(rootfs, old)
os.rename(tmp, rootfs)
shutil.rmtree(old, ignore_errors=True)
remove_tree(old)
return True
@@ -441,7 +473,7 @@ def link_guest_sdk(root):
continue
os.makedirs(os.path.dirname(link), exist_ok=True)
if os.path.lexists(link):
os.unlink(link)
remove_tree(link)
os.symlink(directory, link)
@@ -513,8 +545,6 @@ def prepare(app=SNIPER_APPID):
root = os.path.join(platform, "files")
if materialize(platform):
say("prepared %s as the x86 runtime" % os.path.basename(platform))
install_preloads(root)
install_fonts(root)
provider = None
if app == SNIPER_APPID:
provider = graphics_platform()
@@ -531,6 +561,8 @@ def prepare(app=SNIPER_APPID):
say("built the x86 runtime from %s with the C and C++ libraries of %s"
% (os.path.basename(platform), os.path.basename(provider)))
root = os.path.join(STATE, "rootfs")
install_preloads(root)
install_fonts(root)
with contextlib.suppress(OSError):
link_guest_sdk(root)
write_config(fex, root)
+67
View File
@@ -5,6 +5,7 @@ import json
import os
import runpy
import shutil
import stat
import struct
import subprocess
import tempfile
@@ -450,6 +451,72 @@ class PrepareTest(FexTestCase):
self.assertIn("built the x86 runtime", self.quiet(FEX["prepare"])[1])
self.assertEqual(sorted(os.listdir(root.parent)), ["config", "lock", "rootfs", "status.json"])
def test_the_built_x86_runtime_holds_nothing_that_leaves_it(self):
self.fex_tool()
sniper = self.runtime()
FEX["materialize"](str(sniper))
self.graphics(sniper)
os.chmod(sniper / "files/share/ca", 0o750)
self.quiet(FEX["prepare"])
root = (self.home / ".local/share/droiddeck-fex/rootfs").resolve()
self.assertEqual(stat.S_IMODE(os.stat(root / "share/ca").st_mode), 0o750)
entries = 0
for directory, dirs, files in os.walk(root):
for name in dirs + files:
path = Path(directory) / name
entries += 1
if path.is_symlink():
self.assertFalse(os.path.isabs(os.readlink(path)), path)
self.assertTrue(path.resolve().is_relative_to(root), path)
self.assertGreater(entries, 10)
self.assertEqual((root / "bin/[").read_bytes(), b"blob")
self.assertEqual((root / "share/ca/real.crt").read_bytes(), b"cert")
self.assertFalse(os.path.lexists(root / "ab"))
def test_guest_writes_do_not_mutate_runtime_blob_files(self):
self.fex_tool()
sniper = self.runtime()
FEX["materialize"](str(sniper))
self.graphics(sniper)
self.quiet(FEX["prepare"])
root = self.home / ".local/share/droiddeck-fex/rootfs"
marker = root / ".droiddeck-fex"
stamp = marker.read_text()
marker.write_text("3\n" + stamp.partition("\n")[2])
source = sniper / "files/ab/cdef-1.bin"
guest = root / "bin/["
guest.unlink()
os.link(source, guest)
self.assertEqual(os.stat(source).st_ino, os.stat(guest).st_ino)
self.assertIn("built the x86 runtime", self.quiet(FEX["prepare"])[1])
self.assertNotEqual(os.stat(source).st_ino, os.stat(guest).st_ino)
guest.write_bytes(b"guest mutation")
self.assertEqual(source.read_bytes(), b"blob")
self.assertEqual(guest.read_bytes(), b"guest mutation")
def test_a_rebuilt_x86_runtime_refreshes_the_steam_client_links_and_drops_read_only_trees(self):
self.fex_tool()
sniper = self.runtime()
FEX["materialize"](str(sniper))
self.graphics(sniper)
base = sniper / "files"
(base / str(self.home).lstrip("/") / ".steam").mkdir(parents=True)
os.symlink(self.steam / "linuxarm64", base / str(self.home).lstrip("/") / ".steam/sdk64")
self.write(self.steam / "linuxarm64/steamclient.so", elf(183))
os.chmod(base / "share/ca", 0o500)
self.addCleanup(os.chmod, base / "share/ca", 0o700)
self.quiet(FEX["prepare"])
rootfs = self.home / ".local/share/droiddeck-fex/rootfs"
os.chmod(rootfs / "share/ca", 0o000)
self.write(self.steam / "linux64/steamclient.so", elf(62))
self.write(self.steam / "linux32/steamclient.so", elf(3))
os.utime(self.steam / "steamapps/common/SteamLinuxRuntime_4/steamrt4_platform_4.0.20260928.262390/usr-mtree.txt.gz", ns=(1, 1))
self.assertIn("built the x86 runtime", self.quiet(FEX["prepare"])[1])
guest = rootfs / str(self.home).lstrip("/") / ".steam"
self.assertEqual(os.readlink(guest / "sdk64"), str(self.steam / "linux64"))
self.assertEqual(os.readlink(guest / "sdk32"), str(self.steam / "linux32"))
self.assertEqual(sorted(os.listdir(rootfs.parent)), ["config", "lock", "rootfs", "status.json"])
def test_a_runtime_that_is_not_newer_is_left_out(self):
self.fex_tool()
sniper = self.runtime()