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.
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
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, 14 January 2023 - 04:55 PM.