mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
feat: run Flatpak apps through a proot bwrap stand-in and add a Flathub store
This commit is contained in:
1 parent
9644a4cc7f
commit
c63355be6c
22 files changed
+2106
-13
No files matched your search
@@ -0,0 +1,318 @@
|
||||
#!/usr/bin/python3
|
||||
# bubblewrap for Flatpak (FLATPAK_BWRAP), in a runtime that cannot have it.
|
||||
#
|
||||
# bwrap builds each sandbox from mount namespaces, which Android gives no app. proot gives the same
|
||||
# picture - Flatpak's runtime as /usr, the app as /app, host paths bound where Flatpak asks - but a
|
||||
# proot nested inside the session's proot runs every system call through two tracers, some thirty
|
||||
# times slower on a program's start. So a sandbox is described to the app instead, over a socket
|
||||
# only this app's uid may use, and the app starts it as a proot of its own beside the session's
|
||||
# (BwrapSpawner). This process stands in for the sandbox until it ends: its output, its pid for
|
||||
# --info-fd, and its exit status come back here.
|
||||
#
|
||||
# Sandboxes that remap nothing - Flatpak's install triggers (`--ro-bind / /` and the directory they
|
||||
# update) and the D-Bus proxy (every top-level directory bound onto itself) - need no proot at all
|
||||
# and run here directly, with the file descriptors Flatpak handed them.
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import signal
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
|
||||
SOCKET = "\0com.droiddeck.launcher.bwrap"
|
||||
VERSION = "bubblewrap 0.11.0 (droiddeck)"
|
||||
|
||||
# Options and how many arguments each takes; bwrap(1).
|
||||
NO_ARG = {
|
||||
"--unshare-user", "--unshare-user-try", "--unshare-ipc", "--unshare-pid", "--unshare-net",
|
||||
"--unshare-uts", "--unshare-cgroup", "--unshare-cgroup-try", "--unshare-all", "--share-net",
|
||||
"--disable-userns", "--assert-userns-disabled", "--clearenv", "--new-session",
|
||||
"--die-with-parent", "--as-pid-1", "--level-prefix", "--tmp-overlay", "--ro-overlay",
|
||||
}
|
||||
ONE_ARG = {
|
||||
"--args", "--argv0", "--userns", "--userns2", "--pidns", "--uid", "--gid", "--hostname",
|
||||
"--chdir", "--unsetenv", "--lock-file", "--sync-fd", "--remount-ro", "--exec-label",
|
||||
"--file-label", "--proc", "--dev", "--tmpfs", "--mqueue", "--dir", "--seccomp",
|
||||
"--add-seccomp-fd", "--block-fd", "--userns-block-fd", "--info-fd", "--json-status-fd",
|
||||
"--cap-add", "--cap-drop", "--perms", "--size", "--overlay-src",
|
||||
}
|
||||
TWO_ARG = {
|
||||
"--setenv", "--bind", "--bind-try", "--dev-bind", "--dev-bind-try", "--ro-bind",
|
||||
"--ro-bind-try", "--bind-fd", "--ro-bind-fd", "--file", "--bind-data", "--ro-bind-data",
|
||||
"--symlink", "--chmod",
|
||||
}
|
||||
THREE_ARG = {"--overlay"}
|
||||
|
||||
|
||||
def read_fd(fd):
|
||||
chunks = []
|
||||
while True:
|
||||
b = os.read(fd, 65536)
|
||||
if not b:
|
||||
break
|
||||
chunks.append(b)
|
||||
os.close(fd)
|
||||
return b"".join(chunks)
|
||||
|
||||
|
||||
def fd_path(fd):
|
||||
return os.readlink("/proc/self/fd/%d" % fd)
|
||||
|
||||
|
||||
def expand(argv):
|
||||
"""argv with every --args FD replaced by the NUL-separated arguments read from FD."""
|
||||
out = []
|
||||
i = 0
|
||||
while i < len(argv):
|
||||
a = argv[i]
|
||||
if a == "--args" and i + 1 < len(argv):
|
||||
data = read_fd(int(argv[i + 1]))
|
||||
# Each argument ends in a NUL; an empty one (--setenv LD_LIBRARY_PATH "") is kept.
|
||||
parts = data.split(b"\0")
|
||||
if parts and parts[-1] == b"":
|
||||
parts.pop()
|
||||
out.extend(x.decode("utf-8", "surrogateescape") for x in parts)
|
||||
i += 2
|
||||
continue
|
||||
out.append(a)
|
||||
i += 1
|
||||
return out
|
||||
|
||||
|
||||
class Spec:
|
||||
def __init__(self):
|
||||
self.ops = []
|
||||
self.env = dict(os.environ)
|
||||
self.cwd = None
|
||||
self.info_fds = []
|
||||
self.json_fds = []
|
||||
self.keep_fds = []
|
||||
self.command = []
|
||||
self.argv0 = None
|
||||
|
||||
|
||||
def parse(argv):
|
||||
s = Spec()
|
||||
perms = None
|
||||
i = 0
|
||||
while i < len(argv):
|
||||
a = argv[i]
|
||||
if a == "--":
|
||||
s.command = argv[i + 1:]
|
||||
break
|
||||
if not a.startswith("--"):
|
||||
s.command = argv[i:]
|
||||
break
|
||||
if a in ("--version",):
|
||||
print(VERSION)
|
||||
sys.exit(0)
|
||||
if a == "--help":
|
||||
print("usage: bwrap [OPTIONS...] [--] COMMAND [ARGS...]")
|
||||
sys.exit(0)
|
||||
n = 0 if a in NO_ARG else 1 if a in ONE_ARG else 2 if a in TWO_ARG else 3 if a in THREE_ARG else -1
|
||||
if n < 0:
|
||||
sys.stderr.write("bwrap: Unknown option %s\n" % a)
|
||||
sys.exit(1)
|
||||
args = argv[i + 1:i + 1 + n]
|
||||
if len(args) < n:
|
||||
sys.stderr.write("bwrap: %s takes %d arguments\n" % (a, n))
|
||||
sys.exit(1)
|
||||
i += 1 + n
|
||||
op_perms, perms = perms, None
|
||||
if a == "--perms":
|
||||
perms = int(args[0], 8)
|
||||
elif a == "--clearenv":
|
||||
s.env = {"PWD": os.getcwd()} if "PWD" in os.environ else {}
|
||||
elif a == "--setenv":
|
||||
s.env[args[0]] = args[1]
|
||||
elif a == "--unsetenv":
|
||||
s.env.pop(args[0], None)
|
||||
elif a == "--chdir":
|
||||
s.cwd = args[0]
|
||||
elif a == "--argv0":
|
||||
s.argv0 = args[0]
|
||||
elif a in ("--bind", "--bind-try", "--dev-bind", "--dev-bind-try", "--ro-bind", "--ro-bind-try"):
|
||||
s.ops.append({"t": "bind", "src": args[0], "dest": args[1], "try": a.endswith("-try"),
|
||||
"ro": a.startswith("--ro")})
|
||||
elif a in ("--bind-fd", "--ro-bind-fd"):
|
||||
fd = int(args[0])
|
||||
s.ops.append({"t": "bind", "src": fd_path(fd), "dest": args[1], "try": False,
|
||||
"ro": a.startswith("--ro")})
|
||||
os.close(fd)
|
||||
elif a in ("--file", "--bind-data", "--ro-bind-data"):
|
||||
s.ops.append({"t": "file", "dest": args[1], "data": read_fd(int(args[0])),
|
||||
"perms": op_perms if op_perms is not None else 0o666 if a == "--file" else 0o600})
|
||||
elif a == "--symlink":
|
||||
s.ops.append({"t": "symlink", "target": args[0], "dest": args[1]})
|
||||
elif a == "--dir":
|
||||
s.ops.append({"t": "dir", "dest": args[0], "perms": op_perms if op_perms is not None else 0o755})
|
||||
elif a in ("--tmpfs", "--mqueue"):
|
||||
s.ops.append({"t": "dir", "dest": args[0], "perms": op_perms if op_perms is not None else 0o755,
|
||||
"empty": True})
|
||||
elif a == "--proc":
|
||||
s.ops.append({"t": "proc", "dest": args[0]})
|
||||
elif a == "--dev":
|
||||
s.ops.append({"t": "dev", "dest": args[0]})
|
||||
elif a in ("--seccomp", "--add-seccomp-fd"):
|
||||
# proot is the sandbox; there is no filter to install under it.
|
||||
os.close(int(args[0]))
|
||||
elif a == "--block-fd":
|
||||
read_fd(int(args[0]))
|
||||
elif a == "--sync-fd":
|
||||
s.keep_fds.append(int(args[0]))
|
||||
elif a == "--info-fd":
|
||||
s.info_fds.append(int(args[0]))
|
||||
elif a == "--json-status-fd":
|
||||
s.json_fds.append(int(args[0]))
|
||||
# Everything else - namespaces, capabilities, labels, locks, --remount-ro, --chmod - has
|
||||
# nothing to act on under proot.
|
||||
if not s.command:
|
||||
sys.stderr.write("bwrap: No command specified\n")
|
||||
sys.exit(1)
|
||||
return s
|
||||
|
||||
|
||||
def real(path):
|
||||
try:
|
||||
return os.path.realpath(path)
|
||||
except OSError:
|
||||
return path
|
||||
|
||||
|
||||
def remaps_nothing(s):
|
||||
"""True when every path in the sandbox is what it is out here already."""
|
||||
for op in s.ops:
|
||||
t = op["t"]
|
||||
if t == "bind":
|
||||
src = real(op["src"])
|
||||
if not os.path.exists(src):
|
||||
if op["try"]:
|
||||
continue
|
||||
return False
|
||||
if src != real(op["dest"]) and not (op["src"] == "/" and op["dest"] == "/"):
|
||||
return False
|
||||
elif t == "symlink":
|
||||
if not os.path.islink(op["dest"]) or os.readlink(op["dest"]) != op["target"]:
|
||||
return False
|
||||
elif t == "file":
|
||||
# The D-Bus proxy's /.flatpak-info names the app to the bus; nothing here reads it.
|
||||
if op["dest"] != "/.flatpak-info":
|
||||
return False
|
||||
elif t == "dir":
|
||||
if not os.path.isdir(op["dest"]) or op.get("empty") and op["dest"] not in ("/tmp", "/var/tmp", "/run"):
|
||||
return False
|
||||
elif t in ("proc", "dev"):
|
||||
if op["dest"] != "/" + t:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def report_pid(s, pid):
|
||||
for fd in s.info_fds:
|
||||
try:
|
||||
os.write(fd, json.dumps({"child-pid": pid}).encode() + b"\n")
|
||||
os.close(fd)
|
||||
except OSError:
|
||||
pass
|
||||
for fd in s.json_fds:
|
||||
try:
|
||||
os.write(fd, json.dumps({"child-pid": pid}).encode() + b"\n")
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def run_here(s):
|
||||
report_pid(s, os.getpid())
|
||||
for fd in s.json_fds:
|
||||
os.close(fd)
|
||||
if s.cwd:
|
||||
os.chdir(s.cwd)
|
||||
argv = list(s.command)
|
||||
program = argv[0]
|
||||
if s.argv0:
|
||||
argv[0] = s.argv0
|
||||
try:
|
||||
os.execvpe(program, argv, s.env)
|
||||
except OSError as e:
|
||||
sys.stderr.write("bwrap: execvp %s: %s\n" % (program, e.strerror))
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def send(sock, kind, payload):
|
||||
sock.sendall(kind + struct.pack(">I", len(payload)) + payload)
|
||||
|
||||
|
||||
def recv_exact(sock, n):
|
||||
buf = b""
|
||||
while len(buf) < n:
|
||||
b = sock.recv(n - len(buf))
|
||||
if not b:
|
||||
return None
|
||||
buf += b
|
||||
return buf
|
||||
|
||||
|
||||
def run_by_app(s):
|
||||
ops = []
|
||||
for op in s.ops:
|
||||
op = dict(op)
|
||||
if op["t"] == "bind":
|
||||
# The app resolves no guest symlinks; hand it the path they lead to.
|
||||
op["src"] = real(op["src"])
|
||||
if op["t"] == "file":
|
||||
op["data"] = base64.b64encode(op["data"]).decode()
|
||||
ops.append(op)
|
||||
argv = list(s.command)
|
||||
if s.argv0:
|
||||
argv[0] = s.argv0
|
||||
request = {"argv": argv, "env": s.env, "cwd": s.cwd or "/", "ops": ops}
|
||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
try:
|
||||
sock.connect(SOCKET)
|
||||
except OSError as e:
|
||||
sys.stderr.write("bwrap: DroidDeck is not answering (%s); open DroidDeck and try again\n" % e)
|
||||
sys.exit(1)
|
||||
send(sock, b"R", json.dumps(request).encode())
|
||||
# This process is the sandbox as far as Flatpak is concerned: ended, it takes the sandbox down.
|
||||
for sig in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP):
|
||||
signal.signal(sig, lambda *_: os._exit(128 + 15))
|
||||
status = 1
|
||||
out = sys.stdout.buffer
|
||||
while True:
|
||||
head = recv_exact(sock, 5)
|
||||
if head is None:
|
||||
sys.stderr.write("bwrap: DroidDeck closed the sandbox\n")
|
||||
break
|
||||
kind, size = head[:1], struct.unpack(">I", head[1:])[0]
|
||||
payload = recv_exact(sock, size) if size else b""
|
||||
if payload is None:
|
||||
break
|
||||
if kind == b"P":
|
||||
report_pid(s, int(payload))
|
||||
elif kind == b"O":
|
||||
out.write(payload)
|
||||
out.flush()
|
||||
elif kind == b"E":
|
||||
sys.stderr.write("bwrap: %s\n" % payload.decode("utf-8", "replace"))
|
||||
elif kind == b"X":
|
||||
status = int(payload)
|
||||
break
|
||||
for fd in s.json_fds:
|
||||
try:
|
||||
os.write(fd, json.dumps({"exit-code": status}).encode() + b"\n")
|
||||
except OSError:
|
||||
pass
|
||||
sys.exit(status)
|
||||
|
||||
|
||||
def main():
|
||||
s = parse(expand(sys.argv[1:]))
|
||||
if remaps_nothing(s):
|
||||
run_here(s)
|
||||
else:
|
||||
run_by_app(s)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,186 @@
|
||||
#!/usr/bin/python3
|
||||
# The app's store talks to Flatpak through this: one JSON object per line on stdout, so progress
|
||||
# reaches the front end as numbers rather than a terminal's redrawn bar. Per-user installation,
|
||||
# Flathub as the remote, this device's architecture.
|
||||
#
|
||||
# bannerlator-flatpak install <app-id> the app and whatever runtimes it needs
|
||||
# bannerlator-flatpak uninstall <app-id> the app, then runtimes nothing uses any more
|
||||
# bannerlator-flatpak update [<app-id>] one app, or everything with an update
|
||||
# bannerlator-flatpak updates what has one (a "updates" line)
|
||||
#
|
||||
# Lines: {"e":"op","i":1,"n":3,"kind":"install","ref":"..."} as each step starts,
|
||||
# {"e":"progress","i":1,"n":3,"percent":45,"status":"..."} while it runs,
|
||||
# {"e":"error","message":"..."} and {"e":"done"} at the end. Exit status 0 on success.
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
os.environ.setdefault("FLATPAK_BWRAP", "/usr/local/bin/bannerlator-bwrap")
|
||||
|
||||
import gi # noqa: E402
|
||||
|
||||
gi.require_version("Flatpak", "1.0")
|
||||
from gi.repository import Flatpak, GLib # noqa: E402
|
||||
|
||||
REMOTE = "flathub"
|
||||
|
||||
|
||||
def emit(**kw):
|
||||
sys.stdout.write(json.dumps(kw) + "\n")
|
||||
sys.stdout.flush()
|
||||
|
||||
|
||||
def installation():
|
||||
return Flatpak.Installation.new_user(None)
|
||||
|
||||
|
||||
def app_ref(inst, app_id):
|
||||
"""The installed ref of an app, or None."""
|
||||
for ref in inst.list_installed_refs_by_kind(Flatpak.RefKind.APP, None):
|
||||
if ref.get_name() == app_id:
|
||||
return ref
|
||||
return None
|
||||
|
||||
|
||||
def transaction(inst):
|
||||
t = Flatpak.Transaction.new_for_installation(inst, None)
|
||||
t.set_no_interaction(True)
|
||||
state = {"i": 0, "n": 0, "error": None}
|
||||
|
||||
def on_ready(tr):
|
||||
state["n"] = len(tr.get_operations())
|
||||
return True
|
||||
|
||||
def on_new_operation(tr, op, progress):
|
||||
state["i"] += 1
|
||||
emit(e="op", i=state["i"], n=state["n"], kind=op.get_operation_type().value_nick, ref=op.get_ref())
|
||||
progress.set_update_frequency(500)
|
||||
|
||||
def changed(p):
|
||||
emit(e="progress", i=state["i"], n=state["n"], percent=p.get_progress(), status=p.get_status() or "")
|
||||
progress.connect("changed", changed)
|
||||
|
||||
def on_operation_error(tr, op, err, details):
|
||||
# A runtime extension that fails to install (a missing translation, an unneeded GL
|
||||
# driver) is not worth losing the app over; anything else ends the transaction.
|
||||
non_fatal = bool(details & Flatpak.TransactionErrorDetails.NON_FATAL)
|
||||
emit(e="warning" if non_fatal else "error", message="%s: %s" % (op.get_ref(), err.message))
|
||||
if not non_fatal:
|
||||
state["error"] = err.message
|
||||
return non_fatal
|
||||
|
||||
t.connect("ready", on_ready)
|
||||
t.connect("new-operation", on_new_operation)
|
||||
t.connect("operation-error", on_operation_error)
|
||||
# A dependency found on more than one remote comes from the first (Flathub, the only one).
|
||||
t.connect("choose-remote-for-ref", lambda tr, ref, runtime_ref, remotes: 0)
|
||||
t.connect("end-of-lifed-with-rebase", lambda *a: False)
|
||||
t.connect("add-new-remote", lambda *a: False)
|
||||
return t, state
|
||||
|
||||
|
||||
def run(t, state):
|
||||
try:
|
||||
t.run(None)
|
||||
except GLib.Error as err:
|
||||
if state["error"] is None and not err.matches(Flatpak.error_quark(), Flatpak.Error.ABORTED):
|
||||
state["error"] = err.message
|
||||
if state["error"]:
|
||||
emit(e="error", message=state["error"])
|
||||
return 1
|
||||
emit(e="done")
|
||||
return 0
|
||||
|
||||
|
||||
def install(app_id):
|
||||
inst = installation()
|
||||
arch = Flatpak.get_default_arch()
|
||||
if app_ref(inst, app_id) is not None:
|
||||
emit(e="done", note="already installed")
|
||||
return 0
|
||||
try:
|
||||
remote_ref = inst.fetch_remote_ref_sync(REMOTE, Flatpak.RefKind.APP, app_id, arch, None, None)
|
||||
branch = remote_ref.get_branch()
|
||||
except GLib.Error:
|
||||
emit(e="error", message="%s is not on Flathub for %s" % (app_id, arch))
|
||||
return 1
|
||||
t, state = transaction(inst)
|
||||
t.add_install(REMOTE, "app/%s/%s/%s" % (app_id, arch, branch), None)
|
||||
return run(t, state)
|
||||
|
||||
|
||||
def uninstall(app_id):
|
||||
inst = installation()
|
||||
ref = app_ref(inst, app_id)
|
||||
if ref is None:
|
||||
emit(e="done", note="not installed")
|
||||
return 0
|
||||
t, state = transaction(inst)
|
||||
t.add_uninstall(ref.format_ref())
|
||||
rc = run(t, state)
|
||||
if rc == 0:
|
||||
# Runtimes only this app used; Flatpak's own `uninstall --unused`.
|
||||
try:
|
||||
unused = inst.list_unused_refs(None, None)
|
||||
except GLib.Error:
|
||||
unused = []
|
||||
if unused:
|
||||
t, state = transaction(inst)
|
||||
for r in unused:
|
||||
t.add_uninstall(r.format_ref())
|
||||
run(t, state)
|
||||
return rc
|
||||
|
||||
|
||||
def updatable(inst):
|
||||
try:
|
||||
return inst.list_installed_refs_for_update(None)
|
||||
except GLib.Error as err:
|
||||
emit(e="error", message=err.message)
|
||||
return None
|
||||
|
||||
|
||||
def update(app_id=None):
|
||||
inst = installation()
|
||||
refs = updatable(inst)
|
||||
if refs is None:
|
||||
return 1
|
||||
if app_id:
|
||||
refs = [r for r in refs if r.get_name() == app_id or r.get_kind() == Flatpak.RefKind.RUNTIME]
|
||||
if not refs:
|
||||
emit(e="done", note="up to date")
|
||||
return 0
|
||||
t, state = transaction(inst)
|
||||
for r in refs:
|
||||
t.add_update(r.format_ref(), None, None)
|
||||
return run(t, state)
|
||||
|
||||
|
||||
def updates():
|
||||
refs = updatable(installation())
|
||||
if refs is None:
|
||||
return 1
|
||||
emit(e="updates", apps=sorted({r.get_name() for r in refs if r.get_kind() == Flatpak.RefKind.APP}),
|
||||
runtimes=len([r for r in refs if r.get_kind() == Flatpak.RefKind.RUNTIME]))
|
||||
return 0
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
sys.stderr.write(__doc__ or "usage: bannerlator-flatpak install|uninstall|update|updates [app-id]\n")
|
||||
return 2
|
||||
cmd, args = sys.argv[1], sys.argv[2:]
|
||||
if cmd == "install" and args:
|
||||
return install(args[0])
|
||||
if cmd == "uninstall" and args:
|
||||
return uninstall(args[0])
|
||||
if cmd == "update":
|
||||
return update(args[0] if args else None)
|
||||
if cmd == "updates":
|
||||
return updates()
|
||||
sys.stderr.write("usage: bannerlator-flatpak install|uninstall|update|updates [app-id]\n")
|
||||
return 2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
# A Flatpak app from the front end, full screen under gamescope like an emulator:
|
||||
# bannerlator-flatpak-run <app-id> [args...]
|
||||
set -u
|
||||
export FLATPAK_BWRAP=/usr/local/bin/bannerlator-bwrap
|
||||
case ":${XDG_DATA_DIRS:-}:" in
|
||||
*"/flatpak/exports/share:"*) ;;
|
||||
*) export XDG_DATA_DIRS=$HOME/.local/share/flatpak/exports/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share} ;;
|
||||
esac
|
||||
# gamescope manages X windows. Flatpak would hand an app the app's own Wayland socket instead (it
|
||||
# finds wayland-0 in the runtime directory even with WAYLAND_DISPLAY unset), and the window would
|
||||
# open behind gamescope - or not at all, with SDL told to use X11. So under gamescope: X11 only.
|
||||
socket=()
|
||||
if [ -n "${BL_INSIDE:-}" ]; then
|
||||
unset WAYLAND_DISPLAY
|
||||
socket=(--nosocket=wayland --socket=x11)
|
||||
fi
|
||||
echo "== flatpak run ${socket[*]} $*"
|
||||
# Apps expect a session bus (settings, portals, single-instance checks); a run mode has none.
|
||||
if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ] && command -v dbus-run-session >/dev/null 2>&1; then
|
||||
exec dbus-run-session -- flatpak run "${socket[@]}" "$@"
|
||||
fi
|
||||
exec flatpak run "${socket[@]}" "$@"
|
||||
@@ -0,0 +1,83 @@
|
||||
#!/bin/bash
|
||||
# Puts Flatpak into the runtime, with Flathub as a per-user remote. Run by the app under proot's
|
||||
# fake root (pacman and pacman-key refuse any other uid), outside a session.
|
||||
#
|
||||
# The runtime's pacman database lists only the base image: the desktop and emulator packages were
|
||||
# unpacked over it without registering. Asked for flatpak the usual way, pacman "reinstalls" about
|
||||
# a hundred of those - Mesa among them, over the runtime's own Turnip build with its KGSL backend -
|
||||
# and then runs every hook in the image, mkinitcpio included. So pacman only fetches and checks the
|
||||
# few packages Flatpak needs on top of this image, verifying each against the Arch Linux ARM
|
||||
# keyring, and they are unpacked here without hooks or scripts, like the desktop package itself.
|
||||
#
|
||||
# bannerlator-flatpak-setup install or repair; prints "== STEP ..." milestones
|
||||
set -u
|
||||
export LANG=C.UTF-8
|
||||
step() { echo "== STEP $*"; }
|
||||
fail() { echo "== FAIL $*"; exit 1; }
|
||||
|
||||
# Flatpak, the libraries it and appstream add to this image, and the D-Bus proxy flatpak run puts
|
||||
# between an app and the session bus. bubblewrap is not among them: Android gives an app no user
|
||||
# namespaces, and bannerlator-bwrap stands in for it (FLATPAK_BWRAP).
|
||||
PACKAGES="flatpak ostree appstream libxmlb libmalcontent libfyaml accountsservice composefs xdg-dbus-proxy"
|
||||
WORK=/var/cache/droiddeck-flatpak
|
||||
CONF=$WORK/pacman.conf
|
||||
mkdir -p "$WORK/pkg" "$WORK/db" || fail "cannot create $WORK"
|
||||
|
||||
# The image's pacman.conf names a download user this rootfs does not have (only root exists) and a
|
||||
# sandbox proot cannot set up. Its one mirror is plain HTTP; these serve the same repos over TLS.
|
||||
{ for m in https://ca.us.mirror.archlinuxarm.org https://fl.us.mirror.archlinuxarm.org \
|
||||
https://de3.mirror.archlinuxarm.org https://nl.mirror.archlinuxarm.org; do
|
||||
echo "Server = $m/\$arch/\$repo"; done
|
||||
grep '^Server' /etc/pacman.d/mirrorlist 2>/dev/null; } > "$WORK/mirrorlist"
|
||||
sed -e 's/^DownloadUser/#DownloadUser/' \
|
||||
-e 's/^\[options\]/[options]\nDisableSandbox/' \
|
||||
-e "s#^Include *= */etc/pacman.d/mirrorlist#Include = $WORK/mirrorlist#" \
|
||||
/etc/pacman.conf > "$CONF" || fail "no /etc/pacman.conf"
|
||||
|
||||
if [ ! -f /etc/pacman.d/gnupg/pubring.gpg ] && [ ! -f /etc/pacman.d/gnupg/pubring.kbx ]; then
|
||||
step "Preparing the package keyring"
|
||||
pacman-key --init >/dev/null 2>&1 || fail "pacman-key --init failed"
|
||||
pacman-key --populate archlinuxarm >/dev/null 2>&1 || fail "pacman-key --populate failed"
|
||||
fi
|
||||
|
||||
# A database of our own, so the image's stays exactly as it was built: -Sy on it would leave the
|
||||
# rest of the image a partial upgrade behind the repos.
|
||||
step "Reading the package lists"
|
||||
pacman --config "$CONF" --dbpath "$WORK/db" --noconfirm -Sy >/dev/null 2>&1 \
|
||||
|| pacman --config "$CONF" --dbpath "$WORK/db" --noconfirm -Sy 2>&1 | tail -3 \
|
||||
|| fail "could not reach the Arch Linux ARM mirrors"
|
||||
[ -f "$WORK/db/sync/extra.db" ] || fail "could not reach the Arch Linux ARM mirrors"
|
||||
|
||||
step "Downloading Flatpak"
|
||||
# -w only downloads; pacman still checks each package's signature against the keyring. -dd: the
|
||||
# dependencies are already in the image (and checked below by what the binaries actually link).
|
||||
out=$(pacman --config "$CONF" --dbpath "$WORK/db" --cachedir "$WORK/pkg" --noconfirm -Sw -dd $PACKAGES 2>&1)
|
||||
rc=$?
|
||||
echo "$out" | grep -vE '^\s*$' | tail -4
|
||||
[ $rc -eq 0 ] || fail "download failed"
|
||||
|
||||
step "Installing Flatpak"
|
||||
for name in $PACKAGES; do
|
||||
ver=$(pacman --config "$CONF" --dbpath "$WORK/db" -Sp --print-format '%v' -dd "$name" 2>/dev/null | tail -1)
|
||||
file=$(ls "$WORK/pkg/$name-$ver-"*.pkg.tar.* 2>/dev/null | grep -v '\.sig$' | head -1)
|
||||
[ -n "$file" ] || fail "no package file for $name $ver"
|
||||
bsdtar -xpf "$file" -C / --exclude .PKGINFO --exclude .BUILDINFO --exclude .MTREE --exclude .INSTALL \
|
||||
|| fail "could not unpack $name"
|
||||
echo "$name $ver" >> "$WORK/installed.new"
|
||||
done
|
||||
mv -f "$WORK/installed.new" "$WORK/installed"
|
||||
# Only the newest file of each package is worth keeping for a repair.
|
||||
find "$WORK/pkg" -type f -mtime +1 -delete 2>/dev/null
|
||||
|
||||
# What the binaries link is the real dependency list: anything this image lacks shows here.
|
||||
missing=$(for f in /usr/bin/flatpak /usr/lib/libflatpak.so.0 /usr/lib/libostree-1.so.1 /usr/bin/xdg-dbus-proxy; do
|
||||
ldd "$f" 2>&1 | grep -E 'not found|version .* not found'
|
||||
done | sort -u)
|
||||
[ -z "$missing" ] || { echo "$missing"; fail "Flatpak is missing libraries"; }
|
||||
flatpak --version >/dev/null 2>&1 || fail "flatpak does not start"
|
||||
|
||||
step "Adding Flathub"
|
||||
export FLATPAK_BWRAP=/usr/local/bin/bannerlator-bwrap
|
||||
flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo 2>&1 \
|
||||
|| fail "could not add the Flathub remote"
|
||||
echo "== DONE $(flatpak --version)"
|
||||
@@ -81,6 +81,10 @@ export XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
|
||||
export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
|
||||
export XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}
|
||||
mkdir -p "$XDG_DATA_HOME" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME"
|
||||
# Flatpak (the store's apps): its sandboxes through the app instead of bubblewrap, which needs the
|
||||
# user namespaces Android keeps from apps, and its apps' menu entries and icons on the desktop.
|
||||
export FLATPAK_BWRAP=/usr/local/bin/bannerlator-bwrap
|
||||
export XDG_DATA_DIRS=$XDG_DATA_HOME/flatpak/exports/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
|
||||
|
||||
# The cores a program runs on outside Steam (the desktop and everything started from it, a
|
||||
# program from the rail): the user's game cores when set (BL_GAME_CPUS), else every core but the
|
||||
|
||||
Reference in new issue
Block a user