If you are booting from a USB drive, then it is not enough to translate the grub4dos root drive to the linux device name as your clever script does.
Under grub4dos, the booting USB device will be hd0 and the first internal hdd will be hd1.
So if you wanted to boot to hd1 and set that as root before calling your code, you would get sdb which is incorrect because the first internal hdd will be sda under linux.
So in the context of booting to grub4dos from USB, your code will need modifying.
My use of changing and then re-setting the root device is not very elegant. I would prefer a way to temporarily save the current root device, set the desired device as root, and then restore the original root device to whatever it was before. Any bright ideas?