-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathiscsi.go
801 lines (697 loc) · 22.5 KB
/
iscsi.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
package iscsi
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"time"
klog "k8s.io/klog/v2"
)
const defaultPort = "3260"
var (
execCommand = exec.Command
execCommandContext = exec.CommandContext
execWithTimeout = ExecWithTimeout
osStat = os.Stat
filepathGlob = filepath.Glob
osOpenFile = os.OpenFile
sleep = time.Sleep
)
// iscsiSession contains information about an iSCSI session
type iscsiSession struct {
Protocol string
ID int32
Portal string
IQN string
Name string
}
type deviceInfo []Device
// Device contains information about a device
type Device struct {
Name string `json:"name"`
Hctl string `json:"hctl"`
Children []Device `json:"children"`
Type string `json:"type"`
Transport string `json:"tran"`
Size string `json:"size,omitempty"`
}
type HCTL struct {
HBA int
Channel int
Target int
LUN int
}
// Connector provides a struct to hold all the needed parameters to make our iSCSI connection
type Connector struct {
VolumeName string `json:"volume_name"`
TargetIqn string `json:"target_iqn"`
TargetPortals []string `json:"target_portal"`
Lun int32 `json:"lun"`
AuthType string `json:"auth_type"`
DiscoverySecrets Secrets `json:"discovery_secrets"`
SessionSecrets Secrets `json:"session_secrets"`
Interface string `json:"interface"`
MountTargetDevice *Device `json:"mount_target_device"`
Devices []Device `json:"devices"`
RetryCount uint `json:"retry_count"`
CheckInterval uint `json:"check_interval"`
DoDiscovery bool `json:"do_discovery"`
DoCHAPDiscovery bool `json:"do_chap_discovery"`
}
// parseSession takes the raw stdout from the `iscsiadm -m session` command and encodes it into an iSCSI session type
func parseSessions(lines string) []iscsiSession {
entries := strings.Split(strings.TrimSpace(lines), "\n")
r := strings.NewReplacer("[", "",
"]", "")
var sessions []iscsiSession
for _, entry := range entries {
e := strings.Fields(entry)
if len(e) < 4 {
continue
}
protocol := strings.Split(e[0], ":")[0]
id := r.Replace(e[1])
id64, _ := strconv.ParseInt(id, 10, 32)
portal := strings.Split(e[2], ",")[0]
s := iscsiSession{
Protocol: protocol,
ID: int32(id64),
Portal: portal,
IQN: e[3],
Name: strings.Split(e[3], ":")[1],
}
sessions = append(sessions, s)
}
return sessions
}
// sessionExists checks if an iSCSI session exists
func sessionExists(tgtPortal, tgtIQN string) (bool, error) {
sessions, err := getCurrentSessions()
if err != nil {
return false, err
}
for _, s := range sessions {
if tgtIQN == s.IQN && tgtPortal == s.Portal {
return true, nil
}
}
return false, nil
}
// extractTransportName returns a transport_name from getCurrentSessions output
func extractTransportName(output string) string {
res := regexp.MustCompile(`iface.transport_name = (.*)\n`).FindStringSubmatch(output)
if res == nil {
return ""
}
if res[1] == "" {
return "tcp"
}
return res[1]
}
// getCurrentSessions list current iSCSI sessions
func getCurrentSessions() ([]iscsiSession, error) {
out, err := GetSessions()
if err != nil {
exitErr, ok := err.(*exec.ExitError)
if ok && exitErr.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() == 21 {
return []iscsiSession{}, nil
}
return nil, err
}
sessions := parseSessions(out)
return sessions, err
}
// waitForPathToExist wait for a file at a path to exists on disk
func waitForPathToExist(devicePath *string, maxRetries, intervalSeconds uint, deviceTransport string) error {
if devicePath == nil || *devicePath == "" {
return fmt.Errorf("unable to check unspecified devicePath")
}
for i := uint(0); i <= maxRetries; i++ {
if i != 0 {
klog.V(2).Infof("Device path %q doesn't exists yet, retrying in %d seconds (%d/%d)", *devicePath, intervalSeconds, i, maxRetries)
sleep(time.Second * time.Duration(intervalSeconds))
}
if err := pathExists(devicePath, deviceTransport); err == nil {
return nil
} else if !os.IsNotExist(err) {
return err
}
}
return os.ErrNotExist
}
// pathExists checks if a file at a path exists on disk
func pathExists(devicePath *string, deviceTransport string) error {
if deviceTransport == "tcp" {
_, err := osStat(*devicePath)
if err != nil {
if !os.IsNotExist(err) {
klog.V(2).Infof("Error attempting to stat device: %s", err.Error())
return err
}
klog.V(2).Infof("Device not found for: %s", *devicePath)
return err
}
} else {
fpath, err := filepathGlob(*devicePath)
if err != nil {
return err
}
if fpath == nil {
return os.ErrNotExist
}
// There might be a case that fpath contains multiple device paths if
// multiple PCI devices connect to same iscsi target. We handle this
// case at subsequent logic. Pick up only first path here.
*devicePath = fpath[0]
}
return nil
}
// getMultipathDevice returns a multipath device for the configured targets if it exists
func getMultipathDevice(devices []Device) (*Device, error) {
var multipathDevice *Device
for _, device := range devices {
if len(device.Children) != 1 {
multipathdNotRunning := ""
if len(device.Children) == 0 {
multipathdNotRunning = " (is multipathd running?)"
}
return nil, fmt.Errorf("device is not mapped to exactly one multipath device%s: %v", multipathdNotRunning, device.Children)
}
if multipathDevice != nil && device.Children[0].Name != multipathDevice.Name {
return nil, fmt.Errorf("devices don't share a common multipath device: %v", devices)
}
multipathDevice = &device.Children[0]
}
if multipathDevice == nil {
return nil, fmt.Errorf("multipath device not found")
}
if multipathDevice.Type != "mpath" {
return nil, fmt.Errorf("device is not of mpath type: %v", multipathDevice)
}
return multipathDevice, nil
}
// Connect is for backward-compatibility with c.Connect()
func Connect(c Connector) (string, error) {
return c.Connect()
}
// Connect attempts to connect a volume to this node using the provided Connector info
func (c *Connector) Connect() (string, error) {
if c.RetryCount == 0 {
c.RetryCount = 10
}
if c.CheckInterval == 0 {
c.CheckInterval = 1
}
iFace := "default"
if c.Interface != "" {
iFace = c.Interface
}
// make sure our iface exists and extract the transport type
out, err := ShowInterface(iFace)
if err != nil {
return "", err
}
iscsiTransport := extractTransportName(out)
var lastErr error
var devicePaths []string
for _, target := range c.TargetPortals {
devicePath, err := c.connectTarget(c.TargetIqn, target, iFace, iscsiTransport)
if err != nil {
lastErr = err
} else {
klog.V(2).Infof("Appending device path: %s", devicePath)
devicePaths = append(devicePaths, devicePath)
}
}
// GetISCSIDevices returns all devices if no paths are given
if len(devicePaths) < 1 {
c.Devices = []Device{}
} else if c.Devices, err = GetISCSIDevices(devicePaths, true); err != nil {
return "", err
}
if len(c.Devices) < 1 {
_, _ = iscsiCmd([]string{"-m", "iface", "-I", iFace, "-o", "delete"}...)
return "", fmt.Errorf("failed to find device path: %s, last error seen: %v", devicePaths, lastErr)
}
mountTargetDevice, err := c.getMountTargetDevice()
c.MountTargetDevice = mountTargetDevice
if err != nil {
klog.V(2).Infof("Connect failed: %v", err)
err := RemoveSCSIDevices(c.Devices...)
if err != nil {
return "", err
}
c.MountTargetDevice = nil
c.Devices = []Device{}
return "", err
}
if c.IsMultipathEnabled() {
if err := c.IsMultipathConsistent(); err != nil {
return "", fmt.Errorf("multipath is inconsistent: %v", err)
}
}
return c.MountTargetDevice.GetPath(), nil
}
func (c *Connector) connectTarget(targetIqn string, target string, iFace string, iscsiTransport string) (string, error) {
klog.V(2).Infof("Process targetIqn: %s, portal: %s\n", targetIqn, target)
targetParts := strings.Split(target, ":")
targetPortal := targetParts[0]
targetPort := defaultPort
if len(targetParts) > 1 {
targetPort = targetParts[1]
}
baseArgs := []string{"-m", "node", "-T", targetIqn, "-p", targetPortal}
// Rescan sessions to discover newly mapped LUNs. Do not specify the interface when rescanning
// to avoid establishing additional sessions to the same target.
if _, err := iscsiCmd(append(baseArgs, []string{"-R"}...)...); err != nil {
klog.V(2).Infof("Failed to rescan session, err: %v", err)
if os.IsTimeout(err) {
klog.V(2).Infof("iscsiadm timeout, logging out")
cmd := execCommand("iscsiadm", append(baseArgs, []string{"-u"}...)...)
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("could not logout from target: %s", out)
}
}
}
// create our devicePath that we'll be looking for based on the transport being used
// portal with port
portal := strings.Join([]string{targetPortal, targetPort}, ":")
devicePath := strings.Join([]string{"/dev/disk/by-path/ip", portal, "iscsi", targetIqn, "lun", fmt.Sprint(c.Lun)}, "-")
if iscsiTransport != "tcp" {
devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", portal, "iscsi", targetIqn, "lun", fmt.Sprint(c.Lun)}, "-")
}
exists, _ := sessionExists(portal, targetIqn)
if exists {
klog.V(2).Infof("Session already exists, checking if device path %q exists", devicePath)
if err := waitForPathToExist(&devicePath, c.RetryCount, c.CheckInterval, iscsiTransport); err != nil {
return "", err
}
return devicePath, nil
}
if err := c.discoverTarget(targetIqn, iFace, portal); err != nil {
return "", err
}
// perform the login
err := Login(targetIqn, portal)
if err != nil {
klog.V(2).Infof("Failed to login: %v", err)
return "", err
}
klog.V(2).Infof("Waiting for device path %q to exist", devicePath)
if err := waitForPathToExist(&devicePath, c.RetryCount, c.CheckInterval, iscsiTransport); err != nil {
return "", err
}
return devicePath, nil
}
func (c *Connector) discoverTarget(targetIqn string, iFace string, portal string) error {
if c.DoDiscovery {
// build discoverydb and discover iscsi target
if err := Discoverydb(portal, iFace, c.DiscoverySecrets, c.DoCHAPDiscovery); err != nil {
klog.V(2).Infof("Error in discovery of the target: %s\n", err.Error())
return err
}
}
if c.DoCHAPDiscovery {
// Make sure we don't log the secrets
err := CreateDBEntry(targetIqn, portal, iFace, c.DiscoverySecrets, c.SessionSecrets)
if err != nil {
klog.V(2).Infof("Error creating db entry: %s\n", err.Error())
return err
}
}
return nil
}
// Disconnect is for backward-compatibility with c.Disconnect()
func Disconnect(targetIqn string, targets []string) {
for _, target := range targets {
targetPortal := strings.Split(target, ":")[0]
err := Logout(targetIqn, targetPortal)
if err != nil {
return
}
}
deleted := map[string]bool{}
if _, ok := deleted[targetIqn]; ok {
return
}
deleted[targetIqn] = true
err := DeleteDBEntry(targetIqn)
if err != nil {
return
}
}
// Disconnect performs a disconnect operation from an appliance.
// Be sure to disconnect all devices properly before doing this as it can result in data loss.
func (c *Connector) Disconnect() {
Disconnect(c.TargetIqn, c.TargetPortals)
}
// DisconnectVolume removes a volume from a Linux host.
func (c *Connector) DisconnectVolume() error {
// Steps to safely remove an iSCSI storage volume from a Linux host are as following:
// 1. Unmount the disk from a filesystem on the system.
// 2. Flush the multipath map for the disk we’re removing (if multipath is enabled).
// 3. Remove the physical disk entities that Linux maintains.
// 4. Take the storage volume (disk) offline on the storage subsystem.
// 5. Rescan the iSCSI sessions (after unmapping only).
//
// DisconnectVolume focuses on step 2 and 3.
// Note: make sure the volume is already unmounted before calling this method.
if c.IsMultipathEnabled() {
if err := c.IsMultipathConsistent(); err != nil {
return fmt.Errorf("multipath is inconsistent: %v", err)
}
klog.V(2).Infof("Removing multipath device in path %s.\n", c.MountTargetDevice.GetPath())
err := FlushMultipathDevice(c.MountTargetDevice)
if err != nil {
return err
}
if err := RemoveSCSIDevices(c.Devices...); err != nil {
return err
}
} else {
devicePath := c.MountTargetDevice.GetPath()
klog.V(2).Infof("Removing normal device in path %s.\n", devicePath)
if err := RemoveSCSIDevices(*c.MountTargetDevice); err != nil {
return err
}
}
klog.V(2).Infof("Finished disconnecting volume.\n")
return nil
}
// getMountTargetDevice returns the device to be mounted among the configured devices
func (c *Connector) getMountTargetDevice() (*Device, error) {
if len(c.Devices) > 1 {
multipathDevice, err := getMultipathDevice(c.Devices)
if err != nil {
klog.V(2).Infof("mount target is not a multipath device: %v", err)
return nil, err
}
klog.V(2).Infof("mount target is a multipath device")
return multipathDevice, nil
}
if len(c.Devices) == 0 {
return nil, fmt.Errorf("could not find mount target device: connector does not contain any device")
}
return &c.Devices[0], nil
}
// IsMultipathEnabled check if multipath is enabled on devices handled by this connector
func (c *Connector) IsMultipathEnabled() bool {
return c.MountTargetDevice.Type == "mpath"
}
// GetSCSIDevices get SCSI devices from device paths
// It will returns all SCSI devices if no paths are given
func GetSCSIDevices(devicePaths []string, strict bool) ([]Device, error) {
klog.V(2).Infof("Getting info about SCSI devices %s.\n", devicePaths)
deviceInfo, err := lsblk(devicePaths, strict)
if err != nil {
klog.V(2).Infof("An error occurred while looking info about SCSI devices: %v", err)
return nil, err
}
return deviceInfo, nil
}
// GetISCSIDevices get iSCSI devices from device paths
// It will returns all iSCSI devices if no paths are given
func GetISCSIDevices(devicePaths []string, strict bool) (devices []Device, err error) {
scsiDevices, err := GetSCSIDevices(devicePaths, strict)
if err != nil {
return
}
for i := range scsiDevices {
device := &scsiDevices[i]
if device.Transport == "iscsi" {
devices = append(devices, *device)
}
}
return
}
// lsblk execute the lsblk commands
func lsblk(devicePaths []string, strict bool) (deviceInfo, error) {
flags := []string{"-rn", "-o", "NAME,KNAME,PKNAME,HCTL,TYPE,TRAN,SIZE"}
command := execCommand("lsblk", append(flags, devicePaths...)...)
klog.V(2).Infof(command.String())
out, err := command.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("%s, (%w)", strings.Trim(string(ee.Stderr), "\n"), ee)
if strict || ee.ExitCode() != 64 { // ignore the error if some devices have been found when not strict
return nil, err
}
klog.V(2).Infof("Could find only some devices: %v", err)
} else {
return nil, err
}
}
var devices []*Device
devicesMap := make(map[string]*Device)
pkNames := []string{}
// Parse devices
lines := strings.Split(strings.Trim(string(out), "\n"), "\n")
for _, line := range lines {
columns := strings.Split(line, " ")
if len(columns) < 5 {
return nil, fmt.Errorf("invalid output from lsblk: %s", line)
}
device := &Device{
Name: columns[0],
Hctl: columns[3],
Type: columns[4],
Transport: columns[5],
Size: columns[6],
}
devices = append(devices, device)
pkNames = append(pkNames, columns[2])
devicesMap[columns[1]] = device
}
// Reconstruct devices tree
for i, pkName := range pkNames {
if pkName == "" {
continue
}
device := devices[i]
parent, ok := devicesMap[pkName]
if !ok {
return nil, fmt.Errorf("invalid output from lsblk: parent device %q not found", pkName)
}
if parent.Children == nil {
parent.Children = []Device{}
}
parent.Children = append(devicesMap[pkName].Children, *device)
}
// Filter devices to keep only the roots of the tree
var deviceInfo deviceInfo
for i, device := range devices {
if pkNames[i] == "" {
deviceInfo = append(deviceInfo, *device)
}
}
return deviceInfo, nil
}
// writeInSCSIDeviceFile write into special devices files to change devices state
func writeInSCSIDeviceFile(hctl string, file string, content string) error {
filename := filepath.Join("/sys/class/scsi_device", hctl, "device", file)
klog.V(2).Infof("Write %q in %q.\n", content, filename)
f, err := osOpenFile(filename, os.O_TRUNC|os.O_WRONLY, 0o200)
if err != nil {
klog.V(2).Infof("Error while opening file %v: %v\n", filename, err)
return err
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {
klog.V(2).Infof("Error while writing to file %v: %v", filename, err)
return err
}
return nil
}
// RemoveSCSIDevices removes SCSI device(s) from a Linux host.
func RemoveSCSIDevices(devices ...Device) error {
klog.V(2).Infof("Removing SCSI devices %v.\n", devices)
var errs []error
for _, device := range devices {
klog.V(2).Infof("Flush SCSI device %v.\n", device.Name)
if err := device.Exists(); err == nil {
out, err := execCommand("blockdev", "--flushbufs", device.GetPath()).CombinedOutput()
if err != nil {
klog.V(2).Infof("Command 'blockdev --flushbufs %s' did not succeed to flush the device: %v\n", device.GetPath(), err)
return errors.New(string(out))
}
} else if !os.IsNotExist(err) {
return err
}
klog.V(2).Infof("Put SCSI device %q offline.\n", device.Name)
err := device.Shutdown()
if err != nil {
if !os.IsNotExist(err) { // Ignore device already removed
errs = append(errs, err)
}
continue
}
klog.V(2).Infof("Delete SCSI device %q.\n", device.Name)
err = device.Delete()
if err != nil {
if !os.IsNotExist(err) { // Ignore device already removed
errs = append(errs, err)
}
continue
}
}
if len(errs) > 0 {
return errs[0]
}
klog.V(2).Infof("Finished removing SCSI devices.")
return nil
}
// PersistConnector is for backward-compatibility with c.Persist()
func PersistConnector(c *Connector, filePath string) error {
return c.Persist(filePath)
}
// Persist persists the Connector to the specified file (ie /var/lib/pfile/myConnector.json)
func (c *Connector) Persist(filePath string) error {
// file := path.Join("mnt", c.VolumeName+".json")
f, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("error creating iSCSI persistence file %s: %s", filePath, err)
}
defer f.Close()
encoder := json.NewEncoder(f)
if err = encoder.Encode(c); err != nil {
return fmt.Errorf("error encoding connector: %v", err)
}
return nil
}
// GetConnectorFromFile attempts to create a Connector using the specified json file (ie /var/lib/pfile/myConnector.json)
func GetConnectorFromFile(filePath string) (*Connector, error) {
f, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
c := Connector{}
err = json.Unmarshal([]byte(f), &c)
if err != nil {
return nil, err
}
devicePaths := []string{}
for _, device := range c.Devices {
devicePaths = append(devicePaths, device.GetPath())
}
if c.MountTargetDevice == nil {
return nil, fmt.Errorf("mountTargetDevice in the connector is nil")
}
devices, err := GetSCSIDevices([]string{c.MountTargetDevice.GetPath()}, false)
if err != nil {
return nil, err
}
if len(devices) == 0 {
return nil, fmt.Errorf("mountTargetDevice %s not found", c.MountTargetDevice.GetPath())
}
c.MountTargetDevice = &devices[0]
if c.Devices, err = GetSCSIDevices(devicePaths, false); err != nil {
return nil, err
}
return &c, nil
}
// IsMultipathConsistent check if the currently used device is using a consistent multipath mapping
func (c *Connector) IsMultipathConsistent() error {
devices := append([]Device{*c.MountTargetDevice}, c.Devices...)
referenceLUN := struct {
LUN int
Name string
}{LUN: -1, Name: ""}
HBA := map[int]string{}
referenceDevice := devices[0]
for _, device := range devices {
if device.Size != referenceDevice.Size {
return fmt.Errorf("devices size differ: %s (%s) != %s (%s)", device.Name, device.Size, referenceDevice.Name, referenceDevice.Size)
}
if device.Type != "mpath" {
hctl, err := device.HCTL()
if err != nil {
return err
}
if referenceLUN.LUN == -1 {
referenceLUN.LUN = hctl.LUN
referenceLUN.Name = device.Name
} else if hctl.LUN != referenceLUN.LUN {
return fmt.Errorf("devices LUNs differ: %s (%d) != %s (%d)", device.Name, hctl.LUN, referenceLUN.Name, referenceLUN.LUN)
}
if name, ok := HBA[hctl.HBA]; !ok {
HBA[hctl.HBA] = device.Name
} else {
return fmt.Errorf("two devices are using the same controller (%d): %s and %s", hctl.HBA, device.Name, name)
}
}
wwid, err := device.WWID()
if err != nil {
return fmt.Errorf("could not find WWID for device %s: %v", device.Name, err)
}
if wwid != referenceDevice.Name {
return fmt.Errorf("devices WWIDs differ: %s (wwid:%s) != %s (wwid:%s)", device.Name, wwid, referenceDevice.Name, referenceDevice.Name)
}
}
return nil
}
// Exists check if the device exists at its path and returns an error otherwise
func (d *Device) Exists() error {
_, err := osStat(d.GetPath())
return err
}
// GetPath returns the path of a device
func (d *Device) GetPath() string {
if d.Type == "mpath" {
return filepath.Join("/dev/mapper", d.Name)
}
return filepath.Join("/dev", d.Name)
}
// WWID returns the WWID of a device
func (d *Device) WWID() (string, error) {
timeout := 1 * time.Second
out, err := execWithTimeout("scsi_id", []string{"-g", "-u", d.GetPath()}, timeout)
if err != nil {
return "", err
}
return string(out[:len(out)-1]), nil
}
// HCTL returns the HCTL of a device
func (d *Device) HCTL() (*HCTL, error) {
var hctl []int
for _, idstr := range strings.Split(d.Hctl, ":") {
id, err := strconv.Atoi(idstr)
if err != nil {
hctl = []int{}
break
}
hctl = append(hctl, id)
}
if len(hctl) != 4 {
return nil, fmt.Errorf("invalid HCTL (%s) for device %q", d.Hctl, d.Name)
}
return &HCTL{
HBA: hctl[0],
Channel: hctl[1],
Target: hctl[2],
LUN: hctl[3],
}, nil
}
// WriteDeviceFile write in a device file
func (d *Device) WriteDeviceFile(name string, content string) error {
return writeInSCSIDeviceFile(d.Hctl, name, content)
}
// Shutdown turn off an SCSI device by writing offline\n in /sys/class/scsi_device/h:c:t:l/device/state
func (d *Device) Shutdown() error {
return d.WriteDeviceFile("state", "offline\n")
}
// Delete detach an SCSI device by writing 1 in /sys/class/scsi_device/h:c:t:l/device/delete
func (d *Device) Delete() error {
return d.WriteDeviceFile("delete", "1")
}
// Rescan does a rescan of SCSI device by writing 1 in /sys/class/scsi_device/h:c:t:l/device/rescan
func (d *Device) Rescan() error {
return d.WriteDeviceFile("rescan", "1")
}