Milliways
Posts: 434
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

[Solved]:How to change Disk identifier

Sat Aug 26, 2017 11:08 am

I have routinely duplicated SD Cards for use in multiple PI.
I have processes to allocate unique hostnames.

Now that boot is based on PARTUUID it would be useful to allocate unique Disk identifier.

Is there an existing script to change Disk identifier, and the cmdline.txt and /etc/fstab entries?
Last edited by Milliways on Sun Aug 27, 2017 10:53 am, edited 1 time in total.

User avatar
rpdom
Posts: 15004
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to change Disk identifier

Sat Aug 26, 2017 11:36 am

I believe there is an option in the fdisk advanced menu to change the disk uuids

hommar
Posts: 172
Joined: Sat Mar 25, 2017 1:55 pm
Location: Russia, Yekaterinburg

Re: How to change Disk identifier

Sat Aug 26, 2017 12:55 pm

resize_root script change UUID

RonR
Posts: 479
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: How to change Disk identifier

Sat Aug 26, 2017 5:54 pm

Milliways wrote:
Sat Aug 26, 2017 11:08 am
Is there an existing script to change Disk identifier, and the cmdline.txt and /etc/fstab entries?
Here's a script I wrote to do that to an image file.

Usage is:

image-set-ptuuid imagefile ptuuid

ptuuid must be 8 hex digits.

Milliways
Posts: 434
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: How to change Disk identifier

Sun Aug 27, 2017 1:31 am

RonR wrote:
Sat Aug 26, 2017 5:54 pm
Milliways wrote:
Sat Aug 26, 2017 11:08 am
Is there an existing script to change Disk identifier, and the cmdline.txt and /etc/fstab entries?
Here's a script I wrote to do that to an image file.
Thanks for the script. I have looked through it, and it looks quite comprehensive, and would be good for modifying images to burn.

I must admit I was hoping someone had already done something that just modifies the partition table (which I should be able to do on a live Pi, like I do with hostname).

I was thinking of trying something like the following to dump the partition table, then re-write.

Code: Select all

sfdisk --dump /dev/mmcblk0 > mmcblk0.dump

RonR
Posts: 479
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: How to change Disk identifier

Sun Aug 27, 2017 3:12 am

The disk identifier is the Partition UUID (PTUUID displayed in blkid) and is stored just prior to the partition table in the MBR The PARTUUID's aren't actually stored anywhere, they're simply PTUUID-01 for partition 1 and PTUUID-02 for partition 2 You can change PTUUID on a live system with fdisk as I did on an image file in the script:

Code: Select all

fdisk /dev/mmcblk0 <<EOF > /dev/null
p
x
i
0x${PTUUID}
r
p
w
EOF
Or manually run fdisk and enter p, x, I, 0x_disk_identifier, r,p,w

Similarly for /boot/cmdline.txt and /etc/fstab:

Code: Select all

PARTUUID="$(sed -n 's|^.*PARTUUID=\(\S\+\)\s.*|\1|p' /boot/cmdline.txt)"
if [ "${PARTUUID}" != "" ]; then
  sed -i "s|PARTUUID=\S\+\s|PARTUUID=${PTUUID}-02 |" /boot/cmdline.txt
  sed -i "s|${PARTUUID:0:(${#PARTUUID} - 1)}|${PTUUID}-0|" /etc/fstab
fi
Or manually run nano on /boot/cmdline.txt and /etc/fstab and change the PARTUUID's.

RonR
Posts: 479
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: How to change Disk identifier

Sun Aug 27, 2017 6:24 am

The attached set-diskid script will set the disk identifier (partition table UUID), cmdline.txt, and fstab on /dev/mmcblk0 to the disk identifier of your choice.

Usage:

sudo ./set-diskid diskid

diskid must be 8 hex digits.
Attachments
set-diskid.zip
(635 Bytes) Downloaded 271 times

Milliways
Posts: 434
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: How to change Disk identifier

Sun Aug 27, 2017 10:50 am

RonR wrote:
Sun Aug 27, 2017 6:24 am
The attached set-diskid script will set the disk identifier (partition table UUID), cmdline.txt, and fstab on /dev/mmcblk0 to the disk identifier of your choice.

Usage:

sudo ./set-diskid diskid

diskid must be 8 hex digits.
Thanks for the script.

I ran the script with

Code: Select all

sudo set-diskid $(uuid | cut -c-8)
and it worked OK.

One minor issue, the script responded with "Re-reading the partition table failed.: Device or resource busy", but the change was successful.

User avatar
rpdom
Posts: 15004
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: [Solved]:How to change Disk identifier

Sun Aug 27, 2017 5:24 pm

Milliways wrote:
Sun Aug 27, 2017 10:50 am
One minor issue, the script responded with "Re-reading the partition table failed.: Device or resource busy", but the change was successful.
That message is normal and to be expected when you are changing the partition that is in use at the time. Nothing to worry about.

RonR
Posts: 479
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: How to change Disk identifier

Sun Aug 27, 2017 11:35 pm

Milliways wrote:
Sun Aug 27, 2017 10:50 am
One minor issue, the script responded with "Re-reading the partition table failed.: Device or resource busy", but the change was successful.
That message comes from fdisk, not the script, and is normal as rpdom explained.

Return to “Raspbian”