Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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 4 '13 at 8: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 4 '13 at 14:00
    
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
add comment

7 Answers

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.

share|improve this answer
    
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. –  Michael Kjörling Oct 3 '13 at 10:50
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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 4 '13 at 7: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 4 '13 at 8:51
add comment

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" 
share|improve this answer
    
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 2 '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 2 '13 at 15:17
    
No, I actually wanted to label the disks, not the bays. But I'll consider this option. –  dummzeuch Oct 2 '13 at 16:58
add comment

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|improve this answer
add comment

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.

share|improve this answer
add comment

The disk is already labeled as sda, sdb, sdc... The partitions are then labeled for example on sda: sda1, sda2...

You can list all the labels for disks and partitions with ls /dev/sd*

share|improve this answer
2  
Unfortunately these device names change if I move the disk from one computer to another or from one controller to another. –  dummzeuch Oct 2 '13 at 16:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.