forked from rook/rook
When printing pointers of struct (regardless if it is also a slice or
not) we must implement a stringer interface so they can be displayed
properly, this is especially useful when looking at the prepare job
logs.
Before this patch:
```
2021-02-10 06:39:06.783508 D | inventory: discovered disks are [0xc00029c240 0xc00024a000 0xc00086c480 0xc00024a480]
```
After this patch:
```
2021-02-11 13:05:15.730500 D | inventory: discovered disks are:
2021-02-11 13:05:15.730636 D | inventory: &{Name:dm-0 Parent: HasChildren:false DevLinks:/dev/disk/by-id/dm-uuid-LVM-aIFRq5P3hwE2YhukbOmsEZoLY61HxvX62A1xxb6ajTymVE0N8S4Ga0KWlHSgpvNw /dev/disk/by-id/dm-name-ceph--e29bf82e--86f1--4c8e--8412--c468def5a844-osd--block--2044a1ea--6e5e--4423--88cf--8947bf6957d1 Size:32208060416 UUID:91a852c2-8727-4277-9d92-879ee1996503 Serial: Type:lvm Rotational:true Readonly:false Partitions:[] Filesystem:ceph_bluestore Vendor: Model: WWN: WWNVendorExtension: Empty:false CephVolumeData: RealPath:/dev/mapper/ceph--e29bf82e--86f1--4c8e--8412--c468def5a844-osd--block--2044a1ea--6e5e--4423--88cf--8947bf6957d1 KernelName:dm-0 Encrypted:false}
2021-02-11 13:05:15.730657 D | inventory: &{Name:dm-1 Parent: HasChildren:false DevLinks:/dev/disk/by-id/dm-uuid-LVM-GyLVW5UxDRcws8PwsbQuY2CjrQwdn1VxgPQjsKZ2pGVaS1r2Ct0OGZ4P80yEMtqW /dev/disk/by-id/dm-name-ceph--334c640f--7a7f--4fdb--9db4--2f9459e19f9a-osd--block--66ce11c8--20f0--4600--ae50--e8849c9a0af6 Size:32208060416 UUID:cdbcb218-1b03-4d8c-b7d2-25cb88e33b6a Serial: Type:lvm Rotational:true Readonly:false Partitions:[] Filesystem:ceph_bluestore Vendor: Model: WWN: WWNVendorExtension: Empty:false CephVolumeData: RealPath:/dev/mapper/ceph--334c640f--7a7f--4fdb--9db4--2f9459e19f9a-osd--block--66ce11c8--20f0--4600--ae50--e8849c9a0af6 KernelName:dm-1 Encrypted:false}
2021-02-11 13:05:15.730686 D | inventory: &{Name:dm-2 Parent: HasChildren:false DevLinks:/dev/disk/by-id/dm-name-ceph--a011b5c2--cc3f--497a--b19a--6d284461cbc4-osd--block--4de5491c--ecf8--455d--9858--547001fd0321 /dev/disk/by-id/dm-uuid-LVM-QGzgO4E228fvT3NsighNABGdvAKxQEhFOh9rKiDLnSOTk7jLZWu2r1RIMXe5GNqW Size:32208060416 UUID:2238ce24-79e4-41a0-b8ce-e381ac41d5b0 Serial: Type:lvm Rotational:true Readonly:false Partitions:[] Filesystem:ceph_bluestore Vendor: Model: WWN: WWNVendorExtension: Empty:false CephVolumeData: RealPath:/dev/mapper/ceph--a011b5c2--cc3f--497a--b19a--6d284461cbc4-osd--block--4de5491c--ecf8--455d--9858--547001fd0321 KernelName:dm-2 Encrypted:false}
2021-02-11 13:05:15.730713 D | inventory: &{Name:vda1 Parent:vda HasChildren:false DevLinks:/dev/disk/by-path/pci-0000:00:05.0-part1 /dev/disk/by-path/virtio-pci-0000:00:05.0-part1 /dev/disk/by-uuid/0fdf5e49-6b3c-42f2-a1c5-bf77e490dfa4 /dev/disk/by-partuuid/f9dd32c9-a4a5-654f-aee4-651276fdc754 /dev/disk/by-label/boot2docker-data Size:19998934528 UUID: Serial: Type:part Rotational:true Readonly:false Partitions:[] Filesystem:ext4 Vendor: Model: WWN: WWNVendorExtension: Empty:false CephVolumeData: RealPath:/dev/vda1 KernelName:vda1 Encrypted:false}
```
Signed-off-by: Sébastien Han <seb@redhat.com>
208 lines
5.3 KiB
Go
208 lines
5.3 KiB
Go
/*
|
|
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.
|
|
*/
|
|
|
|
package clusterd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"path"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"github.com/coreos/pkg/capnslog"
|
|
|
|
"github.com/rook/rook/pkg/util/exec"
|
|
"github.com/rook/rook/pkg/util/sys"
|
|
)
|
|
|
|
var (
|
|
logger = capnslog.NewPackageLogger("github.com/rook/rook", "inventory")
|
|
isRBD = regexp.MustCompile("^rbd[0-9]+p?[0-9]{0,}$")
|
|
)
|
|
|
|
func supportedDeviceType(device string) bool {
|
|
return device == sys.DiskType ||
|
|
device == sys.SSDType ||
|
|
device == sys.CryptType ||
|
|
device == sys.LVMType ||
|
|
device == sys.MultiPath ||
|
|
device == sys.PartType ||
|
|
device == sys.LinearType
|
|
}
|
|
|
|
// GetDeviceEmpty check whether a device is completely empty
|
|
func GetDeviceEmpty(device *sys.LocalDisk) bool {
|
|
return device.Parent == "" && supportedDeviceType(device.Type) && len(device.Partitions) == 0 && device.Filesystem == ""
|
|
}
|
|
|
|
func ignoreDevice(d string) bool {
|
|
return isRBD.MatchString(d)
|
|
}
|
|
|
|
// DiscoverDevices returns all the details of devices available on the local node
|
|
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 {
|
|
// Ignore RBD device
|
|
if ignoreDevice(d) {
|
|
// skip device
|
|
logger.Warningf("skipping rbd device %q", d)
|
|
continue
|
|
}
|
|
|
|
// Populate device information coming from lsblk
|
|
disk, err := PopulateDeviceInfo(d, executor)
|
|
if err != nil {
|
|
logger.Warningf("skipping device %q. %v", d, err)
|
|
continue
|
|
}
|
|
|
|
// Populate udev information coming from udev
|
|
disk, err = PopulateDeviceUdevInfo(d, executor, disk)
|
|
if err != nil {
|
|
// go on without udev info
|
|
// 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)
|
|
}
|
|
|
|
// 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, d)
|
|
if err != nil {
|
|
logger.Warningf("failed to detect child devices for device %q, assuming they are none. %v", d, err)
|
|
}
|
|
// 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)
|
|
}
|
|
logger.Debug("discovered disks are:")
|
|
for _, disk := range disks {
|
|
logger.Debugf("%+v", disk)
|
|
}
|
|
|
|
return disks, nil
|
|
}
|
|
|
|
// 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 {
|
|
return nil, err
|
|
}
|
|
|
|
diskType, ok := diskProps["TYPE"]
|
|
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
|
|
if diskType != sys.PartType {
|
|
diskUUID, err = sys.GetDiskUUID(d, executor)
|
|
if err != nil {
|
|
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 {
|
|
if val != "" {
|
|
disk.Parent = path.Base(val)
|
|
}
|
|
}
|
|
if val, ok := diskProps["NAME"]; ok {
|
|
disk.RealPath = val
|
|
}
|
|
if val, ok := diskProps["KNAME"]; ok {
|
|
disk.KernelName = path.Base(val)
|
|
}
|
|
|
|
return disk, nil
|
|
}
|
|
|
|
// 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 {
|
|
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
|
|
}
|
|
|
|
return disk, nil
|
|
}
|