summaryrefslogtreecommitdiff
path: root/cmd/podman/volumes/export.go
blob: f9e08be87993b6e36875c642567924e7f2ae01ea (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package volumes

import (
	"context"
	"errors"
	"fmt"

	"github.com/containers/common/pkg/completion"
	"github.com/containers/podman/v4/cmd/podman/common"
	"github.com/containers/podman/v4/cmd/podman/registry"
	"github.com/containers/podman/v4/pkg/domain/entities"
	"github.com/containers/podman/v4/pkg/errorhandling"
	"github.com/containers/podman/v4/utils"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

var (
	volumeExportDescription = `
podman volume export

Allow content of volume to be exported into external tar.`
	exportCommand = &cobra.Command{
		Annotations:       map[string]string{registry.EngineMode: registry.ABIMode},
		Use:               "export [options] VOLUME",
		Short:             "Export volumes",
		Args:              cobra.ExactArgs(1),
		Long:              volumeExportDescription,
		RunE:              export,
		ValidArgsFunction: common.AutocompleteVolumes,
	}
)

var (
	// Temporary struct to hold cli values.
	cliExportOpts = struct {
		Output string
	}{}
)

func init() {
	registry.Commands = append(registry.Commands, registry.CliCommand{
		Command: exportCommand,
		Parent:  volumeCmd,
	})
	flags := exportCommand.Flags()

	outputFlagName := "output"
	flags.StringVarP(&cliExportOpts.Output, outputFlagName, "o", "/dev/stdout", "Write to a specified file (default: stdout, which must be redirected)")
	_ = exportCommand.RegisterFlagCompletionFunc(outputFlagName, completion.AutocompleteDefault)
}

func export(cmd *cobra.Command, args []string) error {
	var inspectOpts entities.InspectOptions
	containerEngine := registry.ContainerEngine()
	ctx := context.Background()

	if cliExportOpts.Output == "" {
		return errors.New("expects output path, use --output=[path]")
	}
	inspectOpts.Type = common.VolumeType
	volumeData, errs, err := containerEngine.VolumeInspect(ctx, args, inspectOpts)
	if err != nil {
		return err
	}
	if len(errs) > 0 {
		return errorhandling.JoinErrors(errs)
	}
	if len(volumeData) < 1 {
		return errors.New("no volume data found")
	}
	mountPoint := volumeData[0].VolumeConfigResponse.Mountpoint
	driver := volumeData[0].VolumeConfigResponse.Driver
	volumeOptions := volumeData[0].VolumeConfigResponse.Options
	volumeMountStatus, err := containerEngine.VolumeMounted(ctx, args[0])
	if err != nil {
		return err
	}
	if mountPoint == "" {
		return errors.New("volume is not mounted anywhere on host")
	}
	// Check if volume is using external plugin and export only if volume is mounted
	if driver != "" && driver != "local" {
		if !volumeMountStatus.Value {
			return fmt.Errorf("volume is using a driver %s and volume is not mounted on %s", driver, mountPoint)
		}
	}
	// Check if volume is using `local` driver and has mount options type other than tmpfs
	if driver == "local" {
		if mountOptionType, ok := volumeOptions["type"]; ok {
			if mountOptionType != "tmpfs" && !volumeMountStatus.Value {
				return fmt.Errorf("volume is using a driver %s and volume is not mounted on %s", driver, mountPoint)
			}
		}
	}
	logrus.Debugf("Exporting volume data from %s to %s", mountPoint, cliExportOpts.Output)
	err = utils.CreateTarFromSrc(mountPoint, cliExportOpts.Output)
	return err
}