Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubeapi for matching pv/pvc to /dev/sd devices #4378

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions pkg/pillar/kubeapi/storageutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright (c) 2024 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0

//go:build kubevirt

package kubeapi

import (
"context"
"fmt"
"os"
"strings"
"syscall"

"github.com/lf-edge/eve/pkg/pillar/pubsub"
"golang.org/x/sys/unix"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
longhornDevPath = "/dev/longhorn"
)

func getMajorMinorStr(stat syscall.Stat_t) string {
major := unix.Major(stat.Rdev)
minor := unix.Minor(stat.Rdev)
return fmt.Sprintf("%d:%d", major, minor)
}

func isPvAttachedOnThisNode(pv string) bool {
if _, err := os.Stat(longhornDevPath + "/" + pv); err != nil {
return false
}
return true
}
func isBlockDeviceAttachedOnThisNode(dev string) bool {
if _, err := os.Stat("/dev/" + dev); err != nil {
return false
}
return true
}

// CleanupUnmountedDiskMetrics loops over existing DiskMetric
// objects and unpublishes them if the device no longer exists
func CleanupUnmountedDiskMetrics(pubDiskMetric pubsub.Publication, pvcToPvMap map[string]string) {
existingMetrics := pubDiskMetric.GetAll()

for id := range existingMetrics {
if strings.HasPrefix(id, "pvc-") {
pvName, ok := pvcToPvMap[id]
if ok {
if !isPvAttachedOnThisNode(pvName) {
pubDiskMetric.Unpublish(id)
}
}
} else {
if !isBlockDeviceAttachedOnThisNode(id) {
pubDiskMetric.Unpublish(id)
}
}
}
}

// LonghornGetMajorMinorMaps builds two maps between
// device major:minor -> kube-pv-name/lh-volume-name
// and kube-pv-name/lh-volume-name -> maj:min to
// help callers find a PV/PVC in /proc/diskstats
// which only shows the sdX path.
func LonghornGetMajorMinorMaps() (map[string]string, map[string]string, error) {
lhMajMinToNameMap := make(map[string]string) // maj:min -> kube-pv-name/lh-volume-name
lhNameToMajMinMap := make(map[string]string) // kube-pv-name/lh-volume-name -> maj:min

if _, err := os.Stat(longhornDevPath); err != nil {
return lhMajMinToNameMap, lhNameToMajMinMap, fmt.Errorf("longhorn dev path missing")
}

lhPvcList, err := os.ReadDir(longhornDevPath)
if err != nil {
return lhMajMinToNameMap, lhNameToMajMinMap, fmt.Errorf("unable to read longhorn devs")
}

for _, lhDirEnt := range lhPvcList {
var lhStat syscall.Stat_t
err := syscall.Stat(longhornDevPath+"/"+lhDirEnt.Name(), &lhStat)

if err != nil {
continue
}
majMinKey := getMajorMinorStr(lhStat)
lhMajMinToNameMap[majMinKey] = lhDirEnt.Name()
lhNameToMajMinMap[lhDirEnt.Name()] = majMinKey

}
return lhMajMinToNameMap, lhNameToMajMinMap, nil
}

// SCSIGetMajMinMaps builds two maps to assist linking with other devices
// First map: maj:min -> sdX
// Second map: sdX -> maj:min
func SCSIGetMajMinMaps() (map[string]string, map[string]string, error) {
sdMajMinToNameMap := make(map[string]string) // maj:min -> sdX
sdNameToMajMinMap := make(map[string]string) // sdX -> maj:min

blockDevs, err := os.ReadDir("/sys/class/block/")
if err != nil {
return sdMajMinToNameMap, sdNameToMajMinMap, fmt.Errorf("unable to read sd devs")
}

for _, devEnt := range blockDevs {
if !strings.HasPrefix(devEnt.Name(), "sd") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewd-zededa , why are you considering only sdX devices? What about devices that have NVMes that will be named into something like nvme0n1, nvme0n1p1, nvme0n1p2, nvme0n1p3?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also eMMCs, that in this case will be named as something like mmcblk0p1, mmcblk0p2, etc...

continue
}

var blockStat syscall.Stat_t
err := syscall.Stat("/dev/"+devEnt.Name(), &blockStat)
if err != nil {
continue
}
majMinVal := getMajorMinorStr(blockStat)
sdMajMinToNameMap[majMinVal] = devEnt.Name()
sdNameToMajMinMap[devEnt.Name()] = majMinVal
}
return sdMajMinToNameMap, sdNameToMajMinMap, nil
}

// PvPvcMaps returns two maps of pv-name/longhorn-name -> pvc-name
// and pvc-name -> pv-name/longhorn-name
func PvPvcMaps() (map[string]string, map[string]string, error) {
pvsMap := make(map[string]string)
pvcsMap := make(map[string]string)

clientset, err := GetClientSet()
if err != nil {
return pvsMap, pvcsMap, fmt.Errorf("PvToPvc_Map: can't get clientset %v", err)
}

pvcs, err := clientset.CoreV1().PersistentVolumeClaims(EVEKubeNameSpace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return pvsMap, pvcsMap, fmt.Errorf("get pvcs:%v", err)
}
for _, pvc := range pvcs.Items {
pvsMap[pvc.Spec.VolumeName] = pvc.ObjectMeta.Name
pvcsMap[pvc.ObjectMeta.Name] = pvc.Spec.VolumeName
}
return pvsMap, pvcsMap, nil
}
Loading