Jump to content











Photo
- - - - -

Getting VHD boot source reliably


  • Please log in to reply
No replies to this topic

#1 virgus

virgus

    Newbie

  • Members
  • 26 posts
  •  
    Italy

Posted 3 weeks ago

Dear friends,

I've been using for many years a small script to get the currently booted vhd's source file path. It was useful when doing tests so that I could easily remember from which specific vhd or differencing vhd I was working. The script saves the vhd path to a text file and I read the file with BGinfo or Desktop Info to display it on screen.

 

I got used to it so much that I've been trying to improve it with images on screen (a feature both of BGinfo and Desktop Info), but today I found out that diskpart output is not reliable.

 

I've two main laptops. On the HP the script works fine on W7 Ultimate (not tested with W10 yet). On the Dell the script gets wrong results both on W10 and on W7. The problem is not the script but the output I get from diskpart. At the beginning I suspected the problem was only occurring on W10, but the same problem happens with W7 Pro on the same Dell Laptop and that diskpart version is exactly the same (same CRC) that is working fine on the HP vhd from which I've currently booted from.

 

I also tried VMount, to check what happens with Erwan's tool and the result is coherent but does not help to find a way to get the exact source file from which the OS booted from.

 

Disk-Part-v19041.png

In this first image I booted from W10 and the diskpart output mentions "HarddiskVolume3" as the source of the booted vhd. But Volume 3 is the mounted boot partition itself ! In the HP laptop I would have seen "HarddiskVolume2" instead, as the "LT5X80-W10.vhd" file is on the root of volume E:

 

Expected result should be VDisk 0 Disk 2............\Device\HarddiskVolume2\LT5X80-W10.vhd

And then my script would get the correct letter of \Device\HarddiskVolume2 and the script would end with E:\LT5X80-W10.vhd

Disk-Part-v7601-VMount.png

In this second image I booted from W7 and the error persists. Volume 3 now is an NVME Boot volume which has nothing to do with LT5X80-W7.vhd boot file, which in this case is on volume F:

 

Expected result should be VDisk 0 Disk 3 ............ \Device\HarddiskVolume2\LT5X80-W10.vhd

And then my script would get the correct letter of \Device\HarddiskVolume2 and the script would end with F:\LT5X80-W10.vhd

 

I have no clue at all about how to get the VHD boot path reliably so I'm writing you to ask your experienced advise.

 

This is my diskpart script under revision, the script that seems still reliable on the HP EliteBook but not now on the Dell, for unknown reasons.

@echo off
setlocal EnableDelayedExpansion

SET BOOTLTR=%SystemDrive::=%

::GET BOOTVOL# FROM %SystemDrive::=%
for /f "tokens=2" %%a in ('^(echo list volume^) ^| diskpart ^| findstr /i /r /c:".* %BOOTLTR% .*" ') do SET BOOTVOL=%%a

::GET BOOTDISK# FROM %BOOTVOL%
for /f "tokens=3" %%a in ('^(echo select volume %BOOTVOL% ^& echo detail volume^) ^| diskpart ^| findstr /i /r /c:" Disk [^#]" ') do SET BOOTDISK=%%a

::GET BOOTDEV FROM %BOOTDISK%
for /f "tokens=*" %%a in ('^(echo select disk %BOOTDISK% ^& echo detail disk^) ^| diskpart ^| findstr /i /r /c:".* Device$" /c:".* Virtual .*"') do SET BOOTDEV=%%a

SET BOOTTYPE=%BOOTLTR%: Disk %BOOTDISK%, Volume %BOOTVOL%

:: SVBus Boot Device Cases
IF "%BOOTDEV:SVBus=%" NEQ "%BOOTDEV%" (
   SET BOOTTYPE=SVBus FileBoot
   IF "%BOOTDEV:RAM=%" NEQ "%BOOTDEV%" SET BOOTTYPE=SVBus RamBoot
)

:: Bootmgr Boot Case
IF "%BOOTDEV:Virtual=%" NEQ "%BOOTDEV%" IF "%BOOTDEV:SVBus=%" EQU "%BOOTDEV%" (
   for /f "tokens=*" %%a in ('^(echo list vdisk^) ^| diskpart ^| findstr /i /r /c:" Disk %BOOTDISK%"') do SET output=%%a
   for /f "tokens=2 delims=\" %%c in ('echo !output!') do SET Device=%%c& ECHO !Device!
   for /f "tokens=3 delims=\" %%c in ('echo !output!') do SET VhdSrcVol=%%c& SET VhdSrcVol=!VhdSrcVol:HarddiskVolume=!& ECHO !VhdSrcVol!
   for /f "tokens=3,* delims=\" %%c in ('echo !output!') do SET VhdSrcPath=%%d& ECHO !VhdSrcPath!

   IF !Device!==Device (
      for /f "tokens=3" %%e in ('^(echo list volume^) ^| diskpart ^| findstr /i /r /c:"Volume !VhdSrcVol!" ') do SET VhdSrcRoot=%%e& ECHO VHD Source Root is !VhdSrcRoot!
      SET BOOTTYPE=!VhdSrcRoot!:\!VhdSrcPath!
    )
)

CLS
ECHO Boot is from !BOOTTYPE! & PAUSE

I wish there will be a way also the get the information when booting from SVBus driver. Diskpart outputs VHD file path information only when boot occurs via bootmgr (or NTBOOT, NTloader... ). For now I choosed to write on screen just the way SVBus booted but I wish I could get at least the vhd path when booting from file.

 

Thanks,

V.

 

BTW It just pops into my mind that a dirty solution could be to check all mounted volumes for !VhdSrcPath! path and then get the correct !VhdSrcRoot! letter. But I would prefer something cleaner if possible and I'd like to know if there are maybe easier solutions than this one.


Edited by virgus, 3 weeks ago.


#2 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16065 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 3 weeks ago

Check/compare what happens with dd for windows and ddlist/ddlistw:

 

http://reboot.pro/in...?showtopic=8219

 

Review also this thread:

http://reboot.pro/in...showtopic=20240

 

It is tricky business, you are seemingly  having a mis-match with HardDiskVolume #.

 

:duff:

Wonko


  • virgus likes this

#3 virgus

virgus

    Newbie

  • Members
  • 26 posts
  •  
    Italy

Posted 3 weeks ago

Thanks Wonko for the links.

 

Interesting discussions. Very nice script indeed, I cleaned it and run it and will certainly be useful in the future.

 

As I wrote to Glenn (the author of Desktop Info) a few minutes ago, at the end I choosed to stick to diskpart and do what I didn’t want to do at the beginning: make a for loop to search for the correct path and get rid of the mis-match with HardDiskVolume# output.

   IF NOT EXIST "!BOOTTYPE!" (
      SET BOOTTYPE=^?:\!VhdSrcPath!
      FOR %%G IN (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF EXIST "%%G:\!VhdSrcPath!" SET "BOOTTYPE=%%G:\!VhdSrcPath!"&&GOTO :END
   )

The script might be simplified further but for now it works with the four cases
– Normal “partition” boot
– VHD bootmgr boot (which gets also the vhd path the OS booted from)
– SVBus file boot (no clue of how to get the vhd path here)
– SVBus RAM boot

 

Regarding the SVBus limit with diskpart, do you think there would be a way to log the vhd source path when booting via G4D ? It would be easy to parse a file via Desktop Info and display the information on screen.

 

Read you soon,

Virgus.


Edited by virgus, 3 weeks ago.


#4 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16065 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 3 weeks ago

Sure, if grub4dos "knows" the .vhd path, there is no reason why you cannot write that info to a (already existing) file or (for some reasons better, for some others worse) a specific hard disk sector.

 

Still, grub4dos won't obviously know anything about the drive letter that windows will assign later in booting, so you will need anyway to use one of the other address formats and/or translate the grub4dos path *like* (hd1,0) to a drive letter once it will be assigned by windows.

 

Rare case, but it may happen, about the "IF EXIST", be warned that in some setups (multi card readers, but not only) it won't work/it may cause issues, see;

 

https://msfn.org/boa...-file-in-batch/

 

The actual useful info is in a few posts starting around here:

 

https://msfn.org/boa...#comment-895119

 

Unless you actually have floppies, you can remove A and B from the loop, if you keep that approach.

 

:duff:

Wonko






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users