forked from akutz/gofsutil
/
gofsutil_mount_linux.go
173 lines (145 loc) · 4.01 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
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 'blkid' to see if the given disk is unformatted
func (fs *FS) getDiskFormat(ctx context.Context, disk string) (string, error) {
args := []string{"-s", "TYPE", "-o", "value", disk}
f := log.Fields{
"disk": disk,
}
log.WithFields(f).WithField("args", args).Info(
"checking if disk is formatted using blkid")
buf, err := exec.Command("blkid", args...).CombinedOutput()
out := string(buf)
log.WithField("output", out).Debug("blkid output")
if err != nil {
log.WithFields(f).WithError(err).Error(
"no filesystem found on disk")
return "", nil
}
fsType := strings.TrimSpace(out)
if fsType != "" {
// The device is formatted
log.WithFields(f).WithField("fsType", fsType).Info(
"disk is already formatted")
return fsType, nil
}
// The device is unformatted
return "", 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,
}
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")
return err
}
// the disk has been formatted successfully try to mount it.
log.WithFields(f).Info(
"disk successfully formatted")
return fs.mount(ctx, source, target, fsType, opts...)
}
// Try to mount the disk
log.WithFields(f).WithField("existingFSType", existingFormat).Info("attempting to mount disk")
mountErr := fs.mount(ctx, source, target, fsType, opts...)
if mountErr == nil {
return nil
}
// 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)
}