Skip to content

Commit 039bc90

Browse files
committed
feat(uuid): add partition table uuid support
Signed-off-by: cospotato <i@0x233.cn>
1 parent 1fcc2b6 commit 039bc90

File tree

11 files changed

+201
-21
lines changed

11 files changed

+201
-21
lines changed

cmd/ndm_daemonset/probe/addhandler.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
apis "github.com/openebs/node-disk-manager/api/v1alpha1"
2323
"github.com/openebs/node-disk-manager/blockdevice"
2424
"github.com/openebs/node-disk-manager/db/kubernetes"
25+
"github.com/openebs/node-disk-manager/pkg/features"
2526
"github.com/openebs/node-disk-manager/pkg/partition"
2627

2728
"k8s.io/apimachinery/pkg/api/errors"
@@ -111,18 +112,29 @@ func (pe *ProbeEvent) addBlockDevice(bd blockdevice.BlockDevice, bdAPIList *apis
111112
len(bd.DependentDevices.Holders) > 0 {
112113
klog.V(4).Infof("device: %s has holders/partitions. %+v", bd.DevPath, bd.DependentDevices)
113114
} else {
114-
klog.Infof("starting to create partition on device: %s", bd.DevPath)
115115
d := partition.Disk{
116116
DevPath: bd.DevPath,
117117
DiskSize: bd.Capacity.Storage,
118118
LogicalBlockSize: uint64(bd.DeviceAttributes.LogicalBlockSize),
119119
}
120-
if err := d.CreateSinglePartition(); err != nil {
121-
klog.Errorf("error creating partition for %s, %v", bd.DevPath, err)
122-
return err
120+
121+
if features.FeatureGates.IsEnabled(features.PartitionTableUUID) {
122+
klog.Infof("starting to create partition table on device: %s", bd.DevPath)
123+
if err := d.CreatePartitionTable(); err != nil {
124+
klog.Errorf("error create partition table for %s, %v", bd.DevPath, err)
125+
return err
126+
}
127+
klog.Infof("created new partition table in %s", bd.DevPath)
128+
return ErrNeedRescan
129+
} else {
130+
klog.Infof("starting to create partition on device: %s", bd.DevPath)
131+
if err := d.CreateSinglePartition(); err != nil {
132+
klog.Errorf("error creating partition for %s, %v", bd.DevPath, err)
133+
return err
134+
}
135+
klog.Infof("created new partition in %s", bd.DevPath)
136+
return nil
123137
}
124-
klog.Infof("created new partition in %s", bd.DevPath)
125-
return nil
126138
}
127139
} else {
128140
bd.UUID = uuid
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2020 The OpenEBS Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package probe
18+
19+
import (
20+
"github.com/openebs/node-disk-manager/blockdevice"
21+
"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
22+
"github.com/openebs/node-disk-manager/pkg/blkid"
23+
"k8s.io/klog"
24+
)
25+
26+
const (
27+
blkidProbePriority = 4
28+
)
29+
30+
var (
31+
blkidProbeState = defaultEnabled
32+
)
33+
34+
type blkidProbe struct {
35+
}
36+
37+
var blkidProbeRegister = func() {
38+
// Get a controller object
39+
ctrl := <-controller.ControllerBroadcastChannel
40+
if ctrl == nil {
41+
klog.Error("unable to configure custom tag probe")
42+
return
43+
}
44+
probe := &blkidProbe{}
45+
46+
newRegisterProbe := &registerProbe{
47+
priority: blkidProbePriority,
48+
name: "blkid probe",
49+
state: blkidProbeState,
50+
pi: probe,
51+
controller: ctrl,
52+
}
53+
54+
newRegisterProbe.register()
55+
}
56+
57+
func (bp *blkidProbe) Start() {}
58+
59+
func (bp *blkidProbe) FillBlockDeviceDetails(bd *blockdevice.BlockDevice) {
60+
di := &blkid.DeviceIdentifier{DevPath: bd.DevPath}
61+
62+
bd.FSInfo.FileSystem = di.GetOnDiskFileSystem()
63+
64+
// if the host is CentOS 7, the `libblkid` version on host is `2.23`,
65+
// but the `PTUUID` tag was start to provide from `2.24`. This will cause
66+
// the udev cache fetched from host udevd will not contain env `ID_PART_TABLE_UUID`.
67+
// Fortunately, the `libblkid` version in our base container (ubuntu 16.04)
68+
// is `2.27.1`, will provide `PTUUID` tag, so we should fetch `PTUUID` from `blkid`.
69+
bd.PartitionInfo.PartitionTableUUID = di.GetPartitionUUID()
70+
}

cmd/ndm_daemonset/probe/eventhandler.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package probe
1818

1919
import (
20+
"errors"
21+
2022
"github.com/openebs/node-disk-manager/blockdevice"
2123
"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
2224
"github.com/openebs/node-disk-manager/pkg/features"
@@ -38,6 +40,10 @@ const (
3840
ChangeEA EventAction = libudevwrapper.UDEV_ACTION_CHANGE
3941
)
4042

43+
var (
44+
ErrNeedRescan = errors.New("need rescan")
45+
)
46+
4147
// ProbeEvent struct contain a copy of controller it will update disk resources
4248
type ProbeEvent struct {
4349
Controller *controller.Controller
@@ -55,7 +61,7 @@ func (pe *ProbeEvent) addBlockDeviceEvent(msg controller.EventMessage) {
5561

5662
isGPTBasedUUIDEnabled := features.FeatureGates.IsEnabled(features.GPTBasedUUID)
5763

58-
isErrorDuringUpdate := false
64+
isNeedRescan := false
5965
erroredDevices := make([]string, 0)
6066

6167
// iterate through each block device and perform the add/update operation
@@ -82,9 +88,11 @@ func (pe *ProbeEvent) addBlockDeviceEvent(msg controller.EventMessage) {
8288
}
8389
err := pe.addBlockDevice(*device, bdAPIList)
8490
if err != nil {
85-
isErrorDuringUpdate = true
86-
erroredDevices = append(erroredDevices, device.DevPath)
87-
klog.Error(err)
91+
isNeedRescan = true
92+
if !errors.Is(err, ErrNeedRescan) {
93+
erroredDevices = append(erroredDevices, device.DevPath)
94+
klog.Error(err)
95+
}
8896
}
8997
} else {
9098
// if GPTBasedUUID is disabled and the device type is partition,
@@ -98,13 +106,13 @@ func (pe *ProbeEvent) addBlockDeviceEvent(msg controller.EventMessage) {
98106
existingBlockDeviceResource := pe.Controller.GetExistingBlockDeviceResource(bdAPIList, deviceInfo.UUID)
99107
err := pe.Controller.PushBlockDeviceResource(existingBlockDeviceResource, deviceInfo)
100108
if err != nil {
101-
isErrorDuringUpdate = true
109+
isNeedRescan = true
102110
klog.Error(err)
103111
}
104112
}
105113
}
106114

107-
if isErrorDuringUpdate {
115+
if isNeedRescan {
108116
go Rescan(pe.Controller)
109117
}
110118
}

cmd/ndm_daemonset/probe/probe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var RegisteredProbes = []func(){
3535
sysfsProbeRegister,
3636
usedbyProbeRegister,
3737
customTagProbeRegister,
38+
blkidProbeRegister,
3839
}
3940

4041
type registerProbe struct {

cmd/ndm_daemonset/probe/uuid.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121

2222
"github.com/openebs/node-disk-manager/blockdevice"
23+
"github.com/openebs/node-disk-manager/pkg/features"
2324
"github.com/openebs/node-disk-manager/pkg/util"
2425

2526
"k8s.io/klog"
@@ -84,6 +85,15 @@ func generateUUID(bd blockdevice.BlockDevice) (string, bool) {
8485
klog.Infof("device(%s) has a filesystem, using filesystem UUID: %s", bd.DevPath, bd.FSInfo.FileSystemUUID)
8586
uuidField = bd.FSInfo.FileSystemUUID
8687
ok = true
88+
case features.FeatureGates.IsEnabled(features.PartitionTableUUID) && len(bd.PartitionInfo.PartitionTableType) > 0:
89+
if len(bd.PartitionInfo.PartitionTableUUID) == 0 {
90+
klog.Errorf("device(%s) has a partition table, but can not get partition table uuid", bd.DevPath)
91+
break
92+
}
93+
94+
klog.Infof("device(%s) has a partition table, use partition table uuid: %s", bd.DevPath, bd.PartitionInfo.PartitionTableUUID)
95+
uuidField = bd.PartitionInfo.PartitionTableUUID
96+
ok = true
8797
}
8898

8999
if ok {

cmd/ndm_daemonset/probe/uuid_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/openebs/node-disk-manager/blockdevice"
24+
"github.com/openebs/node-disk-manager/pkg/features"
2425
"github.com/openebs/node-disk-manager/pkg/util"
2526
"github.com/stretchr/testify/assert"
2627
)
@@ -30,15 +31,30 @@ func TestGenerateUUID(t *testing.T) {
3031
fakeSerial := "CT500MX500SSD1"
3132
fakeFileSystemUUID := "149108ca-f404-4556-a263-04943e6cb0b3"
3233
fakePartitionUUID := "065e2357-05"
34+
fakePartitionTableUUID := "6f479331-dad4-4ccb-b146-5c359c55399b"
3335
fakeLVM_DM_UUID := "LVM-j2xmqvbcVWBQK9Jdttte3CyeVTGgxtVV5VcCi3nxdwihZDxSquMOBaGL5eymBNvk"
3436
fakeCRYPT_DM_UUID := "CRYPT-LUKS1-f4608c76343d4b5badaf6651d32f752b-backup"
3537
loopDevicePath := "/dev/loop98"
3638
hostName, _ := os.Hostname()
39+
features.FeatureGates.SetFeatureFlag([]string{
40+
"GPTBasedUUID=1",
41+
"PartitionTableUUID=1",
42+
})
3743
tests := map[string]struct {
3844
bd blockdevice.BlockDevice
3945
wantUUID string
4046
wantOk bool
4147
}{
48+
"debiceType-disk with PartitionTableUUID": {
49+
bd: blockdevice.BlockDevice{
50+
PartitionInfo: blockdevice.PartitionInformation{
51+
PartitionTableType: "gpt",
52+
PartitionTableUUID: fakePartitionTableUUID,
53+
},
54+
},
55+
wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakePartitionTableUUID),
56+
wantOk: true,
57+
},
4258
"deviceType-disk with WWN": {
4359
bd: blockdevice.BlockDevice{
4460
DeviceAttributes: blockdevice.DeviceAttribute{

deploy/ndm-operator.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ spec:
518518
args:
519519
- -v=4
520520
- --feature-gates="GPTBasedUUID"
521+
# Use partition table UUID instead of create single partition to get
522+
# partition UUID. Require `GPTBasedUUID` to be enabled with.
523+
# - --feature-gates="PartitionTableUUID"
521524
- --feature-gates="APIService"
522525
# Detect changes to device size, filesystem and mount-points without restart.
523526
#- --feature-gates="ChangeDetection"

deploy/yamls/node-disk-manager.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ spec:
3333
args:
3434
- -v=4
3535
- --feature-gates="GPTBasedUUID"
36+
# Use partition table UUID instead of create single partition to get
37+
# partition UUID. Require `GPTBasedUUID` to be enabled with.
38+
# - --feature-gates="PartitionTableUUID"
3639
- --feature-gates="APIService"
3740
# Detect changes to device size, filesystem and mount-points without restart.
3841
#- --feature-gates="ChangeDetection"

pkg/blkid/blkid.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build (linux && ignore) || cgo
2+
// +build linux,ignore cgo
3+
14
/*
25
Copyright 2020 The OpenEBS Authors
36
@@ -14,8 +17,6 @@ See the License for the specific language governing permissions and
1417
limitations under the License.
1518
*/
1619

17-
// +build linux, cgo
18-
1920
package blkid
2021

2122
/*
@@ -30,8 +31,9 @@ import (
3031
)
3132

3233
const (
33-
fsTypeIdentifier = "TYPE"
34-
labelIdentifier = "LABEL"
34+
fsTypeIdentifier = "TYPE"
35+
labelIdentifier = "LABEL"
36+
partitionUUIDIdentifier = "PTUUID"
3537
)
3638

3739
type DeviceIdentifier struct {
@@ -50,6 +52,12 @@ func (di *DeviceIdentifier) GetOnDiskLabel() string {
5052
return di.GetTagValue(labelIdentifier)
5153
}
5254

55+
// GetPartitionUUID returns the partition UUID present on the disk by reading from the disk
56+
// using libblkid
57+
func (di *DeviceIdentifier) GetPartitionUUID() string {
58+
return di.GetTagValue(partitionUUIDIdentifier)
59+
}
60+
5361
func (di *DeviceIdentifier) GetTagValue(tag string) string {
5462
var blkidType *C.char
5563
blkidType = C.CString(tag)

pkg/features/features.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const (
5050
// ChangeDetection is used to enable detecting changes to
5151
// blockdevice size, filesystem, and mount-points.
5252
ChangeDetection Feature = "ChangeDetection"
53+
54+
// PartitionTableUUID feature flag is used to enable use a
55+
// partition table uuid instead of create partition described in
56+
// https://github.com/openebs/node-disk-manager/issues/621 .
57+
// This feature must enabled with GPTBasedUUID.
58+
PartitionTableUUID Feature = "PartitionTableUUID"
5359
)
5460

5561
// supportedFeatures is the list of supported features. This is used while parsing the
@@ -59,17 +65,23 @@ var supportedFeatures = []Feature{
5965
APIService,
6066
UseOSDisk,
6167
ChangeDetection,
68+
PartitionTableUUID,
6269
}
6370

6471
// defaultFeatureGates is the default features that will be applied to the application
6572
var defaultFeatureGates = map[Feature]bool{
66-
GPTBasedUUID: true,
67-
APIService: false,
68-
UseOSDisk: false,
69-
ChangeDetection: false,
73+
GPTBasedUUID: true,
74+
APIService: false,
75+
UseOSDisk: false,
76+
ChangeDetection: false,
77+
PartitionTableUUID: false,
7078
}
7179

72-
var featureDependencies = map[Feature][]Feature{}
80+
var featureDependencies = map[Feature][]Feature{
81+
PartitionTableUUID: {
82+
GPTBasedUUID,
83+
},
84+
}
7385

7486
// featureFlag is a map representing the flag and its state
7587
type featureFlag map[Feature]bool

0 commit comments

Comments
 (0)