Skip to content

Instantly share code, notes, and snippets.

@dwlehman
Created June 12, 2018 19:24
Show Gist options
  • Save dwlehman/eceec2fb443408080b70d4fe2ea70459 to your computer and use it in GitHub Desktop.
Save dwlehman/eceec2fb443408080b70d4fe2ea70459 to your computer and use it in GitHub Desktop.
Finding an unused disk

Finding an Unused Disk

One of the principles of the system roles project is to simplify things for the user. For storage, that means we provide a complete set of usable defaults so that the user has only to specify those details he/she wants to control.

If the user doesn't tell us which disk(s) to use, we should make a reasonable automatic selection. What we want is the "first" "unused" disk. By "first", I mean that we want to consider disks in order of ascending alphanumeric sorted order. More interesting is what we mean by "unused". The obvious (unrealistic) definition of "unused" is "empty". Unfortunately, we can never know what we don't know, so this is not so simple. Even if the disk has no signatures we recognize, we cannot be certain it is completely empty. To do that, we would have to check that every block on the disk is '0', which will rarely be the case. What we can do is a series of checks that, when combined, provide a reasonable degree of certainty.

  1. Ensure that no known signatures exist on the disk, with the exception of partition tables. (lsblk -p <device>)
  2. Ensure that, if there is a partition table on the disk, it contains no partitions.
  3. Ensure that the disk has no holders to eliminate the possibility of it being a multipath or dmraid member device. (/sys/class/block/<device>/holders/ empty)
  4. Ensure that you can open the device with exclusive access to make sure no other software is using it. (see man 2 open and look for O_EXCL)

If all those conditions are met, we may consider the disk to be available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment