Files
my-rook-config/pkg/clusterd/disk.go
T

259 lines
6.7 KiB
Go
Raw Normal View History

2016-11-05 17:09:05 -07:00
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2020-01-14 18:36:45 +01:00
2017-10-17 09:33:37 -07:00
package clusterd
2016-09-15 08:00:43 -07:00
import (
2019-10-15 07:48:01 +00:00
"errors"
"fmt"
2022-11-01 08:41:19 +00:00
"os"
"path"
"regexp"
2016-09-15 08:00:43 -07:00
"strconv"
2017-10-17 09:33:37 -07:00
"github.com/coreos/pkg/capnslog"
2016-10-26 00:38:39 +02:00
"github.com/rook/rook/pkg/util/exec"
"github.com/rook/rook/pkg/util/sys"
2016-09-15 08:00:43 -07:00
)
var (
2023-04-21 13:13:59 -06:00
logger = capnslog.NewPackageLogger("github.com/rook/rook", "inventory")
isRBD = regexp.MustCompile("^rbd[0-9]+p?[0-9]{0,}$")
listAllDevices = "all"
// We need to allow dm- devices (Kubernetes mountpoints) because dm- devices name are used as names for meta device
allowDeviceNamePattern = regexp.MustCompile("dm-")
)
2020-05-01 14:30:02 -06:00
func supportedDeviceType(device string) bool {
return device == sys.DiskType ||
device == sys.SSDType ||
device == sys.CryptType ||
device == sys.LVMType ||
device == sys.MultiPath ||
device == sys.PartType ||
2022-11-01 08:41:19 +00:00
device == sys.LinearType ||
(getAllowLoopDevices() && device == sys.LoopType)
2020-05-01 14:30:02 -06:00
}
2020-01-14 18:36:45 +01:00
// GetDeviceEmpty check whether a device is completely empty
func GetDeviceEmpty(device *sys.LocalDisk) bool {
return supportedDeviceType(device.Type) && len(device.Partitions) == 0 && device.Filesystem == ""
2016-09-15 08:00:43 -07:00
}
func ignoreDevice(d string) bool {
return isRBD.MatchString(d)
}
2023-04-21 13:13:59 -06:00
func DiscoverDevicesWithFilter(executor exec.Executor, deviceFilter, metaDevice string) ([]*sys.LocalDisk, error) {
2018-04-05 18:39:20 +00:00
var disks []*sys.LocalDisk
devices, err := sys.ListDevices(executor)
if err != nil {
return nil, err
}
for _, d := range devices {
2020-01-13 16:58:51 +01:00
// Ignore RBD device
if ignoreDevice(d) {
// skip device
2020-01-13 16:58:51 +01:00
logger.Warningf("skipping rbd device %q", d)
continue
}
2020-01-13 16:58:51 +01:00
2023-04-21 13:13:59 -06:00
if deviceFilter != "" && !deviceMatchWithFilter(d, deviceFilter, metaDevice) {
logger.Warningf("device %q skipped due to filter %q", d, deviceFilter)
continue
}
2020-01-13 16:58:51 +01:00
// Populate device information coming from lsblk
2019-10-15 07:48:01 +00:00
disk, err := PopulateDeviceInfo(d, executor)
if err != nil {
logger.Warningf("skipping device %q. %v", d, err)
continue
}
2020-01-13 16:58:51 +01:00
// Populate udev information coming from udev
2019-10-15 07:48:01 +00:00
disk, err = PopulateDeviceUdevInfo(d, executor, disk)
if err != nil {
// go on without udev info
2020-01-13 16:58:51 +01:00
// not ideal for our filesystem check later but we can't really fail either...
logger.Warningf("failed to get udev info for device %q. %v", d, err)
2019-10-15 07:48:01 +00:00
}
2020-01-14 18:36:45 +01:00
// Test if device has child, if so we skip it and only consider the partitions
// which will come in later iterations of the loop
// We only test if the type is 'disk', this is a property reported by lsblk
// and means it's a parent block device
if disk.Type == sys.DiskType {
deviceChild, err := sys.ListDevicesChild(executor, fmt.Sprintf("/dev/%s", d))
2020-01-14 18:36:45 +01:00
if err != nil {
2020-01-13 16:58:51 +01:00
logger.Warningf("failed to detect child devices for device %q, assuming they are none. %v", d, err)
2020-01-14 18:36:45 +01:00
}
// lsblk will output at least 2 lines if they are partitions, one for the parent
// and N for the child
if len(deviceChild) > 1 {
logger.Infof("skipping device %q because it has child, considering the child instead.", d)
continue
}
}
disks = append(disks, disk)
}
2021-02-10 15:28:37 +01:00
logger.Debug("discovered disks are:")
for _, disk := range disks {
logger.Debugf("%+v", disk)
}
return disks, nil
2016-09-15 08:00:43 -07:00
}
2023-04-21 13:13:59 -06:00
func deviceMatchWithFilter(device string, filter, metaDevice string) bool {
if filter == listAllDevices || device == metaDevice {
return true
}
if allowDeviceNamePattern.MatchString(device) {
return true
}
pattern, err := regexp.Compile(filter)
if err != nil {
return false
}
if !pattern.MatchString(device) {
return false
}
return true
}
// DiscoverDevices returns all the details of devices available on the local node
func DiscoverDevices(executor exec.Executor) ([]*sys.LocalDisk, error) {
disks, err := DiscoverDevicesWithFilter(executor, "", "")
if err != nil {
return nil, err
}
return disks, nil
}
2019-10-15 07:48:01 +00:00
// PopulateDeviceInfo returns the information of the specified block device
func PopulateDeviceInfo(d string, executor exec.Executor) (*sys.LocalDisk, error) {
diskProps, err := sys.GetDeviceProperties(d, executor)
if err != nil {
2019-10-15 07:48:01 +00:00
return nil, err
}
diskType, ok := diskProps["TYPE"]
2020-05-01 14:30:02 -06:00
if !ok {
return nil, errors.New("diskType is empty")
}
if !supportedDeviceType(diskType) {
return nil, fmt.Errorf("unsupported diskType %+s", diskType)
}
// get the UUID for disks
var diskUUID string
2022-05-05 11:46:56 +00:00
if diskType == sys.DiskType {
uuid, err := sys.GetDiskUUID(d, executor)
if err != nil {
2022-05-05 11:46:56 +00:00
logger.Warning(err)
} else {
diskUUID = uuid
}
}
disk := &sys.LocalDisk{Name: d, UUID: diskUUID}
if val, ok := diskProps["TYPE"]; ok {
disk.Type = val
}
if val, ok := diskProps["SIZE"]; ok {
if size, err := strconv.ParseUint(val, 10, 64); err == nil {
disk.Size = size
}
}
if val, ok := diskProps["ROTA"]; ok {
if rotates, err := strconv.ParseBool(val); err == nil {
disk.Rotational = rotates
}
}
if val, ok := diskProps["RO"]; ok {
if ro, err := strconv.ParseBool(val); err == nil {
disk.Readonly = ro
}
}
if val, ok := diskProps["PKNAME"]; ok {
2020-04-29 11:48:09 -07:00
if val != "" {
disk.Parent = path.Base(val)
}
}
2020-01-13 16:58:51 +01:00
if val, ok := diskProps["NAME"]; ok {
disk.RealPath = val
2020-01-13 16:58:51 +01:00
}
if val, ok := diskProps["KNAME"]; ok {
disk.KernelName = path.Base(val)
}
2022-05-09 12:02:48 +02:00
if val, ok := diskProps["FSTYPE"]; ok && val != "" {
disk.Filesystem = path.Base(val)
}
if val, ok := diskProps["MOUNTPOINT"]; ok && val != "" {
disk.Mountpoint = path.Base(val)
}
2019-10-15 07:48:01 +00:00
return disk, nil
}
2019-10-15 07:48:01 +00:00
// PopulateDeviceUdevInfo fills the udev info into the block device information
func PopulateDeviceUdevInfo(d string, executor exec.Executor, disk *sys.LocalDisk) (*sys.LocalDisk, error) {
udevInfo, err := sys.GetUdevInfo(d, executor)
if err != nil {
2019-10-15 07:48:01 +00:00
return disk, err
}
// parse udev info output
if val, ok := udevInfo["DEVLINKS"]; ok {
disk.DevLinks = val
}
if val, ok := udevInfo["ID_FS_TYPE"]; ok {
disk.Filesystem = val
}
if val, ok := udevInfo["ID_SERIAL"]; ok {
disk.Serial = val
}
if val, ok := udevInfo["ID_VENDOR"]; ok {
disk.Vendor = val
}
if val, ok := udevInfo["ID_MODEL"]; ok {
disk.Model = val
}
if val, ok := udevInfo["ID_WWN_WITH_EXTENSION"]; ok {
disk.WWNVendorExtension = val
}
if val, ok := udevInfo["ID_WWN"]; ok {
disk.WWN = val
}
2019-10-15 07:48:01 +00:00
return disk, nil
}
2022-11-01 08:41:19 +00:00
func getAllowLoopDevices() bool {
return os.Getenv("CEPH_VOLUME_ALLOW_LOOP_DEVICES") == "true"
}