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 (
|
2017-05-01 19:46:06 +02:00
|
|
|
"regexp"
|
2016-09-15 08:00:43 -07:00
|
|
|
"strconv"
|
|
|
|
|
|
2017-10-17 09:33:37 -07:00
|
|
|
"github.com/coreos/pkg/capnslog"
|
2016-12-22 11:31:24 -08:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
2017-05-01 19:46:06 +02: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,}$")
|
2017-05-01 19:46:06 +02:00
|
|
|
)
|
|
|
|
|
|
2018-04-05 18:39:20 +00:00
|
|
|
func GetAvailableDevices(devices []*sys.LocalDisk) []string {
|
2016-12-22 10:38:44 -08:00
|
|
|
|
|
|
|
|
var available []string
|
2016-12-22 11:31:24 -08:00
|
|
|
for _, device := range devices {
|
|
|
|
|
logger.Debugf("Evaluating device %+v", device)
|
2018-05-01 09:01:29 -04:00
|
|
|
if GetDeviceEmpty(device) {
|
2016-12-22 11:31:24 -08:00
|
|
|
logger.Debugf("Available device: %s", device.Name)
|
|
|
|
|
available = append(available, device.Name)
|
2016-12-22 10:38:44 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-22 11:31:24 -08:00
|
|
|
return available
|
2016-12-22 10:38:44 -08:00
|
|
|
}
|
|
|
|
|
|
2017-01-11 20:19:47 -08:00
|
|
|
// check whether a device is completely empty
|
2018-05-01 09:01:29 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2017-05-01 19:46:06 +02:00
|
|
|
func ignoreDevice(d string) bool {
|
|
|
|
|
return isRBD.MatchString(d)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-22 11:31:24 -08:00
|
|
|
// 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) {
|
2016-09-15 13:52:55 -07:00
|
|
|
|
2018-04-05 18:39:20 +00:00
|
|
|
var disks []*sys.LocalDisk
|
2016-10-21 10:24:56 -07:00
|
|
|
devices, err := sys.ListDevices(executor)
|
2016-09-15 13:52:55 -07:00
|
|
|
if err != nil {
|
2016-12-22 11:31:24 -08:00
|
|
|
return nil, err
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-21 10:24:56 -07:00
|
|
|
for _, d := range devices {
|
2016-09-30 17:38:26 -07:00
|
|
|
|
2017-05-01 19:46:06 +02:00
|
|
|
if ignoreDevice(d) {
|
|
|
|
|
// skip device
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-21 10:24:56 -07:00
|
|
|
diskProps, err := sys.GetDeviceProperties(d, executor)
|
|
|
|
|
if err != nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Warningf("skipping device %s: %+v", d, err)
|
2016-09-15 13:52:55 -07:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-21 10:24:56 -07:00
|
|
|
diskType, ok := diskProps["TYPE"]
|
2018-01-31 17:41:13 +01:00
|
|
|
if !ok || (diskType != sys.SSDType && diskType != sys.CryptType && diskType != sys.DiskType && diskType != sys.PartType) {
|
2016-09-15 13:52:55 -07:00
|
|
|
// unsupported disk type, just continue
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 23:34:06 -07:00
|
|
|
// get the UUID for disks
|
|
|
|
|
var diskUUID string
|
2016-12-28 14:38:35 -08:00
|
|
|
if diskType != sys.PartType {
|
2018-06-19 13:05:51 -07:00
|
|
|
diskUUID, err = sys.GetDiskUUID(d, executor)
|
2016-10-21 10:24:56 -07:00
|
|
|
if err != nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Warningf("skipping device %s with an unknown uuid. %+v", d, err)
|
2016-10-20 23:34:06 -07:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 18:39:20 +00:00
|
|
|
udevInfo, err := sys.GetUdevInfo(d, executor)
|
2016-09-15 13:52:55 -07:00
|
|
|
if err != nil {
|
2018-04-05 18:39:20 +00:00
|
|
|
logger.Warningf("failed to get udev info for device %s: %+v", d, err)
|
|
|
|
|
continue
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-05 18:39:20 +00:00
|
|
|
disk := &sys.LocalDisk{Name: d, UUID: diskUUID}
|
2016-09-15 13:52:55 -07:00
|
|
|
|
2016-12-22 11:31:24 -08:00
|
|
|
if val, ok := diskProps["TYPE"]; ok {
|
|
|
|
|
disk.Type = val
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
2016-12-22 11:31:24 -08:00
|
|
|
if val, ok := diskProps["SIZE"]; ok {
|
|
|
|
|
if size, err := strconv.ParseUint(val, 10, 64); err == nil {
|
|
|
|
|
disk.Size = size
|
|
|
|
|
}
|
2016-12-22 10:38:44 -08:00
|
|
|
}
|
2016-12-22 11:31:24 -08:00
|
|
|
if val, ok := diskProps["ROTA"]; ok {
|
|
|
|
|
if rotates, err := strconv.ParseBool(val); err == nil {
|
|
|
|
|
disk.Rotational = rotates
|
|
|
|
|
}
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
2016-12-22 11:31:24 -08:00
|
|
|
if val, ok := diskProps["RO"]; ok {
|
|
|
|
|
if ro, err := strconv.ParseBool(val); err == nil {
|
|
|
|
|
disk.Readonly = ro
|
|
|
|
|
}
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
2016-12-22 11:31:24 -08:00
|
|
|
if val, ok := diskProps["PKNAME"]; ok {
|
|
|
|
|
disk.Parent = val
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
2016-12-22 11:31:24 -08:00
|
|
|
|
2018-04-05 18:39:20 +00:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-22 11:31:24 -08:00
|
|
|
disks = append(disks, disk)
|
2016-09-15 13:52:55 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-22 11:31:24 -08:00
|
|
|
return disks, nil
|
2016-09-15 08:00:43 -07:00
|
|
|
}
|