forked from dell/gobrick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdm.go
137 lines (124 loc) · 4.06 KB
/
rdm.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
/*
Copyright © 2020-2022 Dell Inc. or its subsidiaries. All Rights Reserved.
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 gobrick is a generated GoMock package.
package gobrick
import (
"context"
"errors"
"fmt"
"os"
"path"
"strconv"
"github.com/dell/gobrick/internal/logger"
"github.com/dell/gobrick/internal/tracer"
"github.com/dell/gobrick/pkg/scsi"
log "github.com/sirupsen/logrus"
)
// RDMVolumeInfo has request info for RDM device
type RDMVolumeInfo struct {
Targets []FCTargetInfo
Lun int
WWN string
}
// ConnectRDMVolume attach RDM to a VM
func (fc *FCConnector) ConnectRDMVolume(ctx context.Context, info RDMVolumeInfo) (Device, error) {
defer tracer.TraceFuncCall(ctx, "FCConnector.ConnectVsphereVolume")()
if err := fc.limiter.Acquire(ctx, 1); err != nil {
return Device{}, errors.New("too many parallel operations. try later")
}
defer fc.limiter.Release(1)
if err := fc.validateRDMVolumeInfo(ctx, info); err != nil {
return Device{}, err
}
device, err := fc.connectRDM(ctx, info)
if err == nil {
return device, nil
}
logger.Error(ctx, "failed to connect RDM volume: %s, try to cleanup", err.Error())
_ = fc.cleanRDMConnection(ctx, true, info)
return Device{}, err
}
func (fc *FCConnector) cleanRDMConnection(ctx context.Context, force bool, info RDMVolumeInfo) error {
defer tracer.TraceFuncCall(ctx, "FCConnector.cleanRDMConnection")()
wwn := fmt.Sprintf("3%s", info.WWN)
devices, err := fc.scsi.GetDevicesByWWN(ctx, wwn)
if err != nil || len(devices) == 0 {
msg := "failed to get devices by WWN: " + wwn
logger.Error(ctx, msg)
return errors.New(msg)
}
logger.Info(ctx, "devices found: %s", devices)
return fc.baseConnector.cleanDevices(ctx, force, devices, wwn)
}
func (fc *FCConnector) validateRDMVolumeInfo(ctx context.Context, info RDMVolumeInfo) error {
defer tracer.TraceFuncCall(ctx, "FCConnector.validateRDMVolumeInfo")()
if len(info.Targets) == 0 {
return errors.New("at least one FC target required")
}
if info.WWN == "" {
return errors.New("wwn of RDM required")
}
return nil
}
func (fc *FCConnector) connectRDM(ctx context.Context, info RDMVolumeInfo) (Device, error) {
defer tracer.TraceFuncCall(ctx, "FCConnector.connectRDM")()
err := fc.rescanAllHosts(ctx)
if err != nil {
return Device{}, err
}
wwn := fmt.Sprintf("3%s", info.WWN)
devices, err := fc.scsi.GetDevicesByWWN(ctx, wwn)
if err != nil || len(devices) == 0 {
msg := "failed to get devices by WWN: " + wwn
logger.Error(ctx, msg)
return Device{}, errors.New(msg)
}
var device string
device, err = fc.waitSingleDevice(ctx, wwn, devices)
if err != nil {
return Device{}, err
}
if !fc.scsi.CheckDeviceIsValid(ctx, path.Join("/dev/", device)) {
msg := "device invalid"
logger.Error(ctx, msg)
return Device{}, errors.New(msg)
}
d := Device{WWN: wwn, Name: device}
return d, nil
}
func (fc *FCConnector) rescanAllHosts(ctx context.Context) error {
defer tracer.TraceFuncCall(ctx, "FCConnector.rescanAllHosts")()
hostsDir := "/sys/class/scsi_host"
hostFiles, err := os.ReadDir(fmt.Sprintf("%s/", hostsDir))
if err != nil {
log.Errorf("rescanSCSIHOSTALL failed to read scsi_host dir, err: %s", err.Error())
return err
}
log.Infof("found (%d) files in hostsDir (%s)", len(hostFiles), hostsDir)
scsiHost := scsi.NewSCSI("")
for host := 0; host < len(hostFiles); host++ {
// at least one target port not found, do full scsi rescan
hctl := scsi.HCTL{
Host: strconv.Itoa(host),
Lun: "-",
Channel: "-",
Target: "-",
}
err := scsiHost.RescanSCSIHostByHCTL(ctx, hctl)
if err != nil {
log.Error(ctx, err.Error())
continue
}
}
return nil
}