qlean 0.2.3

A system-level isolation testing library based on QEMU/KVM.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
use std::{
    fs::Permissions,
    os::unix::{fs::PermissionsExt, process::ExitStatusExt},
    path::{Path, PathBuf},
    process::Output,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    time::Duration,
};

use anyhow::{Result, bail};
use nanoid::nanoid;
use russh_sftp::client::fs::{Metadata, ReadDir};
use serde::{Deserialize, Serialize};
use shell_escape::unix::escape;
use tokio_util::sync::CancellationToken;
use tracing::{debug, info};
use walkdir::WalkDir;

use crate::{
    KVM_AVAILABLE,
    image::Image,
    qemu::launch_qemu,
    ssh::{PersistedSshKeypair, Session, connect_ssh, get_ssh_key},
    utils::{CommandExt, HEX_ALPHABET, QleanDirs, gen_random_mac, get_free_cid},
};

pub struct Machine {
    id: String,
    image: MachineImage,
    config: MachineConfig,
    keypair: PersistedSshKeypair,
    /// SSH session
    ssh: Option<Session>,
    cid: u32,
    /// QEMU process ID
    pid: Option<u32>,
    /// Indicates whether QEMU is expected to exit.
    /// Used to differentiate between expected shutdowns and crashes.
    qemu_should_exit: Arc<AtomicBool>,
    /// Cancellation token for SSH operations.
    /// This is used to cancel ongoing SSH operations when qemu exits.
    /// Set when the machine is initialized or spawned, cleared on shutdown.
    ssh_cancel_token: Option<CancellationToken>,
    mac_address: String,
    ip: Option<String>,
}

#[derive(Clone)]
pub struct MachineImage {
    pub overlay: PathBuf,
    pub kernel: PathBuf,
    pub initrd: PathBuf,
    pub seed: PathBuf,
}

#[derive(Clone, Debug)]
pub struct MachineConfig {
    /// Number of CPU cores
    pub core: u32,
    /// Memory in MB
    pub mem: u32,
    /// Disk size in GB (optional)
    pub disk: Option<u32>,
    /// Clear after use
    pub clear: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MetaData {
    #[serde(rename = "instance-id")]
    pub instance_id: String,
    #[serde(rename = "local-hostname")]
    pub local_hostname: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct UserData {
    pub disable_root: bool,
    pub ssh_authorized_keys: Vec<String>,
}

impl Default for MachineConfig {
    fn default() -> Self {
        Self {
            core: 2,
            mem: 4096,
            disk: None,
            clear: true,
        }
    }
}

// Core methods for Machine
impl Machine {
    /// Create a new Machine instance
    pub async fn new(image: &Image, config: &MachineConfig) -> Result<Self> {
        // Prepare run directory
        let dirs = QleanDirs::new()?;
        let machine_id = nanoid!(12, &HEX_ALPHABET);
        let run_dir = Path::new(&dirs.runs).join(&machine_id);
        let seed_dir = run_dir.join("seed");
        tokio::fs::create_dir_all(&run_dir).await?;
        tokio::fs::create_dir_all(&seed_dir).await?;

        // Create overlay image
        let mut qemu_img_command = tokio::process::Command::new("qemu-img");
        qemu_img_command
            .arg("create")
            .arg("-f")
            .arg("qcow2")
            .arg("-b")
            .arg(image.path())
            .arg("-F")
            .arg("qcow2")
            .arg(run_dir.join("overlay.img"));
        debug!(
            "Creating overlay image with command:\n{:?}",
            qemu_img_command.to_string()
        );
        let output = qemu_img_command.output().await?;
        if !output.status.success() {
            bail!(
                "Failed to create overlay image: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }
        let overlay_image = run_dir.join("overlay.img");

        // Generate SSH keypair
        let ssh_keypair = get_ssh_key(&dirs.secrets)?;

        // Get a free CID
        let cid = get_free_cid(&dirs.runs, &run_dir)?;

        // Prepare cloud-init config
        let meta_data = MetaData {
            instance_id: format!("VM-{}", &machine_id),
            local_hostname: "qlean-vm".to_string(),
        };
        let mut meta_data_str = serde_yml::to_string(&meta_data)?;
        meta_data_str.insert_str(0, "#cloud-config\n");
        debug!("Writing cloud-init meta-data:\n{}", meta_data_str);
        tokio::fs::write(seed_dir.join("meta-data"), meta_data_str).await?;
        let user_data = UserData {
            disable_root: false,
            ssh_authorized_keys: vec![ssh_keypair.pubkey_str.clone()],
        };
        let mut user_data_str = serde_yml::to_string(&user_data)?;
        user_data_str.insert_str(0, "#cloud-config\n");
        debug!("Writing cloud-init user-data:\n{}", user_data_str);
        tokio::fs::write(seed_dir.join("user-data"), user_data_str).await?;

        // Prepare seed ISO
        let seed_iso_path = run_dir.join("seed.iso");
        let mut xorriso_command = tokio::process::Command::new("xorriso");
        xorriso_command
            .args(["-as", "mkisofs"])
            .args(["-V", "cidata"])
            .args(["-J", "-R"])
            .args(["-o", seed_iso_path.to_str().unwrap()])
            .arg(seed_dir);
        debug!(
            "Creating seed ISO with command:\n{:?}",
            xorriso_command.to_string()
        );
        let output = xorriso_command.output().await?;
        if !output.status.success() {
            bail!(
                "Failed to create seed ISO: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }

        let machine_image = MachineImage {
            overlay: overlay_image.clone(),
            kernel: image.kernel().to_owned(),
            initrd: image.initrd().to_owned(),
            seed: seed_iso_path,
        };

        Ok(Self {
            id: machine_id,
            image: machine_image,
            config: config.clone(),
            keypair: ssh_keypair,
            ssh: None,
            cid,
            pid: None,
            qemu_should_exit: Arc::new(AtomicBool::new(false)),
            ssh_cancel_token: None,
            mac_address: gen_random_mac(),
            ip: None,
        })
    }

    /// Initialize the machine (first boot)
    pub async fn init(&mut self) -> Result<()> {
        info!("🚀 Initializing VM-{}", self.id);

        // Resize image if needed
        if let Some(resize_gb) = self.config.disk {
            let mut qemu_img_command = tokio::process::Command::new("qemu-img");
            qemu_img_command
                .arg("resize")
                .arg(&self.image.overlay)
                .arg(format!("{}G", resize_gb));
            debug!(
                "Resizing overlay image with command:\n{:?}",
                qemu_img_command.to_string()
            );
            let output = qemu_img_command.output().await?;
            if !output.status.success() {
                bail!(
                    "Failed to resize image: {}",
                    String::from_utf8_lossy(&output.stderr)
                );
            }
        }

        if self.ssh_cancel_token.is_none() {
            self.ssh_cancel_token = Some(CancellationToken::new());
        } else {
            bail!("Machine already initialized");
        }

        self.launch(true).await?;

        Ok(())
    }

    /// Spawn the machine (normal boot)
    pub async fn spawn(&mut self) -> Result<()> {
        info!("Spawning VM-{}", self.id);

        if self.ssh_cancel_token.is_none() {
            self.ssh_cancel_token = Some(CancellationToken::new());
        } else {
            bail!("Machine already spawned");
        }

        self.launch(false).await?;

        Ok(())
    }

    /// Execute a command on the machine and return the output
    pub async fn exec<S: AsRef<str>>(&mut self, cmd: S) -> Result<Output> {
        let cmd_ref = cmd.as_ref();
        info!("🧬 Executing command `{}` on VM-{}", cmd_ref, self.id);
        if let Some(ssh) = self.ssh.as_mut() {
            let cancel_token = self
                .ssh_cancel_token
                .as_ref()
                .expect("Machine not initialized or spawned")
                .clone();

            let (exit_code, stdout, stderr) = ssh.call_with_output(cmd_ref, cancel_token).await?;

            Ok(Output {
                status: std::process::ExitStatus::from_raw(exit_code as i32),
                stdout,
                stderr,
            })
        } else {
            Err(anyhow::anyhow!("SSH session not established"))
        }
    }

    /// Shutdown the machine
    pub async fn shutdown(&mut self) -> Result<()> {
        if let Some(ssh) = self.ssh.as_mut() {
            // Then shut the system down.
            ssh.call(
                "systemctl poweroff",
                self.ssh_cancel_token
                    .as_ref()
                    .expect("Machine not initialized or spawned")
                    .clone(),
            )
            .await?;
            info!("🔌 Shutting down VM-{}", self.id);

            // Tell the QEMU handler it's now fine to wait for exit.
            self.qemu_should_exit.store(true, Ordering::SeqCst);

            // Ignore whatever error we might get from this as we want to close the connection at this
            // point anyway.
            let _ = ssh.close().await;

            // Wait for QEMU process to actually exit
            if let Some(pid) = self.pid {
                debug!("Waiting for QEMU process {} to exit", pid);
                let max_wait_time = Duration::from_secs(30);
                let poll_interval = Duration::from_millis(100);
                let start = std::time::Instant::now();

                loop {
                    // Check if process is still running
                    let process_exists = std::path::Path::new(&format!("/proc/{}", pid)).exists();

                    if !process_exists {
                        debug!("QEMU process {} has exited", pid);
                        break;
                    }

                    if start.elapsed() > max_wait_time {
                        info!(
                            "QEMU process {} did not exit within timeout, force killing",
                            pid
                        );
                        let _ = std::process::Command::new("kill")
                            .arg("-9")
                            .arg(pid.to_string())
                            .output();
                        break;
                    }

                    tokio::time::sleep(poll_interval).await;
                }
            }

            // Clean up runtime files
            let dirs = QleanDirs::new()?;
            let pid_file_path = dirs.runs.join(&self.id).join("qemu.pid");
            let _ = tokio::fs::remove_file(pid_file_path).await;
            self.ssh = None;
            self.pid = None;
            self.ssh_cancel_token = None;
            self.ip = None;

            Ok(())
        } else {
            Err(anyhow::anyhow!("SSH session not established"))
        }
    }

    /// Upload file or directory to the machine
    pub async fn upload<P: AsRef<Path>, Q: AsRef<Path>>(
        &mut self,
        local_path: P,
        remote_path: Q,
    ) -> Result<()> {
        let local_path = local_path.as_ref();
        let remote_path = remote_path.as_ref();
        info!(
            "📤 Uploading {:?} to {:?} on VM-{}",
            local_path, remote_path, self.id
        );
        let (ssh, cancel_token) = self.get_ssh()?;

        // Normalize local path type
        let meta = tokio::fs::metadata(local_path).await?;
        if meta.is_file() {
            // Decide final remote target path (dir vs file path)
            let remote_target = {
                let is_dir = {
                    let sftp = ssh.get_sftp().await?;
                    (sftp.read_dir(remote_path.to_string_lossy()).await).is_ok()
                };
                if is_dir {
                    remote_path.join(local_path.file_name().expect("local_path has no basename"))
                } else {
                    remote_path.to_path_buf()
                }
            };

            // Ensure remote parent directory exists
            if let Some(parent) = remote_target.parent() {
                ssh.create_dir_all(parent).await?;
            }

            // Upload single file
            ssh.upload_file(local_path, &remote_target, cancel_token.clone())
                .await?;
        } else if meta.is_dir() {
            // For directory: mirror into remote_path/<basename>
            let base = local_path
                .file_name()
                .ok_or_else(|| anyhow::anyhow!("local_path has no basename"))?;
            let remote_root = remote_path.join(base);
            ssh.create_dir_all(&remote_root).await?;

            for entry in WalkDir::new(local_path).follow_links(false) {
                let entry = entry?;
                let ty = entry.file_type();

                // Cancellation check
                if cancel_token.is_cancelled() {
                    bail!("Upload cancelled");
                }

                // Relative path under local_path
                let rel = entry
                    .path()
                    .strip_prefix(local_path)
                    .expect("Failed to get relative path");
                let remote_entry = remote_root.join(rel);

                if ty.is_dir() {
                    ssh.create_dir_all(&remote_entry).await?;
                } else if ty.is_file() {
                    if let Some(parent) = remote_entry.parent() {
                        ssh.create_dir_all(parent).await?;
                    }
                    ssh.upload_file(entry.path(), &remote_entry, cancel_token.clone())
                        .await?;
                } else if ty.is_symlink() {
                    // Try to reproduce symlink if possible
                    match tokio::fs::read_link(entry.path()).await {
                        Ok(target) => {
                            // Ensure parent exists
                            if let Some(parent) = remote_entry.parent() {
                                ssh.create_dir_all(parent).await?;
                            }
                            {
                                let sftp = ssh.get_sftp().await?;
                                let _ = sftp
                                    .symlink(
                                        remote_entry.to_string_lossy(),
                                        target.to_string_lossy(),
                                    )
                                    .await; // best-effort
                            }
                        }
                        Err(_) => {
                            // Fallback: ignore or copy as file (we ignore silently)
                        }
                    }
                }
            }
        } else {
            bail!("Unsupported local path type");
        }

        Ok(())
    }

    /// Download file or directory from the machine
    pub async fn download<P: AsRef<Path>, Q: AsRef<Path>>(
        &mut self,
        remote_path: P,
        local_path: Q,
    ) -> Result<()> {
        let remote_path = remote_path.as_ref();
        let local_path = local_path.as_ref();
        info!(
            "📥 Downloading {:?} from VM-{} to {:?}",
            remote_path, self.id, local_path
        );
        let (ssh, cancel_token) = self.get_ssh()?;

        // Check remote path type
        let remote_meta = {
            let sftp = ssh.get_sftp().await?;
            sftp.metadata(remote_path.to_string_lossy())
                .await
                .map_err(|e| anyhow::anyhow!("Failed to stat remote path: {}", e))?
        };

        if !remote_meta.is_dir() {
            // Decide final local target path (dir vs file path)
            let local_target = match tokio::fs::metadata(local_path).await {
                Ok(attr) if attr.is_dir() => local_path.join(
                    remote_path
                        .file_name()
                        .ok_or_else(|| anyhow::anyhow!("remote_path has no basename"))?,
                ),
                _ => local_path.to_path_buf(),
            };

            // Ensure local parent directory exists
            if let Some(parent) = local_target.parent() {
                tokio::fs::create_dir_all(parent).await.map_err(|e| {
                    anyhow::anyhow!("Failed to create local directory {:?}: {}", parent, e)
                })?;
            }

            // Download single file
            ssh.download_file(remote_path, &local_target, cancel_token.clone())
                .await?;
        } else if remote_meta.is_dir() {
            // For directory: mirror into local_path/<basename>
            let base = remote_path
                .file_name()
                .ok_or_else(|| anyhow::anyhow!("remote_path has no basename"))?;
            let local_root = local_path.join(base);
            tokio::fs::create_dir_all(&local_root).await.map_err(|e| {
                anyhow::anyhow!("Failed to create local directory {:?}: {}", local_root, e)
            })?;

            // Use walk_remote_dir for DFS traversal
            let entries = ssh
                .walk_remote_dir(
                    remote_path,
                    /*follow_links=*/ false,
                    cancel_token.clone(),
                )
                .await?;

            for e in entries {
                if cancel_token.is_cancelled() {
                    bail!("Download cancelled");
                }

                // Compute local path relative to remote root
                let rel = match e.path().strip_prefix(remote_path) {
                    Ok(r) => r,
                    Err(_) => continue,
                };
                let local_entry = local_root.join(rel);

                if e.file_type().is_dir() {
                    tokio::fs::create_dir_all(&local_entry).await.map_err(|e| {
                        anyhow::anyhow!("Failed to create local directory {:?}: {}", local_entry, e)
                    })?;
                } else if e.file_type().is_file() {
                    if let Some(parent) = local_entry.parent() {
                        tokio::fs::create_dir_all(parent).await.map_err(|e| {
                            anyhow::anyhow!("Failed to create local directory {:?}: {}", parent, e)
                        })?;
                    }
                    ssh.download_file(e.path(), &local_entry, cancel_token.clone())
                        .await?;
                } else if e.file_type().is_symlink() {
                    // Best-effort: current SFTP attrs may not distinguish symlinks.
                    // Treat as file or skip depending on future capabilities.
                }
            }
        } else {
            bail!("Unsupported remote path type");
        }

        Ok(())
    }

    /// Helper to get SSH session and cancellation token
    fn get_ssh(&mut self) -> Result<(&mut Session, CancellationToken)> {
        let ssh = self
            .ssh
            .as_mut()
            .expect("Machine not initialized or spawned");
        let cancel_token = self
            .ssh_cancel_token
            .as_ref()
            .cloned()
            .expect("Machine not initialized or spawned");
        Ok((ssh, cancel_token))
    }

    /// Get the IP address of the machine
    pub async fn get_ip(&mut self) -> Result<String> {
        if let Some(ip) = &self.ip {
            Ok(ip.to_owned())
        } else {
            let (ssh, _) = self.get_ssh()?;
            let ip = ssh.get_remote_ip().await?;
            self.ip = Some(ip.to_owned());
            Ok(ip)
        }
    }

    /// Check if the machine is currently running
    pub(crate) async fn is_running(&self) -> Result<bool> {
        if let Some(pid) = self.pid {
            let process_exists = std::path::Path::new(&format!("/proc/{}", pid)).exists();
            let process_running = if process_exists {
                // Further check if the process is a QEMU process
                let cmdline_path = format!("/proc/{}/cmdline", pid);
                if let Ok(cmdline) = std::fs::read_to_string(&cmdline_path) {
                    cmdline.contains("qemu-system")
                } else {
                    false
                }
            } else {
                false
            };
            Ok(process_running)
        } else {
            Ok(false)
        }
    }

    /// Launch QEMU and connect SSH concurrently
    async fn launch(&mut self, is_init: bool) -> Result<()> {
        debug!(
            "SSH command for manual debugging:\nssh root@vsock/{} -i {:?}",
            self.cid, self.keypair.privkey_path,
        );

        let qemu_params = crate::qemu::QemuLaunchParams {
            cid: self.cid,
            image: self.image.to_owned(),
            config: self.config.to_owned(),
            vmid: self.id.to_owned(),
            is_init,
            mac_address: self.mac_address.to_owned(),
            cancel_token: self
                .ssh_cancel_token
                .as_ref()
                .expect("Machine not initialized or spawned")
                .clone(),
            expected_to_exit: self.qemu_should_exit.clone(),
        };

        let kvm_available = KVM_AVAILABLE.get().copied().unwrap_or(false);
        let ssh_timeout = if kvm_available {
            Duration::from_secs(60)
        } else {
            // Give more time if KVM is not available
            Duration::from_secs(180)
        };

        let qemu_handle = tokio::spawn(launch_qemu(qemu_params));
        let ssh_handle = tokio::spawn(connect_ssh(
            self.cid,
            ssh_timeout,
            self.keypair.to_owned(),
            self.ssh_cancel_token
                .as_ref()
                .expect("Machine not initialized or spawned")
                .clone(),
        ));

        // Wait for SSH to complete, or abort SSH if QEMU errors
        tokio::select! {
            result = ssh_handle => {
                // SSH completed, QEMU continues running
                match result {
                    Ok(Ok(session)) => {
                        self.ssh = Some(session);
                        let dirs = QleanDirs::new()?;
                        let runs_dir = dirs.runs;
                        let pid_file_path = runs_dir.join(&self.id).join("qemu.pid");
                        let pid_str = tokio::fs::read_to_string(pid_file_path).await?;
                        self.pid = Some(pid_str.trim().parse()?);
                    }
                    Ok(Err(e)) => bail!(e),
                    Err(e) => bail!("SSH task panicked: {}", e),
                }
            }
            result = qemu_handle => {
                // QEMU completed or errored, cancel SSH task
                self.ssh_cancel_token.as_ref().expect("Machine not initialized or spawned").cancel();
                match result {
                    Ok(Err(e)) => bail!(e),
                    Ok(Ok(())) => bail!("QEMU exited unexpectedly"),
                    Err(e) => bail!("QEMU task error: {}", e),
                }
            }
        }

        Ok(())
    }
}

// Filesystem methods for Machine
impl Machine {
    /// Copies the contents of one file to another.
    /// This function will also copy the permission bits of the original file to the destination file.
    pub async fn copy<P: AsRef<Path>, Q: AsRef<Path>>(&mut self, from: P, to: Q) -> Result<()> {
        let from = from.as_ref();
        let to = to.as_ref();
        let (ssh, cancel_token) = self.get_ssh()?;

        // Validate source and destination semantics to mirror std::fs::copy
        {
            let sftp = ssh.get_sftp().await?;
            let src_meta = sftp
                .metadata(from.to_string_lossy())
                .await
                .map_err(|e| anyhow::anyhow!("Failed to stat source: {e}"))?;
            if src_meta.is_dir() {
                bail!("Source is a directory: {:?}", from);
            }

            if let Ok(dst_meta) = sftp.metadata(to.to_string_lossy()).await
                && dst_meta.is_dir()
            {
                bail!("Destination is a directory: {:?}", to);
            }
        }

        // Use cp inside the guest to avoid round-tripping data over SFTP.
        let cmd = format!(
            "cp -p -- {} {}",
            escape(from.to_string_lossy()),
            escape(to.to_string_lossy())
        );
        let (code, _stdout, stderr) = ssh.call_with_output(&cmd, cancel_token).await?;
        if code != 0 {
            bail!(
                "Failed to copy file (exit code {}): {}",
                code,
                String::from_utf8_lossy(&stderr)
            );
        }

        Ok(())
    }

    /// Creates a new, empty directory at the provided path
    pub async fn create_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        sftp.create_dir(path.to_string_lossy()).await?;

        Ok(())
    }

    /// Recursively create a directory and all of its parent components if they are missing.
    pub async fn create_dir_all<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        ssh.create_dir_all(path).await?;

        Ok(())
    }

    /// Returns `Ok(true)` if the path points at an existing entity.
    pub async fn exists<P: AsRef<Path>>(&mut self, path: P) -> Result<bool> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        Ok(sftp.try_exists(path.to_string_lossy()).await?)
    }

    /// Creates a new hard link on the filesystem.
    pub async fn hard_link<P: AsRef<Path>, Q: AsRef<Path>>(
        &mut self,
        original: P,
        link: Q,
    ) -> Result<()> {
        let original = original.as_ref();
        let link = link.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        sftp.hardlink(original.to_string_lossy(), link.to_string_lossy())
            .await?;

        Ok(())
    }

    /// Given a path, queries the file system to get information about a file, directory, etc.
    pub async fn metadata<P: AsRef<Path>>(&mut self, path: P) -> Result<Metadata> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        Ok(sftp.metadata(path.to_string_lossy()).await?)
    }

    /// Reads the entire contents of a file into a bytes vector.
    pub async fn read<P: AsRef<Path>>(&mut self, path: P) -> Result<Vec<u8>> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        Ok(sftp.read(path.to_string_lossy()).await?)
    }

    /// Returns an iterator over the entries within a directory.
    pub async fn read_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<ReadDir> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        Ok(sftp.read_dir(path.to_string_lossy()).await?)
    }

    /// Reads a symbolic link, returning the file that the link points to.
    pub async fn read_link<P: AsRef<Path>>(&mut self, path: P) -> Result<PathBuf> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        Ok(PathBuf::from(sftp.read_link(path.to_string_lossy()).await?))
    }

    /// Reads the entire contents of a file into a string.
    pub async fn read_to_string<P: AsRef<Path>>(&mut self, path: P) -> Result<String> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        let bytes = sftp.read(path.to_string_lossy()).await?;
        Ok(String::from_utf8(bytes)?)
    }

    /// Removes a directory at provided path, after removing all its contents.
    pub async fn remove_dir_all<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        sftp.remove_dir(path.to_string_lossy()).await?;

        Ok(())
    }

    /// Removes a file from the filesystem on VM.
    pub async fn remove_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        sftp.remove_file(path.to_string_lossy()).await?;

        Ok(())
    }

    /// Renames a file or directory to a new name
    pub async fn rename<P: AsRef<Path>, Q: AsRef<Path>>(&mut self, from: P, to: Q) -> Result<()> {
        let from = from.as_ref();
        let to = to.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        sftp.rename(from.to_string_lossy(), to.to_string_lossy())
            .await?;

        Ok(())
    }

    /// Changes the permissions found on a file or a directory.
    pub async fn set_permissions<P: AsRef<Path>>(
        &mut self,
        path: P,
        perm: Permissions,
    ) -> Result<()> {
        let path = path.as_ref();
        let (ssh, _) = self.get_ssh()?;

        {
            let sftp = ssh.get_sftp().await?;
            let mut meta = sftp
                .metadata(path.to_string_lossy())
                .await
                .map_err(|e| anyhow::anyhow!("Failed to stat file: {}", e))?;

            let mode = perm.mode();
            meta.permissions = Some(mode);

            sftp.set_metadata(path.to_string_lossy(), meta).await?;
        }

        Ok(())
    }

    /// Writes a slice as the entire contents of a file.
    ///
    /// This function will create a file if it does not exist, and will entirely replace its contents if it does.
    pub async fn write<P: AsRef<Path>, C: AsRef<[u8]>>(
        &mut self,
        path: P,
        contents: C,
    ) -> Result<()> {
        let path = path.as_ref();
        let contents = contents.as_ref();
        let (ssh, _) = self.get_ssh()?;

        let sftp = ssh.get_sftp().await?;
        let _ = sftp.create(path.to_string_lossy()).await?;
        sftp.write(path.to_string_lossy(), contents).await?;

        Ok(())
    }
}

impl Drop for Machine {
    fn drop(&mut self) {
        // Ensure QEMU process is killed
        if let Some(pid) = self.pid {
            let _ = std::process::Command::new("kill")
                .arg("-9")
                .arg(pid.to_string())
                .output();
        }
        // Clean up runtime files if configured to do so
        if self.config.clear {
            let dirs = QleanDirs::new().expect("Failed to get QleanDirs in Drop");
            let run_dir = dirs.runs.join(&self.id);
            let _ = std::fs::remove_dir_all(run_dir);
        }
    }
}