Can I label a disk device rather than a partition?

14,983

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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" 

Solution 5

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.

Share:
14,983

Related videos on Youtube

dummzeuch
Author by

dummzeuch

Updated on September 18, 2022

Comments

  • dummzeuch
    dummzeuch almost 2 years

    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
      dummzeuch over 10 years
      Thanks, but I doubt that I am really one of the first. This problem has irked my for a long time already.
    • Rahly
      Rahly over 10 years
      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
    • velut luna
      velut luna over 4 years
      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?
  • Jeff Hewitt
    Jeff Hewitt over 10 years
    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.
  • Jeff Hewitt
    Jeff Hewitt over 10 years
    @Marco But blkid seems to only list partitions not devices, which is not what the OP is interested in.
  • dummzeuch
    dummzeuch over 10 years
    No, I actually wanted to label the disks, not the bays. But I'll consider this option.
  • user
    user over 10 years
    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.
  • dummzeuch
    dummzeuch over 10 years
    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.
  • Alessio
    Alessio over 10 years
    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.
  • Tom Hale
    Tom Hale over 7 years
    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
    Tom Hale over 7 years
    Better still is preferring the guaranteed-unique WWN. See this answer.
  • slm
    slm over 7 years
    @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.
  • carloscolombo
    carloscolombo over 4 years
    In order to also include sata disks in the listing add -o -iname ata-* to the above command
  • Alessio
    Alessio over 4 years
    @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.
  • glibg10b
    glibg10b about 3 years
    1. This specifies a filesystem label, not a disk label. 2. This only applies to btrfs. 3. You can do this without reformatting the drive (wiki.archlinux.org/title/…)