29

I know about the e2label and friends program for attaching a label to a disk partition e.g.

e2label /dev/sda1 bla

What I want to do is something different: I want to create a label for a hard drive, that is for /dev/sda, not for a partition.

Is that possible and if yes, how?

EDIT: Background: In a case with multiple HD bays I would like to put (paper) labels on the physical bay's door and put the same label logically on the disk I have put in there.

EDIT2: This is about Ubuntu Linux 12.04 server.

EDIT3: On GPT partitions there is also the partlabel, which can be set with e.g. parted. See https://serverfault.com/q/681088/76442

dummzeuch
  • 403
  • 1
    This is a great question as you are one of the fitst, who got the concept right. The labels' main purpose is to uniquely identify a partition even when the drives are enumerated differently after the next reboot. But while drives might get reordered, partitions stay fixed on their drive. So it would be more correct to give drives a UUID/label and not partitions. This would mean, that partitions would be names as UUID/1, UUID/2 and so on. There is nothing wrong in adding a label to the partition, but the default case should be to use a UUID per drive and not per partition. – Patrick Häcker Oct 04 '13 at 08:27
  • Thanks, but I doubt that I am really one of the first. This problem has irked my for a long time already. – dummzeuch Oct 04 '13 at 14:00
  • 1
    If you format your disk as a GUID partition table (GPT) instead of MSDOS or unix, (using gnu parted). Your drive will have a uuid, partitions will have a uuid, and ext2/3/4 drive can have their own uuid. I usually can find the drives using /dev/disk/by-uuid and the partitions by /dev/disk/by-partuuid – Rahly Nov 19 '13 at 22:04
  • That's also what I want to do. I think the OP is referring to the "name" of the entire physical disk. My example is I have a "Seagate BUP Fast HDD". For some reason I formatted it and created only one single partition. I didn't give name to the partition nor the file system. And I know I can do both easily in Disk. However, even now when I mount the disk, Disk shows "Seagate BUP Fast HDD" even that I've formatted the whole disk and didn't give name to the only partition and the file system. How does Disk get that name? And is it possible for me to change it? – velut luna Dec 23 '19 at 08:23
  • I have a device. It has a device label, and a partition or file-system label. I am still trying to workout how/what? – ctrl-alt-delor Jul 23 '22 at 09:59

11 Answers11

12

Your initial question already has a few good answers, so i'll focus on the background to your question, labeling disks.

I use the drive's serial number to print labels for hot swap bays - it's the best way to uniquely and consistently identify the drive. It won't change regardless of which bay, or which controller it is plugged in to.

You don't mention whether you're using Linux or some other Unix, but in Linux you can get a list of disks (and partitions, which we want to exclude) with their brand, model, and serial number by looking in the /dev/disk/by-id/ directory. I find the following bash alias useful for this:

alias list_disks='find /dev/disk/by-id/ -iname 'scsi-*' | grep -v -- -part | while read disk ; do echo $(readlink $disk | sed -e s:../../:: ) $(basename $disk); done'

(matching for scsi-* finds all "scsi-like" drives, including SATA and SAS drives. on systems with only SATA drives, ata-* would work as well)

e.g. on one of my ZFS server systems, it produces output like this:

# list_disks | sort
sdb scsi-SATA_WDC_WD10EACS-00Z_WD-WCASJ2195141
sdc scsi-SATA_WDC_WD10EACS-00Z_WD-WCASJ2114122
sdd scsi-SATA_ST31000528AS_9VP4P4LN
sde scsi-SATA_ST31000528AS_6VP3FWAG
sdf scsi-SATA_ST31000528AS_9VP509T5
sdg scsi-SATA_ST31000528AS_9VP4RPXK
sdh scsi-SATA_OCZ-VECTOR_OCZ-0974C023I4P2G1B8
sdi scsi-SATA_OCZ-VECTOR_OCZ-8RL5XW08536INH7R
sdj scsi-SATA_ST31000528AS_9VP18CCV
sdk scsi-SATA_WDC_WD10EARS-00Y_WD-WMAV50933036

As you can see, the /dev/disk/by-id listing includes each drive's brand and model number, as well as the serial number. They're all SATA drives plugged into either SATA ports or SAS ports on an LSI SAS-2008 controller.

If I had a label printer attached, it would be easy enough to print labels based on the output of list_disks. I used an old manual label-writer instead because that's what I had. The printed labels contain only the serial number without the brand/model (that's enough for me to identify the drive when I need to). The labels come in very handy when a drive fails and needs to be replaced.

cas
  • 78,579
  • I think this is the best solution to my actual problem, but I can't accept this as answer because it does not answer the question. Unless I modify the question which is probably not a good idea. Thanks. – dummzeuch Oct 04 '13 at 07:40
  • no problem, as you said it didn't directly answer the question. but it was useful and hopefully someone else will find it useful in future. btw, here's an improved version of the alias: alias list_disks='find /dev/disk/by-id/ -iname scsi-* -o -iname usb-* | grep -v -- -part | while read disk ; do echo $(basename $(readlink $disk)) $(basename $disk); done | sed -re "s/(usb|scsi)-// ; s/(SATA|Generic)_//" | sort'. it finds usb disks as well and gets rid of extraneous data in the output. – cas Oct 04 '13 at 08:51
  • In order to also include sata disks in the listing add -o -iname ata-* to the above command – carloscolombo Mar 20 '20 at 01:07
  • @carloscolombo yeah, that's changed several times over the years, and it depends on both the kernel version, and the udev version (and the udev script that creates the /dev/disk/by-id/ symlinks). IME, sata disks are exposed as /dev/disk/by-id/scsi-SATA-* - e.g. all the drives in my example above are sata disks, not scsi or sas. – cas Mar 20 '20 at 07:20
7

If this is Linux we're talking about, then I think you can achieve what you want using udev to create a symlink to your devices. If you look at /etc/udev/rules.d, there's a file called 70-persistent-cd.rules whose contents have lines like:

SUBSYSTEM=="block", ENV{ID_CDROM}=="?*", ENV{ID_PATH}=="pci-0000:00:1f.2-scsi-:0:0:0", SYMLINK+="cdrom", ENV{GENERATED}="1"

What this does is it creates the symlink /dev/cdrom to the device identified by its PCI bus address. You should be able to accomplish what you want by identifying the HDD using its (bay number? sorry no experience there) and using similar syntax to the above to add a symlink like /dev/bay1, for example. The following article on writing udev rules may be helpful:

Writing udev rules

Note

Thanks to slm's answer, please note that this solution makes the labels specific to the HDD bays rather than the disks inside them. Thus, if you use the labels /dev/bay1 and /dev/bay2 and you have hard disk A in bay 1 and hard disk B in bay 2, then /dev/bay1 refers to disk A and /dev/bay2 refers to disk B. If, for some reason, you swap the A and B disks, you get /dev/bay1 referring to disk B and /dev/bay2 referring to disk A. From your question, I believe that this is your intent. Please correct me if I'm wrong.

Joseph R.
  • 39,549
  • 1
    I would recommend using model number and serial number for such matching. It does still come with the caveats mentioned by slm, but will ensure that the disk "label" remains consistent regardless of what happens to it or any other disks in the system. – user Oct 03 '13 at 10:50
  • Better still is preferring the guaranteed-unique WWN. See this answer. – Tom Hale Jan 11 '17 at 04:55
7

You don't e2label partitions, you e2label file systems. Those file systems can be on partitions on files, on whole disks, on network block devices... it doesn't matter.

So yes, if there's an ext2/3/4 filesystem directly on /dev/sda, you can label it just the same as one that would be on /dev/sda1.

Now, if there's no filesystem directly on /dev/sda but for instance, /dev/sda is partitioned (using GPT, MBR, LVM...) into several partitions, then abviously no you can't use e2label /dev/sda.

In the GPT partitioning, you can give names to partitions, so you could create a one sector large partition with a name to help identify your disk. You can do that with the c command in gdisk.

4

I assume that your main goal is to unambiguously identify a physical disk. Unfortunately, I don't believe there is a standard way to label a disk the same way that you can label a filesystem.

One way is to use the disk's model and serial number. In Linux, you can retrieve those identifiers with hdparm -i /dev/sdX. The nice thing about the serial number is that it might be already printed on the manufacturer's sticker. (I assume you mostly care about Linux since you mentioned e2label in your question. If so, please edit your question and add the tag.)

If you are using a hardware RAID controller, hdparm might not work for you, in which case you'll have to consult your RAID controller's command-line tools instead.

If you want to identify it by something in the contents of the disk, you could use the disk GUID, which is stored in the GPT partition table header (bytes 56-71 of LBA 1). It's an arbitrary random string rather than a label of your choice, but it's like a label in that it is persistent until you decide to clobber the partition table. In Linux, you can see the disk GUID using gdisk -l /dev/sdX.

200_success
  • 5,535
  • I needed to use uppercase I as hdparm's option, otherwise I'd get the error "HDIO_GET_IDENTITY failed: Invalid argument". Also, the following might be more convenient since it doesn't need root, as described here: udevadm info --query=all --name=/dev/sda | grep ID_SERIAL – Matthias Braun Aug 04 '22 at 11:05
3

To answer your question if it's possible to label a disk as opposed to a partition, the answer is no. I'm not aware of any method to do so.

You can use the method outlined by @JosephR, but realize that this setup is system dependent, so if you were to take that HDD to another system, the "labeling" he's proposed would be lost. It's not tied to the disk in any meaningful way.

Incidentally, you can see the labels and UUID's set for different drives using the blkid command.

Example

$ blkid
/dev/sda1: LABEL="SYSTEM_DRV" UUID="XXXXXX" TYPE="ntfs" 
/dev/sda2: LABEL="Windows7_OS" UUID="XXXXXX" TYPE="ntfs" 
/dev/sda3: LABEL="Lenovo_Recovery" UUID="XXXX" TYPE="ntfs" 
/dev/sda5: UUID="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" TYPE="ext4" 
/dev/sda6: UUID="XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXXfG93LA" TYPE="LVM2_member" 
/dev/mapper/vg_grinchy-lv_root: UUID="XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXX" TYPE="ext4" 
/dev/mapper/vg_grinchy-lv_swap: UUID="XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXX" TYPE="swap" 
/dev/mapper/vg_grinchy-lv_home: UUID="XXXXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXXX" TYPE="ext4" 
slm
  • 369,824
  • Good point about the setup being system-dependent. From the question, I think the OP is aware of that: they want to label HDD bays rather than the actual hard drives inside them. I'm adding it as a note to my answer just in case. – Joseph R. Oct 02 '13 at 14:53
  • @Marco But blkid seems to only list partitions not devices, which is not what the OP is interested in. – Joseph R. Oct 02 '13 at 15:17
  • No, I actually wanted to label the disks, not the bays. But I'll consider this option. – dummzeuch Oct 02 '13 at 16:58
  • It is possible to label a disk, Linux itself does this in /dev/disk/by-id and you can do it udev-style as shown in this answer. – Tom Hale Jan 11 '17 at 04:51
  • @TomHale - you're missing my point. That label is not applied to the HDD itself, but rather is maintained within the Linux system itself. If you were to remove that HDD from the system, the label is no longer available on other systems that the HDD would be mounted to. It is system dependent, therefore my advice is correct. Please reverse your DV. – slm Jan 11 '17 at 18:33
1

Create a udev rule using the disk's guaranteed unique WWN (if it has one), otherwise using the combined vendor and serial IDs:

  1. Get the required values:

    udevadm info -q all /dev/<YOUR-DEVICE-HERE> | egrep "(WWN|SERIAL|VENDOR_ID)="
    
  2. Massage these valuesinto udev rules such as:

    ENV{DEVTYPE}=="disk", ENV{ID_WWN}=="0x5000c1cd31d48c87", SYMLINK+="4TBseagate"
    
    ENV{DEVTYPE}=="disk", ENV{ID_VENDOR_ID}=="0781", ENV{ID_SERIAL}=="SanDisk_Cruzer_Blade_4C530263101009    121490-0:0", SYMLINK+="8GBsandisk"
    
  3. udevadm trigger

To automatically name the symlink using values given by udevadm info -q all /dev/<YOUR-DEVICE>, you could use:

SYMLINK+="disk-$env{ID_VENDOR}-$env{ID_SERIAL_SHORT}"

It's also possible to create ENV variables from ATTRS values to possibly get a shorter unique ID.

Tom Hale
  • 30,455
1

Based on the answers I got (Thanks guys!) there seems to be no way to actually label disks but only partitions. So the answer probably is "NO".

I will go with the suggestions proposed by @cas and @200_success: I'll stick the disks' serial numbers with a sticker to the bay doors.

Creating a small partition on each disk just to be able to label this partition (as suggested by @Stephane Chazelas) is also an option, but I think that would be too confusing for somebody else who might have to maintain the system at a later time.

dummzeuch
  • 403
0

If you ls /dev/disk-by-path/pci*...

You will definitely see a PCI path for each drive bay. You just need to use 1 known disk to ensure you get the labels right.

Use blkid /dev/disk-by-path/pci*to see which disk partitions are on the PCI path.

You can mount using that path too.

slm
  • 369,824
Robert
  • 1
-1

I like @cas solution, but here is my 2¢

for i in /dev/sd?
do
  echo "$i"
  /usr/sbin/smartctl -a "$i" | grep Serial
  /usr/sbin/smartctl -a "$i" | grep "Device Model"
  echo
done
MERM
  • 188
-1

There is a possibilty on an empty disk : use -L option to format the full disk in btrfs mode :

mkfs.btrfs -L MYLABEL /dev/sdz

then recreate partition table...

  • This specifies a filesystem label, not a disk label. 2. This only applies to btrfs. 3. You can do this without reformatting the drive (https://wiki.archlinux.org/title/Persistent_block_device_naming#by-label)
  • – glibg10b May 22 '21 at 14:57