cloudbase / windows-imaging-tools

Tools to automate the creation of a Windows image for OpenStack, supporting KVM, Hyper-V, ESXi and more.
Apache License 2.0
675 stars 227 forks source link

Error on resizing UEFI drives #248

Closed jacekchalupka closed 5 years ago

jacekchalupka commented 5 years ago

When creating UEFI drive there is more than one partition, but Resize-VHDImage function assumes there is only one.

The error occurs, because Drive variable is an array not a single letter when there is more partitions. $Drive = (Mount-VHD -Path $VirtualDiskPath -Passthru | Get-Disk | Get-Partition | Get-Volume).DriveLetter

Fast workaround is to change this line to: $Drive = (Mount-VHD -Path $VirtualDiskPath -Passthru | Get-Disk | Get-Partition | Get-Volume | Where-Object FileSystemLabel -eq 'OS').DriveLetter

Real solution would be to detect correct partition. Maybe pick the one with the biggest size?

$Drive = (Mount-VHD -Path $VirtualDiskPath -Passthru | `
    Get-Disk | Get-Partition | Get-Volume | `
    Sort-Object -Property Size -Descending | Select-Object -First 1).DriveLetter

STEPS TO REPRODUCE

Import-Module .\WinImageBuilder.psm1
Import-Module .\Config.psm1
Import-Module .\UnattendResources\ini.psm1
$ConfigFilePath = ".\config.ini"
[System.Net.WebClient]::new().DownloadFile('https://pastebin.com/raw/tvYEPimc', '.\config.ini')
Write-Host 'Press enter and put proper paths in config file when it openes...' -NoNewLine
Read-Host
notepad.exe .\config.ini  | Out-Null
New-WindowsOnlineImage -ConfigFilePath $ConfigFilePath

ERROR MESSAGE

09.03.2019 19:37:45 - Shrinking VHD to minimum size
09.03.2019 19:37:45 - Initial VHD size is: 50 GB
Optimize-Volume : This volume cannot be optimized.
Activity ID: {8114f79a-51f0-42b0-8eae-184834cf8dcb}
At C:\OpenStackImages\windows-openstack-imaging-tools\WinImageBuilder.psm1:913 char:9
+         Optimize-Volume -DriveLetter $Drive -Defrag -ReTrim -SlabCons ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (StorageWMI:ROOT/Microsoft/...age/MSFT_Volume) [Optimize-Volume], CimExcep
   tion
    + FullyQualifiedErrorId : StorageWMI 43025,Optimize-Volume

09.03.2019 19:37:50 - Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.
Resize-VHDImage : Method invocation failed because [System.Object[]] does not contain a method named 'op_Division'.
At C:\OpenStackImages\windows-openstack-imaging-tools\WinImageBuilder.psm1:1201 char:9
+         Resize-VHDImage $virtualDiskPath
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Division:String) [Resize-VHDImage], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound,Resize-VHDImage
ader1990 commented 5 years ago

Fix already merge, closing this PR.