|
| 1 | +//go:build darwin && arm64 && !no_vz |
| 2 | + |
| 3 | +package vz |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/Code-Hex/vz/v3" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | +) |
| 13 | + |
| 14 | +func saveVM(vm *vz.VirtualMachine, machineStatePath string) error { |
| 15 | + if !vm.CanPause() { |
| 16 | + return fmt.Errorf("can't pause the VZ machine") |
| 17 | + } |
| 18 | + |
| 19 | + // Remove the old machine state file if it exists, |
| 20 | + // because saving the machine state will fail if the file already exists. |
| 21 | + if err := os.Remove(machineStatePath); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 22 | + logrus.WithError(err).Errorf("Failed to remove the old VZ machine state file %q", machineStatePath) |
| 23 | + return err |
| 24 | + } |
| 25 | + |
| 26 | + logrus.Info("Pausing VZ") |
| 27 | + if err := vm.Pause(); err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + // If we can't stop the machine after pausing, saving the machine state will be useless. |
| 32 | + // So we should check this before saving the machine state. |
| 33 | + if !vm.CanStop() { |
| 34 | + return fmt.Errorf("can't stop the VZ machine after pausing") |
| 35 | + } |
| 36 | + |
| 37 | + logrus.Info("Saving VZ machine state for resuming later") |
| 38 | + 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 | + 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 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + logrus.Info("Stopping VZ") |
| 48 | + if err := vm.Stop(); err != nil { |
| 49 | + // If we fail to stop the machine, we should resume the machine to call RequestStop() later |
| 50 | + logrus.WithError(err).Error("Failed to stop the VZ machine") |
| 51 | + if resumeError := vm.Resume(); resumeError != nil { |
| 52 | + return resumeError |
| 53 | + } |
| 54 | + return err |
| 55 | + } |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func restoreVM(vm *vz.VirtualMachine, machineStatePath string) error { |
| 60 | + if _, err := os.Stat(machineStatePath); err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + logrus.Info("Saved VZ machine state found, resuming VZ") |
| 64 | + if err := vm.RestoreMachineStateFromURL(machineStatePath); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + if err := vm.Resume(); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + // Remove the machine state file after resuming the machine |
| 71 | + if err := os.Remove(machineStatePath); err != nil { |
| 72 | + // We should log the error but continue the process, because the machine state is already restored |
| 73 | + logrus.WithError(err).Errorf("Failed to remove the VZ machine state file %q", machineStatePath) |
| 74 | + } |
| 75 | + return nil |
| 76 | +} |
0 commit comments