Skip to content

Commit a05a26e

Browse files
authored
Merge pull request #4758 from blueelvis/4661-force-poweroff-in-hyperv
hyperv: Run "sudo poweroff" before stopping VM
2 parents ff14075 + ed94288 commit a05a26e

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

cmd/minikube/cmd/stop.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func runStop(cmd *cobra.Command, args []string) {
5252
defer api.Close()
5353

5454
nonexistent := false
55-
5655
stop := func() (err error) {
5756
err = cluster.StopHost(api)
5857
switch err := errors.Cause(err).(type) {
@@ -64,9 +63,10 @@ func runStop(cmd *cobra.Command, args []string) {
6463
return err
6564
}
6665
}
67-
if err := pkgutil.RetryAfter(5, stop, 2*time.Second); err != nil {
66+
if err := pkgutil.RetryAfter(3, stop, 2*time.Second); err != nil {
6867
exit.WithError("Unable to stop VM", err)
6968
}
69+
7070
if !nonexistent {
7171
out.T(out.Stopped, `"{{.profile_name}}" stopped.`, out.V{"profile_name": profile})
7272
}

pkg/minikube/cluster/cluster.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,22 @@ func adjustGuestClock(h hostRunner, t time.Time) error {
228228
}
229229

230230
// trySSHPowerOff runs the poweroff command on the guest VM to speed up deletion
231-
func trySSHPowerOff(h *host.Host) {
231+
func trySSHPowerOff(h *host.Host) error {
232232
s, err := h.Driver.GetState()
233233
if err != nil {
234234
glog.Warningf("unable to get state: %v", err)
235-
return
235+
return err
236236
}
237237
if s != state.Running {
238238
glog.Infof("host is in state %s", s)
239-
return
239+
return nil
240240
}
241241

242242
out.T(out.Shutdown, `Powering off "{{.profile_name}}" via SSH ...`, out.V{"profile_name": cfg.GetMachineName()})
243243
out, err := h.RunSSHCommand("sudo poweroff")
244244
// poweroff always results in an error, since the host disconnects.
245245
glog.Infof("poweroff result: out=%s, err=%v", out, err)
246+
return nil
246247
}
247248

248249
// StopHost stops the host VM, saving state to disk.
@@ -251,7 +252,15 @@ func StopHost(api libmachine.API) error {
251252
if err != nil {
252253
return errors.Wrapf(err, "load")
253254
}
255+
254256
out.T(out.Stopping, `Stopping "{{.profile_name}}" in {{.driver_name}} ...`, out.V{"profile_name": cfg.GetMachineName(), "driver_name": host.DriverName})
257+
if host.DriverName == constants.DriverHyperv {
258+
glog.Infof("As there are issues with stopping Hyper-V VMs using API, trying to shut down using SSH")
259+
if err := trySSHPowerOff(host); err != nil {
260+
return errors.Wrap(err, "ssh power off")
261+
}
262+
}
263+
255264
if err := host.Stop(); err != nil {
256265
alreadyInStateError, ok := err.(mcnerror.ErrHostAlreadyInState)
257266
if ok && alreadyInStateError.State == state.Stopped {
@@ -270,7 +279,9 @@ func DeleteHost(api libmachine.API) error {
270279
}
271280
// This is slow if SSH is not responding, but HyperV hangs otherwise, See issue #2914
272281
if host.Driver.DriverName() == constants.DriverHyperv {
273-
trySSHPowerOff(host)
282+
if err := trySSHPowerOff(host); err != nil {
283+
glog.Infof("Unable to power off minikube because the host was not found.")
284+
}
274285
}
275286

276287
out.T(out.DeletingHost, `Deleting "{{.profile_name}}" in {{.driver_name}} ...`, out.V{"profile_name": cfg.GetMachineName(), "driver_name": host.DriverName})

0 commit comments

Comments
 (0)