forked from juju/juju
/
blockdevices.go
106 lines (100 loc) · 3.03 KB
/
blockdevices.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package storagecommon
import (
"github.com/juju/loggo"
"github.com/juju/juju/state"
"github.com/juju/juju/storage"
)
var logger = loggo.GetLogger("juju.apiserver.storagecommon")
// BlockDeviceFromState translates a state.BlockDeviceInfo to a
// storage.BlockDevice.
func BlockDeviceFromState(in state.BlockDeviceInfo) storage.BlockDevice {
return storage.BlockDevice{
in.DeviceName,
in.DeviceLinks,
in.Label,
in.UUID,
in.HardwareId,
in.WWN,
in.BusAddress,
in.Size,
in.FilesystemType,
in.InUse,
in.MountPoint,
}
}
// MatchingBlockDevice finds the block device that matches the
// provided volume info and volume attachment info.
func MatchingBlockDevice(
blockDevices []state.BlockDeviceInfo,
volumeInfo state.VolumeInfo,
attachmentInfo state.VolumeAttachmentInfo,
planBlockInfo state.BlockDeviceInfo,
) (*state.BlockDeviceInfo, bool) {
logger.Tracef("looking for block device for volume %#v", volumeInfo)
for _, dev := range blockDevices {
if planBlockInfo.HardwareId != "" {
if planBlockInfo.HardwareId == dev.HardwareId {
logger.Tracef("plan hwid match on %v", volumeInfo.HardwareId)
return &dev, true
}
}
if planBlockInfo.WWN != "" {
if planBlockInfo.WWN == dev.WWN {
logger.Tracef("plan wwn match on %v", volumeInfo.WWN)
return &dev, true
}
continue
}
if planBlockInfo.DeviceName != "" {
if planBlockInfo.DeviceName == dev.DeviceName {
logger.Tracef("plan device name match on %v", attachmentInfo.DeviceName)
return &dev, true
}
continue
}
if volumeInfo.WWN != "" {
if volumeInfo.WWN == dev.WWN {
logger.Tracef("wwn match on %v", volumeInfo.WWN)
return &dev, true
}
logger.Tracef("no match for block device WWN: %v", dev.WWN)
continue
}
if volumeInfo.HardwareId != "" {
if volumeInfo.HardwareId == dev.HardwareId {
logger.Tracef("hwid match on %v", volumeInfo.HardwareId)
return &dev, true
}
logger.Tracef("no match for block device hardware id: %v", dev.HardwareId)
continue
}
if attachmentInfo.BusAddress != "" {
if attachmentInfo.BusAddress == dev.BusAddress {
logger.Tracef("bus address match on %v", attachmentInfo.BusAddress)
return &dev, true
}
logger.Tracef("no match for block device bus address: %v", dev.BusAddress)
continue
}
// Only match on block device link if the block device is published
// with device link information.
if attachmentInfo.DeviceLink != "" && len(dev.DeviceLinks) > 0 {
for _, link := range dev.DeviceLinks {
if attachmentInfo.DeviceLink == link {
logger.Tracef("device link match on %v", attachmentInfo.DeviceLink)
return &dev, true
}
}
logger.Tracef("no match for block device dev links: %v", dev.DeviceLinks)
continue
}
if attachmentInfo.DeviceName == dev.DeviceName {
logger.Tracef("device name match on %v", attachmentInfo.DeviceName)
return &dev, true
}
logger.Tracef("no match for block device name: %v", dev.DeviceName)
}
return nil, false
}