Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c181cc3c50 | ||
|
|
ee45f40888 | ||
|
|
7d02b77d7a |
No files matched your search
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"group": "fixes",
|
||||
"order": 0,
|
||||
"authors": ["yxc0915"],
|
||||
"zh": "修复 ARM macOS 虚拟机从客户端停止时无法触发正常关机的问题。",
|
||||
"en": "Fix Stop for ARM macOS VMs so the guest can shut down normally."
|
||||
}
|
||||
@@ -822,20 +822,59 @@ func (d *StambhaDriver) Stop(ctx context.Context, name string, force bool, _, _
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
if err := conn.Execute("system_powerdown", nil); err != nil {
|
||||
err = requestStambhaShutdown(ctx, conn)
|
||||
_ = conn.Close()
|
||||
// Stela interprets two power-button pulses within three seconds as a
|
||||
// held hardware button. Keep the VM lock across that interval even
|
||||
// if the caller cancels or QMP cannot confirm delivery.
|
||||
retryAfter := time.Now().Add(4 * time.Second)
|
||||
defer func() { time.Sleep(time.Until(retryAfter)) }()
|
||||
if err != nil {
|
||||
if live, checkErr := stambhaLiveProcess(dir); checkErr == nil && live.PID == 0 {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
wait, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
wait, cancel := context.WithTimeout(ctx, 90*time.Second)
|
||||
defer cancel()
|
||||
err = host.WaitProcessExit(wait, uint32(proc.PID), "qemu-system-x86_64.exe")
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return fmt.Errorf("macOS is still running. Shut down from the Apple menu or explicitly force stop the VM (%w)", err)
|
||||
return fmt.Errorf("macOS did not finish shutting down. Check the VM for apps that need attention or shut down from the Apple menu (%w)", err)
|
||||
}
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func requestStambhaShutdown(ctx context.Context, conn qmp.Executor) (err error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
// Stela translates system_powerdown into an SMC power-button pulse.
|
||||
// Send the three modifiers in the same input report before the pulse.
|
||||
// macOS can then perform an orderly shutdown and prompt to save work.
|
||||
keys := []string{"ctrl", "alt", "meta_l"}
|
||||
defer func() { err = errors.Join(err, qmp.ReleaseInputs(conn, keys, nil)) }()
|
||||
if err := qmp.PressKeys(conn, keys...); err != nil {
|
||||
return err
|
||||
}
|
||||
timer := time.NewTimer(650 * time.Millisecond)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := conn.Execute("system_powerdown", nil); err != nil {
|
||||
return err
|
||||
}
|
||||
// Keep the modifiers down while Stela and the guest handle the pulse.
|
||||
time.Sleep(1500 * time.Millisecond)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *StambhaDriver) Delete(_ context.Context, name string, keepFiles bool, _, _ io.Writer) error {
|
||||
return d.withVM(name, func(dir string, proc stambhaProcess) error {
|
||||
if proc.PID != 0 {
|
||||
|
||||
@@ -495,3 +495,45 @@ func assertEnvVal(t *testing.T, env []string, key, want string) {
|
||||
t.Fatalf("env %s = %q, want %q (env=%v)", key, got, want, env)
|
||||
}
|
||||
}
|
||||
|
||||
type shutdownRecorder struct {
|
||||
commands []string
|
||||
onHold func()
|
||||
}
|
||||
|
||||
func (r *shutdownRecorder) Execute(command string, args any) error {
|
||||
data, err := json.Marshal(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.commands = append(r.commands, command+" "+string(data))
|
||||
if command == "input-send-event" && len(r.commands) == 1 && r.onHold != nil {
|
||||
r.onHold()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestRequestStambhaShutdown(t *testing.T) {
|
||||
const hold = "input-send-event " + `{"events":[{"data":{"down":true,"key":{"data":"ctrl","type":"qcode"}},"type":"key"},{"data":{"down":true,"key":{"data":"alt","type":"qcode"}},"type":"key"},{"data":{"down":true,"key":{"data":"meta_l","type":"qcode"}},"type":"key"}]}`
|
||||
release := strings.ReplaceAll(hold, `"down":true`, `"down":false`)
|
||||
t.Run("holds all modifiers before one power pulse", func(t *testing.T) {
|
||||
qmp := &shutdownRecorder{}
|
||||
if err := requestStambhaShutdown(context.Background(), qmp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []string{hold, "system_powerdown null", release}; !slices.Equal(qmp.commands, want) {
|
||||
t.Fatalf("QMP commands = %v, want %v", qmp.commands, want)
|
||||
}
|
||||
})
|
||||
t.Run("cancellation prevents the power pulse", func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
qmp := &shutdownRecorder{onHold: cancel}
|
||||
if err := requestStambhaShutdown(ctx, qmp); !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("shutdown error = %v, want context cancellation", err)
|
||||
}
|
||||
if !slices.Equal(qmp.commands, []string{hold, release}) {
|
||||
t.Fatalf("unexpected QMP commands after cancellation: %v", qmp.commands)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -120,6 +120,14 @@ func SendKey(q Executor, key string, down bool) error {
|
||||
return sendKeys(q, down, key)
|
||||
}
|
||||
|
||||
// PressKeys sends one input report containing all the key-down events.
|
||||
func PressKeys(q Executor, keys ...string) error {
|
||||
if q == nil {
|
||||
return errors.New("QMP session is unavailable")
|
||||
}
|
||||
return sendKeys(q, true, keys...)
|
||||
}
|
||||
|
||||
// ReleaseInputs clears held keys and pointer buttons in one QMP input report.
|
||||
func ReleaseInputs(q Executor, keys, buttons []string) error {
|
||||
if q == nil {
|
||||
|
||||
Reference in new issue
Block a user