Skip to content

Commit 8b790ac

Browse files
konstantin-s-bogomgvisor-bot
authored andcommitted
Add platform to statefile metadata and check it on restore.
Save/restoring across platforms is not supported. This CL encodifies that. PiperOrigin-RevId: 808760537
1 parent 48fd850 commit 8b790ac

File tree

7 files changed

+29
-3
lines changed

7 files changed

+29
-3
lines changed

pkg/sentry/platform/kvm/kvm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ func (k *KVM) NewContext(pkgcontext.Context) platform.Context {
187187
}
188188
}
189189

190+
// Name implements platform.Platform.Name.
191+
func (*KVM) Name() string {
192+
return "kvm"
193+
}
194+
190195
type constructor struct{}
191196

192197
func (*constructor) New(opts platform.Options) (platform.Platform, error) {

pkg/sentry/platform/platform.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ type Platform interface {
121121
// in parallel. Concurrent calls to Context.Switch() beyond
122122
// ConcurrencyCount() may block until previous calls have returned.
123123
ConcurrencyCount() int
124+
125+
// Name returns the name of the platform.
126+
Name() string
124127
}
125128

126129
// NoCPUPreemptionDetection implements Platform.DetectsCPUPreemption and

pkg/sentry/platform/ptrace/ptrace.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ func (*PTrace) ConcurrencyCount() int {
268268
return math.MaxInt
269269
}
270270

271+
// Name implements platform.Platform.Name.
272+
func (*PTrace) Name() string {
273+
return "ptrace"
274+
}
275+
271276
type constructor struct{}
272277

273278
func (*constructor) New(platform.Options) (platform.Platform, error) {

pkg/sentry/platform/systrap/systrap.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ func (*Systrap) ConcurrencyCount() int {
349349
return maxSysmsgThreads
350350
}
351351

352+
// Name implements platform.Platform.Name.
353+
func (*Systrap) Name() string {
354+
return "systrap"
355+
}
356+
352357
type constructor struct{}
353358

354359
func (*constructor) New(opts platform.Options) (platform.Platform, error) {

pkg/sentry/state/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (opts SaveOpts) Save(ctx context.Context, k *kernel.Kernel, w *watchdog.Wat
9393
if opts.Metadata == nil {
9494
opts.Metadata = make(map[string]string)
9595
}
96-
addSaveMetadata(opts.Metadata)
96+
addSaveMetadata(opts.Metadata, k.Platform.Name())
9797

9898
// Open the statefile.
9999
wc, err := statefile.NewWriter(opts.Destination, opts.Key, opts.Metadata)

pkg/sentry/state/state_metadata.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ import (
2424
"gvisor.dev/gvisor/pkg/log"
2525
)
2626

27-
// The save metadata keys for timestamp.
27+
// The save metadata keys for timestamp and platform.
2828
const (
2929
cpuUsage = "cpu_usage"
3030
metadataTimestamp = "timestamp"
31+
MetadataPlatform = "platform"
3132
)
3233

33-
func addSaveMetadata(m map[string]string) {
34+
func addSaveMetadata(m map[string]string, platform string) {
3435
t, err := CPUTime()
3536
if err != nil {
3637
log.Warningf("Error getting cpu time: %v", err)
@@ -45,4 +46,6 @@ func addSaveMetadata(m map[string]string) {
4546
m[cpuUsage] = t.String()
4647

4748
m[metadataTimestamp] = fmt.Sprintf("%v", time.Now())
49+
50+
m[MetadataPlatform] = platform
4851
}

runsc/boot/controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,11 @@ func (cm *containerManager) Restore(o *RestoreOpts, _ *struct{}) error {
639639
if checkpointVersion != currentVersion {
640640
return fmt.Errorf("runsc version does not match across checkpoint restore, checkpoint: %v current: %v", checkpointVersion, currentVersion)
641641
}
642+
checkpointPlatform := metadata[state.MetadataPlatform]
643+
currentPlatform := cm.l.k.Platform.Name()
644+
if checkpointPlatform != currentPlatform {
645+
return fmt.Errorf("platform does not match across checkpoint restore, checkpoint: %v current: %v", checkpointPlatform, currentPlatform)
646+
}
642647
return cm.restorer.restoreContainerInfo(cm.l, &cm.l.root, timer.Fork("cont:root"))
643648
}
644649

0 commit comments

Comments
 (0)