hvctrl 0.1.0

A hypervisor controller library
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
// Copyright takubokudori.
// This source code is licensed under the MIT or Apache-2.0 license.
//! [VBoxManage](https://www.virtualbox.org/manual/ch08.html) controller.
use crate::{exec_cmd, types::*};
use std::{
    collections::HashMap,
    process::Command,
    time::{Duration, Instant},
};

#[derive(Clone, Debug)]
pub struct VBoxManage {
    executable_path: String,
    vm_name: Option<String>,
    guest_username: Option<String>,
    guest_password: Option<String>,
    guest_password_file: Option<String>,
    guest_domain: Option<String>,
}

impl Default for VBoxManage {
    fn default() -> Self { Self::new() }
}

#[cfg(windows)]
pub const DEFAULT_VBOXMANAGE_PATH: &str =
    r"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe";
#[cfg(not(windows))]
pub const DEFAULT_VBOXMANAGE_PATH: &str = "vboxmanage";

#[cfg(windows)]
const LINE_FEED: &str = "\r\n";
#[cfg(not(windows))]
const LINE_FEED: &str = "\n";

impl VBoxManage {
    pub fn new() -> Self {
        Self {
            executable_path: DEFAULT_VBOXMANAGE_PATH.to_string(),
            vm_name: None,
            guest_username: None,
            guest_password: None,
            guest_password_file: None,
            guest_domain: None,
        }
    }

    impl_setter!(
        /// Sets the path to VBoxManage.
        executable_path: String
    );

    pub fn get_executable_path(&self) -> &str { &self.executable_path }

    /// Sets the VM name to be manipulated.
    pub fn vm_name<T: Into<Option<String>>>(
        &mut self,
        vm_name: T,
    ) -> &mut Self {
        self.vm_name = vm_name.into();
        self
    }

    pub fn get_vm_name(&self) -> Option<&str> { self.vm_name.as_deref() }

    impl_setter!(@opt
    /// Sets the guest username for login.
        guest_username: String
    );

    pub fn get_guest_username(&self) -> Option<&str> {
        self.guest_username.as_deref()
    }

    impl_setter!(@opt
    /// Sets the guest password for login.
        guest_password: String
    );

    pub fn get_guest_password(&self) -> Option<&str> {
        self.guest_password.as_deref()
    }

    impl_setter!(@opt
    /// Sets the absolute path to the guest password file for login.
        guest_password_file: String
    );

    pub fn get_guest_password_file(&self) -> Option<&str> {
        self.guest_password_file.as_deref()
    }

    impl_setter!(@opt
    /// Sets the guest domain for Windows guests.
        guest_domain: String
    );

    pub fn get_guest_domain(&self) -> Option<&str> {
        self.guest_domain.as_deref()
    }

    fn build_auth(&self) -> Vec<&str> {
        let mut v = Vec::with_capacity(8);
        if let Some(x) = &self.guest_username {
            v.extend(&["--username", x]);
        }
        if let Some(x) = &self.guest_password {
            v.extend(&["--password", x]);
        }
        if let Some(x) = &self.guest_password_file {
            v.extend(&["--passwordfile", x]);
        }
        if let Some(x) = &self.guest_domain {
            v.extend(&["--domain", x]);
        }
        v
    }

    #[inline]
    fn handle_error(s: &str) -> VmError {
        use ErrorKind::*;
        use VmPowerState::*;
        starts_err!(s, "Could not find a registered machine named", VmNotFound);
        starts_err!(s, "Could not find a snapshot named ", SnapshotNotFound);
        starts_err!(
            s,
            "This machine does not have any snapshots",
            SnapshotNotFound
        );
        starts_err!(
            s,
            "The specified user was not able to logon on guest",
            GuestAuthenticationFailed
        );
        starts_err!(
            s,
            "Waiting for guest process failed: The guest execution service is \
             not ready (yet)",
            ServiceIsNotRunning
        );
        starts_err!(
            s,
            "Error starting guest session (current status is:",
            ServiceIsNotRunning
        );
        if s.starts_with("FsObjQueryInfo failed on") || s.starts_with("File ") {
            let s = s.lines().last().unwrap();
            return VmError::from(FileError(
                s[s.rfind(':').unwrap() + 2..].to_string(),
            ));
        }

        if let Some(s) = s.strip_prefix("RTPathQueryInfo failed on ") {
            return VmError::from(FileError(s.to_string()));
        }

        if let Some(s) = s.strip_prefix("Invalid machine state: ") {
            starts_err!(s, "PoweredOff", InvalidPowerState(Stopped));
            starts_err!(s, "Paused", InvalidPowerState(Paused));
        }
        if let Some(s) = s.strip_prefix("Machine in invalid state ") {
            starts_err!(s, "1 -- powered off", InvalidPowerState(Stopped));
            starts_err!(s, "2 -- saved", InvalidPowerState(Suspended));
        }
        if s.ends_with(" is not currently running")
            || s.contains("is not running")
        {
            return VmError::from(ErrorKind::InvalidPowerState(NotRunning));
        }
        if s.lines().next().unwrap().ends_with(
            "is already locked by a session (or being locked or unlocked)",
        ) {
            return VmError::from(ErrorKind::InvalidPowerState(Running));
        }
        VmError::from(Repr::Unknown(format!("Unknown error: {}", s)))
    }

    /// Checks `s` UUID.
    pub fn is_uuid(s: &str) -> bool {
        regex::Regex::new(
            r#"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"#
        )
            .unwrap()
            .is_match(s)
    }

    #[inline]
    fn check(s: String) -> VmResult<String> {
        const ERROR_STR: &str = "vboxmanage.exe: error: ";
        if (&s[..ERROR_STR.len()])
            .to_ascii_lowercase()
            .starts_with(ERROR_STR)
        {
            Err(Self::handle_error(&s[ERROR_STR.len()..].trim()))
        } else {
            Ok(s)
        }
    }

    fn exec(cmd: &mut Command) -> VmResult<String> {
        let (stdout, stderr) = exec_cmd(cmd)?;
        if !stderr.is_empty() {
            Self::check(stderr)
        } else {
            Ok(stdout)
        }
    }

    #[inline]
    fn cmd(&self) -> Command { Command::new(&self.executable_path) }

    /// Gets the VBoxManage version.
    pub fn version(&self) -> VmResult<String> {
        Ok(Self::exec(self.cmd().arg("-v"))?.trim().to_string())
    }

    /// Gets a list of VMs.
    pub fn list_vms(&self) -> VmResult<Vec<Vm>> {
        let s = Self::exec(self.cmd().args(&["list", "vms"]))?;
        // "vm name" {uuid}
        Ok(s.lines()
            .map(|x| {
                let v = x.rsplitn(2, ' ').collect::<Vec<&str>>();
                Vm {
                    id: Some(v[0].to_string()),
                    name: Some(v[1][1..v[1].len() - 1].to_string()),
                    path: None,
                }
            })
            .collect())
    }

    pub fn show_vm_info(&self) -> VmResult<String> {
        self.show_vm_info2(&self.get_vm()?)
    }

    pub fn get_os_version(&self) -> VmResult<String> {
        let s = self.show_vm_info()?;
        let hm = Self::parse_info(&s, Some("Guest OS"));
        Ok(hm["ostype"].to_string())
    }

    fn parse_info<'a>(
        s: &'a str,
        stopper: Option<&str>,
    ) -> HashMap<&'a str, &'a str> {
        let l = s.lines();
        let mut hm = HashMap::new();
        for x in l {
            let x: Vec<&str> = x.splitn(2, '=').collect();
            if x.len() != 2 {
                continue;
            }
            let (key, value) = (x[0].trim(), x[1].trim());
            let value = if value.len() >= 2 {
                if value.starts_with('"') && value.ends_with('"') {
                    // strip ""
                    &value[1..value.len() - 1]
                } else {
                    value
                }
            } else {
                value
            };
            hm.insert(key, value);
            if let Some(stopper) = stopper {
                if key == stopper {
                    break;
                }
            }
        }
        hm
    }

    fn show_vm_info2(&self, id: &str) -> VmResult<String> {
        Self::exec(self.cmd().args(&["showvminfo", id, "--machinereadable"]))
    }

    fn get_vm(&self) -> VmResult<&str> {
        self.vm_name
            .as_deref()
            .ok_or_else(|| VmError::from(ErrorKind::VmIsNotSpecified))
    }

    pub fn start_vm(&self) -> VmResult<()> {
        Self::exec(self.cmd().args(&["startvm", self.get_vm()?]))?;
        Ok(())
    }

    pub fn poweroff_vm(&self) -> VmResult<()> {
        Self::exec(self.cmd().args(&[
            "controlvm",
            &self.get_vm()?,
            "poweroff",
        ]))?;
        Ok(())
    }

    /// Sends ACPI shutdown signal.
    ///
    /// If the VM is running, this function returns Ok(()) regardless of whether the VM was shut down.
    pub fn acpi_power_button_vm(&self) -> VmResult<()> {
        Self::exec(self.cmd().args(&[
            "controlvm",
            &self.get_vm()?,
            "acpipowerbutton",
        ]))?;
        Ok(())
    }

    pub fn reset_vm(&self) -> VmResult<()> {
        Self::exec(self.cmd().args(&["controlvm", &self.get_vm()?, "reset"]))?;
        Ok(())
    }

    pub fn pause_vm(&self) -> VmResult<()> {
        Self::exec(self.cmd().args(&["controlvm", &self.get_vm()?, "pause"]))?;
        Ok(())
    }

    pub fn resume_vm(&self) -> VmResult<()> {
        Self::exec(self.cmd().args(&["controlvm", &self.get_vm()?, "resume"]))?;
        Ok(())
    }

    pub fn save_state_vm(&self) -> VmResult<()> {
        Self::exec(self.cmd().args(&[
            "controlvm",
            &self.get_vm()?,
            "savestate",
        ]))?;
        Ok(())
    }

    /// Gets a list of snapshots.
    pub fn list_snapshots(&self) -> VmResult<Vec<Snapshot>> {
        const SN_NAME: &str = "SnapshotName";
        const SN_UUID: &str = "SnapshotUUID";
        const SN_DESC: &str = "SnapshotDescription";
        #[derive(Eq, PartialEq)]
        enum State {
            Init,
            Name,
            Uuid,
            Desc,
            DescCont,
        }
        let s = Self::exec(self.cmd().args(&[
            "snapshot",
            &self.get_vm()?,
            "list",
            "--machinereadable",
        ]))?;
        let mut ret = vec![];
        if s.trim() == "This machine does not have any snapshots" {
            return Ok(ret);
        }
        let mut last_state = State::Init;

        let mut sn = Snapshot {
            id: None,
            name: None,
            detail: None,
        };
        let mut cur_detail = "".to_string();
        for x in s.lines() {
            let now_data = if x.starts_with(SN_NAME) {
                State::Name
            } else if x.starts_with(SN_UUID) {
                State::Uuid
            } else if x.starts_with(SN_DESC) {
                State::Desc
            } else if x.starts_with("CurrentSnapshotName=\"") {
                // End
                return if last_state == State::Desc
                    || last_state == State::DescCont
                {
                    cur_detail.pop(); // Remove last "
                    Ok(ret)
                } else {
                    vmerr!(ErrorKind::UnexpectedResponse(x.to_string()))
                };
            } else {
                State::DescCont
            };
            match last_state {
                State::Init => match now_data {
                    State::Name => {
                        let p = x.find('=').expect("Invalid name");
                        sn.name = Some(x[p + 2..x.len() - 1].to_string());
                        last_state = State::Name;
                    }
                    _ => {
                        return vmerr!(ErrorKind::UnexpectedResponse(
                            x.to_string()
                        ))
                    }
                },
                State::Name => match now_data {
                    State::Uuid => {
                        let p = x.find('=').expect("Invalid UUID");
                        sn.id = Some(x[p + 2..x.len() - 1].to_string());
                        last_state = State::Uuid;
                    }
                    _ => {
                        return vmerr!(ErrorKind::UnexpectedResponse(
                            x.to_string()
                        ))
                    }
                },
                State::Uuid => match now_data {
                    State::Desc => {
                        let p = x.find('=').expect("Invalid description");
                        cur_detail = x[p + 2..].to_string();
                        last_state = State::Desc;
                    }
                    _ => {
                        return vmerr!(ErrorKind::UnexpectedResponse(
                            x.to_string()
                        ))
                    }
                },
                State::Desc => match now_data {
                    State::Name => {
                        sn.detail = Some(
                            cur_detail[..cur_detail.len() - 1].to_string(),
                        );
                        ret.push(sn.clone());
                        cur_detail = "".to_string();
                        let p = x.find('=').expect("Invalid name");
                        sn.name = Some(x[p + 2..x.len() - 1].to_string());
                        last_state = State::Name;
                    }
                    State::DescCont => {
                        cur_detail += LINE_FEED;
                        cur_detail += x;
                        last_state = State::DescCont;
                    }
                    _ => {
                        return vmerr!(ErrorKind::UnexpectedResponse(
                            x.to_string()
                        ))
                    }
                },
                State::DescCont => match now_data {
                    State::Name => {
                        sn.detail = Some(
                            cur_detail[..cur_detail.len() - 1].to_string(),
                        );
                        ret.push(sn.clone());
                        cur_detail = "".to_string();
                        let p = x.find('=').expect("Invalid name");
                        sn.name = Some(x[p + 2..x.len() - 1].to_string());
                        last_state = State::Name;
                    }
                    State::DescCont => {
                        cur_detail += LINE_FEED;
                        cur_detail += x;
                        last_state = State::DescCont;
                    }
                    _ => {
                        return vmerr!(ErrorKind::UnexpectedResponse(
                            x.to_string()
                        ))
                    }
                },
            };
        }
        Ok(ret)
    }

    pub fn take_snapshot(
        &self,
        name: &str,
        description: Option<&str>,
        is_live: bool,
    ) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["snapshot", &self.get_vm()?, "take", name]);
        if let Some(x) = description {
            cmd.args(&["--description", x]);
        }
        if is_live {
            cmd.arg("--live");
        }
        Self::exec(&mut cmd)?;
        Ok(())
    }

    pub fn delete_snapshot(&self, name: &str) -> VmResult<()> {
        Self::exec(self.cmd().args(&[
            "snapshot",
            &self.get_vm()?,
            "delete",
            name,
        ]))?;
        Ok(())
    }

    pub fn restore_snapshot(&self, name: &str) -> VmResult<()> {
        Self::exec(self.cmd().args(&[
            "snapshot",
            &self.get_vm()?,
            "restore",
            name,
        ]))?;
        Ok(())
    }

    pub fn restore_current_snapshot(&self) -> VmResult<()> {
        Self::exec(self.cmd().args(&[
            "snapshot",
            &self.get_vm()?,
            "restorecurrent",
        ]))?;
        Ok(())
    }

    pub fn run(&self, guest_args: &[&str]) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["guestcontrol", &self.get_vm()?, "run"]);
        cmd.args(self.build_auth());
        cmd.args(guest_args);
        Self::exec(&mut cmd)?;
        Ok(())
    }

    /// Copies files from guest to host.
    pub fn copy_from(
        &self,
        follow: bool,
        recursive: bool,
        from_guest_paths: &[&str],
        to_host_path: &str,
    ) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["guestcontrol", &self.get_vm()?, "copyfrom"]);
        cmd.args(self.build_auth());
        if follow {
            cmd.arg("--follow");
        }
        if recursive {
            cmd.arg("-R");
        }

        cmd.args(from_guest_paths);
        cmd.arg(to_host_path);
        Self::exec(&mut cmd)?;
        Ok(())
    }

    /// Copies files from host to guest.
    pub fn copy_to(
        &self,
        follow: bool,
        recursive: bool,
        from_host_paths: &[&str],
        to_guest_path: &str,
    ) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["guestcontrol", &self.get_vm()?, "copyto"]);
        cmd.args(self.build_auth());
        if follow {
            cmd.arg("--follow");
        }
        if recursive {
            cmd.arg("-R");
        }
        cmd.args(from_host_paths);
        cmd.arg(to_guest_path);
        Self::exec(&mut cmd)?;
        Ok(())
    }

    /// Remove files from guest.
    pub fn remove_file(&self, guest_paths: &[&str]) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["guestcontrol", &self.get_vm()?, "rm"]);
        cmd.args(self.build_auth());
        cmd.arg("-f");
        cmd.args(guest_paths);
        Self::exec(&mut cmd)?;
        Ok(())
    }

    /// Sends keyboard scancodes to the guest.
    pub fn keyboard_put_scancode<T: Iterator<Item = u8>>(
        &self,
        v: T,
    ) -> VmResult<()> {
        use std::fmt::Write;
        let mut cmd = self.cmd();
        cmd.args(&["controlvm", &self.get_vm()?, "keyboardputscancode"]);
        cmd.args(self.build_auth());

        cmd.args(
            v.into_iter()
                .map(|x| {
                    let mut ret = String::new();
                    ret.write_fmt(format_args!("{:x}", x)).unwrap();
                    ret
                })
                .collect::<Vec<String>>(),
        );
        Self::exec(&mut cmd)?;
        Ok(())
    }

    pub fn keyboard_put_string(&self, v: &[&str]) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["controlvm", &self.get_vm()?, "keyboardputstring"]);
        cmd.args(self.build_auth());
        cmd.args(v);
        Self::exec(&mut cmd)?;
        Ok(())
    }

    pub fn install_ext_pack(
        &self,
        replace: bool,
        accept_license: bool,
        ext_pack_path: &str,
    ) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["extpack", "install"]);
        if replace {
            cmd.arg("--replace");
        }
        if accept_license {
            cmd.arg("--accept-license=sha256");
        }
        cmd.arg(ext_pack_path);
        Self::exec(&mut cmd)?;
        Ok(())
    }

    pub fn uninstall_ext_pack(
        &self,
        force: bool,
        ext_pack_path: &str,
    ) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["extpack", "uninstall"]);
        if force {
            cmd.arg("--force");
        }
        cmd.arg(ext_pack_path);
        Self::exec(&mut cmd)?;
        Ok(())
    }

    pub fn cleanup_ext_pack(&self) -> VmResult<()> {
        let mut cmd = self.cmd();
        cmd.args(&["extpack", "cleanup"]);
        Self::exec(&mut cmd)?;
        Ok(())
    }
}

impl VmCmd for VBoxManage {
    fn list_vms(&self) -> VmResult<Vec<Vm>> { self.list_vms() }

    fn set_vm_by_id(&mut self, id: &str) -> VmResult<()> {
        // VBoxManage can be passed an ID.
        self.set_vm_by_name(id)
    }

    fn set_vm_by_name(&mut self, name: &str) -> VmResult<()> {
        self.show_vm_info2(name)?; // Checks if the corresponding VM exists.
        self.vm_name = Some(name.to_string());
        Ok(())
    }

    /// `path` is the absolute path of a `vbox` file.
    fn set_vm_by_path(&mut self, path: &str) -> VmResult<()> {
        use ErrorKind::UnexpectedResponse;
        // `\` in CfgFile of show_vm_info is escaped, So `path` also needs to be escaped.
        let path = path.replace('\\', "\\\\");
        let vms = self.list_vms()?;
        // VBoxManage's machine readable format collapses if the snapshot detail contains `"` or `\`.
        // To avoid this, call show_vm_info multiple times (it takes time).
        for vm in vms {
            let id = vm.id.as_ref().unwrap();
            let s = self.show_vm_info2(id)?;
            let s2 = s.clone();
            let cfg_path = s
                .lines()
                .nth(4)
                .ok_or_else(|| VmError::from(UnexpectedResponse(s2.clone())))?
                .strip_prefix("CfgFile=\"")
                .ok_or_else(|| VmError::from(UnexpectedResponse(s2.clone())))?;
            let cfg_path = &cfg_path[..cfg_path.len() - 1];
            if path == cfg_path {
                self.vm_name = Some(id.to_string());
                return Ok(());
            }
        }
        vmerr!(ErrorKind::VmNotFound)
    }
}

impl PowerCmd for VBoxManage {
    fn start(&self) -> VmResult<()> { self.start_vm() }

    /// Sends ACPI shutdown signals.
    fn stop<D: Into<Option<Duration>>>(&self, timeout: D) -> VmResult<()> {
        let timeout = timeout.into();
        let s = Instant::now();
        let mut ok_flag = false;
        loop {
            match self.acpi_power_button_vm() {
                Ok(()) => {
                    ok_flag = true;
                }
                Err(x) => {
                    if let Some(is_running) = x.is_invalid_state_running() {
                        if !is_running {
                            // !InvalidVmState(Running)
                            return if ok_flag { Ok(()) } else { Err(x) };
                        }
                    } else {
                        return Err(x);
                    }
                }
            }

            if let Some(timeout) = timeout {
                if s.elapsed() >= timeout {
                    return vmerr!(ErrorKind::Timeout);
                }
            }
            std::thread::sleep(Duration::from_millis(200));
        }
    }

    fn hard_stop(&self) -> VmResult<()> {
        let mut ok_flag = false;
        loop {
            match self.poweroff_vm() {
                Ok(()) => {
                    ok_flag = true;
                }
                Err(x) => {
                    match x.get_invalid_state() {
                        Some(VmPowerState::Stopped) => { /* Does nothing */ }
                        Some(VmPowerState::NotRunning) => {
                            return if ok_flag { Ok(()) } else { Err(x) }
                        }
                        _ => return Err(x),
                    }
                }
            }
            std::thread::sleep(Duration::from_millis(200));
        }
    }

    fn suspend(&self) -> VmResult<()> {
        let mut ok_flag = false;
        loop {
            // NotRunningが返ってきたらSuspendに成功した証なのだが、
            //
            let status = self.save_state_vm();
            match status {
                Ok(_) => {
                    ok_flag = true;
                }
                Err(x) => {
                    match x.get_invalid_state() {
                        Some(VmPowerState::Suspended) => { /* Does nothing */ }
                        Some(VmPowerState::NotRunning) => {
                            return if ok_flag { Ok(()) } else { Err(x) }
                        }
                        _ => return Err(x),
                    }
                }
            }
            std::thread::sleep(Duration::from_millis(200));
        }
    }

    fn resume(&self) -> VmResult<()> { self.start_vm() }

    fn is_running(&self) -> VmResult<bool> {
        const VMS: &str = "VMState=\"";
        let s = self.show_vm_info()?;
        for x in s.lines() {
            if x.starts_with(VMS) {
                return Ok(&x[VMS.len()..x.len() - 1] == "running");
            }
        }
        vmerr!(ErrorKind::UnexpectedResponse(s))
    }

    fn reboot<D: Into<Option<Duration>>>(&self, timeout: D) -> VmResult<()> {
        self.stop(timeout)?;
        loop {
            match self.start() {
                Ok(()) => return Ok(()),
                Err(x) => {
                    if x.is_invalid_state_running() == Some(true) { /* Does nothing */
                    } else {
                        return Err(x);
                    }
                }
            }
            std::thread::sleep(Duration::from_millis(200));
        }
    }

    fn hard_reboot(&self) -> VmResult<()> { self.reset_vm() }

    fn pause(&self) -> VmResult<()> { Self::pause_vm(self) }

    fn unpause(&self) -> VmResult<()> { self.resume_vm() }
}

impl GuestCmd for VBoxManage {
    fn exec_cmd(&self, guest_args: &[&str]) -> VmResult<()> {
        self.run(guest_args)
    }

    fn copy_from_guest_to_host(
        &self,
        from_guest_path: &str,
        to_host_path: &str,
    ) -> VmResult<()> {
        self.copy_from(false, true, &[from_guest_path], to_host_path)
    }

    fn copy_from_host_to_guest(
        &self,
        from_host_path: &str,
        to_guest_path: &str,
    ) -> VmResult<()> {
        self.copy_to(false, true, &[from_host_path], to_guest_path)
    }
}

impl SnapshotCmd for VBoxManage {
    fn list_snapshots(&self) -> VmResult<Vec<Snapshot>> {
        Self::list_snapshots(self)
    }

    fn take_snapshot(&self, name: &str) -> VmResult<()> {
        Self::take_snapshot(self, name, None, true)
    }

    fn revert_snapshot(&self, name: &str) -> VmResult<()> {
        self.restore_snapshot(name)
    }

    fn delete_snapshot(&self, name: &str) -> VmResult<()> {
        Self::delete_snapshot(self, name)
    }
}