patches and low-level development discussion
 help / color / mirror / code / Atom feed
From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [PATCH 1/2] host/rootfs: atomically delete VM directories
Date: Wed, 10 Dec 2025 11:55:14 +0100	[thread overview]
Message-ID: <20251210105515.1033548-1-hi@alyssa.is> (raw)

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


             reply	other threads:[~2025-12-10 10:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-10 10:55 Alyssa Ross [this message]
2025-12-10 10:55 ` [PATCH 2/2] host/rootfs: prevent VM ID reuse Alyssa Ross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251210105515.1033548-1-hi@alyssa.is \
    --to=hi@alyssa.is \
    --cc=devel@spectrum-os.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).