From 11b78255de6544fc91d5f523bdfec2bef2a711ca Mon Sep 17 00:00:00 2001 From: Qiyuan Liang Date: Mon, 9 Dec 2024 13:06:37 +0800 Subject: [PATCH] cmd: add syncfs option to ctr command Signed-off-by: Qiyuan Liang --- cmd/ctr/commands/images/import.go | 7 ++++++- cmd/ctr/commands/images/mount.go | 7 ++++++- cmd/ctr/commands/images/pull.go | 7 ++++++- cmd/ctr/commands/run/run.go | 4 ++++ cmd/ctr/commands/run/run_unix.go | 3 ++- cmd/ctr/commands/run/run_windows.go | 3 ++- cmd/ctr/commands/snapshots/snapshots.go | 19 +++++++++++++------ 7 files changed, 39 insertions(+), 11 deletions(-) diff --git a/cmd/ctr/commands/images/import.go b/cmd/ctr/commands/images/import.go index cb94d97a22eb..4a775507e391 100644 --- a/cmd/ctr/commands/images/import.go +++ b/cmd/ctr/commands/images/import.go @@ -28,6 +28,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/containerd/v2/cmd/ctr/commands" + "github.com/containerd/containerd/v2/core/diff" "github.com/containerd/containerd/v2/core/images/archive" "github.com/containerd/containerd/v2/core/transfer" tarchive "github.com/containerd/containerd/v2/core/transfer/archive" @@ -99,6 +100,10 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb Name: "discard-unpacked-layers", Usage: "Allow the garbage collector to clean layers up from the content store after unpacking, cannot be used with --no-unpack, false by default", }, + &cli.BoolFlag{ + Name: "sync-fs", + Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default", + }, }, append(commands.SnapshotterFlags, commands.LabelFlag)...), Action: func(cliContext *cli.Context) error { @@ -290,7 +295,7 @@ If foobar.tar contains an OCI ref named "latest" and anonymous ref "sha256:deadb // TODO: Show unpack status fmt.Printf("unpacking %s (%s)...", img.Name, img.Target.Digest) - err = image.Unpack(ctx, cliContext.String("snapshotter")) + err = image.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))) if err != nil { return err } diff --git a/cmd/ctr/commands/images/mount.go b/cmd/ctr/commands/images/mount.go index c979542671a5..87c0b3e0bbaf 100644 --- a/cmd/ctr/commands/images/mount.go +++ b/cmd/ctr/commands/images/mount.go @@ -23,6 +23,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/containerd/v2/cmd/ctr/commands" + "github.com/containerd/containerd/v2/core/diff" "github.com/containerd/containerd/v2/core/leases" "github.com/containerd/containerd/v2/core/mount" "github.com/containerd/containerd/v2/defaults" @@ -50,6 +51,10 @@ When you are done, use the unmount command. Usage: "Mount the image for the specified platform", Value: platforms.DefaultString(), }, + &cli.BoolFlag{ + Name: "sync-fs", + Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default", + }, ), Action: func(cliContext *cli.Context) (retErr error) { var ( @@ -101,7 +106,7 @@ When you are done, use the unmount command. } i := containerd.NewImageWithPlatform(client, img, platforms.Only(p)) - if err := i.Unpack(ctx, snapshotter); err != nil { + if err := i.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil { return fmt.Errorf("error unpacking image: %w", err) } diff --git a/cmd/ctr/commands/images/pull.go b/cmd/ctr/commands/images/pull.go index d8e4b847fa22..8f0b19937576 100644 --- a/cmd/ctr/commands/images/pull.go +++ b/cmd/ctr/commands/images/pull.go @@ -28,6 +28,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/containerd/v2/cmd/ctr/commands" "github.com/containerd/containerd/v2/cmd/ctr/commands/content" + "github.com/containerd/containerd/v2/core/diff" "github.com/containerd/containerd/v2/core/images" "github.com/containerd/containerd/v2/core/transfer" "github.com/containerd/containerd/v2/core/transfer/image" @@ -84,6 +85,10 @@ command. As part of this process, we do the following: Name: "local", Usage: "Fetch content from local client rather than using transfer service", }, + &cli.BoolFlag{ + Name: "sync-fs", + Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default", + }, ), Action: func(cliContext *cli.Context) error { var ( @@ -203,7 +208,7 @@ command. As part of this process, we do the following: for _, platform := range p { fmt.Printf("unpacking %s %s...\n", platforms.Format(platform), img.Target.Digest) i := containerd.NewImageWithPlatform(client, img, platforms.Only(platform)) - err = i.Unpack(ctx, cliContext.String("snapshotter")) + err = i.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))) if err != nil { return err } diff --git a/cmd/ctr/commands/run/run.go b/cmd/ctr/commands/run/run.go index 799e37305544..e104c9577126 100644 --- a/cmd/ctr/commands/run/run.go +++ b/cmd/ctr/commands/run/run.go @@ -127,6 +127,10 @@ var Command = &cli.Command{ Name: "cni", Usage: "Enable cni networking for the container", }, + &cli.BoolFlag{ + Name: "sync-fs", + Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default", + }, }, append(platformRunFlags, append(commands.RuntimeFlags, append(append(commands.SnapshotterFlags, []cli.Flag{commands.SnapshotterLabels}...), diff --git a/cmd/ctr/commands/run/run_unix.go b/cmd/ctr/commands/run/run_unix.go index af58ed1c360a..5ab526d51eae 100644 --- a/cmd/ctr/commands/run/run_unix.go +++ b/cmd/ctr/commands/run/run_unix.go @@ -33,6 +33,7 @@ import ( "github.com/containerd/containerd/v2/contrib/nvidia" "github.com/containerd/containerd/v2/contrib/seccomp" "github.com/containerd/containerd/v2/core/containers" + "github.com/containerd/containerd/v2/core/diff" "github.com/containerd/containerd/v2/core/snapshots" "github.com/containerd/containerd/v2/pkg/oci" "github.com/containerd/log" @@ -149,7 +150,7 @@ func NewContainer(ctx context.Context, client *containerd.Client, cliContext *cl return nil, err } if !unpacked { - if err := image.Unpack(ctx, snapshotter); err != nil { + if err := image.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil { return nil, err } } diff --git a/cmd/ctr/commands/run/run_windows.go b/cmd/ctr/commands/run/run_windows.go index 56c0bdd6ebf6..f5c4f8f26878 100644 --- a/cmd/ctr/commands/run/run_windows.go +++ b/cmd/ctr/commands/run/run_windows.go @@ -25,6 +25,7 @@ import ( "github.com/containerd/console" containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/containerd/v2/cmd/ctr/commands" + "github.com/containerd/containerd/v2/core/diff" "github.com/containerd/containerd/v2/core/snapshots" "github.com/containerd/containerd/v2/pkg/netns" "github.com/containerd/containerd/v2/pkg/oci" @@ -91,7 +92,7 @@ func NewContainer(ctx context.Context, client *containerd.Client, cliContext *cl return nil, err } if !unpacked { - if err := image.Unpack(ctx, snapshotter); err != nil { + if err := image.Unpack(ctx, snapshotter, containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil { return nil, err } } diff --git a/cmd/ctr/commands/snapshots/snapshots.go b/cmd/ctr/commands/snapshots/snapshots.go index ce7fbc1b04dd..3ae1491cd24f 100644 --- a/cmd/ctr/commands/snapshots/snapshots.go +++ b/cmd/ctr/commands/snapshots/snapshots.go @@ -28,6 +28,12 @@ import ( "text/tabwriter" "time" + "github.com/containerd/log" + digest "github.com/opencontainers/go-digest" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/urfave/cli/v2" + + containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/containerd/v2/cmd/ctr/commands" "github.com/containerd/containerd/v2/core/content" "github.com/containerd/containerd/v2/core/diff" @@ -35,10 +41,6 @@ import ( "github.com/containerd/containerd/v2/core/snapshots" "github.com/containerd/containerd/v2/pkg/progress" "github.com/containerd/containerd/v2/pkg/rootfs" - "github.com/containerd/log" - digest "github.com/opencontainers/go-digest" - ocispec "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/urfave/cli/v2" ) // Command is the cli command for managing snapshots @@ -536,7 +538,12 @@ var unpackCommand = &cli.Command{ Name: "unpack", Usage: "Unpack applies layers from a manifest to a snapshot", ArgsUsage: "[flags] ", - Flags: commands.SnapshotterFlags, + Flags: append([]cli.Flag{ + &cli.BoolFlag{ + Name: "sync-fs", + Usage: "Synchronize the underlying filesystem containing files when unpack images, false by default", + }, + }, commands.SnapshotterFlags...), Action: func(cliContext *cli.Context) error { dgst, err := digest.Parse(cliContext.Args().First()) if err != nil { @@ -557,7 +564,7 @@ var unpackCommand = &cli.Command{ for _, image := range images { if image.Target().Digest == dgst { fmt.Printf("unpacking %s (%s)...", dgst, image.Target().MediaType) - if err := image.Unpack(ctx, cliContext.String("snapshotter")); err != nil { + if err := image.Unpack(ctx, cliContext.String("snapshotter"), containerd.WithUnpackApplyOpts(diff.WithSyncFs(cliContext.Bool("sync-fs")))); err != nil { fmt.Println() return err }