github.com/containerd/Containerd@v1.4.13/cmd/ctr/commands/images/unmount.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package images
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/containerd/containerd/cmd/ctr/commands"
    23  	"github.com/containerd/containerd/errdefs"
    24  	"github.com/containerd/containerd/leases"
    25  	"github.com/containerd/containerd/mount"
    26  	"github.com/pkg/errors"
    27  	"github.com/urfave/cli"
    28  )
    29  
    30  var unmountCommand = cli.Command{
    31  	Name:        "unmount",
    32  	Usage:       "unmount the image from the target",
    33  	ArgsUsage:   "[flags] <target>",
    34  	Description: "Unmount the image rootfs from the specified target.",
    35  	Flags: append(append(commands.RegistryFlags, append(commands.SnapshotterFlags, commands.LabelFlag)...),
    36  		cli.BoolFlag{
    37  			Name:  "rm",
    38  			Usage: "remove the snapshot after a successful unmount",
    39  		},
    40  	),
    41  	Action: func(context *cli.Context) error {
    42  		var (
    43  			target = context.Args().First()
    44  		)
    45  		if target == "" {
    46  			return fmt.Errorf("please provide a target path to mount to")
    47  		}
    48  
    49  		client, ctx, cancel, err := commands.NewClient(context)
    50  		if err != nil {
    51  			return err
    52  		}
    53  		defer cancel()
    54  
    55  		if err := mount.UnmountAll(target, 0); err != nil {
    56  			return err
    57  		}
    58  
    59  		if context.Bool("rm") {
    60  			snapshotter := context.String("snapshotter")
    61  			s := client.SnapshotService(snapshotter)
    62  			if err := client.LeasesService().Delete(ctx, leases.Lease{ID: target}); err != nil && !errdefs.IsNotFound(err) {
    63  				return errors.Wrap(err, "error deleting lease")
    64  			}
    65  			if err := s.Remove(ctx, target); err != nil && !errdefs.IsNotFound(err) {
    66  				return errors.Wrap(err, "error removing snapshot")
    67  			}
    68  		}
    69  
    70  		fmt.Fprintln(context.App.Writer, target)
    71  		return nil
    72  	},
    73  }