patches and low-level development discussion
 help / color / mirror / code / Atom feed
* [PATCH 1/2] host/rootfs: atomically delete VM directories
@ 2025-12-10 10:55 Alyssa Ross
  2025-12-10 10:55 ` [PATCH 2/2] host/rootfs: prevent VM ID reuse Alyssa Ross
  0 siblings, 1 reply; 2+ messages in thread
From: Alyssa Ross @ 2025-12-10 10:55 UTC (permalink / raw)
  To: devel

Create a hidden empty directory, swap it with the original one, then
remove both.  Teach lsvm to ignore both hidden and empty directories,
and now we no longer have a race where lsvm would get confused if a VM
directory was in the process of being recursively removed when it
looked at it.

Fixes: b51a97f ("host/rootfs: add run-appimage program")
Signed-off-by: Alyssa Ross <hi@alyssa.is>
---
 host/rootfs/image/usr/bin/run-appimage |  7 ++++++-
 host/rootfs/image/usr/bin/run-flatpak  |  7 ++++++-
 tools/lsvm.rs                          | 19 ++++++++++++++-----
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/host/rootfs/image/usr/bin/run-appimage b/host/rootfs/image/usr/bin/run-appimage
index 36f57b85..d61a79c6 100755
--- a/host/rootfs/image/usr/bin/run-appimage
+++ b/host/rootfs/image/usr/bin/run-appimage
@@ -43,4 +43,9 @@ if { s6-instance-delete /run/service/vm-services $id }
 
 if { umount ${dir}/mount } # mount namespace
 if { umount ${dir}/mount } # private bind mount
-rm -r $dir /run/configs/${id}
+
+# Atomically swap the VM directory with an empty one, which will be ignored by
+# lsvm, then remove it, so we never have half a VM directory.
+if { mkdir /run/vm/by-id/.${id} }
+if { exch /run/vm/by-id/.${id} $dir }
+background { rm -rf /run/vm/by-id/.${id} $dir }
diff --git a/host/rootfs/image/usr/bin/run-flatpak b/host/rootfs/image/usr/bin/run-flatpak
index 2ef20433..6be21075 100755
--- a/host/rootfs/image/usr/bin/run-flatpak
+++ b/host/rootfs/image/usr/bin/run-flatpak
@@ -45,4 +45,9 @@ if { s6-instance-delete -- /run/service/vm-services $id }
 
 if { umount ${dir}/mount } # mount namespace
 if { umount ${dir}/mount } # private bind mount
-rm -r $dir /run/configs/${id}
+
+# Atomically swap the VM directory with an empty one, which will be ignored by
+# lsvm, then remove it, so we never have half a VM directory.
+if { mkdir /run/vm/by-id/.${id} }
+if { exch /run/vm/by-id/.${id} $dir }
+background { rm -rf /run/vm/by-id/.${id} $dir }
diff --git a/tools/lsvm.rs b/tools/lsvm.rs
index 8f98e4f6..5d67ac6f 100644
--- a/tools/lsvm.rs
+++ b/tools/lsvm.rs
@@ -81,12 +81,21 @@ fn run() -> Result<(), String> {
 
     for entry in read_dir("/run/vm/by-id").map_err(|e| format!("reading /run/vm/by-id: {e}"))? {
         let entry = entry.map_err(|e| format!("iterating /run/vm/by-id: {e}"))?;
-        if entry
-            .file_type()
-            .map_err(|e| format!("getting type of {:?}: {e}", entry.path()))?
-            .is_dir()
+        if !entry.file_name().as_bytes().starts_with(b".")
+            && entry
+                .file_type()
+                .map_err(|e| format!("getting type of {:?}: {e}", entry.path()))?
+                .is_dir()
         {
-            names.insert(entry.file_name(), Vec::new());
+            let path = entry.path();
+            if path
+                .read_dir()
+                .map_err(|e| format!("reading {path:?}: {e}"))?
+                .next()
+                .is_some()
+            {
+                names.insert(entry.file_name(), Vec::new());
+            }
         }
     }
 
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-12-10 10:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-10 10:55 [PATCH 1/2] host/rootfs: atomically delete VM directories Alyssa Ross
2025-12-10 10:55 ` [PATCH 2/2] host/rootfs: prevent VM ID reuse Alyssa Ross

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).