This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathblock_device_mounter_utils.go
211 lines (184 loc) · 7.87 KB
/
block_device_mounter_utils.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Copyright 2017 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package block_device_mounter_utils
import (
"os"
"path/filepath"
"time"
"github.com/IBM/ubiquity/remote/mounter/block_device_utils"
"github.com/IBM/ubiquity/utils/logs"
"github.com/nightlyone/lockfile"
)
const (
WarningMessageIdempotentDeviceAlreadyMounted = "Device is already mounted, so skip mounting (Idempotent)."
)
type blockDeviceMounterUtils struct {
logger logs.Logger
blockDeviceUtils block_device_utils.BlockDeviceUtils
rescanFlock lockfile.Lockfile
mpathFlock lockfile.Lockfile
}
func NewBlockDeviceMounterUtilsWithBlockDeviceUtils(blockDeviceUtils block_device_utils.BlockDeviceUtils) BlockDeviceMounterUtils {
return newBlockDeviceMounterUtils(blockDeviceUtils)
}
func NewBlockDeviceMounterUtils() BlockDeviceMounterUtils {
return newBlockDeviceMounterUtils(block_device_utils.NewBlockDeviceUtils())
}
func newBlockDeviceMounterUtils(blockDeviceUtils block_device_utils.BlockDeviceUtils) BlockDeviceMounterUtils {
rescanLock, err := lockfile.New(filepath.Join(os.TempDir(), "ubiquity.rescan.lock"))
if err != nil {
panic(err)
}
mpathLock, err := lockfile.New(filepath.Join(os.TempDir(), "ubiquity.mpath.lock"))
if err != nil {
panic(err)
}
return &blockDeviceMounterUtils{logger: logs.GetLogger(),
blockDeviceUtils: blockDeviceUtils,
rescanFlock: rescanLock,
mpathFlock: mpathLock,
}
}
// MountDeviceFlow create filesystem on the device (if needed) and then mount it on a given mountpoint
func (b *blockDeviceMounterUtils) MountDeviceFlow(devicePath string, fsType string, mountPoint string) error {
defer b.logger.Trace(logs.INFO, logs.Args{{"devicePath", devicePath}, {"fsType", fsType}, {"mountPoint", mountPoint}})()
needToCreateFS, err := b.blockDeviceUtils.CheckFs(devicePath)
if err != nil {
return b.logger.ErrorRet(err, "CheckFs failed")
}
if needToCreateFS {
if err = b.blockDeviceUtils.MakeFs(devicePath, fsType); err != nil {
return b.logger.ErrorRet(err, "MakeFs failed")
}
}
// Check if need to mount the device, if its already mounted then skip mounting
isMounted, mountpointRefs, err := b.blockDeviceUtils.IsDeviceMounted(devicePath)
if err != nil {
return b.logger.ErrorRet(err, "fail to identify if device is mounted")
}
if isMounted {
for _, mountpointi := range mountpointRefs {
if mountpointi == mountPoint {
b.logger.Warning(WarningMessageIdempotentDeviceAlreadyMounted, logs.Args{{"Device", devicePath}, {"mountpoint", mountPoint}})
return nil // Indicate idempotent issue
}
}
// In case device mounted but to different mountpoint as expected we fail with error. # TODO we may support it in the future after allow the umount flow to umount by mountpoint and not by device path.
return b.logger.ErrorRet(&DeviceAlreadyMountedToWrongMountpoint{devicePath, mountPoint}, "fail")
} else {
// Check if mountpoint directory is not already mounted to un expected device. If so raise error to prevent double mounting.
isMounted, devicesRefs, err := b.blockDeviceUtils.IsDirAMountPoint(mountPoint)
if err != nil {
return b.logger.ErrorRet(err, "fail to identify if mountpoint dir is actually mounted")
}
if isMounted {
return b.logger.ErrorRet(&DirPathAlreadyMountedToWrongDevice{
mountPoint: mountPoint, expectedDevice: devicePath, unexpectedDevicesRefs: devicesRefs},
"fail")
}
if err = b.blockDeviceUtils.MountFs(devicePath, mountPoint); err != nil {
return b.logger.ErrorRet(err, "MountFs failed")
}
}
return nil
}
// UnmountDeviceFlow umount device, clean device and remove mountpoint folder
func (b *blockDeviceMounterUtils) UnmountDeviceFlow(devicePath string, volumeWwn string) error {
// volumeWWN param is only used for logging - for the XAVI automation idempotent tests
// TODO consider also to receive the mountpoint(not only the devicepath), so the umount will use the mountpoint instead of the devicepath for future support double mounting of the same device.
defer b.logger.Trace(logs.INFO, logs.Args{{"devicePath", devicePath}})
err := b.blockDeviceUtils.SetDmsetup(devicePath)
if err != nil {
return b.logger.ErrorRet(err, "Dmsetup failed")
}
err = b.blockDeviceUtils.UmountFs(devicePath, volumeWwn)
if err != nil {
return b.logger.ErrorRet(err, "UmountFs failed")
}
// locking for concurrent md delete operation
b.logger.Debug("Ask for mpathLock for device", logs.Args{{"device", devicePath}})
for {
err := b.mpathFlock.TryLock()
if err == nil {
break
}
b.logger.Debug("mpathFlock.TryLock failed", logs.Args{{"error", err}})
time.Sleep(time.Duration(500 * time.Millisecond))
}
b.logger.Debug("Got mpathLock for device", logs.Args{{"device", devicePath}})
defer b.mpathFlock.Unlock()
defer b.logger.Debug("Released mpathLock for device", logs.Args{{"device", devicePath}})
if err := b.blockDeviceUtils.Cleanup(devicePath); err != nil {
return b.logger.ErrorRet(err, "Cleanup failed")
}
return nil
}
// RescanAll triggers the following OS rescanning :
// 1. iSCSI rescan (if protocol given is iscsi)
// 2. SCSI rescan
// 3. multipathing rescan
// return error if one of the steps fail
func (b *blockDeviceMounterUtils) RescanAll(wwn string, rescanForCleanUp bool, extraLunZeroScanning bool) error {
defer b.logger.Trace(logs.INFO)
// locking for concurrent rescans and reduce rescans if no need
b.logger.Debug("Ask for rescanLock for volumeWWN", logs.Args{{"volumeWWN", wwn}})
for {
err := b.rescanFlock.TryLock()
if err == nil {
break
}
b.logger.Debug("rescanLock.TryLock failed", logs.Args{{"error", err}})
time.Sleep(time.Duration(500 * time.Millisecond))
}
b.logger.Debug("Got rescanLock for volumeWWN", logs.Args{{"volumeWWN", wwn}})
defer b.rescanFlock.Unlock()
defer b.logger.Debug("Released rescanLock for volumeWWN", logs.Args{{"volumeWWN", wwn}})
if !rescanForCleanUp {
// Only when run rescan for new device, try to check if its already exist to reduce rescans
device, _ := b.Discover(wwn, false) // no deep discovery
if device != "" {
// if need rescan for discover new device but the new device is already exist then skip the rescan
b.logger.Debug(
"Skip rescan, because there is already multiple device for volumeWWN",
logs.Args{{"volumeWWN", wwn}, {"multiple", device}})
return nil
}
}
// TODO : if rescanForCleanUp we need to check if block device is not longer exist and if so skip the rescan!
// Do the rescans operations
// in case of FC : if no iscsiadm on the machine or no session login - this will log a warning not fail!
if err := b.blockDeviceUtils.Rescan(block_device_utils.ISCSI); err != nil {
return b.logger.ErrorRet(err, "ISCSI Rescan failed", logs.Args{{"protocol", block_device_utils.ISCSI}})
}
if extraLunZeroScanning {
if err := b.blockDeviceUtils.RescanSCSILun0(); err != nil {
return b.logger.ErrorRet(err, "Rescan failed for FC Lun0", logs.Args{{"protocol", block_device_utils.SCSI}})
}
} else {
if err := b.blockDeviceUtils.Rescan(block_device_utils.SCSI); err != nil {
return b.logger.ErrorRet(err, "OS Rescan failed", logs.Args{{"protocol", block_device_utils.SCSI}})
}
}
if !rescanForCleanUp {
if err := b.blockDeviceUtils.ReloadMultipath(); err != nil {
return b.logger.ErrorRet(err, "ReloadMultipath failed")
}
}
return nil
}
func (b *blockDeviceMounterUtils) Discover(volumeWwn string, deepDiscovery bool) (string, error) {
return b.blockDeviceUtils.Discover(volumeWwn, deepDiscovery)
}