@@ -72,15 +72,25 @@ impl Vm {
7272
7373 impl Drop for Vm {
7474 fn drop(&mut self) {
75- if let Ok(pid) = fs::read_to_string(paths::vm_sock(&self.name, "pid")) {
75+ reap(&self.name);
76+ }
77+}
78+
79+/// Stops VM `name` and deletes its sockets and overlay. QEMU daemonizes, so a VM outlives a
80+/// daemon that stopped without dropping it, and keeps its overlay locked.
81+pub fn reap(name: &str) {
82+ if let Ok(pid) = fs::read_to_string(paths::vm_sock(name, "pid")) {
83+ // Only the QEMU running this VM: the pid file can outlive it, and its pid be reused.
84+ let (cmd, arg) = (fs::read(format!("/proc/{}/cmdline", pid.trim())).unwrap_or_default(), format!("-name\0{name}\0"));
85+ if cmd.windows(arg.len()).any(|w| w == arg.as_bytes()) {
7686 let _ = Command::new("kill").arg(pid.trim()).status();
7787 std::thread::sleep(Duration::from_secs(1));
7888 }
79- for k in ["qmp", "qga", "vnc", "pid"] {
80- let _ = fs::remove_file(paths::vm_sock(&self.name, k));
81- }
82- let _ = fs::remove_file(&self.disk);
8389 }
90+ for k in ["qmp", "qga", "vnc", "pid", "verbs"] {
91+ let _ = fs::remove_file(paths::vm_sock(name, k));
92+ }
93+ let _ = fs::remove_file(paths::vms().join(format!("{name}.qcow2")));
8494 }
8595
8696 fn ssh_opts() -> Vec<String> {