diff --git a/Duplicati/UnitTest/DiskImageTests.cs b/Duplicati/UnitTest/DiskImageTests.cs index 2ee48f42e..5efe84420 100644 --- a/Duplicati/UnitTest/DiskImageTests.cs +++ b/Duplicati/UnitTest/DiskImageTests.cs @@ -222,6 +222,7 @@ namespace Duplicati.UnitTest VerifyPartitionTableMatches(); // Verify data matches byte-for-byte + DiskImageVhdHelper.MountForReading(_restoreVhdPath); VerifyRestoredData(_sourceDriveLetter); } diff --git a/Duplicati/UnitTest/DiskImageVhdHelper.cs b/Duplicati/UnitTest/DiskImageVhdHelper.cs index 61913ef15..952be37b2 100644 --- a/Duplicati/UnitTest/DiskImageVhdHelper.cs +++ b/Duplicati/UnitTest/DiskImageVhdHelper.cs @@ -280,6 +280,52 @@ namespace Duplicati.UnitTest } } + public static void UnmountForWriting(string vhdPath, char driveLetter) + { + // Remove the drive letter + var diskNumber = GetDiskNumber(vhdPath); + var script = $@" + Get-Volume -Drive {driveLetter} | Get-Partition | Remove-PartitionAccessPath -AccessPath {driveLetter}:\ + "; + RunPowerShell(script); + + // Pull the disk offline to ensure it's not in use + script = $@" + Set-Disk -Number {diskNumber} -IsOffline $true + "; + RunPowerShell(script); + + // Clear the readonly flag + script = $@" + Set-Disk -Number {diskNumber} -IsReadOnly $false + "; + RunPowerShell(script); + } + + public static void MountForReading(string vhdPath, char? driveLetter = null) + { + if (driveLetter is null) + { + driveLetter = FindAvailableDriveLetter(); + } + try + { + var diskNumber = GetDiskNumber(vhdPath); + if (diskNumber >= 0) + { + var script = $@" + Set-Disk -Number {diskNumber} -IsOffline $false + Get-Partition -DiskNumber {diskNumber} | Set-Partition -NewDriveLetter {driveLetter} + "; + RunPowerShell(script); + } + } + catch (Exception ex) + { + TestContext.Progress.WriteLine($"Warning: Failed to mount VHD for reading: {ex.Message}"); + } + } + /// /// Gets the disk number for an attached VHD. ///