Wednesday 31 July 2019

Reliably identify a disk (not a partition) in Linux (Debian)


I want to backup the partition table of a disk in a script like this:


sfdisk --dump /dev/sda > partition_table.dump

But




  • I do not want to use /dev/sda, because it may not be reliable.




  • /dev/disk/by-id seems also not to be reliable.




  • /dev/disk/by-uuid and df include partitions only (not disks).




However fdisk -l shows a "Disk Identifier" UUID. Can I somehow get the disk name (sda) from the disk identifier? Or is there another way to reliably identify a disk?



Answer



Use /dev/disk/by-id/ anyway. The issue you link to looks to me like a rare hardware malfunction or something similarly bad.


Compare


ls -l /dev/disk/by-id/

to


# 'lsblk' is from 'util-linux' package
lsblk -pdo NAME,VENDOR,MODEL,SERIAL,WWN

However on some of my systems the above command displays blank values; still you can try:


# the below 'smartctl' command (from 'smartmontools' package) probably needs 'sudo'
for sd in $(lsblk -pdo NAME | tail -n +2); do
printf "%s\n" "$sd"; smartctl -i "$sd" |
grep -E "Model:|Number:|WWN"
done

I don't know much about WWN but serial numbers should be hardcoded in hardware. My point is, if any serial number changes for whatever reason then you may have bigger problems than a backup script that suddenly doesn't work.




Note a serial number identifies a physical device no matter what its partition table is (or if there is one at all, study the term "superfloppy" and this question). If you want to identify partition tables themselves then these "Disk Identifier" UUIDs you discovered will be the right approach (note they are 128-bit UUIDs in GPT scheme but 32-bit optional signatures in MBR, example here). They are just few bytes on disk that can be changed, cloned, backed up. I haven't found any quick way to identify a device node by this type of identifier, other than browsing through available devices:


id=8080fdb4-6905-4f34-91fa-61389615d7d3
# the below 'fdisk' command (from 'util-linux' package) probably needs 'sudo'
for sd in $(lsblk -pdo NAME | tail -n +2); do
fdisk -l "$sd" | grep -qi " ${id}$" && printf "%s\n" "$sd"
done




/dev/disk/by-uuid [...] include partitions only (not disks).



Well, it may include not even all of them because these UUIDs refer to structures inside partitions (like filesystems or swap), not to partitions themselves (and if you deal with superfloppy, it will be here as a whole disk). Each of these UUIDs is written somewhere inside its corresponding partition (or device, if superfloppy). These are identifiers you see when you invoke e.g.


# 'file' is from 'file' package
file -s /dev/sda2

If you need partitions identifiers, they are in /dev/disk/by-partuuid/. These are the same as in


# 'partx' is from 'util-linux' package
partx --show /dev/sda

and they are stored in a corresponding partition table, not inside any partition. You can get a broader picture by running


# 'blkid' (from 'util-linux' package) probably needs 'sudo'
blkid

To identify a device that holds a partition with a given UUID, try:


id=68e3b991-5c7a-4d18-a120-834ef4effe00
readlink -e "/dev/disk/by-partuuid/$id" | sed 's/[0-9]*$//'

(sed just strips trailing digits here). This solution can be easily adapted to retrieve information from /dev/disk/by-uuid/ if you need.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...