-
Notifications
You must be signed in to change notification settings - Fork 64
/
iscsi_util.go
120 lines (99 loc) · 3.5 KB
/
iscsi_util.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
/*
Copyright 2017 The Kubernetes Authors.
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 iscsi
import (
"fmt"
"os"
"path"
"github.com/golang/glog"
iscsi_lib "github.com/kubernetes-csi/csi-lib-iscsi/iscsi"
"k8s.io/kubernetes/pkg/util/mount"
)
type ISCSIUtil struct{}
func (util *ISCSIUtil) AttachDisk(b iscsiDiskMounter) (string, error) {
devicePath, err := iscsi_lib.Connect(*b.connector)
if err != nil {
return "", err
}
if devicePath == "" {
return "", fmt.Errorf("connect reported success, but no path returned")
}
// Mount device
mntPath := b.targetPath
notMnt, err := b.mounter.IsLikelyNotMountPoint(mntPath)
if err != nil && !os.IsNotExist(err) {
return "", fmt.Errorf("Heuristic determination of mount point failed:%v", err)
}
if !notMnt {
glog.Infof("iscsi: %s already mounted", mntPath)
return "", nil
}
if err := os.MkdirAll(mntPath, 0750); err != nil {
glog.Errorf("iscsi: failed to mkdir %s, error", mntPath)
return "", err
}
// Persist iscsi disk config to json file for DetachDisk path
file := path.Join(mntPath, b.VolName+".json")
err = iscsi_lib.PersistConnector(b.connector, file)
if err != nil {
glog.Errorf("failed to persist connection info: %v", err)
glog.Errorf("disconnecting volume and failing the publish request because persistence files are required for reliable Unpublish")
return "", fmt.Errorf("unable to create persistence file for connection")
}
var options []string
if b.readOnly {
options = append(options, "ro")
} else {
options = append(options, "rw")
}
options = append(options, b.mountOptions...)
err = b.mounter.FormatAndMount(devicePath, mntPath, b.fsType, options)
if err != nil {
glog.Errorf("iscsi: failed to mount iscsi volume %s [%s] to %s, error %v", devicePath, b.fsType, mntPath, err)
}
return devicePath, err
}
func (util *ISCSIUtil) DetachDisk(c iscsiDiskUnmounter, targetPath string) error {
_, cnt, err := mount.GetDeviceNameFromMount(c.mounter, targetPath)
if err != nil {
glog.Errorf("iscsi detach disk: failed to get device from mnt: %s\nError: %v", targetPath, err)
return err
}
if pathExists, pathErr := mount.PathExists(targetPath); pathErr != nil {
return fmt.Errorf("Error checking if path exists: %v", pathErr)
} else if !pathExists {
glog.Warningf("Warning: Unmount skipped because path does not exist: %v", targetPath)
return nil
}
if err = c.mounter.Unmount(targetPath); err != nil {
glog.Errorf("iscsi detach disk: failed to unmount: %s\nError: %v", targetPath, err)
return err
}
cnt--
if cnt != 0 {
return nil
}
// load iscsi disk config from json file
file := path.Join(targetPath, c.iscsiDisk.VolName+".json")
connector, err := iscsi_lib.GetConnectorFromFile(file)
if err != nil {
glog.Errorf("iscsi detach disk: failed to get iscsi config from path %s Error: %v", targetPath, err)
return err
}
iscsi_lib.Disconnect(connector.TargetIqn, connector.TargetPortals)
if err := os.RemoveAll(targetPath); err != nil {
glog.Errorf("iscsi: failed to remove mount path Error: %v", err)
return err
}
return nil
}