Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

fix(avf): distinguish an absent GPU API from a failed declaration

ExTv701e274233dc
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfDiagnostics.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfDiagnostics.kt
index a18c1a9..5fd34d6 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/AvfDiagnostics.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfDiagnostics.kt
@@ -35,6 +35,8 @@ data class AvfReport(
val capabilitiesRaw: Int = 0,
val capabilitiesDecoded: String = "n/a",
val activeBackend: String = "?",
+ val gpuConfigApiPresent: Boolean = false,
+ val lastGpuConfigAttempt: String = "not attempted (no AVF VM started this process)",
) {
fun pretty(): String = buildString {
appendLine("Active backend")
@@ -63,6 +65,10 @@ data class AvfReport(
appendLine()
appendLine("Hypervisor capabilities")
appendLine(" raw = $capabilitiesRaw ($capabilitiesDecoded)")
+ appendLine()
+ appendLine("Headless GPU declaration (net-capable crosvm workaround)")
+ appendLine(" API present = $gpuConfigApiPresent")
+ appendLine(" last attempt = $lastGpuConfigAttempt")
if (smokeTestResult != null) {
appendLine()
appendLine("Smoke test")
@@ -116,6 +122,13 @@ object AvfDiagnostics {
runCatching { AvfReflect.getCapabilities(AvfReflect.manager(context)) }.getOrDefault(0)
} else 0
+ val lastGpuConfigAttempt = when (AvfReflect.lastGpuConfigOutcome.value) {
+ null -> "not attempted (no AVF VM started this process)"
+ AvfReflect.GpuConfigOutcome.ATTACHED -> "attached"
+ AvfReflect.GpuConfigOutcome.UNSUPPORTED -> "unsupported (API absent, benign; full crosvm already in use)"
+ AvfReflect.GpuConfigOutcome.FAILED -> "FAILED (API present, call threw; guest networking may not come up)"
+ }
+
return AvfReport(
featureSupported = featureSupported,
managePermissionGranted = managePermissionGranted,
@@ -127,6 +140,8 @@ object AvfDiagnostics {
smokeTestResult = null,
capabilitiesRaw = capabilitiesRaw,
capabilitiesDecoded = AvfCapabilities.decode(capabilitiesRaw),
+ gpuConfigApiPresent = AvfReflect.gpuConfigApiPresent(),
+ lastGpuConfigAttempt = lastGpuConfigAttempt,
)
}
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
index 01dd6f0..2e7f638 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfEngine.kt
@@ -1027,8 +1027,15 @@ class AvfEngine @Inject constructor(
// Declaring a (headless, display-less) GPU forces virtmgr onto the full
// `crosvm` instead of `crosvm_minimal`, which on Pixel's APEX lacks the
// `net` feature and aborts a networked VM at `--net`. See setGpuConfig.
- val gpuAttached = AvfReflect.setGpuConfig(cb)
- Log.i(TAG, "avf: headless GPU attached=$gpuAttached (forces net-capable crosvm)")
+ when (AvfReflect.setGpuConfig(cb)) {
+ AvfReflect.GpuConfigOutcome.ATTACHED ->
+ Log.i(TAG, "avf: headless GPU attached (forces net-capable crosvm)")
+ AvfReflect.GpuConfigOutcome.UNSUPPORTED ->
+ Log.i(TAG, "avf: GpuConfig API absent on this AVF revision; full crosvm already in use (benign)")
+ AvfReflect.GpuConfigOutcome.FAILED ->
+ Log.w(TAG, "avf: headless GPU declaration FAILED (API present, call threw); " +
+ "virtmgr may pick crosvm_minimal, guest networking may not come up")
+ }
val customCfg = AvfReflect.build(cb)
val vb = AvfReflect.newVmConfigBuilder(context)
diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt
index 1e729c9..d384a5b 100644
--- a/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt
+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt
@@ -13,6 +13,9 @@
package com.excp.podroid.engine.avf
import android.content.Context
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy
@@ -334,6 +337,17 @@ object AvfReflect {
if (!ok) android.util.Log.w("AvfReflect", "no useNetwork/setNetworkSupported on this AVF API; VM may have no network")
}
+ /** Result of the last [setGpuConfig] attempt. See that function for what each predicts. */
+ enum class GpuConfigOutcome { ATTACHED, UNSUPPORTED, FAILED }
+
+ /** Whether the GpuConfig API classes are present on this device, independent of any VM start. */
+ fun gpuConfigApiPresent(): Boolean = GPU_B != null && GPU != null
+
+ private val _lastGpuConfigOutcome = MutableStateFlow<GpuConfigOutcome?>(null)
+
+ /** Outcome of the most recent [setGpuConfig] call; null until one runs in this process. */
+ val lastGpuConfigOutcome: StateFlow<GpuConfigOutcome?> = _lastGpuConfigOutcome.asStateFlow()
+
/**
* Attaches a minimal headless virtio-GPU (backend=2d, surfaceless; NO
* DisplayConfig, so no display surface is required) to the custom-image
@@ -347,24 +361,43 @@ object AvfReflect {
* `net`. Google's own Terminal app never hits this because it always declares
* a GPU; declaring one here routes us onto the same net-capable binary.
*
- * Returns true if a GPU was attached. No-op (returns false) on AVF revisions
- * that predate the GpuConfig API — those use the full crosvm already, so a
- * headless networked VM is unaffected.
+ * Outcomes (never throws):
+ * - [GpuConfigOutcome.ATTACHED] the declaration took effect; this VM gets the
+ * full net-capable crosvm.
+ * - [GpuConfigOutcome.UNSUPPORTED] the GpuConfig API is absent on this AVF
+ * revision. Benign: those revisions run the full crosvm already, so a
+ * headless networked VM is unaffected.
+ * - [GpuConfigOutcome.FAILED] the API exists but the call did not take effect
+ * (a reflective step threw, or `build()` returned null). Predicts
+ * crosvm_minimal routing, i.e. no virtio-net in the guest.
+ *
+ * The result is also published on [lastGpuConfigOutcome] for diagnostics.
*/
- fun setGpuConfig(b: Any): Boolean = runCatching {
- val gpuBuilderCls = GPU_B ?: return false
- val gpuCls = GPU ?: return false
- val gpuB = gpuBuilderCls.getDeclaredConstructor().apply { isAccessible = true }.newInstance()
- invokeDecl(gpuB, "setBackend", String::class.java to "2d")
- // Surfaceless keeps the virtio-GPU off any scanout/window; harmless if the
- // API revision ignores it (wrapped so a missing setter is not fatal).
- runCatching { invokeDecl(gpuB, "setRendererUseSurfaceless", Boolean::class.javaPrimitiveType!! to true) }
- val gpu = invokeDecl(gpuB, "build") ?: return false
- invokeDecl(b, "setGpuConfig", gpuCls to gpu)
- true
- }.getOrElse { e ->
- android.util.Log.w("AvfReflect", "setGpuConfig unavailable on this AVF API (continuing without GPU)", e)
- false
+ fun setGpuConfig(b: Any): GpuConfigOutcome {
+ val outcome = runCatching {
+ val gpuBuilderCls = GPU_B ?: return@runCatching GpuConfigOutcome.UNSUPPORTED
+ val gpuCls = GPU ?: return@runCatching GpuConfigOutcome.UNSUPPORTED
+ val gpuB = gpuBuilderCls.getDeclaredConstructor().apply { isAccessible = true }.newInstance()
+ invokeDecl(gpuB, "setBackend", String::class.java to "2d")
+ // Surfaceless keeps the virtio-GPU off any scanout/window; harmless if the
+ // API revision ignores it (wrapped so a missing setter is not fatal).
+ runCatching { invokeDecl(gpuB, "setRendererUseSurfaceless", Boolean::class.javaPrimitiveType!! to true) }
+ // Past the class lookups the API exists, so a null build() is a real
+ // failure, not an absent API: route it through the FAILED log below.
+ val gpu = invokeDecl(gpuB, "build") ?: error("GpuConfig.Builder.build() returned null")
+ invokeDecl(b, "setGpuConfig", gpuCls to gpu)
+ GpuConfigOutcome.ATTACHED
+ }.getOrElse { e ->
+ android.util.Log.w(
+ "AvfReflect",
+ "setGpuConfig FAILED though the GpuConfig API is present; " +
+ "virtmgr may route this VM to crosvm_minimal (no virtio-net)",
+ e,
+ )
+ GpuConfigOutcome.FAILED
+ }
+ _lastGpuConfigOutcome.value = outcome
+ return outcome
}
/**
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/home/HomeScreen.kt b/app/src/main/java/com/excp/podroid/ui/screens/home/HomeScreen.kt
index 8ea9141..f215527 100644
--- a/app/src/main/java/com/excp/podroid/ui/screens/home/HomeScreen.kt
+++ b/app/src/main/java/com/excp/podroid/ui/screens/home/HomeScreen.kt
@@ -82,6 +82,7 @@ fun HomeScreen(
val showAvfHint by viewModel.showAvfHint.collectAsStateWithLifecycle()
val avfBootFailure by viewModel.avfBootFailure.collectAsStateWithLifecycle()
val avfFailureAdvice by viewModel.avfFailureAdvice.collectAsStateWithLifecycle()
+ val avfNetWorkaroundFailed by viewModel.avfNetWorkaroundFailed.collectAsStateWithLifecycle()
val stopping by viewModel.stopping.collectAsStateWithLifecycle()
val containerCount by viewModel.containerCount.collectAsStateWithLifecycle()
@@ -185,6 +186,7 @@ fun HomeScreen(
containerCount = containerCount,
avfBootFailure = avfBootFailure,
avfFailureAdvice = avfFailureAdvice,
+ avfNetWorkaroundFailed = avfNetWorkaroundFailed,
onUseOneCore = { viewModel.useOneCoreAndRetry() },
onSwitchToQemu = { viewModel.switchToQemuAndRetry() },
onRetry = { viewModel.restartVm() },
@@ -236,6 +238,7 @@ fun HomeScreen(
containerCount = containerCount,
avfBootFailure = avfBootFailure,
avfFailureAdvice = avfFailureAdvice,
+ avfNetWorkaroundFailed = avfNetWorkaroundFailed,
onUseOneCore = { viewModel.useOneCoreAndRetry() },
onSwitchToQemu = { viewModel.switchToQemuAndRetry() },
onRetry = { viewModel.restartVm() },
@@ -313,6 +316,7 @@ private fun HomeStatusBlock(
containerCount: Int? = null,
avfBootFailure: Boolean = false,
avfFailureAdvice: AvfFailureGuidance.Advice = AvfFailureGuidance.Advice.SWITCH_TO_QEMU,
+ avfNetWorkaroundFailed: Boolean = false,
onUseOneCore: () -> Unit = {},
onSwitchToQemu: () -> Unit = {},
onRetry: () -> Unit = {},
@@ -367,6 +371,14 @@ private fun HomeStatusBlock(
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
+ if (avfNetWorkaroundFailed) {
+ Spacer(Modifier.height(PodroidTokens.Spacing.MD))
+ Text(
+ text = stringResource(R.string.avf_net_workaround_failed),
+ style = MaterialTheme.typography.bodyMedium,
+ color = MaterialTheme.colorScheme.error,
+ )
+ }
if (!isRunning && !isStarting && !isStopping) {
Spacer(Modifier.height(PodroidTokens.Spacing.MD))
PodroidSectionLabel(stringResource(R.string.home_containers_created))
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/home/HomeViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/home/HomeViewModel.kt
index be227ef..e5d1978 100644
--- a/app/src/main/java/com/excp/podroid/ui/screens/home/HomeViewModel.kt
+++ b/app/src/main/java/com/excp/podroid/ui/screens/home/HomeViewModel.kt
@@ -151,6 +151,21 @@ class HomeViewModel @Inject constructor(
com.excp.podroid.engine.avf.AvfFailureGuidance.Advice.SWITCH_TO_QEMU,
)
+ /** True while an AVF VM is starting/running and the headless-GPU networking
+ * workaround failed to attach (see [com.excp.podroid.engine.avf.AvfReflect.GpuConfigOutcome.FAILED]),
+ * which predicts virtmgr routing the VM onto crosvm_minimal (no virtio-net).
+ * AvfEngine always requests networking, so every AVF VM is affected.
+ * Combined live (not sampled) so the flag appears as soon as the outcome
+ * is recorded during startup. */
+ val avfNetWorkaroundFailed: StateFlow<Boolean> = combine(
+ vmState,
+ com.excp.podroid.engine.avf.AvfReflect.lastGpuConfigOutcome,
+ ) { state, outcome ->
+ engine.backendId == "avf" &&
+ (state is VmState.Running || state is VmState.Starting) &&
+ outcome == com.excp.podroid.engine.avf.AvfReflect.GpuConfigOutcome.FAILED
+ }.stateIn(viewModelScope, SharingStarted.Eagerly, false)
+
fun useOneCoreAndRetry() {
viewModelScope.launch {
settingsRepository.setVmCpus(1)
diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml
index 200ea1a..83035d5 100644
--- a/app/src/main/res/values-zh/strings.xml
+++ b/app/src/main/res/values-zh/strings.xml
@@ -298,4 +298,5 @@
<string name="avf_action_use_one_core">使用 1 个核心</string>
<string name="avf_action_switch_qemu">切换至 QEMU</string>
<string name="avf_action_retry">重试</string>
+ <string name="avf_net_workaround_failed">此设备上 AVF 联网所需的兼容性设置失败,Linux 系统启动后可能没有网络连接。如果网络不可用,请将后端切换至 QEMU。</string>
</resources>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 3a32b50..40321d8 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -297,4 +297,5 @@
<string name="avf_action_use_one_core">Use 1 core</string>
<string name="avf_action_switch_qemu">Switch to QEMU</string>
<string name="avf_action_retry">Retry</string>
+ <string name="avf_net_workaround_failed">A compatibility step needed for AVF networking failed on this device. Linux may start without network access. If so, switch the backend to QEMU.</string>
</resources>