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.
- Ensure that no known signatures exist on the disk, with the exception
of partition tables. (
lsblk -p <device>
) - Ensure that, if there is a partition table on the disk, it contains no partitions.
- 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) - 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 forO_EXCL
)
If all those conditions are met, we may consider the disk to be available.