-
Notifications
You must be signed in to change notification settings - Fork 6
/
gofsutil_mount_linux.go
180 lines (152 loc) · 4.44 KB
/
gofsutil_mount_linux.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package gofsutil
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
log "github.com/sirupsen/logrus"
)
const (
procMountsPath = "/proc/self/mountinfo"
// procMountsRetries is number of times to retry for a consistent
// read of procMountsPath.
procMountsRetries = 3
)
var (
bindRemountOpts = []string{"remount"}
)
// getDiskFormat uses 'lsblk' to see if the given disk is unformatted
func (fs *FS) getDiskFormat(ctx context.Context, disk string) (string, error) {
args := []string{"-n", "-o", "FSTYPE", disk}
f := log.Fields{
"disk": disk,
}
log.WithFields(f).WithField("args", args).Info(
"checking if disk is formatted using lsblk")
buf, err := exec.Command("lsblk", args...).CombinedOutput()
out := string(buf)
log.WithField("output", out).Debug("lsblk output")
if err != nil {
log.WithFields(f).WithError(err).Error(
"failed to determine if disk is formatted")
return "", err
}
// Split lsblk output into lines. Unformatted devices should contain only
// "\n". Beware of "\n\n", that's a device with one empty partition.
out = strings.TrimSuffix(out, "\n") // Avoid last empty line
lines := strings.Split(out, "\n")
if lines[0] != "" {
// The device is formatted
return lines[0], nil
}
if len(lines) == 1 {
// The device is unformatted and has no dependent devices
return "", nil
}
// The device has dependent devices, most probably partitions (LVM, LUKS
// and MD RAID are reported as FSTYPE and caught above).
return "unknown data, probably partitions", nil
}
// formatAndMount uses unix utils to format and mount the given disk
func (fs *FS) formatAndMount(
ctx context.Context,
source, target, fsType string,
opts ...string) error {
opts = append(opts, "defaults")
f := log.Fields{
"source": source,
"target": target,
"fsType": fsType,
"options": opts,
}
// Try to mount the disk
log.WithFields(f).Info("attempting to mount disk")
mountErr := fs.mount(ctx, source, target, fsType, opts...)
if mountErr == nil {
return nil
}
// Mount failed. This indicates either that the disk is unformatted or
// it contains an unexpected filesystem.
existingFormat, err := fs.getDiskFormat(ctx, source)
if err != nil {
return err
}
if existingFormat == "" {
// Disk is unformatted so format it.
args := []string{source}
// Use 'ext4' as the default
if len(fsType) == 0 {
fsType = "ext4"
}
if fsType == "ext4" || fsType == "ext3" {
args = []string{"-F", source}
}
f["fsType"] = fsType
log.WithFields(f).Info(
"disk appears unformatted, attempting format")
mkfsCmd := fmt.Sprintf("mkfs.%s", fsType)
if err := exec.Command(mkfsCmd, args...).Run(); err != nil {
log.WithFields(f).WithError(err).Error(
"format of disk failed")
}
// the disk has been formatted successfully try to mount it again.
log.WithFields(f).Info(
"disk successfully formatted")
return fs.mount(ctx, source, target, fsType, opts...)
}
// Disk is already formatted and failed to mount
if len(fsType) == 0 || fsType == existingFormat {
// This is mount error
return mountErr
}
// Block device is formatted with unexpected filesystem
return fmt.Errorf(
"failed to mount volume as %q; already contains %s: error: %v",
fsType, existingFormat, mountErr)
}
// bindMount performs a bind mount
func (fs *FS) bindMount(
ctx context.Context,
source, target string,
opts ...string) error {
err := fs.doMount(ctx, "mount", source, target, "", "bind")
if err != nil {
return err
}
return fs.doMount(ctx, "mount", source, target, "", opts...)
}
// getMounts returns a slice of all the mounted filesystems
func (fs *FS) getMounts(ctx context.Context) ([]Info, error) {
_, hash1, err := fs.readProcMounts(ctx, procMountsPath, false)
if err != nil {
return nil, err
}
for i := 0; i < procMountsRetries; i++ {
mps, hash2, err := fs.readProcMounts(ctx, procMountsPath, true)
if err != nil {
return nil, err
}
if hash1 == hash2 {
// Success
return mps, nil
}
hash1 = hash2
}
return nil, fmt.Errorf(
"failed to get a consistent snapshot of %v after %d tries",
procMountsPath, procMountsRetries)
}
// readProcMounts reads procMountsInfo and produce a hash
// of the contents and a list of the mounts as Info objects.
func (fs *FS) readProcMounts(
ctx context.Context,
path string,
info bool) ([]Info, uint32, error) {
file, err := os.Open(path)
if err != nil {
return nil, 0, err
}
defer file.Close()
return ReadProcMountsFrom(ctx, file, !info, ProcMountsFields, fs.ScanEntry)
}