/
mount.go
144 lines (130 loc) · 3.8 KB
/
mount.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"fmt"
"os"
buildahcli "github.com/containers/buildah/pkg/cli"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type jsonMount struct {
Container string `json:"container,omitempty"`
MountPoint string `json:"mountPoint"`
}
type mountOptions struct {
json bool
}
func init() {
var (
mountDescription = `buildah mount
mounts a working container's root filesystem for manipulation.
Note: In rootless mode you need to first execute buildah unshare, to put you
into the usernamespace. Afterwards you can buildah mount the container and
view/modify the content in the containers root file system.
`
opts mountOptions
noTruncate bool
)
mountCommand := &cobra.Command{
Use: "mount",
Short: "Mount a working container's root filesystem",
Long: mountDescription,
RunE: func(cmd *cobra.Command, args []string) error {
return mountCmd(cmd, args, opts)
},
Example: `buildah mount
buildah mount containerID
buildah mount containerID1 containerID2
In rootless mode you must use buildah unshare first.
buildah unshare
buildah mount containerID
`,
}
mountCommand.SetUsageTemplate(UsageTemplate())
flags := mountCommand.Flags()
flags.SetInterspersed(false)
flags.BoolVar(&opts.json, "json", false, "output in JSON format")
flags.BoolVar(&noTruncate, "notruncate", false, "do not truncate output")
rootCmd.AddCommand(mountCommand)
if err := flags.MarkHidden("notruncate"); err != nil {
logrus.Fatalf("error marking notruncate as hidden: %v", err)
}
}
func mountCmd(c *cobra.Command, args []string, opts mountOptions) error {
if err := buildahcli.VerifyFlagsArgsOrder(args); err != nil {
return err
}
store, err := getStore(c)
if err != nil {
return err
}
var jsonMounts []jsonMount
var lastError error
if len(args) > 0 {
// Do not allow to mount a graphdriver that is not vfs if we are creating the userns as part
// of the mount command.
// Differently, allow the mount if we are already in a userns, as the mount point will still
// be accessible once "buildah mount" exits.
if os.Geteuid() != 0 && store.GraphDriverName() != "vfs" {
return errors.Errorf("cannot mount using driver %s in rootless mode. You need to run it in a `buildah unshare` session", store.GraphDriverName())
}
for _, name := range args {
builder, err := openBuilder(getContext(), store, name)
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "error reading build container %q", name)
continue
}
mountPoint, err := builder.Mount(builder.MountLabel)
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "error mounting %q container %q", name, builder.Container)
continue
}
if len(args) > 1 {
if opts.json {
jsonMounts = append(jsonMounts, jsonMount{Container: name, MountPoint: mountPoint})
continue
}
fmt.Printf("%s %s\n", name, mountPoint)
} else {
if opts.json {
jsonMounts = append(jsonMounts, jsonMount{MountPoint: mountPoint})
continue
}
fmt.Printf("%s\n", mountPoint)
}
}
} else {
builders, err := openBuilders(store)
if err != nil {
return errors.Wrapf(err, "error reading build containers")
}
for _, builder := range builders {
mounted, err := builder.Mounted()
if err != nil {
return err
}
if mounted {
if opts.json {
jsonMounts = append(jsonMounts, jsonMount{Container: builder.Container, MountPoint: builder.MountPoint})
continue
}
fmt.Printf("%s %s\n", builder.Container, builder.MountPoint)
}
}
}
if opts.json {
data, err := json.MarshalIndent(jsonMounts, "", " ")
if err != nil {
return err
}
fmt.Printf("%s\n", data)
}
return lastError
}