Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_partition.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2018, 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 state = @{ type = "str"; choices = "absent", "present"; default = "present" }
16 drive_letter = @{ type = "str" }
17 disk_number = @{ type = "int" }
18 partition_number = @{ type = "int" }
19 partition_size = @{ type = "str" }
20 read_only = @{ type = "bool" }
21 active = @{ type = "bool" }
22 hidden = @{ type = "bool" }
23 offline = @{ type = "bool" }
24 mbr_type = @{ type = "str"; choices = "fat12", "fat16", "extended", "huge", "ifs", "fat32" }
25 gpt_type = @{ type = "str"; choices = "system_partition", "microsoft_reserved", "basic_data", "microsoft_recovery" }
26 }
27 supports_check_mode = $true
28 }
29
30 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
31
32 $state = $module.Params.state
33 $drive_letter = $module.Params.drive_letter
34 $disk_number = $module.Params.disk_number
35 $partition_number = $module.Params.partition_number
36 $partition_size = $module.Params.partition_size
37 $read_only = $module.Params.read_only
38 $active = $module.Params.active
39 $hidden = $module.Params.hidden
40 $offline = $module.Params.offline
41 $mbr_type = $module.Params.mbr_type
42 $gpt_type = $module.Params.gpt_type
43
44 $size_is_maximum = $false
45 $ansible_partition = $false
46 $ansible_partition_size = $null
47 $partition_style = $null
48
49 $gpt_styles = @{
50 system_partition = "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}"
51 microsoft_reserved = "{e3c9e316-0b5c-4db8-817d-f92df00215ae}"
52 basic_data = "{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}"
53 microsoft_recovery = "{de94bba4-06d1-4d40-a16a-bfd50179d6ac}"
54 }
55
56 $mbr_styles = @{
57 fat12 = 1
58 fat16 = 4
59 extended = 5
60 huge = 6
61 ifs = 7
62 fat32 = 12
63 }
64
65 function Convert-SizeToByte {
66 param(
67 $Size,
68 $Units
69 )
70
71 switch ($Units) {
72 "B" { return 1 * $Size }
73 "KB" { return 1000 * $Size }
74 "KiB" { return 1024 * $Size }
75 "MB" { return [Math]::Pow(1000, 2) * $Size }
76 "MiB" { return [Math]::Pow(1024, 2) * $Size }
77 "GB" { return [Math]::Pow(1000, 3) * $Size }
78 "GiB" { return [Math]::Pow(1024, 3) * $Size }
79 "TB" { return [Math]::Pow(1000, 4) * $Size }
80 "TiB" { return [Math]::Pow(1024, 4) * $Size }
81 }
82 }
83
84 if ($null -ne $partition_size) {
85 if ($partition_size -eq -1) {
86 $size_is_maximum = $true
87 }
88 elseif ($partition_size -match '^(?<Size>[0-9]+)[ ]*(?<Units>b|kb|kib|mb|mib|gb|gib|tb|tib)$') {
89 $ansible_partition_size = Convert-SizeToByte -Size $Matches.Size -Units $Matches.Units
90 }
91 else {
92 $module.FailJson("Invalid partition size. B, KB, KiB, MB, MiB, GB, GiB, TB, TiB are valid partition size units")
93 }
94 }
95
96 # If partition_exists, we can change or delete it; otherwise we only need the disk to create a new partition
97 if ($null -ne $disk_number -and $null -ne $partition_number) {
98 $ansible_partition = Get-Partition -DiskNumber $disk_number -PartitionNumber $partition_number -ErrorAction SilentlyContinue
99 }
100 # Check if drive_letter is either auto-assigned or a character from A-Z
101 elseif ($drive_letter -and $drive_letter -ne "auto" -and -not ($disk_number -and $partition_number)) {
102 if ($drive_letter -match "^[a-zA-Z]$") {
103 # This step need to be a bit more complex so that we can support Windows failover cluster disks.
104 # With Windows failover cluster disks every node sees every disk participating in that cluster.
105 # For example a clustered disk in a three node cluster will show up three times.
106 # Fortunatly we can differentiate local from remote disk as only local disk will ever have a disk number.
107 # So with that we just ignore all disk without a number which will result in a local disk being picked.
108 $ansible_partition = Get-Partition -DriveLetter $drive_letter -ErrorAction SilentlyContinue | Where-Object { $null -ne $_.DiskNumber }
109 }
110 else {
111 $module.FailJson("Incorrect usage of drive_letter: specify a drive letter from A-Z or use 'auto' to automatically assign a drive letter")
112 }
113 }
114 elseif ($disk_number) {
115 try {
116 Get-Disk -Number $disk_number | Out-Null
117 }
118 catch {
119 $module.FailJson("Specified disk does not exist")
120 }
121 }
122 else {
123 $module.FailJson("You must provide disk_number, partition_number")
124 }
125
126 # Partition can't have two partition styles
127 if ($null -ne $gpt_type -and $null -ne $mbr_type) {
128 $module.FailJson("Cannot specify both GPT and MBR partition styles. Check which partition style is supported by the disk")
129 }
130
131 function New-AnsiblePartition {
132 param(
133 $DiskNumber,
134 $Letter,
135 $SizeMax,
136 $Size,
137 $MbrType,
138 $GptType,
139 $Style
140 )
141
142 $parameters = @{
143 DiskNumber = $DiskNumber
144 }
145
146 if ($null -ne $Letter) {
147 switch ($Letter) {
148 "auto" {
149 $parameters.Add("AssignDriveLetter", $True)
150 }
151 default {
152 $parameters.Add("DriveLetter", $Letter)
153 }
154 }
155 }
156
157 if ($null -ne $Size) {
158 $parameters.Add("Size", $Size)
159 }
160
161 if ($null -ne $MbrType) {
162 $parameters.Add("MbrType", $Style)
163 }
164
165 if ($null -ne $GptType) {
166 $parameters.Add("GptType", $Style)
167 }
168
169 try {
170 $new_partition = New-Partition @parameters
171 }
172 catch {
173 $module.FailJson("Unable to create a new partition: $($_.Exception.Message)", $_)
174 }
175
176 return $new_partition
177 }
178
179
180 function Set-AnsiblePartitionState {
181 param(
182 $hidden,
183 $read_only,
184 $active,
185 $partition
186 )
187
188 $parameters = @{
189 DiskNumber = $partition.DiskNumber
190 PartitionNumber = $partition.PartitionNumber
191 }
192
193 if ($hidden -NotIn ($null, $partition.IsHidden)) {
194 $parameters.Add("IsHidden", $hidden)
195 }
196
197 if ($read_only -NotIn ($null, $partition.IsReadOnly)) {
198 $parameters.Add("IsReadOnly", $read_only)
199 }
200
201 if ($active -NotIn ($null, $partition.IsActive)) {
202 $parameters.Add("IsActive", $active)
203 }
204
205 try {
206 Set-Partition @parameters
207 }
208 catch {
209 $module.FailJson("Error changing state of partition: $($_.Exception.Message)", $_)
210 }
211 }
212
213
214 if ($ansible_partition) {
215 if ($state -eq "absent") {
216 try {
217 $remove_params = @{
218 DiskNumber = $ansible_partition.DiskNumber
219 PartitionNumber = $ansible_partition.PartitionNumber
220 Confirm = $false
221 WhatIf = $module.CheckMode
222 }
223 Remove-Partition @remove_params
224 }
225 catch {
226 $module.FailJson("There was an error removing the partition: $($_.Exception.Message)", $_)
227 }
228 $module.Result.changed = $true
229 }
230 else {
231
232 if ($null -ne $gpt_type -and $gpt_styles.$gpt_type -ne $ansible_partition.GptType) {
233 $module.FailJson("gpt_type is not a valid parameter for existing partitions")
234 }
235 if ($null -ne $mbr_type -and $mbr_styles.$mbr_type -ne $ansible_partition.MbrType) {
236 $module.FailJson("mbr_type is not a valid parameter for existing partitions")
237 }
238
239 if ($partition_size) {
240 try {
241 $get_params = @{
242 DiskNumber = $ansible_partition.DiskNumber
243 PartitionNumber = $ansible_partition.PartitionNumber
244 }
245 $max_supported_size = (Get-PartitionSupportedSize @get_params).SizeMax
246 }
247 catch {
248 $module.FailJson("Unable to get maximum supported partition size: $($_.Exception.Message)", $_)
249 }
250 if ($size_is_maximum) {
251 $ansible_partition_size = $max_supported_size
252 }
253 if (
254 $ansible_partition_size -ne $ansible_partition.Size -and
255 ($ansible_partition_size - $ansible_partition.Size -gt 1049000 -or $ansible_partition.Size - $ansible_partition_size -gt 1049000)
256 ) {
257 if ($ansible_partition.IsReadOnly) {
258 $module.FailJson("Unable to resize partition: Partition is read only")
259 }
260 else {
261 try {
262 $resize_params = @{
263 DiskNumber = $ansible_partition.DiskNumber
264 PartitionNumber = $ansible_partition.PartitionNumber
265 Size = $ansible_partition_size
266 WhatIf = $module.CheckMode
267 }
268 Resize-Partition @resize_params
269 }
270 catch {
271 $module.FailJson("Unable to change partition size: $($_.Exception.Message)", $_)
272 }
273 $module.Result.changed = $true
274 }
275 }
276 elseif ($ansible_partition_size -gt $max_supported_size) {
277 $module.FailJson("Specified partition size exceeds size supported by the partition")
278 }
279 }
280
281 if ($drive_letter -NotIn ("auto", $null, $ansible_partition.DriveLetter)) {
282 if (-not $module.CheckMode) {
283 try {
284 Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -NewDriveLetter $drive_letter
285 }
286 catch {
287 $module.FailJson("Unable to change drive letter: $($_.Exception.Message)", $_)
288 }
289 }
290 $module.Result.changed = $true
291 }
292 }
293 }
294 else {
295 if ($state -eq "present") {
296 if ($null -eq $disk_number) {
297 $module.FailJson("Missing required parameter: disk_number")
298 }
299 if ($null -eq $ansible_partition_size -and -not $size_is_maximum) {
300 $module.FailJson("Missing required parameter: partition_size")
301 }
302 if (-not $size_is_maximum) {
303 try {
304 $max_supported_size = (Get-Disk -Number $disk_number).LargestFreeExtent
305 }
306 catch {
307 $module.FailJson("Unable to get maximum size supported by disk: $($_.Exception.Message)", $_)
308 }
309
310 if ($ansible_partition_size -gt $max_supported_size) {
311 $module.FailJson("Partition size is not supported by disk. Use partition_size: -1 to get maximum size")
312 }
313 }
314 else {
315 $ansible_partition_size = (Get-Disk -Number $disk_number).LargestFreeExtent
316 }
317
318 $supp_part_type = (Get-Disk -Number $disk_number).PartitionStyle
319 if ($null -ne $mbr_type) {
320 if ($supp_part_type -eq "MBR" -and $mbr_styles.ContainsKey($mbr_type)) {
321 $partition_style = $mbr_styles.$mbr_type
322 }
323 else {
324 $module.FailJson("Incorrect partition style specified")
325 }
326 }
327 if ($null -ne $gpt_type) {
328 if ($supp_part_type -eq "GPT" -and $gpt_styles.ContainsKey($gpt_type)) {
329 $partition_style = $gpt_styles.$gpt_type
330 }
331 else {
332 $module.FailJson("Incorrect partition style specified")
333 }
334 }
335
336 if (-not $module.CheckMode) {
337 $new_params = @{
338 DiskNumber = $disk_number
339 Letter = $drive_letter
340 Size = $ansible_partition_size
341 MbrType = $mbr_type
342 GptType = $gpt_type
343 Style = $partition_style
344 }
345 $ansible_partition = New-AnsiblePartition @new_params
346 }
347 $module.Result.changed = $true
348 }
349 }
350
351 if ($state -eq "present" -and $ansible_partition) {
352 if ($offline -NotIn ($null, $ansible_partition.IsOffline)) {
353 if (-not $module.CheckMode) {
354 try {
355 Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -IsOffline $offline
356 }
357 catch {
358 $module.FailJson("Error setting partition offline: $($_.Exception.Message)", $_)
359 }
360 }
361 $module.Result.changed = $true
362 }
363
364 if (
365 $hidden -NotIn ($null, $ansible_partition.IsHidden) -or
366 $read_only -NotIn ($null, $ansible_partition.IsReadOnly) -or
367 $active -NotIn ($null, $ansible_partition.IsActive)
368 ) {
369 if (-not $module.CheckMode) {
370 Set-AnsiblePartitionState -hidden $hidden -read_only $read_only -active $active -partition $ansible_partition
371 }
372 $module.Result.changed = $true
373 }
374 }
375
376 $module.ExitJson()