github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/disks/disks.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package disks
    21  
    22  import "fmt"
    23  
    24  // Options is a set of options used when querying information about
    25  // partition and disk devices.
    26  type Options struct {
    27  	// IsDecryptedDevice indicates that the mountpoint is referring to a
    28  	// decrypted device.
    29  	IsDecryptedDevice bool
    30  }
    31  
    32  // Disk is a single physical disk device that contains partitions.
    33  type Disk interface {
    34  	// FindMatchingPartitionUUIDWithFsLabel finds the partition uuid for a
    35  	// partition matching the specified filesystem label on the disk. Note that
    36  	// for non-ascii labels like "Some label", the label will be encoded using
    37  	// \x<hex> for potentially non-safe characters like in "Some\x20Label".
    38  	// If the filesystem label was not found on the disk, and no other errors
    39  	// were encountered, a PartitionNotFoundError will be returned.
    40  	FindMatchingPartitionUUIDWithFsLabel(string) (string, error)
    41  
    42  	// FindMatchingPartitionUUIDWithPartLabel is like
    43  	// FindMatchingPartitionUUIDWithFsLabel, but searches for a partition that
    44  	// has a matching partition label instead of the filesystem label. The same
    45  	// encoding scheme is performed on the label as in that function.
    46  	FindMatchingPartitionUUIDWithPartLabel(string) (string, error)
    47  
    48  	// MountPointIsFromDisk returns whether the specified mountpoint corresponds
    49  	// to a partition on the disk. Note that this only considers partitions
    50  	// and mountpoints found when the disk was identified with
    51  	// DiskFromMountPoint.
    52  	// TODO: make this function return what a Disk of where the mount point
    53  	//       is actually from if it is not from the same disk for better
    54  	//       error reporting
    55  	MountPointIsFromDisk(string, *Options) (bool, error)
    56  
    57  	// Dev returns the string "major:minor" number for the disk device.
    58  	Dev() string
    59  
    60  	// HasPartitions returns whether the disk has partitions or not. A physical
    61  	// disk will have partitions, but a mapper device will just be a volume that
    62  	// does not have partitions for example.
    63  	HasPartitions() bool
    64  
    65  	// TODO: add function to get some properties like an associated /dev node
    66  	//       for a disk for better user error reporting, i.e. /dev/vda3 is much
    67  	//       more helpful than 252:3
    68  }
    69  
    70  // PartitionNotFoundError is an error where a partition matching the SearchType
    71  // was not found. SearchType can be either "partition-label" or
    72  // "filesystem-label" to indicate searching by the partition label or the
    73  // filesystem label on a given disk. SearchQuery is the specific query
    74  // parameter attempted to be used.
    75  type PartitionNotFoundError struct {
    76  	SearchType  string
    77  	SearchQuery string
    78  }
    79  
    80  func (e PartitionNotFoundError) Error() string {
    81  	t := ""
    82  	switch e.SearchType {
    83  	case "partition-label":
    84  		t = "partition label"
    85  	case "filesystem-label":
    86  		t = "filesystem label"
    87  	default:
    88  		return fmt.Sprintf("searching with unknown search type %q and search query %q did not return a partition", e.SearchType, e.SearchQuery)
    89  	}
    90  	return fmt.Sprintf("%s %q not found", t, e.SearchQuery)
    91  }
    92  
    93  var (
    94  	_ = error(PartitionNotFoundError{})
    95  )