feat(runtime): accept suspend-compatible Alpha revisions

Read the verified compatibility ID while preserving legacy and media checks.
This commit is contained in:
YXC0915 committed 2026-12-02 03:15:54 +08:00
1 parent 08b3089569
commit b78f4b3028
7 files changed
+103 -4

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
{
"group": "improvements",
"order": 0,
"authors": ["yxc0915"],
"zh": "具有相同挂起兼容标识的 Alpha 修订可恢复同一主编号下保存的虚拟机挂起状态。",
"en": "Alpha revisions with the same suspend compatibility ID can resume VM state saved under the same base version."
}
+4
View File
@@ -2680,6 +2680,7 @@ export namespace reims {
extra_args?: string;
runtime_commit: string;
vendor_qemu_commit?: string;
suspend_compat_id?: string;
created_at: string;
vmstate_size: number;
@@ -2705,6 +2706,7 @@ export namespace reims {
this.extra_args = source["extra_args"];
this.runtime_commit = source["runtime_commit"];
this.vendor_qemu_commit = source["vendor_qemu_commit"];
this.suspend_compat_id = source["suspend_compat_id"];
this.created_at = source["created_at"];
this.vmstate_size = source["vmstate_size"];
}
@@ -2873,6 +2875,7 @@ export namespace runtime {
raw_bytes: number;
sha256: Record<string, string>;
vendor_qemu_commit?: string;
suspend_compat_id?: string;
util_commit?: string;
metal2vulkan_commit?: string;
patches?: Patch[];
@@ -2905,6 +2908,7 @@ export namespace runtime {
this.raw_bytes = source["raw_bytes"];
this.sha256 = source["sha256"];
this.vendor_qemu_commit = source["vendor_qemu_commit"];
this.suspend_compat_id = source["suspend_compat_id"];
this.util_commit = source["util_commit"];
this.metal2vulkan_commit = source["metal2vulkan_commit"];
this.patches = this.convertValues(source["patches"], Patch);
+13 -4
View File
@@ -39,6 +39,7 @@ type SuspendInfo struct {
ExtraArgs string `json:"extra_args,omitempty"`
RuntimeCommit string `json:"runtime_commit"`
VendorQemuCommit string `json:"vendor_qemu_commit,omitempty"`
SuspendCompatID string `json:"suspend_compat_id,omitempty"`
CreatedAt string `json:"created_at"`
VMStateSize int64 `json:"vmstate_size"`
}
@@ -128,6 +129,7 @@ func BuildSuspendInfo(d VMDetail, active rt.Installed, vmstateSize int64) Suspen
ExtraArgs: StripIncomingDeferArgs(d.ExtraArgs),
RuntimeCommit: strings.TrimSpace(active.Commit),
VendorQemuCommit: strings.TrimSpace(active.Manifest.VendorQemuCommit),
SuspendCompatID: rt.SuspendCompatibilityID(active.Commit, active.Manifest.SuspendCompatID),
CreatedAt: time.Now().UTC().Format(time.RFC3339),
VMStateSize: vmstateSize,
}
@@ -179,11 +181,18 @@ func ValidateSuspendCompatibility(meta SuspendInfo, cur VMDetail, active rt.Inst
if meta.Network != cur.Network {
diffs = append(diffs, fmt.Sprintf("Network (%v -> %v)", meta.Network, cur.Network))
}
if meta.VendorQemuCommit != "" && active.Manifest.VendorQemuCommit != "" &&
!strings.EqualFold(strings.TrimSpace(meta.VendorQemuCommit), strings.TrimSpace(active.Manifest.VendorQemuCommit)) {
savedID := rt.SuspendCompatibilityID(meta.RuntimeCommit, meta.SuspendCompatID)
activeID := rt.SuspendCompatibilityID(active.Commit, active.Manifest.SuspendCompatID)
switch {
case savedID != "" && activeID != "":
if savedID != activeID {
diffs = append(diffs, fmt.Sprintf("Suspend compatibility (%s -> %s)", savedID, activeID))
}
case meta.VendorQemuCommit != "" && active.Manifest.VendorQemuCommit != "" &&
!strings.EqualFold(strings.TrimSpace(meta.VendorQemuCommit), strings.TrimSpace(active.Manifest.VendorQemuCommit)):
diffs = append(diffs, fmt.Sprintf("QEMU commit (%s -> %s)", shortRev(meta.VendorQemuCommit), shortRev(active.Manifest.VendorQemuCommit)))
} else if meta.RuntimeCommit != "" && active.Commit != "" &&
!strings.EqualFold(strings.TrimSpace(meta.RuntimeCommit), strings.TrimSpace(active.Commit)) {
case meta.RuntimeCommit != "" && active.Commit != "" &&
!strings.EqualFold(strings.TrimSpace(meta.RuntimeCommit), strings.TrimSpace(active.Commit)):
diffs = append(diffs, fmt.Sprintf("Runtime (%s -> %s)", meta.RuntimeCommit, active.Commit))
}
return diffs
+20
View File
@@ -105,6 +105,26 @@ func TestIncomingDeferArgsIdempotent(t *testing.T) {
}
}
func TestSuspendLegacyRuntimeStillRequiresExactVersion(t *testing.T) {
qemu := strings.Repeat("a", 40)
state := SuspendInfo{RuntimeCommit: "a1b2c3d", VendorQemuCommit: qemu}
active := rt.Installed{Commit: "a1b2c3d", Manifest: rt.Manifest{VendorQemuCommit: qemu}}
if diffs := ValidateSuspendCompatibility(state, VMDetail{}, active); len(diffs) != 0 {
t.Fatalf("legacy exact version rejected: %v", diffs)
}
active.Commit = "a1b2c3d-p1"
active.Manifest.SuspendCompatID = "a1b2c3d"
if diffs := ValidateSuspendCompatibility(state, VMDetail{}, active); len(diffs) != 1 || !strings.Contains(diffs[0], "Runtime") {
t.Fatalf("legacy state accepted a different revision: %v", diffs)
}
state.SuspendCompatID = "a1b2c3d"
active.Manifest.SuspendCompatID = "d4e5f6a"
if diffs := ValidateSuspendCompatibility(state, VMDetail{}, active); len(diffs) == 0 {
t.Fatal("compatibility declaration for another family was accepted")
}
}
func TestValidateSetRejectsSuspendedVM(t *testing.T) {
issues := ValidateSetLocalized(
SetOpts{Name: "tahoe", CPUs: 6},
@@ -87,3 +87,47 @@ func TestSuspendTransactionKeepsSealedFilesWhenPublicationFails(t *testing.T) {
t.Fatalf("recoverable snapshot = %q, %v, %v", state, paths, err)
}
}
func TestSuspendTransactionChecksRuntimeCompatibilityFamily(t *testing.T) {
dir := t.TempDir()
media := filepath.Join(dir, "disk.qcow2")
for _, path := range []string{media, filepath.Join(dir, "vm.toml"), SuspendStatePath(dir) + ".tmp"} {
if err := os.WriteFile(path, []byte("test state"), 0o600); err != nil {
t.Fatal(err)
}
}
d := VMDetail{Name: "test", Directory: dir, CPUs: 2}
saved := rt.Installed{Commit: "a1b2c3d-p1", Manifest: rt.Manifest{
SuspendCompatID: "a1b2c3d", VendorQemuCommit: strings.Repeat("a", 40),
}}
if _, err := SealSuspend(d, saved, []string{media}); err != nil {
t.Fatal(err)
}
info, err := ReadSuspendInfo(dir)
if err != nil || info.SuspendCompatID != "a1b2c3d" {
t.Fatalf("saved suspend compatibility = %+v, %v", info, err)
}
candidate := rt.Installed{Commit: "a1b2c3d-p2", Manifest: rt.Manifest{
SuspendCompatID: "a1b2c3d", VendorQemuCommit: strings.Repeat("b", 40),
}}
if _, _, err := ValidateSuspendFiles(d, candidate); err != nil {
t.Fatalf("compatible revision rejected: %v", err)
}
otherFamily := candidate
otherFamily.Commit = "d4e5f6a"
otherFamily.Manifest.SuspendCompatID = "d4e5f6a"
if _, _, err := ValidateSuspendFiles(d, otherFamily); err == nil || !strings.Contains(err.Error(), "Suspend compatibility") {
t.Fatalf("different family was not rejected: %v", err)
}
if !HasSuspendState(dir) {
t.Fatal("compatibility check removed the saved state")
}
if err := os.WriteFile(media, []byte("changed disk content"), 0o600); err != nil {
t.Fatal(err)
}
if _, _, err := ValidateSuspendFiles(d, candidate); err == nil || !strings.Contains(err.Error(), "disk.qcow2") {
t.Fatalf("compatible revision bypassed media verification: %v", err)
}
}
+14
View File
@@ -448,6 +448,20 @@ func splitRuntimeID(id string) (base, rev string) {
return id, ""
}
var suspendReleaseIDPattern = regexp.MustCompile(`^[0-9a-f]{7,40}(-p[1-9][0-9]*)?$`)
// SuspendCompatibilityID accepts a declaration only for its own release family.
// An absent declaration keeps the legacy exact-runtime checks.
func SuspendCompatibilityID(releaseID, declared string) string {
releaseID = strings.ToLower(strings.TrimSpace(releaseID))
declared = strings.ToLower(strings.TrimSpace(declared))
base, _ := splitRuntimeID(releaseID)
if !suspendReleaseIDPattern.MatchString(releaseID) || declared != base {
return ""
}
return declared
}
// sameRuntimeID reports whether a and b refer to the same runtime build,
// allowing short/full commit prefix matching only within the same revision.
func sameRuntimeID(a, b string) bool {
+1
View File
@@ -38,6 +38,7 @@ type Manifest struct {
// Schema 2 provenance fields: upstream commits, applied patches, build channel, and source archive.
VendorQemuCommit string `json:"vendor_qemu_commit,omitempty"`
SuspendCompatID string `json:"suspend_compat_id,omitempty"`
UtilCommit string `json:"util_commit,omitempty"`
MetalCommit string `json:"metal2vulkan_commit,omitempty"`
Patches []Patch `json:"patches,omitempty"`