2

I'm studying partitioning under Linux, starting with sfdisk. If I copy a partition table from one drive to another, it'll copy the device UUID and the PTUUIDs for each partition, but if I'm creating a new device, I can specify a UUID for GPT drives, but not MBR drives. This leads me to think that UUIDs and PTUUIDs are not necessary for MBR drives. What is the situation with that?

And if I need a UUID for the drive, plus PTUUIDs for the partitions, how do I do that by hand? I see that sfdisk allows me to specify a UUID for GPT devices, but only a label for MBR devices. How can I create a UUID for an MBR and what can I do to make sure the partition PTUUIDs are created based on that device UUID? I can't find out how to make the primary UUID for the device or how to make PTUUIDs based on it for the partitions.

0

2 Answers 2

5

I assume you mean "a PTUUID for the disk and PARTUUIDs for the partitions", since there is no such thing as "PTUUIDs for partitions".

To answer to your question title: No, PTUUIDs and PARTUUIDs are not important for MBR, because MBR was developed back in the 1980s when the concept of 128-bit UUIDs was not common yet. A single primary partition entry in the MBR partition table is just 16 bytes: the idea of adding another 16 bytes per partition for just one long identifier would have been considered a crazy waste of space at the time. Since the primary MBR partition table must fit into the same single 512-byte disk block with the actual boot record code, there is not really any space to add it later either.

You may have noticed that real UUIDs tend to be of the format 12345678-9abc-def0-1234-56789abcdef0, but on MBR-partitioned disks, both PTUUID and PARTUUIDs, if they are reported at all, are much shorter strings: a PTUUID might be 12345678 and a PARTUUID 12345678-01, respectively?

This is because the identifiers displayed in place of PTUUID and PARTUUIDs for MBR-partitioned disks are not real UUIDs. They are based on a 32-bit disk identifier, which was added to MBR for the first time by Windows NT 3.5. It was originally called "Windows Disk Signature", but I guess "disk identifier" is a more vendor-neutral term. Since operating systems before Windows NT 3.5 did not have such a disk identifier, it must be treated as an optional identifier, unlike on a GPT-partitioned disk, where a PTUUID and PARTUUIDs are mandatory parts of the partitioning scheme.

The "PTUUID string" of a MBR-partitioned disk is just that 32-bit disk identifier by itself, and "PARTUUIDs" are derived from it by simply adding a dash and a two-digit partition number at the end. These strings do not fulfill the specification for real UUIDs, as they are not long enough to ensure uniqueness and have not been created using the same rules. They are just the best available substitute for real UUIDs.

When you start creating a new MBR partition table on a completely unused disk, a modern Linux fdisk includes this initial message:

Device does not contain a recognized partition table.
Created a new DOS (MBR) disklabel with disk identifier 0x1234abcd.

Command (m for help):

So when creating a new MBR partition table, it automatically creates a disk identifier for you. The "PTUUID" of this disk would be 1234abcd and the "PARTUUID" of the first partition would be 1234abcd-01.

To change an existing disk identifier for a MBR disk, you can use sfdisk --disk-id as mentioned by Philip Couling, or you can start a modern enough version of Linux fdisk for that disk, type x to enter the expert commands menu, and there type i for the change the disk identifier action.

Changing the disk identifier/PTUUID on a MBR-partitioned disk will necessarily also change all PARTUUIDs, since they are all derived from the same disk identifier.

If the filesystem inside a partition also supports an UUID, that is displayed by Linux tools like blkid and lsblk as just UUID. Its format and existence depends on the filesystem type: for filesystems of the FAT family, you can see a short identifier such as UUID=1A2B-3C4D instead of a real 128-bit UUID. This is really a FAT volume serial number (also known as volume ID) which is assigned at the time of filesystem creation. To change this, use a filesystem-specific tool, like tune2fs -U for ext2/ext3/ext4 filesystems, xfs_admin -U for XFS, or mlabel -N for filesystems of the FAT family.

3

This leads me to think that UUIDs and PTUUIDs are not necessary for MBR drives.

That's mostly correct. PTUUIDs on the partition table itself don't serve any purpose to MBR itself, but it's used by the system. It doesn't have PARTUUIDs on the partitions at all, just their position in the table.

And if I need a UUID for the drive, plus PTUUIDs for the partitions, how do I do that by hand? I see that sfdisk allows me to specify a UUID for GPT devices, but only a label for MBR devices.

Actually sfdisk does allow you to set the "disk-id". MBR partition UUIDs are just the disk-id followed by the position in the table. So if I set a disk-id to 12345 the first PARTUUID will be 12345-1. See sfdisk manual:

  --disk-id device [id]
      Change the disk identifier. If id is not specified, then
      print the current identifier. The identifier is UUID for GPT
      or unsigned integer for MBR.

Background

The naming convention used by Linux erroneously uses the term "UUID" which is very misleading. Universally Unique Identifier is a very specific standardised format of unique identifier with strictly standardised rules on generation that depend on the version embedded in each ID. To avoid confusion I'll avoid using the acronym "UUID" for anything other than the Linux partitioning use:

The Linux naming convention is:

  • "PTUUID" is the unique identifier for partition table itself embedded in the table. GPT uses Universally Unique Identifiers for this, MBR does not. However MBR does have a short PTUUID because humans are bad at picking unique names for their drives.
  • "PARTUUID" is the unique identifier for a partition inside the partition table.
    • MBR does not record any such ID. However it only has a maximum of 4 partitions. So conventionally partitions can be easily referred to by their position in the table (1,2,3,4). So to identify an MBR partition on a system you need both the PTUUID of the MBR table and the position in the table. Eg on my virtual machine I have a drive ee7273d0 with partition ee7273d0-1. This can be found under /dev/disk/by-partuuid/ee7273d0-1.
    • GPT does record these and uses Universally Unique Identifiers for them. So because of the strict rules of Universally Unique Identifiers, the PTUUID is not needed to identify the partition: they will always be unique as long as they are properly generated.
  • "UUID" is the unique identifier embedded in the file system, LUKS header, or LVM physical volume etc. An unformatted partition will not have one likewise a LUKS encrypted drive with a detached header will not have one.
    • An ext4 file system uses Universally Unique Identifiers for it's UUID
    • FAT and FAT32 use a much shorter ID that could be the same as another drive somewhere.
    • LVM doesn't seem to use Universally Unique Identifiers but it's IDs are so long and randomised the risk of collision with another drive negligible.
8
  • A few points/questions - since I want to be sure I get this all. First, I'm working on a project when I can, so I tend to focus on what I can NOW and sometimes forget what I knew a month ago, which may be the last time I worked on it. Thanks for the background and the reference from the man page. I did read the man page, but sometimes something like that has so much information that it's hard to remember it all or sort tit out, so all that clarification is a big help! Commented Jul 21, 2023 at 8:10
  • When using sfdisk, and I use --disk-id to set an ID, will the OS see that as the PTUUID, then update the PARTUUIDs to match it? This is all for a drive copy program for the Raspberry Pi and Pi disks (for me that's microSD cards or USB sticks) and several of the Linux distros for the Pi use PARTUUID in /boot/cmdline.txt and in /etc/fstab to specify the disk to use for booting or for the mount points. (Important, since many people use USB sticks that can change device nodes between reboots.) And do I need to add "PTUUID" to the ID or will it be seen as the PTUUID anyway? Commented Jul 21, 2023 at 8:28
  • @Tango don't worry about it. Naming conventions in this area are messy making it super easy to miss things. You can basically ignore the PTUUID except to know how the mechanism works. If you use cfdisk to change the ID of an MBR file system, sfdisk will try to ask the OS to update immediately and this will include correcting PARTUUID. Even if that fails, a rebooting or disconnecting the drive and reconnecting will force it to re-read from disk. so as an experiment sfdisk --disk-id /dev/sdc 255 immediately results in the first partition /dev/disk/by-partuuid/000000ff-01 -> ../../sdc1 Commented Jul 21, 2023 at 9:03
  • I ran into something last night, after reading this. I was tired and about to go to bed, so I haven't had time to check more into it, but the version of sfdisk that comes in Raspberry PiOS does not have the --disk-id option. So now I know why I don't remember it from the man page. Really frustrating when some distros lack features in some commands. (Seen it before - can't remember which commands.) In fact, doing quick searches on man pages shows no way to change the label with cfdisk, fdisk, sfdisk, or parted. You can set a label for partitions, but nothing to set it for the main device! Commented Jul 21, 2023 at 15:41
  • @Tango I'm not sure which variant of sfdisk that is but I wonder if the disk-id is present in the dump sfdisk -d /dev/sdc. If so you may be able to modify it in the dump file and re-load with sfdisk /dev/sdc < dump-file.txt. Commented Jul 21, 2023 at 15:57

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.