summaryrefslogtreecommitdiff
path: root/cmd/podman/images/unmount.go
blob: 3ada099371e88ca6bd1b9a5ca8624d21be64583b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package images

import (
	"fmt"

	"github.com/containers/podman/v4/cmd/podman/common"
	"github.com/containers/podman/v4/cmd/podman/registry"
	"github.com/containers/podman/v4/cmd/podman/utils"
	"github.com/containers/podman/v4/pkg/domain/entities"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

var (
	description = `Image storage increments a mount counter each time an image is mounted.

  When an image is unmounted, the mount counter is decremented. The image's root filesystem is physically unmounted only when the mount counter reaches zero indicating no other processes are using the mount.

  An unmount can be forced with the --force flag.
`
	unmountCommand = &cobra.Command{
		Annotations:       map[string]string{registry.EngineMode: registry.ABIMode},
		Use:               "unmount [options] IMAGE [IMAGE...]",
		Aliases:           []string{"umount"},
		Short:             "Unmount an image's root filesystem",
		Long:              description,
		RunE:              unmount,
		ValidArgsFunction: common.AutocompleteImages,
		Example: `podman unmount imgID
  podman unmount imgID1 imgID2 imgID3
  podman unmount --all`,
	}
)

var (
	unmountOpts entities.ImageUnmountOptions
)

func unmountFlags(flags *pflag.FlagSet) {
	flags.BoolVarP(&unmountOpts.All, "all", "a", false, "Unmount all of the currently mounted images")
	flags.BoolVarP(&unmountOpts.Force, "force", "f", false, "Force the complete unmount of the specified mounted images")
}

func init() {
	registry.Commands = append(registry.Commands, registry.CliCommand{
		Parent:  imageCmd,
		Command: unmountCommand,
	})
	unmountFlags(unmountCommand.Flags())
}

func unmount(cmd *cobra.Command, args []string) error {
	var errs utils.OutputErrors
	if len(args) < 1 && !unmountOpts.All {
		return errors.New("image name or ID must be specified")
	}
	if len(args) > 0 && unmountOpts.All {
		return errors.New("when using the --all switch, you may not pass any image names or IDs")
	}
	reports, err := registry.ImageEngine().Unmount(registry.GetContext(), args, unmountOpts)
	if err != nil {
		return err
	}
	for _, r := range reports {
		if r.Err == nil {
			fmt.Println(r.Id)
		} else {
			errs = append(errs, r.Err)
		}
	}
	return errs.PrintErrors()
}