| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2019, Varun Chopra (@chopraaa) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
#AnsibleRequires -OSVersion 6.2 |
| 8 |
|
| 9 |
Set-StrictMode -Version 2 |
| 10 |
|
| 11 |
$ErrorActionPreference = "Stop" |
| 12 |
|
| 13 |
$spec = @{ |
| 14 |
options = @{ |
| 15 |
drive_letter = @{ type = "str" } |
| 16 |
path = @{ type = "str" } |
| 17 |
label = @{ type = "str" } |
| 18 |
new_label = @{ type = "str" } |
| 19 |
file_system = @{ type = "str"; choices = "ntfs", "refs", "exfat", "fat32", "fat" } |
| 20 |
allocation_unit_size = @{ type = "int" } |
| 21 |
large_frs = @{ type = "bool" } |
| 22 |
full = @{ type = "bool"; default = $false } |
| 23 |
compress = @{ type = "bool" } |
| 24 |
integrity_streams = @{ type = "bool" } |
| 25 |
force = @{ type = "bool"; default = $false } |
| 26 |
} |
| 27 |
mutually_exclusive = @( |
| 28 |
, @('drive_letter', 'path', 'label') |
| 29 |
) |
| 30 |
required_one_of = @( |
| 31 |
, @('drive_letter', 'path', 'label') |
| 32 |
) |
| 33 |
supports_check_mode = $true |
| 34 |
} |
| 35 |
|
| 36 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 37 |
|
| 38 |
$drive_letter = $module.Params.drive_letter |
| 39 |
$path = $module.Params.path |
| 40 |
$label = $module.Params.label |
| 41 |
$new_label = $module.Params.new_label |
| 42 |
$file_system = $module.Params.file_system |
| 43 |
$allocation_unit_size = $module.Params.allocation_unit_size |
| 44 |
$large_frs = $module.Params.large_frs |
| 45 |
$full_format = $module.Params.full |
| 46 |
$compress_volume = $module.Params.compress |
| 47 |
$integrity_streams = $module.Params.integrity_streams |
| 48 |
$force_format = $module.Params.force |
| 49 |
|
| 50 |
# Some pre-checks |
| 51 |
if ($null -ne $drive_letter -and $drive_letter -notmatch "^[a-zA-Z]$") { |
| 52 |
$module.FailJson("The parameter drive_letter should be a single character A-Z") |
| 53 |
} |
| 54 |
if ($integrity_streams -eq $true -and $file_system -ne "refs") { |
| 55 |
$module.FailJson("Integrity streams can be enabled only on ReFS volumes. You specified: $($file_system)") |
| 56 |
} |
| 57 |
if ($compress_volume -eq $true) { |
| 58 |
if ($file_system -eq "ntfs") { |
| 59 |
if ($null -ne $allocation_unit_size -and $allocation_unit_size -gt 4096) { |
| 60 |
$module.FailJson("NTFS compression is not supported for allocation unit sizes above 4096") |
| 61 |
} |
| 62 |
} |
| 63 |
else { |
| 64 |
$module.FailJson("Compression can be enabled only on NTFS volumes. You specified: $($file_system)") |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
function Get-AnsibleVolume { |
| 69 |
param( |
| 70 |
$DriveLetter, |
| 71 |
$Path, |
| 72 |
$Label |
| 73 |
) |
| 74 |
|
| 75 |
if ($null -ne $DriveLetter) { |
| 76 |
try { |
| 77 |
# This needs to be a two step process so that we can support Windows failover cluster disks. |
| 78 |
# With Windows failover cluster disks every node sees every disk participating in that cluster. |
| 79 |
# For example a clustered disk in a three node cluster will show up three times. |
| 80 |
# Fortunatly we can differentiate local from remote disk as only local disk will ever have a disk number. |
| 81 |
# So with that we just ignore all disk without a number which will result in a local disk being picked. |
| 82 |
$partition = Get-Partition -DriveLetter $DriveLetter | Where-Object { $null -ne $_.DiskNumber } |
| 83 |
$volume = Get-Volume -Partition $partition |
| 84 |
} |
| 85 |
catch { |
| 86 |
$module.FailJson("There was an error retrieving the volume using drive_letter $($DriveLetter): $($_.Exception.Message)", $_) |
| 87 |
} |
| 88 |
} |
| 89 |
elseif ($null -ne $Path) { |
| 90 |
try { |
| 91 |
$volume = Get-Volume -Path $Path |
| 92 |
} |
| 93 |
catch { |
| 94 |
$module.FailJson("There was an error retrieving the volume using path $($Path): $($_.Exception.Message)", $_) |
| 95 |
} |
| 96 |
} |
| 97 |
elseif ($null -ne $Label) { |
| 98 |
try { |
| 99 |
$volume = Get-Volume -FileSystemLabel $Label |
| 100 |
} |
| 101 |
catch { |
| 102 |
$module.FailJson("There was an error retrieving the volume using label $($Label): $($_.Exception.Message)", $_) |
| 103 |
} |
| 104 |
} |
| 105 |
else { |
| 106 |
$module.FailJson("Unable to locate volume: drive_letter, path and label were not specified") |
| 107 |
} |
| 108 |
|
| 109 |
return $volume |
| 110 |
} |
| 111 |
|
| 112 |
function Format-AnsibleVolume { |
| 113 |
param( |
| 114 |
$Path, |
| 115 |
$Label, |
| 116 |
$FileSystem, |
| 117 |
$Full, |
| 118 |
$UseLargeFRS, |
| 119 |
$Compress, |
| 120 |
$SetIntegrityStreams, |
| 121 |
$AllocationUnitSize |
| 122 |
) |
| 123 |
$parameters = @{ |
| 124 |
Path = $Path |
| 125 |
Full = $Full |
| 126 |
} |
| 127 |
if ($null -ne $UseLargeFRS) { |
| 128 |
$parameters.Add("UseLargeFRS", $UseLargeFRS) |
| 129 |
} |
| 130 |
if ($null -ne $SetIntegrityStreams) { |
| 131 |
$parameters.Add("SetIntegrityStreams", $SetIntegrityStreams) |
| 132 |
} |
| 133 |
if ($null -ne $Compress) { |
| 134 |
$parameters.Add("Compress", $Compress) |
| 135 |
} |
| 136 |
if ($null -ne $Label) { |
| 137 |
$parameters.Add("NewFileSystemLabel", $Label) |
| 138 |
} |
| 139 |
if ($null -ne $FileSystem) { |
| 140 |
$parameters.Add("FileSystem", $FileSystem) |
| 141 |
} |
| 142 |
if ($null -ne $AllocationUnitSize) { |
| 143 |
$parameters.Add("AllocationUnitSize", $AllocationUnitSize) |
| 144 |
} |
| 145 |
|
| 146 |
Format-Volume @parameters -Confirm:$false | Out-Null |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
$ansible_volume = Get-AnsibleVolume -DriveLetter $drive_letter -Path $path -Label $label |
| 151 |
$ansible_file_system = $ansible_volume.FileSystem |
| 152 |
$ansible_volume_size = $ansible_volume.Size |
| 153 |
$ansible_volume_alu = (Get-CimInstance -ClassName Win32_Volume -Filter "DeviceId = '$($ansible_volume.path.replace('\','\\'))'" -Property BlockSize).BlockSize |
| 154 |
|
| 155 |
if ( |
| 156 |
-not $force_format -and |
| 157 |
$null -ne $allocation_unit_size -and |
| 158 |
$ansible_volume_alu -ne 0 -and |
| 159 |
$null -ne $ansible_volume_alu -and |
| 160 |
$allocation_unit_size -ne $ansible_volume_alu |
| 161 |
) { |
| 162 |
$msg = -join @( |
| 163 |
"Force format must be specified since target allocation unit size: $($allocation_unit_size) " |
| 164 |
"is different from the current allocation unit size of the volume: $($ansible_volume_alu)" |
| 165 |
) |
| 166 |
$module.FailJson($msg) |
| 167 |
} |
| 168 |
|
| 169 |
if ($null -ne $file_system -and |
| 170 |
-not [string]::IsNullOrEmpty($ansible_file_system) -and |
| 171 |
$file_system -ne $ansible_file_system) { |
| 172 |
if (-not $force_format) { |
| 173 |
$no_files_in_volume = (Get-ChildItem -LiteralPath $ansible_volume.Path -ErrorAction SilentlyContinue | Measure-Object).Count -eq 0 |
| 174 |
if ($no_files_in_volume) { |
| 175 |
$msg = -join @( |
| 176 |
"Force format must be specified since target file system: $($file_system) " |
| 177 |
"is different from the current file system of the volume: $($ansible_file_system.ToLower())" |
| 178 |
) |
| 179 |
$module.FailJson($msg) |
| 180 |
} |
| 181 |
else { |
| 182 |
$module.FailJson("Force format must be specified to format non-pristine volumes") |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
else { |
| 187 |
$pristine = -not $force_format |
| 188 |
} |
| 189 |
|
| 190 |
if ($force_format) { |
| 191 |
if (-not $module.CheckMode) { |
| 192 |
$format_params = @{ |
| 193 |
Path = $ansible_volume.Path |
| 194 |
Full = $full_format |
| 195 |
Label = $new_label |
| 196 |
FileSystem = $file_system |
| 197 |
SetIntegrityStreams = $integrity_streams |
| 198 |
UseLargeFRS = $large_frs |
| 199 |
Compress = $compress_volume |
| 200 |
AllocationUnitSize = $allocation_unit_size |
| 201 |
} |
| 202 |
Format-AnsibleVolume @format_params |
| 203 |
} |
| 204 |
$module.Result.changed = $true |
| 205 |
} |
| 206 |
else { |
| 207 |
if ($pristine) { |
| 208 |
if ($null -eq $new_label) { |
| 209 |
$new_label = $ansible_volume.FileSystemLabel |
| 210 |
} |
| 211 |
# Conditions for formatting |
| 212 |
if ($ansible_volume_size -eq 0 -or |
| 213 |
$ansible_volume.FileSystemLabel -ne $new_label) { |
| 214 |
if (-not $module.CheckMode) { |
| 215 |
$format_params = @{ |
| 216 |
Path = $ansible_volume.Path |
| 217 |
Full = $full_format |
| 218 |
Label = $new_label |
| 219 |
FileSystem = $file_system |
| 220 |
SetIntegrityStreams = $integrity_streams |
| 221 |
UseLargeFRS = $large_frs |
| 222 |
Compress = $compress_volume |
| 223 |
AllocationUnitSize = $allocation_unit_size |
| 224 |
} |
| 225 |
Format-AnsibleVolume @format_params |
| 226 |
} |
| 227 |
$module.Result.changed = $true |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
$module.ExitJson() |