diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt index e7f393fd8..7de69d529 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt @@ -75,7 +75,10 @@ object BwrapSpawner { } } - class Plan(val root: File, val binds: List, val cwd: String, val argv: List, val env: Map) + class Plan( + val root: File, val binds: List, val cwd: String, val argv: List, val env: Map, + val launcher: List = emptyList(), + ) data class Bind(val host: String, val guest: String) { val spec: String get() = if (host == guest) host else "$host:$guest" @@ -179,10 +182,67 @@ object BwrapSpawner { val argv = request.getJSONArray("argv").let { a -> (0 until a.length()).map { a.getString(it) } } val env = LinkedHashMap() request.optJSONObject("env")?.let { e -> e.keys().forEach { k -> env[k] = e.getString(k) } } - if (binds.any { it.guest == "/dev" }) gpu(rootfs, root, env)?.forEach { addBind(it) } - preload(session, rootfs, env).forEach { addBind(it) } + val x86 = LinuxFex.isX86(LinuxFex.elfMachine(File(hostPath(binds, root.path, "/usr/bin/env")))) + if (x86) { + fex(rootfs, dir, env)?.forEach { addBind(it) } + ?: throw IOException("this app is built for x86-64 and the Linux runtime has no FEX to run it") + fakeEvdev(session, rootfs, env).forEach { addBind(it) } + } else { + if (binds.any { it.guest == "/dev" }) gpu(rootfs, root, env)?.forEach { addBind(it) } + preload(session, rootfs, env).forEach { addBind(it) } + } browserSandboxes(env) - return Plan(root, binds, request.optString("cwd", "/").ifEmpty { "/" }, argv, env) + return Plan(root, binds, request.optString("cwd", "/").ifEmpty { "/" }, argv, env, + if (x86) listOf("$FEX_DIR/bin/FEX") else emptyList()) + } + + private const val FEX_DIR = "/run/droiddeck-fex" + private val FEX_HOST_LIBS = listOf("ld-linux-aarch64.so.1", "libc.so.6", "libm.so.6", "libstdc++.so.6", "libgcc_s.so.1") + private val FEX_PRELOADS = listOf("libblsession.so", "libfakeinput.so") + + // An x86-64 runtime starts under the rootfs's FEX. The sandbox's own tree is the x86 root, so FEX needs no RootFS. + internal fun fex(rootfs: String, dir: File, env: MutableMap): List? { + val bin = File(rootfs, "usr/local/bin") + val fex = File(bin, "FEX") + val server = File(bin, "FEXServer") + if (!fex.isFile || !server.isFile) return null + val binds = arrayListOf(Bind(fex.path, "$FEX_DIR/bin/FEX"), Bind(server.path, "$FEX_DIR/bin/FEXServer")) + // FEXServer has no RUNPATH, so the ARM64 libraries go where the ARM64 loader looks by default. x86 programs + // search /usr/lib/x86_64-linux-gnu first and skip an ARM64 library they meet. + for (name in FEX_HOST_LIBS + "libxxhash.so.0") { + val lib = listOf(File(rootfs, "usr/lib/$name"), File(rootfs, "usr/local/lib/$name")).firstOrNull { it.exists() } + ?: run { Log.w(TAG, "fex: the runtime has no $name"); return null } + binds.add(Bind(lib.canonicalPath, "/usr/lib/$name")) + } + // One ld.so.preload serves both: $LIB is lib for the ARM64 loader and lib/x86_64-linux-gnu (or i386) for the + // runtime's. libblsession turns FEX's execveat of /proc/self/exe into the execve proot supports. + val listed = FileUtils.readString(File(rootfs, "etc/ld.so.preload")) ?: "" + val lines = ArrayList() + for (name in FEX_PRELOADS) { + val builds = listOf( + File(rootfs, "usr/local/lib/$name") to "lib", + File(rootfs, "usr/local/lib/droiddeck-fex/x86_64/$name") to "lib/x86_64-linux-gnu", + File(rootfs, "usr/local/lib/droiddeck-fex/i386/$name") to "lib/i386-linux-gnu", + ) + if (name !in listed || builds.any { !it.first.isFile }) continue + builds.forEach { (lib, sub) -> binds.add(Bind(lib.path, "$FEX_DIR/$sub/$name")) } + lines.add("$FEX_DIR/\$LIB/$name") + } + if ("libblsession.so" !in lines.joinToString()) Log.w(TAG, "fex: no libblsession; x86 programs cannot start others") + val preload = File(dir, "ld.so.preload").apply { writeText(lines.joinToString("\n", postfix = "\n")) } + binds.add(Bind(preload.path, "/etc/ld.so.preload")) + val config = File(dir, "fex-config").apply { mkdirs() } + val cache = File(rootfs, "root/.cache/droiddeck-fex-flatpak").apply { mkdirs() } + binds.add(Bind(config.path, "$FEX_DIR/config")) + binds.add(Bind(cache.path, "$FEX_DIR/cache")) + env["FEX_APP_CONFIG_LOCATION"] = "$FEX_DIR/config/" + env["FEX_APP_DATA_LOCATION"] = "$FEX_DIR/config/" + env["FEX_APP_CACHE_LOCATION"] = "$FEX_DIR/cache/" + // Each sandbox gets its own server: the session's x86 Steam has one on the default socket, set up for its RootFS. + env["FEX_SERVERSOCKETPATH"] = "droiddeck-bwrap-${dir.name}-${android.os.SystemClock.elapsedRealtime()}.FEXServer.Socket" + // The session's tunables are for the ARM64 libc; an x86 ld.so copying them under FEX fails in sbrk. + env.remove("GLIBC_TUNABLES") + return binds } private const val GPU_DIR = "/run/droiddeck-gpu" @@ -219,12 +279,12 @@ object BwrapSpawner { val binds = ArrayList() val paths = libs.map { lib -> "$PRELOAD_DIR/${lib.substringAfterLast('/')}".also { binds.add(Bind(rootfs + lib, it)) } } env["LD_PRELOAD"] = (paths + listOfNotNull(env["LD_PRELOAD"]?.takeIf { it.isNotBlank() })).joinToString(":") - env["FAKE_EVDEV_DIR"]?.let { File(it).parent }?.let { dev -> - binds.addAll(bindsFor(session, rootfs, dev, dev)) - } - return binds + return binds + fakeEvdev(session, rootfs, env) } + private fun fakeEvdev(session: List, rootfs: String, env: Map): List = + env["FAKE_EVDEV_DIR"]?.let { File(it).parent }?.let { dev -> bindsFor(session, rootfs, dev, dev) } ?: emptyList() + private const val PRELOAD_DIR = "/run/droiddeck-preload" private fun browserSandboxes(env: MutableMap) { @@ -282,6 +342,7 @@ object BwrapSpawner { val verbose = plan.env["DROIDDECK_PROOT_VERBOSE"]?.toIntOrNull() if (verbose != null) { cmd.add(1, "-v"); cmd.add(2, verbose.toString()) } plan.binds.forEach { cmd.add("-b"); cmd.add(it.spec) } + cmd.addAll(plan.launcher) cmd.add("/usr/bin/env") cmd.add("-i") plan.env.forEach { (k, v) -> cmd.add("$k=$v") } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/FlatpakManager.kt b/app/src/main/java/com/droiddeck/launcher/runtime/FlatpakManager.kt index 56340b111..18c59e990 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/FlatpakManager.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/FlatpakManager.kt @@ -19,7 +19,7 @@ object FlatpakManager { @Volatile var busy: String? = null private set - class App(val id: String, val name: String, val icon: File?, val branch: String, val summary: String?) + class App(val id: String, val name: String, val icon: File?, val branch: String, val summary: String?, val arch: String = arch()) fun ready(context: Context): Boolean { val root = LinuxRuntime.rootDir(context) @@ -33,24 +33,28 @@ object FlatpakManager { else -> "aarch64" } + /** The arches apps can be installed for: this device's, then x86-64 under FEX. */ + private fun arches(): List = listOf(arch(), "x86_64").distinct() + + private fun activeBranch(app: File): Pair? = arches().firstNotNullOfOrNull { arch -> + File(app, arch).listFiles()?.firstOrNull { File(it, "active").exists() }?.let { arch to it } + } + fun installedApps(context: Context): List { val apps = File(LinuxRuntime.rootDir(context), "$USER_DIR/app") - val arch = arch() return apps.listFiles()?.mapNotNull { dir -> val id = dir.name - val archDir = File(dir, arch) - val branch = archDir.listFiles()?.firstOrNull { File(it, "active").exists() } ?: return@mapNotNull null + val (arch, branch) = activeBranch(dir) ?: return@mapNotNull null val active = File(branch, "active") val export = File(active, "export/share") val desktop = FileUtils.readString(File(export, "applications/$id.desktop")) val name = desktop?.let { entry(it, "Name") } ?: id.substringAfterLast('.') val summary = desktop?.let { entry(it, "Comment") } - App(id, name, icon(export, id), branch.name, summary) + App(id, name, icon(export, id), branch.name, summary, arch) }?.sortedBy { it.name.lowercase() } ?: emptyList() } - fun isInstalled(context: Context, id: String): Boolean = - File(LinuxRuntime.rootDir(context), "$USER_DIR/app/$id/${arch()}").listFiles()?.any { File(it, "active").exists() } == true + fun isInstalled(context: Context, id: String): Boolean = activeBranch(File(LinuxRuntime.rootDir(context), "$USER_DIR/app/$id")) != null private fun entry(desktop: String, key: String): String? { var inMain = false @@ -175,6 +179,12 @@ object FlatpakManager { val problem = transaction(context, "install-bundle", guestPath, onProgress) { id = it.optString("id").ifEmpty { null } } return id to problem } + /** Adds the remote a .flatpakrepo file describes and installs the one app it carries (GeForce NOW's is one). */ + fun installRepo(context: Context, url: String, onProgress: (String, Int) -> Unit): Pair { + var id: String? = null + val problem = transaction(context, "install-repo", url, onProgress) { id = it.optString("id").ifEmpty { null } } + return id to problem + } fun update(context: Context, id: String?, onProgress: (String, Int) -> Unit) = transaction(context, "update", id, onProgress) fun prune(context: Context, onProgress: (String, Int) -> Unit) = transaction(context, "prune", null, onProgress) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/UserApps.kt b/app/src/main/java/com/droiddeck/launcher/runtime/UserApps.kt index 445a56348..314fa2090 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/UserApps.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/UserApps.kt @@ -58,6 +58,7 @@ object UserApps { class GitHub(val repo: String) : Source() class Flatpak(val id: String) : Source() class FlatpakBundle(val file: File) : Source() + class FlatpakRepo(val url: String) : Source() } class Request(val source: Source, val name: String?, val icon: String?, val meta: Map = emptyMap()) @@ -111,6 +112,7 @@ object UserApps { is Source.GitHub -> addFromGitHub(context, s.repo, name, icon, onProgress) is Source.Flatpak -> addFlatpak(context, s.id, name, icon, onProgress) is Source.FlatpakBundle -> addFlatpakBundle(context, s.file, name, icon, onProgress) + is Source.FlatpakRepo -> addFlatpakRepo(context, s.url, name, icon, onProgress) } } } @@ -392,6 +394,17 @@ object UserApps { return t.takeIf { Regex("^[A-Za-z][\\w-]*(\\.[A-Za-z0-9_][\\w-]*){2,}$").matches(it) } } + fun flatpakRepo(input: String): String? = + input.trim().takeIf { it.startsWith("https://") && it.substringBefore('?').endsWith(".flatpakrepo", ignoreCase = true) && ' ' !in it } + + private fun addFlatpakRepo(context: Context, url: String, name: String?, icon: File?, onProgress: (String, Int) -> Unit): String? { + if (!FlatpakManager.ready(context)) FlatpakManager.setup(context, onProgress)?.let { return it } + val (id, problem) = FlatpakManager.installRepo(context, url, onProgress) + problem?.let { return it } + if (id != null) customiseFlatpak(context, id, name, icon) + return null + } + private fun addFlatpak(context: Context, id: String, name: String?, icon: File?, onProgress: (String, Int) -> Unit): String? { if (!FlatpakManager.ready(context)) FlatpakManager.setup(context, onProgress)?.let { return it } FlatpakManager.install(context, id, onProgress)?.let { return it } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt index bc9618c96..61b6b2791 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt @@ -143,6 +143,7 @@ internal fun AddAppDialog(runtimeReady: Boolean, onDismiss: () -> Unit, onAdd: ( val repoId = UserApps.githubRepo(repo) val flatpakId = UserApps.flatpakId(flatpak) + val flatpakRepo = UserApps.flatpakRepo(flatpak) val custom = name.trim().takeIf { it.isNotEmpty() } val request: Pair? = when (source) { AddSource.SCRIPT -> scriptPath?.takeIf { it.endsWith(".sh", ignoreCase = true) }?.let { File(it) } @@ -152,6 +153,7 @@ internal fun AddAppDialog(runtimeReady: Boolean, onDismiss: () -> Unit, onAdd: ( AddSource.GITHUB -> repoId?.let { UserApps.Request(UserApps.Source.GitHub(it), custom, iconPath) to (custom ?: it) } AddSource.FLATPAK -> bundlePath?.takeIf { it.endsWith(".flatpak", ignoreCase = true) }?.let { File(it) } ?.let { UserApps.Request(UserApps.Source.FlatpakBundle(it), custom, iconPath) to (custom ?: it.nameWithoutExtension) } + ?: flatpakRepo?.let { UserApps.Request(UserApps.Source.FlatpakRepo(it), custom, iconPath) to (custom ?: it.substringAfterLast('/').substringBefore('.')) } ?: flatpakId?.let { UserApps.Request(UserApps.Source.Flatpak(it), custom, iconPath) to (custom ?: it) } } val busy = UserAppsState.working @@ -201,7 +203,7 @@ internal fun AddAppDialog(runtimeReady: Boolean, onDismiss: () -> Unit, onAdd: ( CheckedField( flatpak, { flatpak = it.take(200); bundlePath = null }, stringResource(R.string.add_app_flatpak_id), stringResource(R.string.add_app_flatpak_placeholder), - stringResource(R.string.add_app_flatpak_invalid).takeIf { flatpak.isNotBlank() && flatpakId == null }, + stringResource(R.string.add_app_flatpak_invalid).takeIf { flatpak.isNotBlank() && flatpakId == null && flatpakRepo == null }, ) Small(stringResource(R.string.add_app_flatpak_bundle_hint)) FileRow(bundlePath) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 021c3217c..067b9b020 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -181,7 +181,7 @@ This program is built for %1$s PCs and runs through FEX, which is not set up yet. Open Steam once while signed in: DroidDeck has it download FEX and the Steam Linux Runtime it needs, then this opens. This program %1$s is built for x86 PCs and runs through FEX, which is not installed yet. Start Steam once while signed in and let it download FEX and the Steam Linux Runtime, then start this again. - A Flathub app ID or its Flathub page. Flatpak is set up first if it is not yet. + A Flathub app ID, its Flathub page, or a .flatpakrepo link such as GeForce NOW\'s. Flatpak is set up first if it is not yet. Apps with only an x86-64 build run under FEX. File No file chosen Choose file @@ -194,7 +194,7 @@ Not a GitHub repository App ID org.ppsspp.PPSSPP - Not a Flathub app ID + Not a Flathub app ID or .flatpakrepo link Or a .flatpak file, for an app not on Flathub. Choose a .flatpak file Source diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt index e1e6827ab..02f78c27f 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/BwrapSpawnerTest.kt @@ -4,12 +4,15 @@ import android.util.Base64 import org.robolectric.RuntimeEnvironment import com.droiddeck.launcher.runtime.BwrapSpawner.Bind import java.io.File +import java.io.IOException import java.nio.file.Files import org.json.JSONArray import org.json.JSONObject import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertThrows import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test @@ -118,4 +121,66 @@ class BwrapSpawnerPlanTest { assertEquals("/app", plan.cwd) assertEquals("org.example.Game", plan.env["FLATPAK_ID"]) } + + private fun elf(file: File, machine: Int) = file.apply { + parentFile!!.mkdirs() + writeBytes(ByteArray(64).also { b -> + "\u007fELF".toByteArray().copyInto(b); b[4] = 2; b[5] = 1; b[18] = machine.toByte() + }) + } + + private fun x86Request() = request( + op("t" to "bind", "src" to "/runtime-files", "dest" to "/usr", "ro" to true), + op("t" to "symlink", "target" to "usr/lib", "dest" to "/lib"), + ) + + private fun fexInRootfs(rootfs: File) { + for (name in listOf("usr/local/bin/FEX", "usr/local/bin/FEXServer", "usr/lib/ld-linux-aarch64.so.1", "usr/lib/libc.so.6", + "usr/lib/libm.so.6", "usr/lib/libstdc++.so.6", "usr/lib/libgcc_s.so.1", "usr/local/lib/libxxhash.so.0")) { + elf(File(rootfs, name), LinuxFex.ELF_AARCH64) + } + for (dir in listOf("usr/local/lib", "usr/local/lib/droiddeck-fex/x86_64", "usr/local/lib/droiddeck-fex/i386")) { + for (name in listOf("libblsession.so", "libfakeinput.so")) File(rootfs, "$dir/$name").apply { parentFile!!.mkdirs(); writeText("") } + } + File(rootfs, "etc/ld.so.preload").apply { parentFile!!.mkdirs() } + .writeText("/usr/local/lib/libblsession.so\n/usr/local/lib/libfakeinput.so\n/usr/local/lib/libblfastpath.so\n") + } + + @Test fun anX86RuntimeStartsUnderFexWithPreloadsForBothArches() { + val context = RuntimeEnvironment.getApplication() + val rootfs = LinuxRuntime.rootDir(context) + fexInRootfs(rootfs) + elf(File(runtime, "bin/env"), LinuxFex.ELF_X86_64) + try { + val plan = BwrapSpawner.plan(context, x86Request(), dir) + assertEquals(listOf("/run/droiddeck-fex/bin/FEX"), plan.launcher) + assertTrue(Bind(File(rootfs, "usr/local/bin/FEXServer").path, "/run/droiddeck-fex/bin/FEXServer") in plan.binds) + assertTrue(plan.binds.any { it.guest == "/usr/lib/ld-linux-aarch64.so.1" }) + assertTrue(plan.binds.any { it.guest == "/usr/lib/libxxhash.so.0" }) + assertTrue(plan.binds.any { it.guest == "/run/droiddeck-fex/lib/x86_64-linux-gnu/libblsession.so" }) + val preload = File(plan.binds.single { it.guest == "/etc/ld.so.preload" }.host).readText() + assertEquals("/run/droiddeck-fex/\$LIB/libblsession.so\n/run/droiddeck-fex/\$LIB/libfakeinput.so\n", preload) + assertNull(plan.env["LD_PRELOAD"]) + assertNull(plan.env["VK_ICD_FILENAMES"]) + assertTrue(plan.env.getValue("FEX_SERVERSOCKETPATH").endsWith(".FEXServer.Socket")) + assertEquals("/run/droiddeck-fex/config/", plan.env["FEX_APP_CONFIG_LOCATION"]) + } finally { + rootfs.deleteRecursively() + } + } + + @Test fun anArmRuntimeIsStartedDirectly() { + val context = RuntimeEnvironment.getApplication() + elf(File(runtime, "bin/env"), LinuxFex.ELF_AARCH64) + val plan = BwrapSpawner.plan(context, x86Request(), dir) + assertEquals(emptyList(), plan.launcher) + assertFalse(plan.binds.any { it.guest == "/etc/ld.so.preload" }) + } + + @Test fun anX86RuntimeWithoutFexIsRefused() { + val context = RuntimeEnvironment.getApplication() + elf(File(runtime, "bin/env"), LinuxFex.ELF_X86_64) + File(LinuxRuntime.rootDir(context), "usr/local/bin/FEX").delete() + assertThrows(IOException::class.java) { BwrapSpawner.plan(context, x86Request(), dir) } + } } diff --git a/docs/development/flatpak.md b/docs/development/flatpak.md index dd7faae50..dad67d1fa 100644 --- a/docs/development/flatpak.md +++ b/docs/development/flatpak.md @@ -105,14 +105,28 @@ Known to fail: ## x86_64 apps -Flathub lists x86_64-only apps (Heroic, Discord, Spotify, Steam), and the store hides them. Running them -would need an x86 emulator in the sandbox. There's no FEX or box64 package in Arch Linux ARM; Valve's -ARM64 Steam can install its FEX compatibility tool, but only through the Steam client. With a FEX binary -available, the path would be: +Flathub lists x86_64-only apps (Heroic, Discord, Spotify, Steam), and the store still hides them. They can be +added from the Desktop page's "Add app" dialog instead: by Flathub ID (`droiddeck-flatpak install` falls back to +x86_64 when the app has no ARM64 build and the rootfs has FEX), from a `.flatpak` bundle, or from a +`.flatpakrepo` link (`install-repo`), which adds that remote and installs the one app it carries. NVIDIA's +GeForce NOW is the case in point: `https://international.download.nvidia.com/GFNLinux/flatpak/geforcenow.flatpakrepo`, +x86_64 only, on the Freedesktop 24.08 runtime, which Flatpak then pulls from Flathub for x86_64. +`droiddeck-flatpak-run` passes `--arch=x86_64` for an app installed only for that arch. -1. `flatpak install --arch=x86_64`. Flatpak accepts it, and the x86 runtime is also the x86 root FEX needs. -2. `BwrapSpawner` starts those sandboxes with `proot -q`, through a small wrapper that runs FEXInterpreter. - `-q` passes QEMU-style arguments. -3. GPU drivers would need FEX's thunks, or Mesa runs emulated on the CPU. +`BwrapSpawner` looks at the sandbox's `/usr/bin/env`. When it is an x86 ELF, the sandbox starts as +`/run/droiddeck-fex/bin/FEX /usr/bin/env ...`, and no RootFS is set: the sandbox's own tree is the x86 root. + +- The rootfs's FEX and FEXServer go in under `/run/droiddeck-fex/bin`. FEXServer has no RUNPATH, so the ARM64 + loader, libc, libm, libstdc++, libgcc_s and libxxhash are bound into `/usr/lib`, where the ARM64 loader looks by + default. x86 programs search `/usr/lib/x86_64-linux-gnu` first and skip an ARM64 library. +- `/etc/ld.so.preload` lists `/run/droiddeck-fex/$LIB/libblsession.so` and `libfakeinput.so`. `$LIB` is `lib` for + the ARM64 loader and `lib/x86_64-linux-gnu` or `lib/i386-linux-gnu` for the runtime's, so FEX gets the rootfs's + ARM64 builds and the app the x86 builds `droiddeck-fex` uses. The ARM64 libblsession matters: without binfmt_misc, + FEX starts an x86 child with `execveat(fd, "/proc/self/exe")`, which proot refuses, and libblsession turns it into + an `execve`. +- Each sandbox gets its own FEXServer socket (`FEX_SERVERSOCKETPATH`), because the session's x86 Steam runs one on + the default socket for its own RootFS. FEX's code cache is kept in `/root/.cache/droiddeck-fex-flatpak`. +- The GPU and preload additions for ARM64 sandboxes are skipped. There are no FEX thunks yet, so Mesa runs + emulated on the CPU (llvmpipe): fine for a launcher or a video stream at a modest size, slow for 3D. x86 AppImages would also need an x86 rootfs for the libraries they don't bundle. diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak index d91cae6ad..203ce25ad 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak @@ -1,7 +1,9 @@ #!/usr/bin/python3 import json import os +import re import sys +import urllib.request os.environ.setdefault("FLATPAK_BWRAP", "/usr/local/bin/droiddeck-bwrap") @@ -11,6 +13,8 @@ gi.require_version("Flatpak", "1.0") from gi.repository import Flatpak, Gio, GLib REMOTE = "flathub" +FEX = "/usr/local/bin/FEX" +X86 = "x86_64" def prepare_rootfs(): @@ -109,33 +113,73 @@ def run(t, state, report_done=True): return 0 -def install(app_id): +def arches(): + """This device's arch, then x86-64 when FEX is there to run it (droiddeck-bwrap starts those sandboxes under it).""" + native = Flatpak.get_default_arch() + return [native] + ([X86] if native != X86 and os.access(FEX, os.X_OK) else []) + + +def remote_branch(inst, remote, app_id): + """(arch, branch) of the first arch and branch the remote has the app for.""" + last = None + for arch in arches(): + for candidate in ("stable", "master"): + try: + return arch, inst.fetch_remote_ref_sync(remote, Flatpak.RefKind.APP, app_id, arch, candidate, None).get_branch() + except GLib.Error as err: + last = err + raise LookupError(last.message if last else "no branch") + + +def install(app_id, remote=REMOTE): inst = installation() - arch = Flatpak.get_default_arch() if app_ref(inst, app_id) is not None: - emit(e="done", note="already installed") + emit(e="done", id=app_id, note="already installed") return 0 - branch, last = None, None - for candidate in ("stable", "master"): - try: - branch = inst.fetch_remote_ref_sync(REMOTE, Flatpak.RefKind.APP, app_id, arch, candidate, None).get_branch() - break - except GLib.Error as err: - last = err - if branch is None: - emit(e="error", message="%s is not on Flathub for %s (%s)" % (app_id, arch, last.message if last else "no branch")) + try: + arch, branch = remote_branch(inst, remote, app_id) + except LookupError as err: + emit(e="error", message="%s is not on %s for %s (%s)" % (app_id, remote, " or ".join(arches()), err)) return 1 + if arch == X86: + emit(e="warning", message="%s has no %s build; installing the x86-64 one, which runs under FEX" % (app_id, arches()[0])) t, state = transaction(inst) - t.add_install(REMOTE, "app/%s/%s/%s" % (app_id, arch, branch), None) + t.add_install(remote, "app/%s/%s/%s" % (app_id, arch, branch), None) rc = run(t, state, report_done=False) if rc == 0 and app_ref(installation(), app_id) is None: emit(e="error", message="%s did not install; see flatpak-install.log" % app_id) return 1 if rc == 0: - emit(e="done") + emit(e="done", id=app_id) return rc +def install_repo(url): + """Adds the remote a .flatpakrepo file describes (like NVIDIA's for GeForce NOW) and installs the app it carries.""" + try: + with urllib.request.urlopen(url, timeout=60) as response: + data = response.read(1 << 20) + except (OSError, ValueError) as err: + emit(e="error", message="could not download %s (%s)" % (url, err)) + return 1 + title = re.search(rb"^Title=(.+)$", data, re.M) + name = re.sub(r"[^A-Za-z0-9_-]", "", (title.group(1).decode("utf-8", "replace") if title else "")) or "repo" + inst = installation() + try: + remote = Flatpak.Remote.new_from_file(name, GLib.Bytes.new(data)) + inst.add_remote(remote, True, None) + inst.update_remote_sync(name, None) + refs = inst.list_remote_refs_sync(name, None) + except GLib.Error as err: + emit(e="error", message="%s is not a Flatpak repository file (%s)" % (url, err.message)) + return 1 + apps = sorted({r.get_name() for r in refs if r.get_kind() == Flatpak.RefKind.APP and r.get_arch() in arches()}) + if len(apps) != 1: + emit(e="error", message="%s carries %s; add one by its ID" % (name, ", ".join(apps) if apps else "no app for " + " or ".join(arches()))) + return 1 + return install(apps[0], name) + + def install_bundle(path): """A single-file bundle (flatpak build-bundle), for apps Flathub does not carry.""" inst = installation() @@ -237,15 +281,17 @@ def updates(): def main(): if len(sys.argv) < 2: - sys.stderr.write(__doc__ or "usage: droiddeck-flatpak install|install-bundle|uninstall|update|updates|prune [app-id|file]\n") + sys.stderr.write(__doc__ or "usage: droiddeck-flatpak install|install-bundle|install-repo|uninstall|update|updates|prune [app-id|file]\n") return 2 cmd, args = sys.argv[1], sys.argv[2:] - if cmd in ("install", "install-bundle", "update"): + if cmd in ("install", "install-bundle", "install-repo", "update"): prepare_rootfs() if cmd == "install" and args: return install(args[0]) if cmd == "install-bundle" and args: return install_bundle(args[0]) + if cmd == "install-repo" and args: + return install_repo(args[0]) if cmd == "uninstall" and args: return uninstall(args[0]) if cmd == "update": @@ -254,7 +300,7 @@ def main(): return updates() if cmd == "prune": return prune() - sys.stderr.write("usage: droiddeck-flatpak install|install-bundle|uninstall|update|updates|prune [app-id|file]\n") + sys.stderr.write("usage: droiddeck-flatpak install|install-bundle|install-repo|uninstall|update|updates|prune [app-id|file]\n") return 2 diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-run b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-run index 08e6e7a26..a20bf6d10 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-run +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-flatpak-run @@ -11,6 +11,11 @@ if [ -n "${BL_INSIDE:-}" ] || [ -n "${GAMESCOPE_WAYLAND_DISPLAY:-}" ]; then export XDG_SESSION_TYPE=x11 socket=(--nosocket=wayland --socket=x11) fi +# An app installed only for x86-64 is found only when asked for; droiddeck-bwrap starts it under FEX. +apps=$HOME/.local/share/flatpak/app +if [ -n "${1:-}" ] && [ -d "$apps/$1/x86_64" ] && [ ! -d "$apps/$1/$(flatpak --default-arch 2>/dev/null)" ]; then + socket+=(--arch=x86_64) +fi echo "== flatpak run ${socket[*]} $*" if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ] && command -v dbus-run-session >/dev/null 2>&1; then exec dbus-run-session -- flatpak run "${socket[@]}" "$@"