Skip to content

Commit ca01e0a

Browse files
committed
feat(uuid): refine code
1 parent f608d27 commit ca01e0a

File tree

8 files changed

+102
-41
lines changed

8 files changed

+102
-41
lines changed
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 = 2
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/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: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"os"
2121

2222
"github.com/openebs/node-disk-manager/blockdevice"
23-
"github.com/openebs/node-disk-manager/pkg/blkid"
2423
"github.com/openebs/node-disk-manager/pkg/features"
2524
"github.com/openebs/node-disk-manager/pkg/util"
2625

@@ -87,26 +86,13 @@ func generateUUID(bd blockdevice.BlockDevice) (string, bool) {
8786
uuidField = bd.FSInfo.FileSystemUUID
8887
ok = true
8988
case features.FeatureGates.IsEnabled(features.PartitionTableUUID) && len(bd.PartitionInfo.PartitionTableType) > 0:
90-
// if the host is CentOS 7, the `libblkid` version on host is `2.23`,
91-
// but the `PTUUID` tag was start to provide from `2.24`. This will cause
92-
// the udev cache fetched from host udevd will not contain env `ID_PART_TABLE_UUID`.
93-
// Fortunately, the `libblkid` version in our base container (ubuntu 16.04)
94-
// is `2.27.1`, will provide `PTUUID` tag, so we should fetch `PTUUID` from `blkid`.
95-
partUUID := ""
9689
if len(bd.PartitionInfo.PartitionTableUUID) == 0 {
97-
deviceIdentifier := blkid.DeviceIdentifier{DevPath: bd.DevPath}
98-
partUUID = deviceIdentifier.GetPartitionUUID()
99-
} else {
100-
partUUID = bd.PartitionInfo.PartitionTableUUID
101-
}
102-
103-
if len(partUUID) == 0 {
10490
klog.Errorf("device(%s) has a partition table, but can not get partition table uuid", bd.DevPath)
10591
break
10692
}
10793

108-
klog.Infof("device(%s) has a partition table, use partition table uuid: %s", bd.DevPath, partUUID)
109-
uuidField = partUUID
94+
klog.Infof("device(%s) has a partition table, use partition table uuid: %s", bd.DevPath, bd.PartitionInfo.PartitionTableUUID)
95+
uuidField = bd.PartitionInfo.PartitionTableUUID
11096
ok = true
11197
}
11298

deploy/helm/charts/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apiVersion: v2
22
name: openebs-ndm
33
description: Helm chart for OpenEBS Node Disk Manager - a Kubernetes native storage device management solution. For instructions on how to install, refer to https://openebs.github.io/node-disk-manager/.
4-
version: 1.6.1
5-
appVersion: 1.6.1
4+
version: 1.6.2
5+
appVersion: 1.6.2
66
icon: https://raw.githubusercontent.com/cncf/artwork/master/projects/openebs/icon/color/openebs-icon-color.png
77
home: http://www.openebs.io/
88
keywords:

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
# Uncomment the line below to enable the feature.

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
# Uncomment the line below to enable the feature.

pkg/blkid/blkid.go

Lines changed: 16 additions & 23 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
/*
@@ -38,36 +39,28 @@ type DeviceIdentifier struct {
3839
DevPath string
3940
}
4041

41-
// GetOnDiskFileSystem returns the filesystem present on the disk by reading from the disk
42-
// using libblkid
43-
func (di *DeviceIdentifier) GetOnDiskFileSystem() string {
42+
func (di *DeviceIdentifier) getTagValue(typeName string) string {
4443
var blkidType *C.char
45-
blkidType = C.CString(fsTypeIdentifier)
44+
blkidType = C.CString(typeName)
4645
defer C.free(unsafe.Pointer(blkidType))
4746

4847
var device *C.char
4948
device = C.CString(di.DevPath)
5049
defer C.free(unsafe.Pointer(device))
5150

52-
var fstype *C.char
53-
fstype = C.blkid_get_tag_value(nil, blkidType, device)
54-
defer C.free(unsafe.Pointer(fstype))
51+
var value *C.char
52+
value = C.blkid_get_tag_value(nil, blkidType, device)
53+
defer C.free(unsafe.Pointer(value))
5554

56-
return C.GoString(fstype)
55+
return C.GoString(value)
5756
}
5857

59-
func (di *DeviceIdentifier) GetPartitionUUID() string {
60-
var blkidType *C.char
61-
blkidType = C.CString(partitionUUIDIdentifier)
62-
defer C.free(unsafe.Pointer(blkidType))
63-
64-
var device *C.char
65-
device = C.CString(di.DevPath)
66-
defer C.free(unsafe.Pointer(device))
67-
68-
var ptuuid *C.char
69-
ptuuid = C.blkid_get_tag_value(nil, blkidType, device)
70-
defer C.free(unsafe.Pointer(ptuuid))
58+
// GetOnDiskFileSystem returns the filesystem present on the disk by reading from the disk
59+
// using libblkid
60+
func (di *DeviceIdentifier) GetOnDiskFileSystem() string {
61+
return di.getTagValue(fsTypeIdentifier)
62+
}
7163

72-
return C.GoString(ptuuid)
64+
func (di *DeviceIdentifier) GetPartitionUUID() string {
65+
return di.getTagValue(partitionUUIDIdentifier)
7366
}

pkg/partition/partition.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/diskfs/go-diskfs/disk"
2424
"github.com/diskfs/go-diskfs/partition/gpt"
2525
"github.com/openebs/node-disk-manager/pkg/blkid"
26+
"github.com/openebs/node-disk-manager/pkg/features"
2627

2728
"k8s.io/klog"
2829
)
@@ -167,6 +168,10 @@ func (d *Disk) CreateSinglePartition() error {
167168

168169
// CreatePartitionTable create a GPT header on the disk
169170
func (d *Disk) CreatePartitionTable() error {
171+
if !features.FeatureGates.IsEnabled(features.GPTBasedUUID) {
172+
return fmt.Errorf("feature gate GPTBasedUUID not enabled which required")
173+
}
174+
170175
fd, err := diskfs.Open(d.DevPath)
171176
if err != nil {
172177
return fmt.Errorf("error opening disk fd for disk %s: %v", d.DevPath, err)

0 commit comments

Comments
 (0)