Linux – What happens to partition labels after removing a partition

filesystemsgptlinuxpartition

What will happen to all the remaining partition labels if I remove a single partition?
For example if I have a layout that looks like this:

/dev/sda1
/dev/sda2
/dev/sda3
/dev/sda4
/dev/sda5

and if I remove /dev/sda2 will /dev/sda3, /dev/sda4 and /dev/sda5 "shift" their numbers, and am I going to get this:

/dev/sda1
/dev/sda2
/dev/sda3
/dev/sda4

or will the "gap" stay there without any changes for the labels, giving me this:

/dev/sda1
/dev/sda3
/dev/sda4
/dev/sda5

Best Answer

Traditionally, Linux on x86 hardware has used MSDOS partition tables. In this case, removing /dev/sda2 won't shift any of the higher numbered partitions down, because the primary partitions act like "slots": you can use them in any order you like, and removing one doesn't affect any of the others.

If instead you had sda{1-7} with sda4 being the extended partition and sda{5-7} being logical partitions within that extended partition, deleting sda6 would shift sda7 down. Logical partitions simply behave differently in this regard.

Newer versions of Linux are switching to GPT partition tables instead, though this is a slow process since there are limitations that prevent wholesale switching at this time.

In the GPT case, you don't need to use extended partitions to get more than 4 partitions on a single disk, and like MSDOS primary partitions, GPT partition numbers work like slots. You can delete a partition from the middle of a range and only leave a hole, with the existing partitions keeping their number. If you then create a new one, it fills the hole.

Your question asks about partition labels, however, and nothing I've talked about so far has anything to do with labels. Partition labels, in the sense used in Linux, are attributes of the filesystem, not the partition table. They exist to prevent changes to device names from causing problems with mounting filesystems. By using filesystem labels, you don't have to worry about device name changes because you're mounting partitions by label, not by device name. This is particularly helpful in cases like USB, where the device naming scheme is dynamic, and depends in part on what has been plugged in previously since the last reboot.

Linux mkfs.* programs typically use the -L flag to specify the label.

To mount a partition by label instead of by device name, use LABEL=mypartname in the first column of /etc/fstab. If you check your current /etc/fstab, you'll probably find that there are already partitions being mounted that way. Linux GUI installers typically do this for you as a convenience.

You can mount a filesystem by label interactively, too, by passing the label with -L to mount(8).

GPT does allow you to name a partition, but I don't know that it has anything to do with anything discussed above.

EDIT: One thing you do get with GPT which is relevant here, however, is a unique identifier for each partition, called a UUID. They work similarly to labels, but are different in several ways:

  1. UUIDs are automatically assigned pseudorandom numbers, rather than a logical name you pick yourself.

  2. You use -U instead of -L to mount(8) a partition by UUID rather than by label.

  3. You use UUID=big-ugly-hex-number instead of LABEL=mynicelabel in /etc/fstab.

  4. They are attributes of the partition, not the filesystem, so they will work with any filesystem as long as you can use GPT. A good example is a FAT32 partition on a USB stick: FAT32 doesn't have a filesystem label, and since it's on a USB stick you can't reliably predict which /dev/sd* name it will get.

Related Question