Skip to content
10 changes: 10 additions & 0 deletions pkg/block/blkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
BLKIDCMD = "blkid"
)

// This gets called multiple times for each device with different tags,
// so for example you'll see the following debug log for a new device:
//
// time="2026-02-18T06:00:07Z" level=debug msg="failed to read disk uuid of nvme0n1 : failed to execute: blkid [-s UUID /dev/nvme0n1 -o value], output , stderr : exit status 2\n"
// time="2026-02-18T06:00:07Z" level=debug msg="failed to read disk uuid of nvme0n1 : failed to execute: blkid [-s PTUUID /dev/nvme0n1 -o value], output , stderr : exit status 2\n"
// time="2026-02-18T06:00:07Z" level=debug msg="failed to read disk uuid of nvme0n1 : failed to execute: blkid [-s TYPE /dev/nvme0n1 -o value], output , stderr : exit status 2\n"
//
// It would be better maybe to batch these up, say calling `blkid -s UUID -s PTUUID -s TYPE /dev/$whatever -o export`
// and then look for name=value pairs in the output matching each of those tags.
// TODO: look at fixing the above

Check notice on line 23 in pkg/block/blkid.go

View check run for this annotation

codefactor.io / CodeFactor

pkg/block/blkid.go#L23

Warning comment detected, consider resolving the issue. (warning-comment)
func doCommandBlkid(partition string, param string) (string, error) {
if !strings.HasPrefix(partition, "/dev") {
partition = "/dev/" + partition
Expand Down
52 changes: 3 additions & 49 deletions pkg/block/block_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package block

import (
"bufio"
"encoding/hex"
"io"
"os"
"path/filepath"
Expand All @@ -14,8 +13,6 @@ import (
"github.com/jaypipes/ghw/pkg/linuxpath"
"github.com/jaypipes/ghw/pkg/option"
"github.com/jaypipes/ghw/pkg/util"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/blake2b"

"github.com/harvester/go-common/common"
ndmutils "github.com/harvester/node-disk-manager/pkg/utils"
Expand Down Expand Up @@ -43,7 +40,6 @@ type Info interface {
GetDisks() []*Disk
GetPartitions() []*Partition
GetDiskByDevPath(name string) *Disk
GetPartitionByDevPath(disk, part string) *Partition
GetFileSystemInfoByDevPath(dname string) *FileSystemInfo
}

Expand Down Expand Up @@ -81,20 +77,14 @@ func (i *infoImpl) GetPartitions() []*Partition {
}

func (i *infoImpl) GetDiskByDevPath(name string) *Disk {
// multipath devices use `/dev/mapper/xxx`, but inside here we need the
// short name (/dev/dm-x) in order to query udev via /sys/block
name, _ = filepath.EvalSymlinks(name)
name = strings.TrimPrefix(name, "/dev/")
paths := linuxpath.New(i.ctx)
return getDisk(i.ctx, paths, name)
}

func (i *infoImpl) GetPartitionByDevPath(disk, part string) *Partition {
disk = strings.TrimPrefix(disk, "/dev/")
part = strings.TrimPrefix(part, "/dev/")
paths := linuxpath.New(i.ctx)
partition := diskPartition(i.ctx, paths, disk, part)
partition.Disk = getDisk(i.ctx, paths, disk)
return partition
}

func (i *infoImpl) GetFileSystemInfoByDevPath(dname string) *FileSystemInfo {
paths := linuxpath.New(i.ctx)
mp, pt, ro := partitionInfo(i.ctx, paths, dname)
Expand Down Expand Up @@ -562,39 +552,3 @@ func parseMountEntry(line string) *mountEntry {
res.Options = opts
return res
}

// GeneratePartitionGUID generates a GUID for partitions.
func GeneratePartitionGUID(part *Partition, nodeName string) string {
if valueExists(part.UUID) {
return makeHashGUID(nodeName + part.UUID)
}
logrus.Debugf("failed to generate Partition GUID for device %s", part.Name)
return ""
}

// GenerateDiskGUID generates a GUID for disks.
func GenerateDiskGUID(disk *Disk, nodeName string) string {
var id string
if valueExists(disk.WWN) {
id = disk.WWN + disk.Vendor + disk.Model + disk.SerialNumber
} else if valueExists(disk.UUID) {
id = disk.UUID
} else if valueExists(disk.PtUUID) {
id = disk.PtUUID
}
if valueExists(id) {
return makeHashGUID(nodeName + id)
}
logrus.Debugf("failed to generate GUID for device %s", disk.Name)
return ""
}

func makeHashGUID(payload string) string {
hasher, _ := blake2b.New(16, nil)
hasher.Write([]byte(payload))
return hex.EncodeToString(hasher.Sum(nil))
}

func valueExists(value string) bool {
return len(value) > 0 && value != util.UNKNOWN
}
66 changes: 3 additions & 63 deletions pkg/controller/blockdevice/blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const (
DeviceTypeLabel = "ndm.harvesterhci.io/device-type"
)

// GetDiskBlockDevice gets a blockdevice from a given disk.
// GetDiskBlockDevice creates a BlockDevices from a given disk. Note that the _name_ of
// the BlockDevice retuned is not set by this function. The caller must set it before
// trying to actually create a BD CR based on this, and must take care when comparing BDs.
func GetDiskBlockDevice(disk *block.Disk, nodeName, namespace string) *diskv1.BlockDevice {
fileSystemInfo := &diskv1.FilesystemStatus{
MountPoint: disk.FileSystemInfo.MountPoint,
Expand Down Expand Up @@ -83,67 +85,5 @@ func GetDiskBlockDevice(disk *block.Disk, nodeName, namespace string) *diskv1.Bl
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
}
8 changes: 0 additions & 8 deletions pkg/controller/blockdevice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,6 @@ func (c *Controller) updateDeviceStatus(device *diskv1.BlockDevice, devPath stri
autoProvisioned := c.scanner.ApplyAutoProvisionFiltersForDisk(disk)
// Only disk can be auto-provisioned.
needAutoProvision = c.scanner.NeedsAutoProvision(device, autoProvisioned)
case diskv1.DeviceTypePart:
parentDevPath, err := block.GetParentDevName(devPath)
if err != nil {
return fmt.Errorf("failed to get parent devPath for %s: %v", device.Name, err)
}
part := c.BlockInfo.GetPartitionByDevPath(parentDevPath, devPath)
bd := GetPartitionBlockDevice(part, c.NodeName, c.Namespace)
newStatus = bd.Status.DeviceStatus
default:
return fmt.Errorf("unknown device type %s", device.Status.DeviceStatus.Details.DeviceType)
}
Expand Down
Loading
Loading