Skip to content

Commit 7f5e09e

Browse files
committed
Add volume protect/unprotect command
1 parent 17ae4ff commit 7f5e09e

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

cmd/podman/volumes/protect.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package volumes
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/containers/podman/v5/cmd/podman/common"
8+
"github.com/containers/podman/v5/cmd/podman/registry"
9+
"github.com/containers/podman/v5/pkg/domain/entities"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var (
14+
protectDescription = `Mark or unmark a volume as protected.
15+
16+
Protected volumes are excluded from system prune operations by default.`
17+
18+
protectCommand = &cobra.Command{
19+
Use: "protect [options] VOLUME [VOLUME...]",
20+
Short: "Mark or unmark volume as protected",
21+
Long: protectDescription,
22+
RunE: protect,
23+
ValidArgsFunction: common.AutocompleteVolumes,
24+
Example: `podman volume protect myvol
25+
podman volume protect --unprotect myvol
26+
podman volume protect vol1 vol2 vol3`,
27+
}
28+
)
29+
30+
var (
31+
protectOptions = entities.VolumeProtectOptions{}
32+
)
33+
34+
func init() {
35+
registry.Commands = append(registry.Commands, registry.CliCommand{
36+
Command: protectCommand,
37+
Parent: volumeCmd,
38+
})
39+
flags := protectCommand.Flags()
40+
flags.BoolVar(&protectOptions.Unprotect, "unprotect", false, "Remove protection from volume")
41+
}
42+
43+
func protect(cmd *cobra.Command, args []string) error {
44+
if len(args) < 1 {
45+
return fmt.Errorf("must specify at least one volume name")
46+
}
47+
48+
responses, err := registry.ContainerEngine().VolumeProtect(context.Background(), args, protectOptions)
49+
if err != nil {
50+
return err
51+
}
52+
53+
for _, r := range responses {
54+
if r.Err != nil {
55+
fmt.Printf("Error protecting volume %s: %v\n", r.Id, r.Err)
56+
} else {
57+
if protectOptions.Unprotect {
58+
fmt.Printf("Volume %s is now unprotected\n", r.Id)
59+
} else {
60+
fmt.Printf("Volume %s is now protected\n", r.Id)
61+
}
62+
}
63+
}
64+
65+
return nil
66+
}

0 commit comments

Comments
 (0)