86 - Dynamically change the disk volume label using grub4dos

hit counter
Unsupported embed

Yesterday, I came across the need to have a multiboot grub4dos USB Flash drive that needs to have different volume labels on the same USB drive at different times!

For instance, when I boot one ISO, the volume label of the flash drive needs to be OODIPRO for O&O DiskImage ISO booting, but some other ISOs require a different or at least a known label - e.g. Trinity Rescue and Fedora. It would be nice if grub4dos could change the volume label of the boot device (e.g. USB Flash drive or HDD).

We can use the grub4dos cat command to change the volume label of a FAT16 or FAT32 USB boot drive as follows: 1. First we need to find the sector that that the volume label is in..

.

In RMPrepUSB - select your multiboot drive and then hit CTRL-D to invoke Disk Doctor - click on the Search Disk button and search for the volume label - e.g. 'MYLABEL' by entering the search string TMYLABEL (the T denotes a non-case sensitive Text search string) - DiskDoctor should display the sector and the label will usually be at the start of the sector followed by the first files in the directory. The volume label is 11 bytes followed by 08 or 28. If the label is shorter than 11 bytes it is always padded out by spaces (20).

e.g.

[Sector 30704] 14MB

0000 4F 4F 44 49 50 52 4F 20 - 20 20 20 08 00 00 00 00 OODIPRO .....

0010 00 00 00 00 00 00 AC B4 - 45 41 00 00 00 00 00 00 ......¬´ EA......

0020 41 55 54 4F 45 58 45 43 - 42 41 54 26 10 49 88 45 AUTOEXEC BAT&.IˆE

0030 E5 40 E5 40 00 00 8F B0 - EC 3E 03 00 7F 00 00 00 å@å@..° ì>.....

0040 42 43 44 42 4F 4F 54 20 - 45 58 45 20 18 4D 88 45 BCDBOOT EXE .MˆE

0050 E5 40 45 41 00 00 C7 11 - EE 3A 04 00 00 3C 02 00 å@EA..Ç. î:...<..

0060 42 4F 4F 54 4D 47 52 20 - 20 20 20 20 08 53 88 45 BOOTMGR .SˆE

Let us suppose that the sector it finds the volume label in is sector 30704 as shown in the above listing.

2. Now we could change this label to another string in our grub4dos menu.lst using:

this should display '0' (if debug is on) indicating that it found the string OODIPRO at byte 0 in sector 30704 - if it does not display '0' then you have probably got it wrong! The problem is we need to know what the volume label is currently set to.

However, you could always use this code, which does not need to know what the label is currently set to:

title boot DiskImage Pro

errorcheck off

debug off

# get first character of the current volume label as LAB variable - the number 30704 will be different for each drive you make!

# this will only work if the volume label is the first entry in the sector.

cat --length=1 (hd0)30704+1 | set LAB=

# now replace it with the volume label that we want (one instance only) - MAX=11 LETTERS!

cat --locate=%LAB% --replace=OODIPRO --number=1 (hd0)30704+1

debug 1

errorcheck on

# now continue with rest of menu for an ISO or flat file boot...

map /_ISO/OODI_6.8.1_PRO_ENU.ISO (0xff)

map (hd0) (hd1)

map (hd1) (hd0)

map --hook

chainloader (0xff)/BOOTMGR || chainloader (0xff)

Note that the character after 30704+1 is a vertical bar (OR) character - I recommend you cut and paste the menu so that you get the correct character. Depending on your country and keyboard it may appear as ¦ or | .

You do not need to know which sector the label is in, if you use a large number of sectors to search and already know the volume label - but this will take several seconds and it relies on the volume label being a unique string not found anywhere before the directory entries - e.g.

cat --locate=MYLABEL --replace=OODIPRO --number=1 (hd0)0+80000

All of this will only work on FAT volumes though. For NTFS boot drives you would need to modify the code to search and replace unicode 2-byte strings. The length of the string is also crucial, so changing an NTFS volume label is far more complex!

cat --locate=OODIPRO --replace=NEWLABEL --number=1 (hd0)30704+1

Unsupported embed