3

I need to create and assign a new UUID to an ordinary disk partition under Linux.

What tools are available and how do I use them?

goangit
  • 183
  • 1
  • 8

1 Answers1

2

Those working with ordinary disks can do this simply with tune2fs and uuidgen.

Example: using a not currently mounted device /dev/sdb1

tune2fs /dev/sdb1 -U `uuidgen`

The new UUID will be immediately visible under

ls -l /dev/disk/by-uuid

if, say, you need to copy the value to /etc/fstab for automatic mount.

However, blkid will continue to (erroneously) report the old value until the cache is updated (on reboot, for example; though the cache may be bypassed with sudo blkid -c /dev/null).

Alternatively, the new UUID may be obtained via udev with

sudo lsblk -fo UUID /dev/sdb1

Those working with LVM disks might like to check the answer here.

goangit
  • 183
  • 1
  • 8
  • 1
    So it's a filesystem specific type of label? tune2fs works for ext2/3/4, or do other filesystems even have/use block id's? – Xen2050 Dec 12 '14 at 07:49
  • It's a good question and one I hadn't considered. The filesystem I had in mind when writing is indeed ext4, but I am certainly open to answers for other filesystem contexts. – goangit Dec 12 '14 at 07:52
  • 2
    Yes, many filesystems have unique IDs (though they're not always in UUID format). Use `tune2fs -U` for ext; `xfs_admin -U` for xfs; `jfs_tune -U` for JFS; `ntfslabel --new-serial` for NTFS, and so on. (I think btrfs might not allow changing the UUID as it is extensively used internally.) Likewise, GPT partitions and LVM volumes have their own unique IDs. (The former is called a "PARTUUID" and can be seen in `by-partuuid`.) – u1686_grawity Dec 12 '14 at 09:03
  • 1
    Regarding `blkid`, it works by reading directly from the disk, and because of that it needs to cache the retrieved information somewhere accessible to ordinary users. You can bypass the cache using `blkid -c/dev/null` as root. Though, it's better to use tools like `lsblk -f`, which queries udev's automatically-updated cache. – u1686_grawity Dec 12 '14 at 09:07
  • Thanks for the insights into `blkid`'s cache, much appreciated. Answer updated to reflect this input. – goangit Dec 12 '14 at 10:35