@@ -23,34 +23,40 @@ func saveVM(vm *vz.VirtualMachine, machineStatePath string) error {
23
23
return err
24
24
}
25
25
26
- logrus .Info ("Pausing VZ" )
26
+ logrus .Info ("Pausing VZ machine for saving the machine state " )
27
27
if err := vm .Pause (); err != nil {
28
+ logrus .WithError (err ).Error ("Failed to pause the VZ machine" )
28
29
return err
29
30
}
30
31
32
+ if err := savePausedVM (vm , machineStatePath ); err != nil {
33
+ // If we fail to save the machine state, we should resume the machine before returning the error.
34
+ if resumeError := vm .Resume (); resumeError != nil {
35
+ logrus .WithError (resumeError ).Error ("Failed to resume the VZ machine after pausing" )
36
+ return resumeError
37
+ }
38
+ return err
39
+ }
40
+
41
+ return nil
42
+ }
43
+
44
+ func savePausedVM (vm * vz.VirtualMachine , machineStatePath string ) error {
31
45
// If we can't stop the machine after pausing, saving the machine state will be useless.
32
46
// So we should check this before saving the machine state.
33
47
if ! vm .CanStop () {
34
- return fmt .Errorf ("can't stop the VZ machine after pausing " )
48
+ return fmt .Errorf ("can't stop the VZ machine" )
35
49
}
36
50
37
- logrus .Info ("Saving VZ machine state for resuming later" )
51
+ logrus .Info ("Saving VZ machine state for restoring later" )
38
52
if err := vm .SaveMachineStateToPath (machineStatePath ); err != nil {
39
- // If we fail to save the machine state, we should resume the machine to call RequestStop() later
40
53
logrus .WithError (err ).Errorf ("Failed to save the machine state to %q" , machineStatePath )
41
- if resumeError := vm .Resume (); resumeError != nil {
42
- return resumeError
43
- }
44
54
return err
45
55
}
46
56
47
- logrus .Info ("Stopping VZ" )
57
+ logrus .Info ("Stopping VZ machine after saving the machine state " )
48
58
if err := vm .Stop (); err != nil {
49
- // If we fail to stop the machine, we should resume the machine to call RequestStop() later
50
59
logrus .WithError (err ).Error ("Failed to stop the VZ machine" )
51
- if resumeError := vm .Resume (); resumeError != nil {
52
- return resumeError
53
- }
54
60
return err
55
61
}
56
62
return nil
@@ -60,7 +66,7 @@ func restoreVM(vm *vz.VirtualMachine, machineStatePath string) error {
60
66
if _ , err := os .Stat (machineStatePath ); err != nil {
61
67
return err
62
68
}
63
- logrus .Info ( "Saved VZ machine state found, resuming VZ" )
69
+ logrus .Infof ( "Resuming VZ machine from %q" , machineStatePath )
64
70
if err := vm .RestoreMachineStateFromURL (machineStatePath ); err != nil {
65
71
return err
66
72
}
0 commit comments