github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/storage/path.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage
     5  
     6  import (
     7  	"path"
     8  
     9  	"github.com/juju/errors"
    10  )
    11  
    12  const (
    13  	diskByID         = "/dev/disk/by-id"
    14  	diskByUUID       = "/dev/disk/by-uuid"
    15  	diskByWWN        = "/dev/disk/by-id/wwn-"
    16  	diskByDeviceName = "/dev"
    17  )
    18  
    19  // BlockDevicePath returns the path to a block device, or an error if a path
    20  // cannot be determined. The path is based on the hardware ID, if available;
    21  // the first value in device.DeviceLinks, if non-empty; otherwise the device
    22  // name.
    23  func BlockDevicePath(device BlockDevice) (string, error) {
    24  	if device.WWN != "" {
    25  		return diskByWWN + device.WWN, nil
    26  	}
    27  	if device.HardwareId != "" {
    28  		return path.Join(diskByID, device.HardwareId), nil
    29  	}
    30  	if len(device.DeviceLinks) > 0 {
    31  		// return the first device link in the list
    32  		return device.DeviceLinks[0], nil
    33  	}
    34  	if device.UUID != "" {
    35  		return path.Join(diskByUUID, device.UUID), nil
    36  	}
    37  	if device.DeviceName != "" {
    38  		return path.Join(diskByDeviceName, device.DeviceName), nil
    39  	}
    40  	return "", errors.Errorf("could not determine path for block device")
    41  }