[HOWTO] set a disk identifier of your disk

Post tutorials, HOWTO's and other useful resources here.
User avatar
Rava
Contributor
Contributor
Posts: 5416
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] set a disk identifier of your disk

Post#1 by Rava » 21 Nov 2023, 08:24

You have to be user root. This HOWTO assumes you have a root terminal opened already.

TL;DR:
According to the man pages, only sfdisk can do so (not gparted, nor cfdisk nor fdisk)
DEVICE is the device or your drive. Cave! All partitions of that disk drive should be umounted (and swapoff'd) during this operation. Valid DEVICE is e.g. /mnt/sdb not /mnt/sdb1 !

Code: Select all

sfdisk --disk-id DEVICE
shows you the current disk identifier ID (helpful to not overwrite the disk identifier of the wrong device)

Code: Select all

sfdisk --disk-id DEVICE ID
sets the ID. It should be an appropriate randomized hex code.
Like so:

Code: Select all

sfdisk --disk-id /dev/sdd 0x7be907a5
The long read:
I checked two of my similar blue HAMA 8 GB USB thumbdrives.

One has the "Disk identifier" set

Code: Select all

root@rava:/# fdisk -l /dev/sdc
Disk /dev/sdc: 7.47 GiB, 8016363520 bytes, 15656960 sectors
Disk model: USB Flash Disk  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7ae803a4

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdc1  *       32 15656959 15656928  7.5G  b W95 FAT32
the other not:

Code: Select all

root@rava:/# fdisk -l /dev/sdd
Disk /dev/sdd: 7.47 GiB, 8016363520 bytes, 15656960 sectors
Disk model: USB Flash Disk  
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdd1          32 15656959 15656928  7.5G  b W95 FAT32
I looked through man fdisk but no hit for "identifier", also no hit in man cfdisk or man gparted.

Then I manually searched all appropriate man pages :

Code: Select all

guest@rava:/usr/man/man8$ ls -l *fdisk* gparted*
-rw-r--r-- 1 root root  8218 2022-02-15 19:37 cfdisk.8
-rw-r--r-- 1 root root 17217 2022-02-15 19:37 fdisk.8
-rw-r--r-- 1 root root  2408 2021-07-20 20:44 gparted.8
-rw-r--r-- 1 root root 26620 2022-02-15 19:37 sfdisk.8
(Of course you can only do so when you [downloaded and] activated 06-man.xzm)

And as you can see, at least according to the man pages only sfdisk can do so:
not gparted, nor cfdisk nor fdisk:

Code: Select all

guest@rava:/usr/man/man8$ grep -i identifier *fdisk* gparted*
sfdisk.8:Change the disk identifier. If \fIid\fP is not specified, then print the 
current identifier. The identifier is UUID for GPT or unsigned integer for MBR.
sfdisk.8:Specify the partition table identifier. It should be a hexadecimal number 
(with a 0x prefix) for MBR and a UUID for GPT.
I even searched for grep -i ident *fdisk* gparted* but there is no appropriate additional hit (I did this very search for this abbreviated search pattern because it could be on a line break with a colon in between the wrapped parts)

Let's copy the relevant [ code ] directly from man sfdisk to get rid of the inserted code for bold or underlined highlighting you get when just grep'ing the man page via the terminal:

Code: Select all

--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.
Since it is also the UUID a drive should not have 0x00000000 for its disk identifier.

To get the disk identifier for drive /dev/sdc:

Code: Select all

sfdisk --disk-id /dev/sdc
Mount the partition

Code: Select all

root@rava:~# mount /mnt/sdc1/
to see if it is the correct drive, e.g. via ls /mnt/sdc1/

1. umount all partitions of /mnt/sdc (there is only one partition: /mnt/sdc1/ ) and swapoff all swap partitions that could be on /mnt/sdc (there is no swap partition on /mnt/sdc; using a USB2 thumbdrive for swap would be quite silly)
2. list the current disk identifier
3. set the new disk identifier to "0x7be907a5"
4. list the current disk identifier

Code: Select all

root@rava:~# umount /mnt/sdc1/
root@rava:~# sfdisk --disk-id /dev/sdc
0x00000000
root@rava:~# sfdisk --disk-id /dev/sdc 0x7be907a5
Disk identifier changed from 0x00000000 to 0x7be907a5.

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
root@rava:~# sfdisk --disk-id /dev/sdc
0x7be907a5
That's all folks!
Cheers!
Yours Rava

donald
Full of knowledge
Full of knowledge
Posts: 2073
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

[HOWTO] set a disk identifier of your disk

Post#2 by donald » 21 Nov 2023, 11:50

Rava wrote:
21 Nov 2023, 08:24
....only sfdisk can do so (not gparted, nor cfdisk nor fdisk)

Code: Select all

root@porteus:/home/guest# fdisk -l /dev/sdc
Disk /dev/sdc: 3.8 GiB, 4009754624 bytes, 7831552 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xfa5d0ab8

Device     Boot Start     End Sectors  Size Id Type
/dev/sdc1        2048 7831551 7829504  3.8G 83 Linux

Code: Select all

root@porteus:/home/guest# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): x

Expert command (m for help): i

Enter the new disk identifier: 0x00000000

Disk identifier changed from 0xfa5d0ab8 to 0x00000000.

Expert command (m for help): r

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Code: Select all

root@porteus:/home/guest# fdisk -l /dev/sdc
Disk /dev/sdc: 3.8 GiB, 4009754624 bytes, 7831552 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start     End Sectors  Size Id Type
/dev/sdc1        2048 7831551 7829504  3.8G 83 Linux
root@porteus:/home/guest#
;)

User avatar
Rava
Contributor
Contributor
Posts: 5416
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

[HOWTO] set a disk identifier of your disk

Post#3 by Rava » 21 Nov 2023, 21:58

^ Then why is

Code: Select all

man fdisk
not mentioning "disk identifier"?

Also, my quote said this
Rava wrote:
21 Nov 2023, 08:24
According to the man pages, only sfdisk can do so (not gparted, nor cfdisk nor fdisk)
(highlighting by me)
Cheers!
Yours Rava

Post Reply