31

I have a fedora guest OS in VMware. I want to expand /boot partition, so I add another virtual disk to this VM, and try to clone the disk.

After dd if=/dev/sda1 of=/dev/sdb1, blkid report that /dev/sda1 and /dev/sdb1 have same UUID/GUID.

It's weird that there're 2 same UUIDs in the universe, how to change one of them to another UUID value?


Update 2017-01-25

Subject changed, UUID here means filesystem UUID, not partition UUID.

Since it's filesystem UUID, filesystem specific utils are needed to change the UUID, or use hexeditor to modify raw data on disk (DANGEROUS, not recommended unless you know what you are doing).

  • 10
    It's not weird at all. When you clone a partition with dd, of course the clone will have the same UUID as the original. The UUID is just metadata of the partition - not some inherent property of a physical disk. – nobody May 4 '11 at 3:08
  • Thank you Andrew, I thought UUID is a calculated value before when a new partition been created. Now I know it's just a metadata stuff. – LiuYan 刘研 May 4 '11 at 3:32

10 Answers 10

31

To generate a random new UUID, one can use:

$ uuidgen

To actually change the UUID is file system dependent.

Assuming ext-family filesystem

# tune2fs -U <output of uuidgen> /dev/sdb1

Or if you're confident uuidgen is going to work:

# tune2fs -U $(uuidgen) /dev/sdb1

Assuming btrfs filesystem

# btrfstune -U $(uuidgen) /dev/sdb1

The UUID is stored in the superblock, so a byte-for-byte copy of the filesystem will have the same UUID.

| improve this answer | |
  • 15
    Andy, you don't even need uuidgen command: "tune2fs -U random /dev/sdb1" should work fine – grzuy Mar 21 '12 at 2:58
  • How to run it on FAT? – CMCDragonkai Feb 1 '16 at 10:15
  • @CMCDragonkai, see my answer. – LiuYan 刘研 Feb 10 '17 at 3:42
15

Use:

tune2fs -U random /dev/sdb1

if it's an ext filesystem, or

xfs_admin -U generate /dev/sdb1

if it's an xfs filesystem.


The reason the second partition has the same UUID is because dd just copies data from one file to another (dd if=/dev/sda1 of=/dev/sdb1 = cat /dev/sda1 > /dev/sdb1); dd doesn't know what a partition is, or how to generate a UUID, so it just copied all the data as-is, including the UUID, which is stored in the filysystem superblock near the start of the partition.


By the way,
I also think UUID (Universally Unique IDentifier) is a weird name. It should be called PGUID (Possibly Globally Unique IDentifier), or even better, just ARLN (A Reasonably Large Number).

| improve this answer | |
  • I have backed up one my partitions on /dev/sda6. I get the following error: xfs_admin -U generate /dev/sda6 ERROR: The filesystem has valuable metadata changes in a log which needs to be replayed. Mount the filesystem to replay the log, and unmount it before re-running xfs_admin. If you are unable to mount the filesystem, then use the xfs_repair -L option to destroy the log and attempt a repair. Note that destroying the log may cause corruption -- please attempt a mount of the filesystem before doing this. – gudge Oct 14 '15 at 20:03
  • 1
    +1 for the dirty dirty lie that is UUID... may I also suggest UNUID for "Unfortunately, Not Unique ID" – Foon Mar 20 '19 at 12:04
  • 1
    Well, I would suggest Globally Unique IDentifier (GUID) - "Globally" as in "generally", or "in general" . Oh wait... isn't this actually its original name?... – The Quark May 26 '19 at 21:24
11

Besides of ext2 /ext3 / ex4 and xfs, you can also change UUID of the following filesystem or block device.

Swap

swaplabel -U $NEW_UUID

Software RAID (md raid)

For MD RAID, you must stop the RAID first, then update the UUID when re-assembling. So if your RAID is mounted to /, you need update UUID in offline mode -- use a live CD to do it.

  1. mdadm --stop $RAID_DEVICE
  2. mdadm --assemble $RAID_DEVICE --update=uuid --uuid=$NEW_UUID $MEMBER_DEVICES...

Example:

  1. mdadm --stop /dev/md127
  2. mdadm --assemble /dev/md127 --update=uuid --uuid=2A1D2A1D-2A1D-2A1D-2A1D-2A1D2A1D2A1D /dev/sda2 /dev/sdb2

After UUID been updated, you may need to update grub.cfg (rd.md.uuid=$NEW_UUID in linux/linuxefi/linux16 command line) and/or fstab and/or mdadm.conf and/or update-initramfs (Debian family) and/or dracut --force (Red Hat family) so that you can boot successfully next time.

LUKS (cryptsetup)

cryptsetup luksUUID --uuid=$NEW_UUID $LUKS_DEVICE

Example:

cryptsetup luksUUID --uuid=e0c0e0c0-e0c0-e0c0-e0c0-e0c0e0c0e0c0 ~/encrypted-LUKS.loop

FAT / NTFS / exFAT

This is another story here, currently there're no utilities to modify the filesystem UUID of these filesystems (See reichhart's answer -- the mlabel tool), but we can modify raw data on disk to do this.

WARNING, it's dangerous to modify raw data on disk, and could lost data without careful operation.

Here's detail steps to modify raw data on disk using hexedit.

  1. blkid, write down the UUID of filesystem to paper, or remember it. The UUID string of FAT / exFAT looks like 1122-3344, UUID string of NTFS looks like 1122334455667788.

  2. Open partition device using hexedit

    hexedit <partition device such as /dev/sda1>

  3. Press /, enter the UUID in reverse order (if blkid reported 1234-ABCD, then search CDAB3412) to search the UUID data on disk.

  4. Once UUID is located, change them, press F2 to save and exit.

  5. blkid to verify the UUID.

  6. Once UUID is modified, you may need to update grub.cfg and/or fstab so that you can boot successfully next time.

  7. Execute fsck <partition device such as /dev/sda1> and see a "There are differences between boot sector and its backup" message. In order to solve it, select "1) Copy original to backup", and if you are asked "Perform changes ? (y/n)" you press y.

| improve this answer | |
  • What about hfs / hfs+ or hfsplus? – Wlad Aug 26 '18 at 16:36
  • 1
    You can just use mkfs.exfat -i 01234567 /dev/partitionname where the mkfs.exfatcommand was installed by the package exfat-utils on Fedora Linux. Maybe you should add that to your answer, as the hexedit variant is quite dangerous! – erik Oct 19 '18 at 20:22
  • @erik Thanks for the tip, but we're looking for a method to update old UUID to new UUID on existing file system or block device. – LiuYan 刘研 Oct 22 '18 at 7:20
  • Hey @LiuYan刘研 for NTFS there is ntfslabel tool. askubuntu.com/questions/642591/… – abchk1234 Dec 8 '19 at 9:35
6

For XFS, use: xfs_admin -U <uuid> <device>

Use xfs_admin -u <device> to view a UUID (note lower case option to view, versus upper case option to set). Another post on U&L pointed out the blkid command for viewing all or some of the UUIDs on the system.

| improve this answer | |
2

Partial Answer for VFAT

For ext* and xfs file systems there are already fine answers. But for VFAT I strongly recommend to use mlabel of the mtools package. Using hex editors is DANGEROUS.

Use something like this to create a new UUID:

echo 'drive d: file="/dev/sdb3" exclusive' >>/etc/mtools.conf
mlabel -n d:
| improve this answer | |
  • If you don't mind checking for exclusive access manually (as with all the other commands described here), you could do it as a single command as mlabel -n -i /dev/sdb3 :: – telcoM Sep 23 '18 at 21:21
2

BTRFS

sudo btrfstune -u /dev/sdaX

sudo btrfstune -U 0de6bd81-7013-49a8-bdc5-d832ed209d2c /dev/sdaX

Running btrfsck is recommended because changing UUID in BTRFS is not an atomic operation.

| improve this answer | |
1

For NTFS check this thread for details.

Summary:

sudo ntfslabel --new-half-serial /dev/sdXN
| improve this answer | |
0

Under Fedora 27 I found that the order of the arguments plays a crucial role ... You have to write the device first, otherwise the UUID is not changed. For example

tune2fs /dev/sdb1 -U random
| improve this answer | |
0

To change the dd command resulting duplicated disk partion UUID/s - Use GParted.

From the GParted user interface, select drive, select partition, unmount partition. From the 'Partition folder' choose 'NEW UUID', repeat until all dd drive duplicated partition uuid/s are marked for replacement. For bootable disks, do not replace the UEFI partition uuid.

Review the selected drive letter and partition/s - All OK ? ...

Click the 'Apply All Operations' icon on the GParted user interface.
For bootable disks, edit the /etc/fstab file UUID=string/s and mount point/s.:

| improve this answer | |
0

exFAT

If you are fine just changing the volume label, just use exfatlabel /dev/sda1 newlabel.

However, I didn't found a tool to easily set the UUID/VolumeSerialNumber of exFAT to a specific value and LiuYan's answer didn't work for me as it keeps you left with a wrong boot sector checksum.

Using the exFAT spec I figured out the proper way to change the UUID. Here the result in pure bashism

# set exFAT UUID to aabb-ccdd
$ dd if=/dev/sda1 of=fshead bs=512 count=24
# best keep a backup if something goes wrong
$ cp fshead fshead.bak
# set UUID in main boot sector (in reversed order (little endian))
$ echo "000064: ddccbbaa" | xxd -r - fshead
# set UUID in backup boot sector
$ echo "001864: ddccbbaa" | xxd -r - fshead
# run fsck to get the checksum
$ fsck.exfat fshead
exfatfsck 1.2.5
ERROR: invalid VBR checksum 0x276e3c6f (expected 0x2771966f).
# fix the checksum (again in reversed order (little endian))
$ python -c "print('1600: '+'6f967127 '*64)" | xxd -r -c 256 - fshead
$ python -c "print('1700: '+'6f967127 '*64)" | xxd -r -c 256 - fshead
$ python -c "print('2e00: '+'6f967127 '*64)" | xxd -r -c 256 - fshead
$ python -c "print('2f00: '+'6f967127 '*64)" | xxd -r -c 256 - fshead
# check checksum (then cancel with CTRL-C)
$ fsck.exfat fshead
exfatfsck 1.2.5
WARN: file system is larger than underlying device: 256087425024 > 12288.
^C
# checksum is ok, so lets flash it back
dd if=fshead of=/dev/sda1 bs=512 count=24
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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