diff --git a/internal/qmp/migrate.go b/internal/qmp/migrate.go index 4eb5fe0..fc36d56 100644 --- a/internal/qmp/migrate.go +++ b/internal/qmp/migrate.go @@ -133,7 +133,7 @@ func suspendToFileConn(ctx context.Context, c *Conn, vmstatePath string, onProgr "channels": fileChannels(vmstatePath), } err = runSuspendMigration(ctx, c, args, onProgress) - if err != nil && directIO && isDirectIOErr(err) { + if directIO && errors.Is(err, ErrIncompleteSuspend) && isDirectIOErr(err) { // Fall back to buffered file I/O if the target volume rejects Win32 // direct I/O (e.g., compressed directory or unsupported filesystem). if st, stErr := queryVMStatus(c); stErr == nil && !st.Running && st.Status == "paused" { @@ -397,9 +397,6 @@ func runSuspendMigration(ctx context.Context, c *Conn, args map[string]any, onPr } func isDirectIOErr(err error) bool { - if err == nil { - return false - } s := strings.ToLower(err.Error()) return strings.Contains(s, "direct i/o") || strings.Contains(s, "direct-io") || strings.Contains(s, "o_direct") } diff --git a/internal/qmp/migrate_test.go b/internal/qmp/migrate_test.go index 36d46e9..9ed0860 100644 --- a/internal/qmp/migrate_test.go +++ b/internal/qmp/migrate_test.go @@ -333,53 +333,76 @@ func TestSuspendToFileFallsBackWhenDirectIOUnsupported(t *testing.T) { } } -func TestSuspendToFileRetriesBufferedOnDirectIOVolumeFailure(t *testing.T) { - migrateAttempts := 0 - addr, cmds := startMockQMP(t, func(req mockQMPReq) (map[string]any, bool) { - switch req.Execute { - case "stop", "migrate-set-capabilities", "migrate-set-parameters": - return map[string]any{"return": map[string]any{}}, false - case "query-status": - return map[string]any{"return": map[string]any{"running": false, "status": "paused"}}, false - case "migrate": - migrateAttempts++ - return map[string]any{"return": map[string]any{}}, false - case "query-migrate": - if migrateAttempts == 1 { - return map[string]any{"return": map[string]any{ - "status": "failed", - "error-desc": "Unable to open 'suspend.vmstate.tmp' with direct I/O", - }}, false +func TestSuspendToFileRetriesBufferedOnlyForDirectIOFailures(t *testing.T) { + for _, tc := range []struct { + name string + reason string + retry bool + }{ + {"open rejected", "Unable to open 'suspend.vmstate.tmp' with direct I/O", true}, + {"write rejected", "Unable to pwritev to file with direct I/O: The parameter is incorrect", true}, + {"disk full", "Unable to pwritev to file: The disk is full", false}, + } { + t.Run(tc.name, func(t *testing.T) { + attempts := 0 + addr, cmds := startMockQMP(t, func(req mockQMPReq) (map[string]any, bool) { + switch req.Execute { + case "stop", "migrate-set-capabilities", "migrate-set-parameters", "cont": + return map[string]any{"return": map[string]any{}}, false + case "query-status": + return map[string]any{"return": map[string]any{"running": false, "status": "paused"}}, false + case "migrate": + attempts++ + return map[string]any{"return": map[string]any{}}, false + case "query-migrate": + if attempts == 1 { + return map[string]any{"return": map[string]any{ + "status": "failed", "error-desc": tc.reason, + }}, false + } + return map[string]any{"return": map[string]any{"status": "completed"}}, false + case "quit": + return map[string]any{"return": map[string]any{}}, true + default: + t.Errorf("unexpected QMP command: %s", req.Execute) + return map[string]any{"return": map[string]any{}}, false + } + }) + + ctx := context.Background() + c, err := Dial(ctx, "tcp", addr) + if err != nil { + t.Fatalf("Dial: %v", err) } - return map[string]any{"return": map[string]any{"status": "completed", "total-time": 1500}}, false - case "quit": - return map[string]any{"return": map[string]any{}}, true - default: - t.Errorf("unexpected QMP command: %s", req.Execute) - return map[string]any{"return": map[string]any{}}, false - } - }) + defer c.Close() - c, err := Dial(context.Background(), "tcp", addr) - if err != nil { - t.Fatalf("Dial: %v", err) - } - defer c.Close() + err = suspendToFileConn(ctx, c, `C:\VMs\mac\suspend.vmstate`, nil, nil) + wantAttempts := 1 + if tc.retry { + wantAttempts = 2 + if err != nil { + t.Fatalf("buffered retry failed: %v", err) + } + } else if !errors.Is(err, ErrIncompleteSuspend) || !strings.Contains(err.Error(), tc.reason) { + t.Fatalf("expected original write error, got %v", err) + } - if err := suspendToFileConn(context.Background(), c, `C:\VMs\mac\suspend.vmstate`, nil, nil); err != nil { - t.Fatalf("suspendToFileConn direct-io fallback: %v", err) - } - if migrateAttempts != 2 { - t.Fatalf("migrate attempts = %d, want 2", migrateAttempts) - } - var sawDisableDirectIO bool - for _, r := range cmds() { - if r.Execute == "migrate-set-parameters" && strings.Contains(string(r.Arguments), `"direct-io":false`) { - sawDisableDirectIO = true - } - } - if !sawDisableDirectIO { - t.Fatal("expected retry to explicitly disable direct-io") + gotAttempts := 0 + var disabledDirectIO, resumed bool + for _, req := range cmds() { + switch req.Execute { + case "migrate": + gotAttempts++ + case "migrate-set-parameters": + disabledDirectIO = disabledDirectIO || strings.Contains(string(req.Arguments), `"direct-io":false`) + case "cont": + resumed = true + } + } + if gotAttempts != wantAttempts || disabledDirectIO != tc.retry || resumed == tc.retry { + t.Fatalf("attempts = %d, direct I/O disabled = %t, guest resumed = %t", gotAttempts, disabledDirectIO, resumed) + } + }) } }