How to change vfat partition UUID?

How do I change the UUID of a vfat partition?

For ext2 / ext3 / ext4, this is done with a simple:

tune2fs -U <new-uuid> /dev/<partition>

Is there a similar command for vfat partitions?


Solution 1:

It might be helpful to know why you want to do this. I don't know of a tool to do what you want, but there may be other options....

Technically, FAT partitions don't have UUIDs; they have serial numbers, which several Linux tools and files (such as mount and /etc/fstab) can use in place of UUIDs. On GPT disks, the partitions that hold FAT filesystems have GUIDs, which (on recent enough Linux distributions) can be referred to as PARTUUIDs in mount and /etc/fstab. Both FAT filesystems and (on GPT disks) the partitions that contain them can also have names, which can also be used by mount or in /etc/fstab. Any of these features might be usable in place of the FAT serial number.

Although I don't know of a tool to change FAT partitions' serial numbers, they can be set when the filesystem is created via the -i option to mkdosfs, as in:

mkdosfs -i ABCD1234 /dev/sdc1

This example sets the serial number of /dev/sdc1 to ABCD-1234. If the filesystem is small enough, you could back it up, create a fresh filesystem with the desired serial number, and restore it. If you're trying to set the serial number as part of a disk or installation setup script, setting it via mkdosfs might be all you need.

You can change the partition's GUID with gdisk or sgdisk. Using the latter, you'd do the job via the -u option, as in:

sgdisk -u 1:BC87D91A-02F3-4C52-87CD-536A3DF2A074 /dev/sdc

This example sets the GUID of /dev/sdc1 to BC87D91A-02F3-4C52-87CD-536A3DF2A074. (Note that -u takes a colon-delimited set of options, the first of which is the partition number and the second of which is the GUID value. The device filename for the whole-disk device [not the partition] is the final option.)

Depending on your needs, one of these approaches may work. Moving broader afield, you can set the filesystem's label with the dosfslabel tool; or you can change a GPT partition's name with sgdisk's -c option, which works much like -u, but takes a name rather than a GUID.

Solution 2:

The volume ID of FAT32 is stored in the first sector at offset 67 (0x43), for FAT16 it's at 39 (0x27). One can use the dd command to read it (replace /dev/sdc1 with your real partition):

dd bs=1 skip=67 count=4 if=/dev/sdc1 2>/dev/null \
| xxd -plain -u \
| sed -r 's/(..)(..)(..)(..)/\4\3-\2\1/'

And, of course, one can also store a new UUID (replace 1234-ABCD with your desired value):

UUID="1234-ABCD"
printf "\x${UUID:7:2}\x${UUID:5:2}\x${UUID:2:2}\x${UUID:0:2}" \
| dd bs=1 seek=67 count=4 conv=notrunc of=/dev/sdc1

Reference: FAT physical structure (Volume Serial Number offset): https://technet.microsoft.com/en-us/library/cc776720(v=ws.10).aspx#w2k3tr_fat_how_gkxz

Solution 3:

Often one may want to set a new random UUID for a vfat partition (rather than set a specific one), for example to avoid conflicts after duplicating a partition with dd. In this case, a simple solution is to use gparted: right click on the partition, then "New UUID".

@tarabyte suggests that most people come to this page looking for a GUI-less (e.g. command line) way to do this on headless server. If that's the case, gparted shows that the command it's running under the hood when doing the above is something like:

    sudo mlabel -s -n :: -i /dev/nvme0n1p6

Solution 4:

Following on tommy's answer above, the offset for exFAT drives is 100. Other than that, the commands to change the ID for exFAT drives proceed almost verbatim to those for FAT16/32 drives, save a small fsck at the end. Remember, commands must be elevated with sudo or run in a root shell, and the /dev/sdc1 used in the example should be changed to match your device's block id.

You can check the volume ID with:

dd bs=1 skip=100 count=4 if=/dev/sdc1 2>/dev/null \
| xxd -plain -u \
| sed -r 's/(..)(..)(..)(..)/\4\3-\2\1/'

And the command to write a new ID becomes:

UUID="1234-ABCD"
printf "\x${UUID:7:2}\x${UUID:5:2}\x${UUID:2:2}\x${UUID:0:2}" \
| dd bs=1 seek=100 count=4 conv=notrunc of=/dev/sdc1

Finally, I had to recalculate the checksum, which fsck -a accomplished for me

fsck -a /dev/sdc1

I am very much obliged, tommy, thank you.