Skip to content

Commit

Permalink
Merge a52f9be into a725dab
Browse files Browse the repository at this point in the history
  • Loading branch information
ader1990 committed Aug 28, 2019
2 parents a725dab + a52f9be commit 4703769
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
43 changes: 42 additions & 1 deletion Tests/WinImageBuilder.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Describe "Test Resize-VHDImage" {
}
Mock Get-Volume -Verifiable -ModuleName $moduleName { return @{"DriveLetter" = "F"} }
Mock Optimize-Volume -Verifiable -ModuleName $moduleName { return }
Mock Get-PartitionSupportedSize -Verifiable -ModuleName $moduleName { return @{"SizeMin" = 100} }
Mock Get-PartitionSupportedSize -Verifiable -ModuleName $moduleName { return @{"SizeMin" = 100; "SizeMax" = 1000} }
Mock Resize-Partition -Verifiable -ModuleName $moduleName { return 0 }
Mock Resize-VHD -Verifiable -ModuleName $moduleName { return 0 }
Mock Dismount-VHD -Verifiable -ModuleName $moduleName { return 0 }
Expand All @@ -177,6 +177,47 @@ Describe "Test Resize-VHDImage" {
}
}

Describe "Test Resize-VHDImage with binary search" {
function Get-VHD { }
function Mount-VHD { }
function Resize-VHD { }
function Dismount-VHD { }
Mock Write-Host -Verifiable -ModuleName $moduleName { return 0 }
Mock Get-VHD -Verifiable -ModuleName $moduleName { return @{"Size" = 100; "MinimumSize" = 10} }
Mock Mount-VHD -Verifiable -ModuleName $moduleName {
$b = New-Object System.Management.Automation.PSObject
$b | Add-Member -MemberType NoteProperty -Name "Number" -Value 1 -Force
return $b
}
Mock Get-Disk -Verifiable -ModuleName $moduleName {
$b = New-Object System.Management.Automation.PSObject
$b | Add-Member -MemberType NoteProperty -Name "DiskId" -Value 1 -Force
return $b
}
Mock Get-Partition -Verifiable -ModuleName $moduleName {
$b = New-Object System.Management.Automation.PSObject
$b | Add-Member -MemberType NoteProperty -Name "DriveLetter" -Value "L" -Force
$b | Add-Member -MemberType NoteProperty -Name "Size" -Value 90 -Force
return $b
}
Mock Get-Volume -Verifiable -ModuleName $moduleName { return @{"DriveLetter" = "F"} }
Mock Optimize-Volume -Verifiable -ModuleName $moduleName { return }
Mock Get-PartitionSupportedSize -Verifiable -ModuleName $moduleName { return @{"SizeMin" = 10GB; "SizeMax" = 1000GB} }
Mock Resize-Partition -Verifiable -ModuleName $moduleName { throw "Failure to resize" }
Mock Resize-VHD -Verifiable -ModuleName $moduleName { return 0 }
Mock Dismount-VHD -Verifiable -ModuleName $moduleName { return 0 }
Mock Start-Sleep -Verifiable -ModuleName $moduleName { return }

It "Should resize a vhd image" {
Resize-VHDImage -VirtualDiskPath "fakePath" `
-FreeSpace 100 | Should -Contain 0
}

It "should run all mocked commands" {
Assert-MockCalled -Times 10 -CommandName "Resize-Partition" -ModuleName $moduleName
}
}


Describe "Test New-WindowsOnlineImage" {
function Optimize-VHD { }
Expand Down
58 changes: 41 additions & 17 deletions WinImageBuilder.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -983,12 +983,14 @@ function Resize-VHDImage {
$Drive = (Mount-VHD -Path $VirtualDiskPath -Passthru | `
Get-Disk | Get-Partition | Get-Volume | `
Sort-Object -Property Size -Descending | Select-Object -First 1).DriveLetter
try
{

try {
Optimize-Volume -DriveLetter $Drive -Defrag -ReTrim -SlabConsolidate

$partitionInfo = Get-Partition -DriveLetter $Drive
$MinSize = (Get-PartitionSupportedSize -DriveLetter $Drive).SizeMin
$partitionResizeInfo = Get-PartitionSupportedSize -DriveLetter $Drive
$MinSize = $partitionResizeInfo.SizeMin
$MaxSize = $partitionResizeInfo.SizeMax
$CurrSize = $partitionInfo.Size/1GB
Write-Log "Current partition size: $CurrSize GB"
# Leave free space for making sure Sysprep finishes successfuly
Expand All @@ -997,20 +999,42 @@ function Resize-VHDImage {
Write-Log "New partition size: $newSizeGB GB"

if (($NewSize - $FreeSpace) -gt $MinSize) {
$global:i = 0
$step = 100MB
# Adding 10 retries means increasing the size to a max of 1.5GB,
# which should be enough for the Resize-Partition to succeed.
Execute-Retry {
$sizeIncreased = ($NewSize + ($step * $global:i))
Write-Log "Size increased: $sizeIncreased"
$global:i = $global:i + 1
Resize-Partition -DriveLetter $Drive -Size $sizeIncreased -ErrorAction "Stop"
} -maxRetryCount 10
}
}
finally
{
$global:i = 0
$global:sizeIncreased = 0
try {
$step = 100MB
# Adding 10 retries means increasing the size to a max of 1.5GB,
# which should be enough for the Resize-Partition to succeed.
Execute-Retry {
$global:sizeIncreased = ($NewSize + ($step * $global:i))
Write-Log "Size increased: $sizeIncreased"
$global:i = $global:i + 1
Resize-Partition -DriveLetter $Drive -Size $global:sizeIncreased -ErrorAction "Stop"
} -maxRetryCount 10
} catch {
Write-Log "Partition could not be resized using an incremental method"
Write-Log "Trying to resize partition using a binary search method"
$binaryTries = 0
# For example, with 10 binary tries and a max min difference of 1TB space,
# we will get 1024 / 1024 = 1 GB difference
$binaryMaxTries = 10
$MinSize = $global:sizeIncreased
while (($MinSize -lt $MaxSize) -and ($binaryTries -lt $binaryMaxTries)) {
$desiredSize = $MinSize + ($MaxSize - $MinSize) / 2
Write-Log "Trying to decrease the partition to $desiredSize"
try {
Resize-Partition -DriveLetter $Drive -Size $desiredSize -ErrorAction "Stop"
Write-Log "Partition resized to $desiredSize. MaxSize becomes the desired size"
$MaxSize = $desiredSize
} catch {
Write-Log "Partition could not be resized to $desiredSize. MinSize becomes the desired size"
$MinSize = $desiredSize
}
$binaryTries ++
}
}
}
} finally {
Dismount-VHD -Path $VirtualDiskPath
}

Expand Down

0 comments on commit 4703769

Please sign in to comment.