A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix: gate VM startup on asset extraction and respect resource limits
Commiemusclemommiebb4c897f4ac3
diff --git a/CLAUDE.md b/CLAUDE.mdindex 4520333..ce8c565 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -233,6 +233,8 @@ In `QemuEngine.buildCommand()`: `tcg,thread=multi`, larger `tb-size` for ≥2GB## VM migration / upgrades (how the guest updates without a reset)+Asset extraction readiness fails if any bundled asset cannot be extracted, so `PodroidService` aborts startup rather than booting a mixed asset set. Extraction retries on the next app process launch; temporary-file cleanup targets only known asset destinations. Resource chips keep an existing RAM/storage selection visible when device filtering yields no fitting options, so low-memory devices do not lose their saved configuration.+The guest system layer updates across app versions with **no VM reset and no data loss**, on both backends. The machinery:- **Plain overlay (never re-add metacopy/index/redirect).** `init-podroid` mounts the rootfs overlay as `lowerdir=/mnt/lower,upperdir=/mnt/persist/upper,workdir=/mnt/persist/work` only. Plain overlayfs tolerates a swapped lower, so a new squashfs (re-extracted by `PodroidApplication` on every update) goes live on the next boot while the persistent upper is preserved. **Do not re-add `metacopy=on`/`index=on`/`redirect_dir=on`** - they bind the upper to a specific lower and reintroduce the corruption-on-update bug (the whole reason resets used to be needed).diff --git a/app/src/main/java/com/excp/podroid/PodroidApplication.kt b/app/src/main/java/com/excp/podroid/PodroidApplication.ktindex a0d91db..efd4917 100644--- a/app/src/main/java/com/excp/podroid/PodroidApplication.kt+++ b/app/src/main/java/com/excp/podroid/PodroidApplication.kt@@ -16,10 +16,9 @@ import kotlinx.coroutines.CoroutineScopeimport kotlinx.coroutines.Dispatchersimport kotlinx.coroutines.SupervisorJobimport kotlinx.coroutines.launch+import com.excp.podroid.util.AssetExtractionSupportimport org.lsposed.hiddenapibypass.HiddenApiBypassimport java.io.File-import java.util.concurrent.Executors-import java.util.concurrent.TimeUnit@HiltAndroidAppclass PodroidApplication : Application() {@@ -29,9 +28,9 @@ class PodroidApplication : Application() {// Completion signal for asset extraction. The VM launch path// (PodroidService.launchPodroid) reads the extracted files synchronously,// so it MUST await this before starting the engine — see awaitAssetsReady.- // Completed (never failed) in extractAssets' finally so a waiter can never- // hang even if extraction throws; intactness is enforced by the size-check- // in QemuEngine/AvfEngine's own asset reads, not by this signal.+ // Completes successfully only after every extraction task succeeds. A failed+ // extraction completes exceptionally so the service does not start a VM with+ // missing or partial assets.private val assetsReady = CompletableDeferred<Unit>()override fun onCreate() {@@ -90,10 +89,6 @@ class PodroidApplication : Application() {Log.i(TAG, "asset stamp drift ($previousStamp → $currentStamp) — forcing re-extract")}- // Drop any .tmp files left by a process killed mid-copy so they- // can't accumulate or shadow a fresh atomic write.- deleteStaleTmpFiles(filesDir)-// Fan out the four top-level extractions across a small thread pool.// Disk-write throughput is the bottleneck for the squashfs (~225 MB),// but decompression, asset-FD lookup, and the skip-when-already-@@ -106,30 +101,10 @@ class PodroidApplication : Application() {{ copyAssetIfNeeded("initrd.img", File(filesDir, "initrd.img"), forceCopy) },{ copyAssetIfNeeded("alpine-rootfs.squashfs", File(filesDir, "alpine-rootfs.squashfs"), forceCopy) },)- val pool = Executors.newFixedThreadPool(tasks.size.coerceAtMost(4))- var allSucceeded = true- try {- // invokeAll blocks until every Callable finishes (or times out).- // Each Callable wraps the task so a thrown exception is captured- // in the returned Future rather than killing the worker silently.- val futures = pool.invokeAll(tasks.map { task ->- java.util.concurrent.Callable<Unit> { task() }- })- for (f in futures) {- try { f.get() } catch (e: Exception) {- // copyAssetIfNeeded already logs its own failures and- // rethrows; this catches the propagated exception so- // one failed asset doesn't stop the others.- Log.w(TAG, "Asset extraction task failed", e)- allSucceeded = false- }- }- } finally {- pool.shutdown()- if (!pool.awaitTermination(30, TimeUnit.SECONDS)) {- pool.shutdownNow()- allSucceeded = false- }+ // runTasks waits for every task so one failed asset does not leave+ // the rest of the extraction work or its executor behind.+ val allSucceeded = AssetExtractionSupport.runTasks(tasks) { e ->+ Log.w(TAG, "Asset extraction task failed", e)}// Commit the new stamp ONLY if every extraction task succeeded.@@ -138,29 +113,21 @@ class PodroidApplication : Application() {// current — and because mksquashfs is deterministic the size check// can't catch it either, so a stale rootfs would boot forever. On// failure we leave the stamp stale so the next launch re-extracts.- if (allSucceeded) {- runCatching { stampFile.writeText(currentStamp) }- .onFailure { Log.w(TAG, "Failed to write assets stamp", it) }- } else {- Log.w(TAG, "asset extraction incomplete — leaving stamp stale to force re-extract next launch")- }- } finally {- // Always release waiters — a failed/partial extract is detected by- // the per-file size-check on the next read, not by hanging here.- assetsReady.complete(Unit)- }- }-- /** Recursively removes leftover `<name>.tmp` files under [dir]. */- private fun deleteStaleTmpFiles(dir: File) {- val children = dir.listFiles() ?: return- for (child in children) {- if (child.isDirectory) {- deleteStaleTmpFiles(child)- } else if (child.name.endsWith(TMP_SUFFIX)) {- runCatching { child.delete() }+ if (!allSucceeded) {+ throw IllegalStateException("Asset extraction incomplete")}+ runCatching { stampFile.writeText(currentStamp) }+ .onFailure { Log.w(TAG, "Failed to write assets stamp", it) }+ } catch (e: Throwable) {+ // Always expose a regular exception: PodroidService's existing+ // startup failure branch deliberately rethrows cancellation, while+ // ordinary extraction failures must tear down the service cleanly.+ val failure = IllegalStateException("Asset extraction failed", e)+ Log.e(TAG, "Asset extraction failed", failure)+ assetsReady.completeExceptionally(failure)+ return}+ assetsReady.complete(Unit)}/**@@ -178,6 +145,10 @@ class PodroidApplication : Application() {*/private fun copyAssetIfNeeded(assetPath: String, destFile: File, forceCopy: Boolean) {val assetSize = try { assets.openFd(assetPath).use { it.length } } catch (_: Exception) { -1L }+ // Only inspect the temporary sibling owned by this extraction+ // destination. Never scan filesDir recursively: unrelated app data may+ // legitimately use a .tmp suffix.+ AssetExtractionSupport.deleteStaleTemporaryFile(destFile)if (!forceCopy && destFile.exists() && (assetSize < 0 || destFile.length() == assetSize)) returndestFile.parentFile?.mkdirs()@@ -195,7 +166,9 @@ class PodroidApplication : Application() {* up through this walk to the caller.*/private fun copyAssetDir(assetPath: String, destDir: File, forceCopy: Boolean) {- val entries = assets.list(assetPath) ?: return+ val entries = assets.list(assetPath)+ ?.takeIf { it.isNotEmpty() }+ ?: throw IllegalStateException("Asset directory missing or empty: $assetPath")for (entry in entries) {val src = "$assetPath/$entry"val dest = File(destDir, entry)@@ -214,10 +187,11 @@ class PodroidApplication : Application() {* atomically renames it onto [destFile]. The final canonical path therefore* only ever holds a fully-written file — an async reader (the VM launch)* never sees a half-written squashfs/kernel. Throws on any failure so the- * caller logs it and the stale/missing file is caught by the next size-check.+ * caller logs it and the extraction gate prevents startup with a missing+ * or partial destination.*/private fun copyAssetAtomically(assetPath: String, destFile: File) {- val tmpFile = File(destFile.parentFile, destFile.name + TMP_SUFFIX)+ val tmpFile = AssetExtractionSupport.temporaryFileFor(destFile)try {assets.open(assetPath).use { input ->java.io.FileOutputStream(tmpFile).use { output ->@@ -237,6 +211,5 @@ class PodroidApplication : Application() {companion object {private const val TAG = "PodroidApp"- private const val TMP_SUFFIX = ".tmp"}}diff --git a/app/src/main/java/com/excp/podroid/service/PodroidService.kt b/app/src/main/java/com/excp/podroid/service/PodroidService.ktindex bb6a68c..13f9391 100644--- a/app/src/main/java/com/excp/podroid/service/PodroidService.kt+++ b/app/src/main/java/com/excp/podroid/service/PodroidService.kt@@ -395,7 +395,7 @@ class PodroidService : Service() {} catch (c: CancellationException) {throw c // a stop/teardown cancelled this launch; not a start failure} catch (e: Exception) {- Log.e(TAG, "QEMU failed to start", e)+ Log.e(TAG, "VM failed to start", e)// A Service-side throw here (failed asset extraction, a// snapshot read, or engine.start()) can happen before the// engine state ever leaves Idle. In that window the shutdowndiff --git a/app/src/main/java/com/excp/podroid/ui/components/VmResourceChips.kt b/app/src/main/java/com/excp/podroid/ui/components/VmResourceChips.ktindex 6848be6..d0075b5 100644--- a/app/src/main/java/com/excp/podroid/ui/components/VmResourceChips.kt+++ b/app/src/main/java/com/excp/podroid/ui/components/VmResourceChips.kt@@ -39,9 +39,10 @@ fun VmRamChips(),)val totalRamMb = DeviceResourcePolicy.deviceTotalRamMb(LocalContext.current)- val ramOptions = DeviceResourcePolicy.ramOptionsFor(totalRamMb).let { options ->- if (currentMb in options) options else options + currentMb- }+ val ramOptions = DeviceResourcePolicy.withCurrentOption(+ DeviceResourcePolicy.ramOptionsFor(totalRamMb),+ currentMb,+ )FlowRow(modifier = Modifier.fillMaxWidth(),horizontalArrangement = Arrangement.spacedBy(PodroidTokens.Spacing.SM),@@ -140,9 +141,10 @@ fun VmStorageChips(),)val availableGb = DeviceResourcePolicy.deviceAvailableStorageGb(LocalContext.current)- val storageOptions = DeviceResourcePolicy.storageOptionsFor(availableGb).let { options ->- if (currentGb in options) options else options + currentGb- }+ val storageOptions = DeviceResourcePolicy.withCurrentOption(+ DeviceResourcePolicy.storageOptionsFor(availableGb),+ currentGb,+ )FlowRow(modifier = Modifier.fillMaxWidth(),horizontalArrangement = Arrangement.spacedBy(PodroidTokens.Spacing.SM),@@ -219,7 +221,6 @@ fun VmBandwidthChips(}}-@Composableprivate fun formatRam(mb: Int): String =if (mb >= 1024) "${mb / 1024} GB" else "$mb MB"diff --git a/app/src/main/java/com/excp/podroid/util/AssetExtractionSupport.kt b/app/src/main/java/com/excp/podroid/util/AssetExtractionSupport.ktnew file mode 100644index 0000000..c3e7eea--- /dev/null+++ b/app/src/main/java/com/excp/podroid/util/AssetExtractionSupport.kt@@ -0,0 +1,56 @@+/*+ * Podroid - Rootless Podman for Android+ * Copyright (C) 2024-2026 Podroid contributors+ *+ * Small, platform-independent pieces of the asset extraction pipeline.+ */+package com.excp.podroid.util++import java.io.File+import java.util.concurrent.Callable+import java.util.concurrent.Executors+import java.util.concurrent.TimeUnit++/** Helpers kept separate so extraction failure and cleanup semantics can be unit-tested. */+internal object AssetExtractionSupport {+ private const val TMP_SUFFIX = ".tmp"++ /** Runs every extraction task and reports whether all tasks completed successfully. */+ fun runTasks(tasks: List<() -> Unit>, onTaskFailure: (Exception) -> Unit = {}): Boolean {+ val pool = Executors.newFixedThreadPool(tasks.size.coerceAtMost(4))+ var allSucceeded = true+ try {+ val futures = pool.invokeAll(tasks.map { task ->+ Callable<Unit> { task() }+ })+ for (future in futures) {+ try {+ future.get()+ } catch (e: Exception) {+ onTaskFailure(e)+ allSucceeded = false+ }+ }+ } finally {+ pool.shutdown()+ if (!pool.awaitTermination(30, TimeUnit.SECONDS)) {+ pool.shutdownNow()+ allSucceeded = false+ }+ }+ return allSucceeded+ }++ fun temporaryFileFor(destFile: File): File =+ File(destFile.parentFile, destFile.name + TMP_SUFFIX)++ /** Deletes only the temporary sibling owned by [destFile]. */+ fun deleteStaleTemporaryFile(destFile: File) {+ val temporaryFile = temporaryFileFor(destFile)+ if (temporaryFile.exists() && !temporaryFile.delete()) {+ throw java.io.IOException(+ "stale temporary asset file ${temporaryFile.path} could not be deleted",+ )+ }+ }+}diff --git a/app/src/main/java/com/excp/podroid/util/DeviceResourcePolicy.kt b/app/src/main/java/com/excp/podroid/util/DeviceResourcePolicy.ktindex e128a7f..fc6cc61 100644--- a/app/src/main/java/com/excp/podroid/util/DeviceResourcePolicy.kt+++ b/app/src/main/java/com/excp/podroid/util/DeviceResourcePolicy.kt@@ -38,20 +38,19 @@ object DeviceResourcePolicy {return (stat.availableBytes / (1024L * 1024 * 1024)).toInt()}- /** RAM options that fit the device, leaving ~3 GB for Android. Always at- * least the first three options, so small devices still get a usable list. */+ /** RAM options that fit the device, leaving ~3 GB for Android. */fun ramOptionsFor(totalRamMb: Long): List<Int> {val cap = totalRamMb - 3072- val filtered = RAM_OPTIONS_MB.filter { it <= cap }- return if (filtered.size >= 3) filtered else RAM_OPTIONS_MB.take(3)+ return RAM_OPTIONS_MB.filter { it <= cap }}- /** Storage options that fit the device's free app-private space. Always at- * least the first three options, so small devices still get a usable list. */- fun storageOptionsFor(availableGb: Int): List<Int> {- val filtered = STORAGE_OPTIONS_GB.filter { it <= availableGb }- return if (filtered.size >= 3) filtered else STORAGE_OPTIONS_GB.take(3)- }+ /** Storage options that fit the device's free app-private space. */+ fun storageOptionsFor(availableGb: Int): List<Int> =+ STORAGE_OPTIONS_GB.filter { it <= availableGb }++ /** Keeps an existing setting selectable even when device filtering returns no options. */+ fun withCurrentOption(options: List<Int>, current: Int): List<Int> =+ if (current in options) options else options + currentfun nearestAtMost(options: List<Int>, target: Int): Int =options.filter { it <= target }.maxOrNull() ?: options.first()diff --git a/app/src/test/java/com/excp/podroid/util/AssetExtractionSupportTest.kt b/app/src/test/java/com/excp/podroid/util/AssetExtractionSupportTest.ktnew file mode 100644index 0000000..6d763c3--- /dev/null+++ b/app/src/test/java/com/excp/podroid/util/AssetExtractionSupportTest.kt@@ -0,0 +1,45 @@+package com.excp.podroid.util++import java.io.File+import java.io.IOException+import org.junit.Assert.assertFalse+import org.junit.Assert.assertTrue+import org.junit.Rule+import org.junit.Test+import org.junit.rules.TemporaryFolder++class AssetExtractionSupportTest {+ @get:Rule+ val temporaryFolder = TemporaryFolder()++ @Test+ fun runTasks_reportsExtractionFailureAfterAllTasksFinish() {+ var laterTaskRan = false++ val allSucceeded = AssetExtractionSupport.runTasks(+ listOf(+ { throw IOException("asset read failed") },+ { laterTaskRan = true },+ ),+ )++ assertFalse(allSucceeded)+ assertTrue(laterTaskRan)+ }++ @Test+ fun deleteStaleTemporaryFile_onlyRemovesDestinationSibling() {+ val destination = File(temporaryFolder.root, "assets/vmlinuz-virt").apply {+ parentFile.mkdirs()+ }+ val destinationTemporary = AssetExtractionSupport.temporaryFileFor(destination)+ val unrelatedTemporary = File(temporaryFolder.root, "unrelated.tmp")+ destinationTemporary.writeText("stale")+ unrelatedTemporary.writeText("keep")++ AssetExtractionSupport.deleteStaleTemporaryFile(destination)++ assertFalse(destinationTemporary.exists())+ assertTrue(unrelatedTemporary.exists())+ }+}diff --git a/app/src/test/java/com/excp/podroid/util/DeviceResourcePolicyTest.kt b/app/src/test/java/com/excp/podroid/util/DeviceResourcePolicyTest.ktindex 0fa0e24..fb0cce7 100644--- a/app/src/test/java/com/excp/podroid/util/DeviceResourcePolicyTest.kt+++ b/app/src/test/java/com/excp/podroid/util/DeviceResourcePolicyTest.kt@@ -39,8 +39,7 @@ class DeviceResourcePolicyTest {@Testfun ramOptionsFor_capsToLeaveHeadroomForAndroid() {- // 6 GB device: only options <= 3072 MB fit, but the floor guarantees- // at least the first three options anyway.+ // 6 GB device: only options <= 3072 MB fit.assertEquals(listOf(512, 1024, 2048), DeviceResourcePolicy.ramOptionsFor(6_144))}@@ -50,8 +49,16 @@ class DeviceResourcePolicyTest {}@Test- fun ramOptionsFor_neverDropsBelowThreeOptions() {- assertEquals(listOf(512, 1024, 2048), DeviceResourcePolicy.ramOptionsFor(2_048))+ fun ramOptionsFor_doesNotAdvertiseOptionsAboveCapacity() {+ assertEquals(listOf(512, 1024), DeviceResourcePolicy.ramOptionsFor(4_096))+ assertEquals(emptyList<Int>(), DeviceResourcePolicy.ramOptionsFor(2_048))+ }++ @Test+ fun withCurrentOption_keepsExistingSelectionWhenFilteredOptionsAreEmpty() {+ assertEquals(listOf(512), DeviceResourcePolicy.withCurrentOption(emptyList(), 512))+ assertEquals(listOf(2, 4), DeviceResourcePolicy.withCurrentOption(listOf(2, 4), 4))+ assertEquals(listOf(2, 4, 8), DeviceResourcePolicy.withCurrentOption(listOf(2, 4), 8))}@Test@@ -65,7 +72,8 @@ class DeviceResourcePolicyTest {}@Test- fun storageOptionsFor_neverDropsBelowThreeOptions() {- assertEquals(listOf(2, 4, 8), DeviceResourcePolicy.storageOptionsFor(1))+ fun storageOptionsFor_doesNotAdvertiseOptionsAboveCapacity() {+ assertEquals(listOf(2, 4), DeviceResourcePolicy.storageOptionsFor(5))+ assertEquals(emptyList<Int>(), DeviceResourcePolicy.storageOptionsFor(1))}}