-
Notifications
You must be signed in to change notification settings - Fork 115
/
linux_mounter.go
194 lines (158 loc) · 5.19 KB
/
linux_mounter.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package disk
import (
"fmt"
"strings"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type linuxMounter struct {
runner boshsys.CmdRunner
mountsSearcher MountsSearcher
maxUnmountRetries int
unmountRetrySleep time.Duration
}
func NewLinuxMounter(
runner boshsys.CmdRunner,
mountsSearcher MountsSearcher,
unmountRetrySleep time.Duration,
) Mounter {
return linuxMounter{
runner: runner,
mountsSearcher: mountsSearcher,
maxUnmountRetries: 600,
unmountRetrySleep: unmountRetrySleep,
}
}
func (m linuxMounter) Mount(partitionPath, mountPoint string, mountOptions ...string) error {
return m.MountFilesystem(partitionPath, mountPoint, "", mountOptions...)
}
func (m linuxMounter) MountTmpfs(dir, size string) error {
_, dirIsMounted, err := m.IsMountPoint(dir)
if err != nil {
return bosherr.WrapErrorf(err, "Checking for mount point %s", dir)
}
if !dirIsMounted {
err = m.MountFilesystem("tmpfs", dir, "tmpfs", fmt.Sprintf("size=%s", size))
if err != nil {
return bosherr.WrapErrorf(err, "Mounting tmpfs to %s", dir)
}
}
return nil
}
func (m linuxMounter) MountFilesystem(partitionPath, mountPoint, fstype string, mountOptions ...string) error {
shouldMount, err := m.shouldMount(partitionPath, mountPoint)
if !shouldMount {
return err
}
if err != nil {
return bosherr.WrapError(err, "Checking whether partition should be mounted")
}
mountArgs := []string{partitionPath, mountPoint}
if fstype != "" {
mountArgs = append(mountArgs, "-t", fstype)
}
for _, mountOption := range mountOptions {
mountArgs = append(mountArgs, "-o", mountOption)
}
_, _, _, err = m.runner.RunCommand("mount", mountArgs...)
if err != nil {
return bosherr.WrapError(err, "Shelling out to mount")
}
return nil
}
func (m linuxMounter) RemountAsReadonly(mountPoint string) error {
return m.Remount(mountPoint, mountPoint, "ro")
}
func (m linuxMounter) Remount(fromMountPoint, toMountPoint string, mountOptions ...string) error {
partitionPath, found, err := m.IsMountPoint(fromMountPoint)
if err != nil || !found {
return bosherr.WrapErrorf(err, "Error finding device for mount point %s", fromMountPoint)
}
_, err = m.Unmount(fromMountPoint)
if err != nil {
return bosherr.WrapErrorf(err, "Unmounting %s", fromMountPoint)
}
return m.Mount(partitionPath, toMountPoint, mountOptions...)
}
func (m linuxMounter) SwapOn(partitionPath string) (err error) {
out, _, _, _ := m.runner.RunCommand("swapon", "-s")
for i, swapOnLines := range strings.Split(out, "\n") {
swapOnFields := strings.Fields(swapOnLines)
switch {
case i == 0:
continue
case len(swapOnFields) == 0:
continue
case swapOnFields[0] == partitionPath:
return nil
}
}
_, _, _, err = m.runner.RunCommand("swapon", partitionPath)
if err != nil {
return bosherr.WrapError(err, "Shelling out to swapon")
}
return nil
}
func (m linuxMounter) Unmount(partitionOrMountPoint string) (bool, error) {
isMounted, err := m.IsMounted(partitionOrMountPoint)
if err != nil || !isMounted {
return false, err
}
_, _, _, err = m.runner.RunCommand("umount", partitionOrMountPoint)
for i := 1; i < m.maxUnmountRetries && err != nil; i++ {
time.Sleep(m.unmountRetrySleep)
_, _, _, err = m.runner.RunCommand("umount", partitionOrMountPoint)
}
return err == nil, err
}
func (m linuxMounter) IsMountPoint(path string) (string, bool, error) {
mounts, err := m.mountsSearcher.SearchMounts()
if err != nil {
return "", false, bosherr.WrapError(err, "Searching mounts")
}
for _, mount := range mounts {
if mount.MountPoint == path {
return mount.PartitionPath, true, nil
}
}
return "", false, nil
}
func (m linuxMounter) IsMounted(partitionOrMountPoint string) (bool, error) {
mounts, err := m.mountsSearcher.SearchMounts()
if err != nil {
return false, bosherr.WrapError(err, "Searching mounts")
}
for _, mount := range mounts {
if mount.PartitionPath == partitionOrMountPoint || mount.MountPoint == partitionOrMountPoint {
return true, nil
}
}
return false, nil
}
func (m linuxMounter) shouldMount(partitionPath, mountPoint string) (bool, error) {
mounts, err := m.mountsSearcher.SearchMounts()
if err != nil {
return false, bosherr.WrapError(err, "Searching mounts")
}
for _, mount := range mounts {
switch {
case mount.PartitionPath == partitionPath && mount.MountPoint == mountPoint:
return false, nil
case mount.PartitionPath == partitionPath && mount.MountPoint != mountPoint && partitionPath != "tmpfs":
return false, bosherr.Errorf("Device %s is already mounted to %s, can't mount to %s",
mount.PartitionPath, mount.MountPoint, mountPoint)
case mount.MountPoint == mountPoint && partitionPath != "":
return false, bosherr.Errorf("Device %s is already mounted to %s, can't mount %s",
mount.PartitionPath, mount.MountPoint, partitionPath)
}
}
return true, nil
}
func (m linuxMounter) RemountInPlace(mountPoint string, mountOptions ...string) (err error) {
found, err := m.IsMounted(mountPoint)
if err != nil || !found {
return bosherr.WrapErrorf(err, "Error finding existing mount point %s", mountPoint)
}
return m.Mount("", mountPoint, append([]string{"remount"}, mountOptions...)...)
}