-
Notifications
You must be signed in to change notification settings - Fork 19
/
blockdevice.go
134 lines (122 loc) · 4.05 KB
/
blockdevice.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
package blockdevice
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
diskv1 "github.com/harvester/node-disk-manager/pkg/apis/harvesterhci.io/v1beta1"
"github.com/harvester/node-disk-manager/pkg/block"
"github.com/harvester/node-disk-manager/pkg/utils"
)
const (
// ParentDeviceLabel stores the parent device name of a device
ParentDeviceLabel = "ndm.harvesterhci.io/parent-device"
// DeviceTypeLabel indicates whether the device is a disk or a partition
DeviceTypeLabel = "ndm.harvesterhci.io/device-type"
)
// GetDiskBlockDevice gets a blockdevice from a given disk.
func GetDiskBlockDevice(disk *block.Disk, nodeName, namespace string) *diskv1.BlockDevice {
fileSystemInfo := &diskv1.FilesystemStatus{
MountPoint: disk.FileSystemInfo.MountPoint,
Type: disk.FileSystemInfo.Type,
IsReadOnly: disk.FileSystemInfo.IsReadOnly,
}
devPath := utils.GetFullDevPath(disk.Name)
status := diskv1.BlockDeviceStatus{
State: diskv1.BlockDeviceActive,
ProvisionPhase: diskv1.ProvisionPhaseUnprovisioned,
DeviceStatus: diskv1.DeviceStatus{
Partitioned: block.HasPartitions(disk),
Capacity: diskv1.DeviceCapcity{
SizeBytes: disk.SizeBytes,
PhysicalBlockSizeBytes: disk.PhysicalBlockSizeBytes,
},
Details: diskv1.DeviceDetails{
DeviceType: diskv1.DeviceTypeDisk,
DriveType: disk.DriveType.String(),
IsRemovable: disk.IsRemovable,
StorageController: disk.StorageController.String(),
UUID: disk.UUID,
PtUUID: disk.PtUUID,
BusPath: disk.BusPath,
Model: disk.Model,
Vendor: disk.Vendor,
SerialNumber: disk.SerialNumber,
NUMANodeID: disk.NUMANodeID,
WWN: disk.WWN,
},
DevPath: devPath,
FileSystem: fileSystemInfo,
},
}
bd := &diskv1.BlockDevice{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Labels: map[string]string{
v1.LabelHostname: nodeName,
DeviceTypeLabel: string(diskv1.DeviceTypeDisk),
},
},
Spec: diskv1.BlockDeviceSpec{
NodeName: nodeName,
DevPath: devPath,
FileSystem: &diskv1.FilesystemInfo{},
},
Status: status,
}
if guid := block.GenerateDiskGUID(disk, nodeName); len(guid) > 0 {
bd.ObjectMeta.Name = guid
}
return bd
}
// GetPartitionBlockDevice gets a blockdevice from a given partition.
func GetPartitionBlockDevice(part *block.Partition, nodeName, namespace string) *diskv1.BlockDevice {
fileSystemInfo := &diskv1.FilesystemStatus{
Type: part.FileSystemInfo.Type,
MountPoint: part.FileSystemInfo.MountPoint,
IsReadOnly: part.FileSystemInfo.IsReadOnly,
}
devPath := utils.GetFullDevPath(part.Name)
status := diskv1.BlockDeviceStatus{
State: diskv1.BlockDeviceActive,
ProvisionPhase: diskv1.ProvisionPhaseUnprovisioned,
DeviceStatus: diskv1.DeviceStatus{
Capacity: diskv1.DeviceCapcity{
SizeBytes: part.SizeBytes,
PhysicalBlockSizeBytes: part.Disk.PhysicalBlockSizeBytes,
},
Partitioned: false,
Details: diskv1.DeviceDetails{
DeviceType: diskv1.DeviceTypePart,
Label: part.Label,
PartUUID: part.UUID,
UUID: part.FsUUID,
DriveType: part.DriveType.String(),
StorageController: part.StorageController.String(),
},
FileSystem: fileSystemInfo,
DevPath: devPath,
ParentDevice: utils.GetFullDevPath(part.Disk.Name),
},
}
bd := &diskv1.BlockDevice{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Labels: map[string]string{
v1.LabelHostname: nodeName,
DeviceTypeLabel: string(diskv1.DeviceTypePart),
},
},
Spec: diskv1.BlockDeviceSpec{
NodeName: nodeName,
DevPath: devPath,
FileSystem: &diskv1.FilesystemInfo{},
},
Status: status,
}
if parentDeviceName := block.GenerateDiskGUID(part.Disk, nodeName); len(parentDeviceName) > 0 {
bd.ObjectMeta.Labels[ParentDeviceLabel] = parentDeviceName
}
if guid := block.GeneratePartitionGUID(part, nodeName); len(guid) > 0 {
bd.ObjectMeta.Name = guid
}
return bd
}