From 897c2c0d78e27ba5b1b48ae4925a44ce69826159 Mon Sep 17 00:00:00 2001 From: Jamie Liu Date: Thu, 9 Oct 2025 21:04:34 -0700 Subject: [PATCH] Move checkpoint filenames to a new package. PiperOrigin-RevId: 817464503 --- pkg/sentry/state/checkpointfiles/BUILD | 12 +++++++ .../state/checkpointfiles/checkpointfiles.go | 31 +++++++++++++++++++ runsc/boot/restore.go | 9 ------ runsc/sandbox/BUILD | 1 + runsc/sandbox/sandbox.go | 13 ++++---- 5 files changed, 51 insertions(+), 15 deletions(-) create mode 100644 pkg/sentry/state/checkpointfiles/BUILD create mode 100644 pkg/sentry/state/checkpointfiles/checkpointfiles.go diff --git a/pkg/sentry/state/checkpointfiles/BUILD b/pkg/sentry/state/checkpointfiles/BUILD new file mode 100644 index 0000000000..c0c699edf1 --- /dev/null +++ b/pkg/sentry/state/checkpointfiles/BUILD @@ -0,0 +1,12 @@ +load("//tools:defs.bzl", "go_library") + +package( + default_applicable_licenses = ["//:license"], + licenses = ["notice"], +) + +go_library( + name = "checkpointfiles", + srcs = ["checkpointfiles.go"], + visibility = ["//pkg/sentry:internal"], +) diff --git a/pkg/sentry/state/checkpointfiles/checkpointfiles.go b/pkg/sentry/state/checkpointfiles/checkpointfiles.go new file mode 100644 index 0000000000..f7f5b39475 --- /dev/null +++ b/pkg/sentry/state/checkpointfiles/checkpointfiles.go @@ -0,0 +1,31 @@ +// Copyright 2025 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package checkpointfiles defines constants used when sentry state is +// checkpointed to multiple files in a directory rather than to an opaque FD. +package checkpointfiles + +const ( + // StateFileName is the file in an image-path directory which contains the + // sentry object graph. + StateFileName = "checkpoint.img" + + // PagesMetadataFileName is the file in an image-path directory containing + // MemoryFile metadata. + PagesMetadataFileName = "pages_meta.img" + + // PagesFileName is the file in an image-path directory containing + // MemoryFile page contents. + PagesFileName = "pages.img" +) diff --git a/runsc/boot/restore.go b/runsc/boot/restore.go index cf196cfbea..eac3c297f1 100644 --- a/runsc/boot/restore.go +++ b/runsc/boot/restore.go @@ -50,15 +50,6 @@ import ( ) const ( - // CheckpointStateFileName is the file within the given image-path's - // directory which contains the container's saved state. - CheckpointStateFileName = "checkpoint.img" - // CheckpointPagesMetadataFileName is the file within the given image-path's - // directory containing the container's MemoryFile metadata. - CheckpointPagesMetadataFileName = "pages_meta.img" - // CheckpointPagesFileName is the file within the given image-path's - // directory containing the container's MemoryFile pages. - CheckpointPagesFileName = "pages.img" // VersionKey is the key used to save runsc version in the save metadata and compare // it across checkpoint restore. VersionKey = "runsc_version" diff --git a/runsc/sandbox/BUILD b/runsc/sandbox/BUILD index 9824c307b0..4e0dc927bc 100644 --- a/runsc/sandbox/BUILD +++ b/runsc/sandbox/BUILD @@ -38,6 +38,7 @@ go_library( "//pkg/sentry/platform", "//pkg/sentry/seccheck", "//pkg/sentry/socket/plugin", + "//pkg/sentry/state/checkpointfiles", "//pkg/state/statefile", "//pkg/sync", "//pkg/tcpip/header", diff --git a/runsc/sandbox/sandbox.go b/runsc/sandbox/sandbox.go index fb3a033500..d41449280c 100644 --- a/runsc/sandbox/sandbox.go +++ b/runsc/sandbox/sandbox.go @@ -50,6 +50,7 @@ import ( "gvisor.dev/gvisor/pkg/sentry/fsimpl/erofs" "gvisor.dev/gvisor/pkg/sentry/platform" "gvisor.dev/gvisor/pkg/sentry/seccheck" + "gvisor.dev/gvisor/pkg/sentry/state/checkpointfiles" "gvisor.dev/gvisor/pkg/state/statefile" "gvisor.dev/gvisor/pkg/sync" "gvisor.dev/gvisor/pkg/urpc" @@ -501,7 +502,7 @@ func (s *Sandbox) Restore(conf *config.Config, spec *specs.Spec, cid string, ima log.Debugf("Restore sandbox %q from path %q", s.ID, imagePath) - stateFileName := path.Join(imagePath, boot.CheckpointStateFileName) + stateFileName := path.Join(imagePath, checkpointfiles.StateFileName) sf, err := os.Open(stateFileName) if err != nil { return fmt.Errorf("opening state file %q failed: %v", stateFileName, err) @@ -516,7 +517,7 @@ func (s *Sandbox) Restore(conf *config.Config, spec *specs.Spec, cid string, ima } // If the pages file exists, we must pass it in. - pagesFileName := path.Join(imagePath, boot.CheckpointPagesFileName) + pagesFileName := path.Join(imagePath, checkpointfiles.PagesFileName) pagesReadFlags := os.O_RDONLY if direct { // The contents are page-aligned, so it can be opened with O_DIRECT. @@ -524,7 +525,7 @@ func (s *Sandbox) Restore(conf *config.Config, spec *specs.Spec, cid string, ima } if pf, err := os.OpenFile(pagesFileName, pagesReadFlags, 0); err == nil { defer pf.Close() - pagesMetadataFileName := path.Join(imagePath, boot.CheckpointPagesMetadataFileName) + pagesMetadataFileName := path.Join(imagePath, checkpointfiles.PagesMetadataFileName) pmf, err := os.Open(pagesMetadataFileName) if err != nil { return fmt.Errorf("opening restore image file %q failed: %v", pagesMetadataFileName, err) @@ -1537,7 +1538,7 @@ func (s *Sandbox) Checkpoint(cid string, imagePath string, opts CheckpointOpts) func createSaveFiles(path string, direct bool, compression statefile.CompressionLevel) ([]*os.File, error) { var files []*os.File - stateFilePath := filepath.Join(path, boot.CheckpointStateFileName) + stateFilePath := filepath.Join(path, checkpointfiles.StateFileName) f, err := os.OpenFile(stateFilePath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0644) if err != nil { return nil, fmt.Errorf("creating checkpoint state file %q: %w", stateFilePath, err) @@ -1548,14 +1549,14 @@ func createSaveFiles(path string, direct bool, compression statefile.Compression // It is beneficial to store them separately so certain optimizations can be // applied during restore. See Restore(). if compression == statefile.CompressionLevelNone { - pagesMetadataFilePath := filepath.Join(path, boot.CheckpointPagesMetadataFileName) + pagesMetadataFilePath := filepath.Join(path, checkpointfiles.PagesMetadataFileName) f, err = os.OpenFile(pagesMetadataFilePath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0644) if err != nil { return nil, fmt.Errorf("creating checkpoint pages metadata file %q: %w", pagesMetadataFilePath, err) } files = append(files, f) - pagesFilePath := filepath.Join(path, boot.CheckpointPagesFileName) + pagesFilePath := filepath.Join(path, checkpointfiles.PagesFileName) pagesWriteFlags := os.O_CREATE | os.O_EXCL | os.O_RDWR if direct { // The writes will be page-aligned, so it can be opened with O_DIRECT.