From e03ecd30a7503a9dc8421471daeb5bb4265765c8 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 16 Aug 2017 08:40:11 +0000 Subject: [PATCH] ctr: add `ctr snapshot view` Signed-off-by: Akihiro Suda --- cmd/ctr/snapshot.go | 59 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/cmd/ctr/snapshot.go b/cmd/ctr/snapshot.go index 741e741f4..ad07c4c1e 100644 --- a/cmd/ctr/snapshot.go +++ b/cmd/ctr/snapshot.go @@ -7,6 +7,7 @@ import ( "strings" "text/tabwriter" + "github.com/containerd/containerd/mount" "github.com/containerd/containerd/progress" "github.com/containerd/containerd/snapshot" "github.com/pkg/errors" @@ -22,6 +23,7 @@ var snapshotCommand = cli.Command{ usageSnapshotCommand, removeSnapshotCommand, prepareSnapshotCommand, + viewSnapshotCommand, treeSnapshotCommand, mountSnapshotCommand, commitSnapshotCommand, @@ -174,9 +176,47 @@ var prepareSnapshotCommand = cli.Command{ } if target != "" { - for _, m := range mounts { - fmt.Fprintf(os.Stdout, "mount -t %s %s %s -o %s\n", m.Type, m.Source, target, strings.Join(m.Options, ",")) - } + printMounts(target, mounts) + } + + return nil + }, +} + +var viewSnapshotCommand = cli.Command{ + Name: "view", + Usage: "create a read-only snapshot from a committed snapshot", + ArgsUsage: "[flags] []", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "target, t", + Usage: "mount target path, will print mount, if provided", + }, + }, + Action: func(clicontext *cli.Context) error { + ctx, cancel := appContext(clicontext) + defer cancel() + + if clicontext.NArg() != 2 { + return cli.ShowSubcommandHelp(clicontext) + } + + target := clicontext.String("target") + key := clicontext.Args().Get(0) + parent := clicontext.Args().Get(1) + + snapshotter, err := getSnapshotter(clicontext) + if err != nil { + return err + } + + mounts, err := snapshotter.View(ctx, key, parent) + if err != nil { + return err + } + + if target != "" { + printMounts(target, mounts) } return nil @@ -186,7 +226,7 @@ var prepareSnapshotCommand = cli.Command{ var mountSnapshotCommand = cli.Command{ Name: "mounts", Aliases: []string{"m", "mount"}, - Usage: "mount gets mount commands for the active snapshots", + Usage: "mount gets mount commands for the snapshots", ArgsUsage: "[flags] ", Action: func(clicontext *cli.Context) error { ctx, cancel := appContext(clicontext) @@ -208,9 +248,7 @@ var mountSnapshotCommand = cli.Command{ return err } - for _, m := range mounts { - fmt.Fprintf(os.Stdout, "mount -t %s %s %s -o %s\n", m.Type, m.Source, target, strings.Join(m.Options, ",")) - } + printMounts(target, mounts) return nil }, @@ -308,3 +346,10 @@ func printNode(name string, tree map[string]*snapshotTreeNode, level int) { printNode(child, tree, level) } } + +func printMounts(target string, mounts []mount.Mount) { + // FIXME: This is specific to Unix + for _, m := range mounts { + fmt.Printf("mount -t %s %s %s -o %s\n", m.Type, m.Source, target, strings.Join(m.Options, ",")) + } +}