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