From 3b409f2f50a3f9c71e44ece9dc05205e81e23bea Mon Sep 17 00:00:00 2001 From: Varun Chopra Date: Wed, 25 Sep 2019 18:28:30 +0530 Subject: [PATCH] win_format - Add allocation_unit_size (#56966) * Fix allocation_unit_size * Some final changes and added tests * Cleanup... * Fixes issues on 2012/R2 * Update tests for 2012/R2 * Suggested changes --- .../56966-win_format-allocation-unit-size.yml | 2 ++ lib/ansible/modules/windows/win_format.ps1 | 15 ++++++-- lib/ansible/modules/windows/win_format.py | 1 + .../targets/win_format/tasks/tests.yml | 34 ++++++++++++++++--- 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/56966-win_format-allocation-unit-size.yml diff --git a/changelogs/fragments/56966-win_format-allocation-unit-size.yml b/changelogs/fragments/56966-win_format-allocation-unit-size.yml new file mode 100644 index 00000000000..5ad05610e7e --- /dev/null +++ b/changelogs/fragments/56966-win_format-allocation-unit-size.yml @@ -0,0 +1,2 @@ +bugfixes: + - win_format - fixed issue where module would not change allocation unit size (https://github.com/ansible/ansible/issues/56961) diff --git a/lib/ansible/modules/windows/win_format.ps1 b/lib/ansible/modules/windows/win_format.ps1 index 31a6f3a75af..b5fd3ae0380 100644 --- a/lib/ansible/modules/windows/win_format.ps1 +++ b/lib/ansible/modules/windows/win_format.ps1 @@ -108,7 +108,8 @@ function Format-AnsibleVolume { $Full, $UseLargeFRS, $Compress, - $SetIntegrityStreams + $SetIntegrityStreams, + $AllocationUnitSize ) $parameters = @{ Path = $Path @@ -129,6 +130,9 @@ function Format-AnsibleVolume { if ($null -ne $FileSystem) { $parameters.Add("FileSystem", $FileSystem) } + if ($null -ne $AllocationUnitSize) { + $parameters.Add("AllocationUnitSize", $AllocationUnitSize) + } Format-Volume @parameters -Confirm:$false | Out-Null @@ -137,9 +141,14 @@ function Format-AnsibleVolume { $ansible_volume = Get-AnsibleVolume -DriveLetter $drive_letter -Path $path -Label $label $ansible_file_system = $ansible_volume.FileSystem $ansible_volume_size = $ansible_volume.Size +$ansible_volume_alu = (Get-CimInstance -ClassName Win32_Volume -Filter "DeviceId = '$($ansible_volume.path.replace('\','\\'))'" -Property BlockSize).BlockSize $ansible_partition = Get-Partition -Volume $ansible_volume +if (-not $force_format -and $null -ne $allocation_unit_size -and $ansible_volume_alu -ne 0 -and $null -ne $ansible_volume_alu -and $allocation_unit_size -ne $ansible_volume_alu) { + $module.FailJson("Force format must be specified since target allocation unit size: $($allocation_unit_size) is different from the current allocation unit size of the volume: $($ansible_volume_alu)") +} + foreach ($access_path in $ansible_partition.AccessPaths) { if ($access_path -ne $Path) { if ($null -ne $file_system -and @@ -168,7 +177,7 @@ foreach ($access_path in $ansible_partition.AccessPaths) { if ($force_format) { if (-not $module.CheckMode) { - Format-AnsibleVolume -Path $ansible_volume.Path -Full $full_format -Label $new_label -FileSystem $file_system -SetIntegrityStreams $integrity_streams -UseLargeFRS $large_frs -Compress $compress_volume + Format-AnsibleVolume -Path $ansible_volume.Path -Full $full_format -Label $new_label -FileSystem $file_system -SetIntegrityStreams $integrity_streams -UseLargeFRS $large_frs -Compress $compress_volume -AllocationUnitSize $allocation_unit_size } $module.Result.changed = $true } @@ -181,7 +190,7 @@ else { if ($ansible_volume_size -eq 0 -or $ansible_volume.FileSystemLabel -ne $new_label) { if (-not $module.CheckMode) { - Format-AnsibleVolume -Path $ansible_volume.Path -Full $full_format -Label $new_label -FileSystem $file_system -SetIntegrityStreams $integrity_streams -UseLargeFRS $large_frs -Compress $compress_volume + Format-AnsibleVolume -Path $ansible_volume.Path -Full $full_format -Label $new_label -FileSystem $file_system -SetIntegrityStreams $integrity_streams -UseLargeFRS $large_frs -Compress $compress_volume -AllocationUnitSize $allocation_unit_size } $module.Result.changed = $true } diff --git a/lib/ansible/modules/windows/win_format.py b/lib/ansible/modules/windows/win_format.py index 8b85cce8131..b1bd0339376 100644 --- a/lib/ansible/modules/windows/win_format.py +++ b/lib/ansible/modules/windows/win_format.py @@ -43,6 +43,7 @@ options: - Specifies the cluster size to use when formatting the volume. - If no cluster size is specified when you format a partition, defaults are selected based on the size of the partition. + - This value must be a multiple of the physical sector size of the disk. type: int large_frs: description: diff --git a/test/integration/targets/win_format/tasks/tests.yml b/test/integration/targets/win_format/tasks/tests.yml index eeb1cfb3caa..5036164eab2 100644 --- a/test/integration/targets/win_format/tasks/tests.yml +++ b/test/integration/targets/win_format/tasks/tests.yml @@ -28,10 +28,11 @@ drive_letter: T new_label: Formatted full: True + allocation_unit_size: 8192 register: format_result_check check_mode: True -- win_shell: $AnsiPart = Get-Partition -DriveLetter T; $AnsiVol = Get-Volume -DriveLetter T; "$($AnsiPart.Size),$($AnsiVol.Size),$($AnsiVol.FileSystemLabel)" +- win_shell: $AnsiPart = Get-Partition -DriveLetter T; $AnsiVol = Get-Volume -DriveLetter T; "$($AnsiPart.Size),$($AnsiVol.Size),$($AnsiVol.FileSystemLabel),$((Get-CimInstance -ClassName Win32_Volume -Filter "DriveLetter = 'T:'" -Property BlockSize).BlockSize)" register: formatted_value_result_check - name: Fully format volume and assign label @@ -39,17 +40,18 @@ drive_letter: T new_label: Formatted full: True + allocation_unit_size: 8192 register: format_result -- win_shell: $AnsiPart = Get-Partition -DriveLetter T; $AnsiVol = Get-Volume -DriveLetter T; "$($AnsiPart.Size),$($AnsiVol.Size),$($AnsiVol.FileSystemLabel)" +- win_shell: $AnsiPart = Get-Partition -DriveLetter T; $AnsiVol = Get-Volume -DriveLetter T; "$($AnsiPart.Size),$($AnsiVol.Size),$($AnsiVol.FileSystemLabel),$((Get-CimInstance -ClassName Win32_Volume -Filter "DriveLetter = 'T:'" -Property BlockSize).BlockSize)" register: formatted_value_result - assert: that: - format_result_check is changed - format_result is changed - - formatted_value_result_check.stdout | trim == "2096037888,0," - - formatted_value_result.stdout | trim == "2096037888,2096033792,Formatted" + - formatted_value_result_check.stdout | trim == "2096037888,0,," + - formatted_value_result.stdout | trim == "2096037888,2096029696,Formatted,8192" - name: Format NTFS volume with integrity streams enabled win_format: @@ -154,3 +156,27 @@ - format_volume_without_force_same_fs is not changed - 'format_volume_without_force.msg == "Force format must be specified to format non-pristine volumes"' - format_volume_with_force is changed + +- name: Reformat using different alu without force format + win_format: + path: "{{ shell_partition_result.stdout | trim }}" + allocation_unit_size: 8192 + file_system: ntfs + register: reformat_using_alu_without_force + ignore_errors: True + +- assert: + that: + - reformat_using_alu_without_force is failed + +- name: Reformat using different alu using force format + win_format: + path: "{{ shell_partition_result.stdout | trim }}" + allocation_unit_size: 8192 + file_system: ntfs + force: True + register: reformat_using_alu_with_force + +- assert: + that: + - reformat_using_alu_with_force is changed