@@ -23,34 +23,40 @@ func saveVM(vm *vz.VirtualMachine, machineStatePath string) error {
2323 return err
2424 }
2525
26- logrus .Info ("Pausing VZ" )
26+ logrus .Info ("Pausing VZ machine for saving the machine state " )
2727 if err := vm .Pause (); err != nil {
28+ logrus .WithError (err ).Error ("Failed to pause the VZ machine" )
2829 return err
2930 }
3031
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 {
3145 // If we can't stop the machine after pausing, saving the machine state will be useless.
3246 // So we should check this before saving the machine state.
3347 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" )
3549 }
3650
37- logrus .Info ("Saving VZ machine state for resuming later" )
51+ logrus .Info ("Saving VZ machine state for restoring later" )
3852 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
4053 logrus .WithError (err ).Errorf ("Failed to save the machine state to %q" , machineStatePath )
41- if resumeError := vm .Resume (); resumeError != nil {
42- return resumeError
43- }
4454 return err
4555 }
4656
47- logrus .Info ("Stopping VZ" )
57+ logrus .Info ("Stopping VZ machine after saving the machine state " )
4858 if err := vm .Stop (); err != nil {
49- // If we fail to stop the machine, we should resume the machine to call RequestStop() later
5059 logrus .WithError (err ).Error ("Failed to stop the VZ machine" )
51- if resumeError := vm .Resume (); resumeError != nil {
52- return resumeError
53- }
5460 return err
5561 }
5662 return nil
@@ -60,7 +66,7 @@ func restoreVM(vm *vz.VirtualMachine, machineStatePath string) error {
6066 if _ , err := os .Stat (machineStatePath ); err != nil {
6167 return err
6268 }
63- logrus .Info ( "Saved VZ machine state found, resuming VZ" )
69+ logrus .Infof ( "Resuming VZ machine from %q" , machineStatePath )
6470 if err := vm .RestoreMachineStateFromURL (machineStatePath ); err != nil {
6571 return err
6672 }
0 commit comments