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
This commit is contained in:
Varun Chopra 2019-09-25 18:28:30 +05:30 committed by ansibot
parent 7e01de96d7
commit 3b409f2f50
4 changed files with 45 additions and 7 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- win_format - fixed issue where module would not change allocation unit size (https://github.com/ansible/ansible/issues/56961)

View file

@ -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
}

View file

@ -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:

View file

@ -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