fix linter errors

Signed-off-by: Aidan Leuck <aidan_leuck@selinc.com>
This commit is contained in:
Aidan Leuck
2025-08-21 08:48:26 -06:00
committed by Dimitris Karakasilis
parent 077851419f
commit b93c718b21
4 changed files with 77 additions and 80 deletions

View File

@@ -12,7 +12,7 @@ type DiskPartitionHandler struct {
DiskName string
}
// Validate that DiskPartitionHandler implements PartitionHandler interface
// Validate that DiskPartitionHandler implements PartitionHandler interface.
var _ PartitionHandler = &DiskPartitionHandler{}
func NewDiskPartitionHandler(diskName string) *DiskPartitionHandler {
@@ -30,7 +30,7 @@ func (d *DiskPartitionHandler) GetPartitions(paths *Paths, logger *types.KairosL
}
for _, file := range files {
fname := file.Name()
if !strings.HasPrefix(fname, d.DiskName) {
if !strings.HasPrefix(fname, d.DiskName) {
continue
}
logger.Logger.Debug().Str("file", fname).Msg("Reading partition file")
@@ -55,4 +55,4 @@ func (d *DiskPartitionHandler) GetPartitions(paths *Paths, logger *types.KairosL
out = append(out, p)
}
return out
}
}

View File

@@ -11,6 +11,8 @@ import (
"github.com/kairos-io/kairos-sdk/types"
)
const ext4 = "ext4"
// GhwMock is used to construct a fake disk to present to ghw when scanning block devices
// The way this works is ghw will use the existing files in the system to determine the different disks, partitions and
// mountpoints. It uses /sys/block, /proc/self/mounts and /run/udev/data to gather everything
@@ -97,7 +99,7 @@ func (g *GhwMock) CreateDevices() {
if partition.MountPoint != "" {
// Check if the partition has a fs, otherwise default to ext4
if partition.FS == "" {
partition.FS = "ext4"
partition.FS = ext4
}
// Prepare the g.mounts with all the mount lines
g.mounts = append(
@@ -286,7 +288,7 @@ func (g *GhwMock) createMultipathPartitionWithMountFormat(parentDiskName string,
// Add mount if specified
if partition.MountPoint != "" {
if partition.FS == "" {
partition.FS = "ext4"
partition.FS = ext4
}
var mountDevice string
@@ -307,11 +309,6 @@ func (g *GhwMock) createMultipathPartitionWithMountFormat(parentDiskName string,
}
}
// createMultipathPartition creates a multipath partition structure using /dev/mapper mount format
func (g *GhwMock) createMultipathPartition(parentDiskName string, partition *types.Partition, partNum int) {
g.createMultipathPartitionWithMountFormat(parentDiskName, partition, partNum, false)
}
// AddMultipathPartition adds a multipath partition to a multipath device
// This creates the partition as a holder of the parent device and sets up
// the necessary dm structure for the partition
@@ -369,7 +366,7 @@ func (g *GhwMock) AddMultipathPartition(parentDiskName string, partition *types.
// Add mount if specified
if partition.MountPoint != "" {
if partition.FS == "" {
partition.FS = "ext4"
partition.FS = ext4
}
// For multipath partitions, they can be mounted by /dev/mapper/ name or /dev/dm- name
g.mounts = append(

View File

@@ -18,90 +18,90 @@ func NewMultipathPartitionHandler(diskName string) *MultipathPartitionHandler {
var _ PartitionHandler = &MultipathPartitionHandler{}
func (m *MultipathPartitionHandler) GetPartitions(paths *Paths, logger *types.KairosLogger) types.PartitionList {
out := make(types.PartitionList, 0)
out := make(types.PartitionList, 0)
// For multipath devices, partitions appear as holders of the parent device
// in /sys/block/<disk>/holders/<holder>
holdersPath := filepath.Join(paths.SysBlock, m.DiskName, "holders")
logger.Logger.Debug().Str("path", holdersPath).Msg("Reading multipath holders")
// For multipath devices, partitions appear as holders of the parent device
// in /sys/block/<disk>/holders/<holder>
holdersPath := filepath.Join(paths.SysBlock, m.DiskName, "holders")
logger.Logger.Debug().Str("path", holdersPath).Msg("Reading multipath holders")
holders, err := os.ReadDir(holdersPath)
if err != nil {
logger.Logger.Error().Err(err).Msg("failed to read holders directory")
return out
}
holders, err := os.ReadDir(holdersPath)
if err != nil {
logger.Logger.Error().Err(err).Msg("failed to read holders directory")
return out
}
// Find all multipath partitions by checking each holder
for _, holder := range holders {
partName := holder.Name()
// Find all multipath partitions by checking each holder
for _, holder := range holders {
partName := holder.Name()
// Only consider dm- devices as potential multipath partitions
if !isMultipathDevice(paths, holder, logger) {
logger.Logger.Debug().Str("path", holder.Name()).Msg("Is not a multipath device")
continue
}
// Verify this holder is actually a multipath partition
// We can use the holder DirEntry directly - no need to search for it!
if !isMultipathPartition(holder, paths, logger) {
logger.Logger.Debug().Str("partition", partName).Msg("Holder is not a multipath partition")
continue
}
// Only consider dm- devices as potential multipath partitions
if !isMultipathDevice(paths, holder, logger) {
logger.Logger.Debug().Str("path", holder.Name()).Msg("Is not a multipath device")
continue
}
logger.Logger.Debug().Str("partition", partName).Msg("Found multipath partition")
// Verify this holder is actually a multipath partition
// We can use the holder DirEntry directly - no need to search for it!
if !isMultipathPartition(holder, paths, logger) {
logger.Logger.Debug().Str("partition", partName).Msg("Holder is not a multipath partition")
continue
}
udevInfo, err := udevInfoPartition(paths, partName, logger)
if err != nil {
logger.Logger.Error().Err(err).Str("devNo", partName).Msg("Failed to get udev info")
return out
}
logger.Logger.Debug().Str("partition", partName).Msg("Found multipath partition")
mapperName, ok := udevInfo["DM_NAME"]
if !ok {
logger.Logger.Error().Str("devNo", partName).Msg("DM_NAME not found in udev info")
continue
}
udevInfo, err := udevInfoPartition(paths, partName, logger)
if err != nil {
logger.Logger.Error().Err(err).Str("devNo", partName).Msg("Failed to get udev info")
return out
}
// For multipath partitions, we need to get size directly from the partition device
// since it's a top-level entry in /sys/block, not nested under the parent
mapperName, ok := udevInfo["DM_NAME"]
if !ok {
logger.Logger.Error().Str("devNo", partName).Msg("DM_NAME not found in udev info")
continue
}
// For multipath partitions, we need to get size directly from the partition device
// since it's a top-level entry in /sys/block, not nested under the parent
size := partitionSizeBytes(paths, partName, logger)
du := diskPartUUID(paths, partName, logger)
// The mount point is usually the same as the mapper name
// however you can also mount it as /dev/dm-<n> or /dev/mapper/<mapperName>
// so we need to check both
potentialMountNames := []string{
filepath.Join("/dev/mapper", mapperName),
filepath.Join("/dev", partName),
}
// The mount point is usually the same as the mapper name
// however you can also mount it as /dev/dm-<n> or /dev/mapper/<mapperName>
// so we need to check both
potentialMountNames := []string{
filepath.Join("/dev/mapper", mapperName),
filepath.Join("/dev", partName),
}
// Search for the mount point in the system
var mp, pt string
for _, mountName := range potentialMountNames {
mp, pt = partitionInfo(paths, mountName, logger)
if mp != "" {
logger.Logger.Debug().Str("mountPoint", mp).Msg("Found mount point for partition")
break
}
}
// Search for the mount point in the system
var mp, pt string
for _, mountName := range potentialMountNames {
mp, pt = partitionInfo(paths, mountName, logger)
if mp != "" {
logger.Logger.Debug().Str("mountPoint", mp).Msg("Found mount point for partition")
break
}
}
if pt == "" {
pt = diskPartTypeUdev(paths, partName, logger)
}
fsLabel := diskFSLabel(paths, partName, logger)
p := &types.Partition{
Name: partName,
Size: uint(size / (1024 * 1024)),
MountPoint: mp,
UUID: du,
FilesystemLabel: fsLabel,
FS: pt,
Path: filepath.Join("/dev", partName),
Disk: filepath.Join("/dev", m.DiskName),
}
out = append(out, p)
}
p := &types.Partition{
Name: partName,
Size: uint(size / (1024 * 1024)),
MountPoint: mp,
UUID: du,
FilesystemLabel: fsLabel,
FS: pt,
Path: filepath.Join("/dev", partName),
Disk: filepath.Join("/dev", m.DiskName),
}
out = append(out, p)
}
return out
return out
}

View File

@@ -139,7 +139,7 @@ func diskPartUUID(paths *Paths, partitionPath string, logger *types.KairosLogger
}
// diskPartTypeUdev gets the partition type from the udev database directly and its only used as fallback when
// the partition is not mounted, so we cannot get the type from paths.ProcMounts from the partitionInfo function
// the partition is not mounted, so we cannot get the type from paths.ProcMounts from the partitionInfo function.
func diskPartTypeUdev(paths *Paths, partitionPath string, logger *types.KairosLogger) string {
info, err := udevInfoPartition(paths, partitionPath, logger)
logger.Logger.Trace().Interface("info", info).Msg("Disk Part Type")
@@ -180,7 +180,7 @@ func udevInfoPartition(paths *Paths, partitionPath string, logger *types.KairosL
return UdevInfo(paths, string(devNo), logger)
}
// UdevInfo will return information on udev database about a device number
// UdevInfo will return information on udev database about a device number.
func UdevInfo(paths *Paths, devNo string, logger *types.KairosLogger) (map[string]string, error) {
// Look up block device in udev runtime database
udevID := "b" + strings.TrimSpace(devNo)