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

162 lines
4.0 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.
*/
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"
"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 (
2017-10-17 09:33:37 -07:00
logger = capnslog.NewPackageLogger("github.com/rook/rook", "inventory")
isRBD = regexp.MustCompile("^rbd[0-9]+p?[0-9]{0,}$")
)
// check whether a device is completely empty
func GetDeviceEmpty(device *sys.LocalDisk) bool {
return device.Parent == "" && (device.Type == sys.DiskType || device.Type == sys.SSDType || device.Type == sys.CryptType || device.Type == sys.LVMType) && len(device.Partitions) == 0 && device.Filesystem == ""
2016-09-15 08:00:43 -07:00
}
func ignoreDevice(d string) bool {
return isRBD.MatchString(d)
}
// Discover all the details of devices available on the local node
2018-04-05 18:39:20 +00:00
func DiscoverDevices(executor exec.Executor) ([]*sys.LocalDisk, error) {
var disks []*sys.LocalDisk
devices, err := sys.ListDevices(executor)
if err != nil {
return nil, err
}
for _, d := range devices {
if ignoreDevice(d) {
// skip device
continue
}
2019-10-15 07:48:01 +00:00
disk, err := PopulateDeviceInfo(d, executor)
if err != nil {
// skip device
logger.Warningf("skipping device %s: %+v", d, err)
continue
}
2019-10-15 07:48:01 +00:00
disk, err = PopulateDeviceUdevInfo(d, executor, disk)
if err != nil {
// go on without udev info
logger.Warningf("failed to get udev info for device %s: %+v", d, err)
}
disks = append(disks, disk)
}
return disks, nil
2016-09-15 08:00:43 -07:00
}
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"]
if !ok || (diskType != sys.SSDType && diskType != sys.CryptType && diskType != sys.DiskType && diskType != sys.PartType && diskType != sys.LinearType) {
if !ok {
2019-10-15 07:48:01 +00:00
return nil, errors.New("diskType is empty")
} else {
2019-10-15 07:48:01 +00:00
return nil, fmt.Errorf("unsupported diskType %+s", diskType)
}
}
// get the UUID for disks
var diskUUID string
if diskType != sys.PartType {
diskUUID, err = sys.GetDiskUUID(d, executor)
if err != nil {
2019-10-15 07:48:01 +00:00
return nil, err
}
}
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 {
disk.Parent = 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
}