Skip to content

Commit 8229c3a

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 f2e9965 commit 8229c3a

File tree

7 files changed

+30
-3
lines changed

7 files changed

+30
-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)
9797

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

pkg/sentry/state/state_metadata.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ import (
2222
"time"
2323

2424
"gvisor.dev/gvisor/pkg/log"
25+
"gvisor.dev/gvisor/pkg/sentry/kernel"
2526
)
2627

27-
// The save metadata keys for timestamp.
28+
// The save metadata keys for timestamp and platform.
2829
const (
2930
cpuUsage = "cpu_usage"
3031
metadataTimestamp = "timestamp"
32+
MetadataPlatform = "platform"
3133
)
3234

33-
func addSaveMetadata(m map[string]string) {
35+
func addSaveMetadata(m map[string]string, k *kernel.Kernel) {
3436
t, err := CPUTime()
3537
if err != nil {
3638
log.Warningf("Error getting cpu time: %v", err)
@@ -45,4 +47,6 @@ func addSaveMetadata(m map[string]string) {
4547
m[cpuUsage] = t.String()
4648

4749
m[metadataTimestamp] = fmt.Sprintf("%v", time.Now())
50+
51+
m[metadataPlatform] = k.Platform.Name()
4852
}

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)