Add win_partition (#46292)

* Added win_partition module

* Fixes sanity tests

* Requested changes minus changes in partition_size

* Removed trailing whitespace and fixed docstring

* Changes to parititon_size to allow variable units

* Stricter regex for partition_size
This commit is contained in:
Varun Chopra 2018-11-26 06:31:59 +05:30 committed by Jordan Borean
parent 8f449eec64
commit 90c3337316
8 changed files with 735 additions and 0 deletions

View file

@ -0,0 +1,317 @@
#!powershell
# Copyright: (c) 2018, Varun Chopra (@chopraaa)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#Requires -Module Ansible.ModuleUtils.Legacy
#AnsibleRequires -OSVersion 6.2
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
$params = Parse-Args -arguments $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -failifempty $false -default "present" -validateset "absent", "present"
$drive_letter = Get-AnsibleParam -obj $params -name "drive_letter" -type "str" -failifempty $false
$disk_number = Get-AnsibleParam -obj $params -name "disk_number" -type "int" -failifempty $false
$partition_number = Get-AnsibleParam -obj $params -name "partition_number" -type "int" -failifempty $false
$partition_size = Get-AnsibleParam -obj $params -name "partition_size" -type "str" -failifempty $false
$read_only = Get-AnsibleParam -obj $params -name "read_only" -type "bool" -failifempty $false
$active = Get-AnsibleParam -obj $params -name "active" -type "bool" -failifempty $false
$hidden = Get-AnsibleParam -obj $params -name "hidden" -type "bool" -failifempty $false
$offline = Get-AnsibleParam -obj $params -name "offline" -type "bool" -failifempty $false
$mbr_type = Get-AnsibleParam -obj $params -name "mbr_type" -type "str" -failifempty $false -validateset "fat12", "fat16", "extended", "huge", "ifs", "fat32"
$gpt_type = Get-AnsibleParam -obj $params -name "gpt_type" -type "str" -failifempty $false -validateset "system_partition", "microsoft_reserved", "basic_data", "microsoft_recovery"
$result = @{
changed = $false
}
$size_is_maximum = $false
$ansible_partition = $false
$ansible_partition_size = $null
$partition_style = $null
$gpt_styles = @{
system_partition = "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"
microsoft_reserved = "e3c9e316-0b5c-4db8-817d-f92df00215ae"
basic_data = "ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"
microsoft_recovery = "de94bba4-06d1-4d40-a16a-bfd50179d6ac"
}
$mbr_styles = @{
fat12 = 1
fat16 = 4
extended = 5
huge = 6
ifs = 7
fat32 = 12
}
function Convert-SizeToBytes {
param(
$Size,
$Units
)
switch ($Units) {
"B" { return $Size }
"KB" { return 1000 * $Size }
"KiB" { return 1024 * $Size }
"MB" { return [Math]::Pow(1000, 2) * $Size }
"MiB" { return [Math]::Pow(1024, 2) * $Size }
"GB" { return [Math]::Pow(1000, 3) * $Size }
"GiB" { return [Math]::Pow(1024, 3) * $Size }
"TB" { return [Math]::Pow(1000, 4) * $Size }
"TiB" { return [Math]::Pow(1024, 4) * $Size }
}
}
if ($null -ne $partition_size) {
if ($partition_size -eq -1) {
$size_is_maximum = $true
}
elseif ($partition_size -match '^(?<Size>[0-9]+)[ ]*(?<Units>b|kb|kib|mb|mib|gb|gib|tb|tib)$') {
$ansible_partition_size = Convert-SizeToBytes -Size $Matches.Size -Units $Matches.Units
}
else {
Fail-Json -obj $result -message "Invalid partition size. B, KB, KiB, MB, MiB, GB, GiB, TB, TiB are valid partition size units"
}
}
# If partition_exists, we can change or delete it; otherwise we only need the disk to create a new partition
if ($null -ne $disk_number -and $null -ne $partition_number) {
$ansible_partition = Get-Partition -DiskNumber $disk_number -PartitionNumber $partition_number -ErrorAction SilentlyContinue
}
# Check if drive_letter is either auto-assigned or a character from A-Z
elseif ($drive_letter -and -not ($disk_number -and $partition_number)) {
if ($drive_letter -eq "auto" -or $drive_letter -match "^[a-zA-Z]$") {
$ansible_partition = Get-Partition -DriveLetter $drive_letter -ErrorAction SilentlyContinue
}
else {
Fail-Json -obj $result -message "Incorrect usage of drive_letter: specify a drive letter from A-Z or use 'auto' to automatically assign a drive letter"
}
}
elseif ($disk_number) {
try {
Get-Disk -Number $disk_number | Out-Null
} catch {
Fail-Json -obj $result -message "Specified disk does not exist"
}
}
else {
Fail-Json -obj $result -message "You must provide disk_number, partition_number"
}
# Partition can't have two partition styles
if ($null -ne $gpt_type -and $null -ne $mbr_type) {
Fail-Json "Cannot specify both GPT and MBR parititon styles. Check which partition style is supported by the disk"
}
function New-AnsiblePartition {
param(
$DiskNumber,
$Letter,
$SizeMax,
$Size,
$MbrType,
$GptType,
$Style
)
$parameters = @{
DiskNumber = $DiskNumber
}
if ($null -ne $Letter) {
switch ($Letter) {
"auto" {
$parameters.Add("AssignDriveLetter", $True)
}
default {
$parameters.Add("DriveLetter", $Letter)
}
}
}
switch ($SizeMax) {
$True {
$parameters.Add("UseMaximumSize", $True)
}
$False {
$parameters.Add("Size", $Size)
}
}
if ($null -ne $MbrType) {
$parameters.Add("MbrType", $Style)
}
if ($null -ne $GptType) {
$parameters.Add("GptType", $Style)
}
try {
$new_partition = New-Partition @parameters
} catch {
Fail-Json -obj $result -message "Unable to create a new partition: $($_.Exception.Message)"
}
return $new_partition
}
function Set-AnsiblePartitionState {
param(
$hidden,
$read_only,
$active,
$partition
)
$parameters = @{
DiskNumber = $partition.DiskNumber
PartitionNumber = $partition.PartitionNumber
}
if ($hidden -NotIn ($null, $partition.IsHidden)) {
$parameters.Add("IsHidden", $hidden)
}
if ($read_only -NotIn ($null, $partition.IsReadOnly)) {
$parameters.Add("IsReadOnly", $read_only)
}
if ($active -NotIn ($null, $partition.IsActive)) {
$parameters.Add("IsActive", $active)
}
try {
Set-Partition @parameters
} catch {
Fail-Json -obj $result -message "Error changing state of partition: $($_.Exception.Message)"
}
}
if ($ansible_partition) {
if ($state -eq "absent") {
try {
Remove-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -Confirm:$false -WhatIf:$check_mode
} catch {
Fail-Json -obj $result -message "There was an error removing the partition: $($_.Exception.Message)"
}
$result.changed = $true
}
else {
if ($null -ne $gpt_type -and $gpt_styles.$gpt_type -ne $partition.GptType) {
Fail-Json -obj $result -message "gpt_type is not a valid parameter for existing partitions"
}
if ($null -ne $mbr_type -and $mbr_styles.$mbr_type -ne $partition.MbrType) {
Fail-Json -obj $result -message "mbr_type is not a valid parameter for existing partitions"
}
if ($partition_size) {
try {
$max_supported_size = (Get-PartitionSupportedSize -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber).SizeMax
} catch {
Fail-Json -obj $result -message "Unable to get maximum supported partition size: $($_.Exception.Message)"
}
if ($size_is_maximum) {
$ansible_partition_size = $max_supported_size
}
if ($ansible_partition_size -ne $ansible_partition.Size -and $ansible_partition_size -le $max_supported_size) {
if ($ansible_partition.IsReadOnly) {
Fail-Json -obj $result -message "Unable to resize partition: Partition is read only"
} else {
try {
Resize-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -Size $ansible_partition_size -WhatIf:$check_mode
} catch {
Fail-Json -obj $result -message "Unable to change partition size: $($_.Exception.Message)"
}
$result.changed = $true
}
} elseif ($ansible_partition_size -gt $max_supported_size) {
Fail-Json -obj $result -message "Specified partition size exceeds size supported by partition"
}
}
if ($drive_letter -NotIn ("auto", $null, $ansible_partition.DriveLetter)) {
if (-not $check_mode) {
try {
Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -NewDriveLetter $drive_letter
} catch {
Fail-Json -obj $result -message "Unable to change drive letter: $($_.Exception.Message)"
}
}
$result.changed = $true
}
}
}
else {
if ($state -eq "present") {
if ($null -eq $disk_number) {
Fail-Json -obj $result -message "Missing required parameter: disk_number"
}
if ($null -eq $ansible_partition_size -and -not $size_is_maximum){
Fail-Json -obj $result -message "Missing required parameter: partition_size"
}
if (-not $size_is_maximum) {
try {
$max_supported_size = (Get-Disk -Number $disk_number).LargestFreeExtent
} catch {
Fail-Json -obj $result -message "Unable to get maximum size supported by disk: $($_.Exception.Message)"
}
if ($ansible_partition_size -gt $max_supported_size) {
Fail-Json -obj $result -message "Partition size is not supported by disk. Use partition_size: -1 to get maximum size"
}
}
$supp_part_type = (Get-Disk -Number $disk_number).PartitionStyle
if ($null -ne $mbr_type) {
if ($supp_part_type -eq "MBR" -and $mbr_styles.ContainsKey($mbr_type)) {
$partition_style = $mbr_styles.$mbr_type
} else {
Fail-Json -obj $result -message "Incorrect partition style specified"
}
}
if ($null -ne $gpt_type) {
if ($supp_part_type -eq "GPT" -and $gpt_styles.ContainsKey($gpt_type)) {
$partition_style = $gpt_styles.$gpt_type
} else {
Fail-Json -obj $result -message "Incorrect partition style specified"
}
}
if (-not $check_mode) {
$ansible_partition = New-AnsiblePartition -DiskNumber $disk_number -Letter $drive_letter -SizeMax $size_is_maximum -Size $ansible_partition_size -MbrType $mbr_type -GptType $gpt_type -Style $partition_style
}
$result.changed = $true
}
}
if ($state -eq "present" -and $ansible_partition) {
if ($offline -NotIn ($null, $ansible_partition.IsOffline)) {
if (-not $check_mode) {
try {
Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -IsOffline $offline
} catch {
Fail-Json -obj $result -message "Error setting partition offline: $($_.Exception.Message)"
}
}
$result.changed = $true
}
if ($hidden -NotIn ($null, $ansible_partition.IsHidden) -or $read_only -NotIn ($null, $ansible_partition.IsReadOnly) -or $active -NotIn ($null, $ansible_partition.IsActive)) {
if (-not $check_mode) {
Set-AnsiblePartitionState -hidden $hidden -read_only $read_only -active $active -partition $ansible_partition
}
$result.changed = $true
}
}
Exit-Json -obj $result

View file

@ -0,0 +1,115 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Varun Chopra (@chopraaa)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = r'''
---
module: win_partition
version_added: '2.8'
short_description: Creates, changes and removes partitions on Windows Server
description:
- The M(win_partition) module can create, modify or delete a partition on a disk
options:
state:
description:
- Used to specify the state of the partition. Use C(absent) to specify if a partition should be removed
and C(present) to specify if the partition should be created or updated.
type: str
choices: [ absent, present]
default: present
drive_letter:
description:
- Used for accessing partitions if I(disk_number) and I(partition_number) are not provided.
- Use C(auto) for automatically assigning a drive letter, or a letter A-Z for manually assigning a drive letter to a new partition.
If not specified, no drive letter is assigned when creating a new partition.
type: str
disk_number:
description:
- Disk number is mandatory for creating new partitions.
- A combination of I(disk_number) and I(partition_number) can be used to specify the partition instead of I(drive_letter) if required.
type: int
partition_number:
description:
- Used in conjunction with I(disk_number) to uniquely identify a partition.
type: int
partition_size:
description:
- Specify size of the partition in B, KB, KiB, MB, MiB, GB, GiB, TB or TiB. Use -1 to specify maximum supported size.
- Partition size is mandatory for creating a new partition but not for updating or deleting a partition.
- The decimal SI prefixes kilo, mega, giga, tera, etc., are powers of 10^3 = 1000. The binary prefixes kibi, mebi, gibi, tebi, etc.
respectively refer to the corresponding power of 2^10 = 1024.
Thus, a gigabyte (GB) is 1000000000 (1000^3) bytes while 1 gibibyte (GiB) is 1073741824 (1024^3) bytes.
type: str
read_only:
description:
- Make the partition read only, restricting changes from being made to the partition.
type: bool
active:
description:
- Specifies if the partition is active and can be used to start the system. This property is only valid when the disk's partition style is MBR.
type: bool
hidden:
description:
- Hides the target partition, making it undetectable by the mount manager.
type: bool
offline:
description:
- Sets the partition offline.
- Adding a mount point (such as a drive letter) will cause the partition to go online again.
required: no
type: bool
mbr_type:
description:
- Specify the partition's MBR type if the disk's partition style is MBR.
- This only applies to new partitions.
- This does not relate to the partitions file system formatting.
type: str
choices: [ fat12, fat16, extended, huge, ifs, fat32 ]
gpt_type:
description:
- Specify the partition's GPT type if the disk's partition style is GPT.
- This only applies to new partitions.
- This does not relate to the partitions file system formatting.
type: str
choices: [ system_partition, microsoft_reserved, basic_data, microsoft_recovery ]
notes:
- A minimum Operating System Version of 6.2 is required to use this module. To check if your OS is compatible, see
U(https://docs.microsoft.com/en-us/windows/desktop/sysinfo/operating-system-version).
- This module cannot be used for removing the drive letter associated with a partition, initializing a disk or, file system formatting.
- Idempotence works only if you're specifying a drive letter or other unique attributes such as a combination of disk number and partition number.
- For more information, see U(https://msdn.microsoft.com/en-us/library/windows/desktop/hh830524(v=vs.85).aspx).
author:
- Varun Chopra (@chopraaa)
'''
EXAMPLES = r'''
- name: Create a partition with drive letter D and size 5 GiB
win_partition:
drive_letter: D
partition_size: 5 GiB
disk_number: 1
- name: Resize previously created partition to it's maximum size and change it's drive letter to E
win_partition:
drive_letter: E
partition_size: -1
partition_number: 1
disk_number: 1
- name: Delete partition
win_partition:
disk_number: 1
partition_number: 1
state: absent
'''
RETURN = r'''
#
'''

View file

@ -0,0 +1,3 @@
shippable/windows/group4
skip/windows/2008
skip/windows/2008-R2

View file

@ -0,0 +1 @@
AnsibleVhdx: C:\win_partition_tests\AnsiblePart.vhdx

View file

@ -0,0 +1,28 @@
---
- name: Create the temp directory
win_file:
path: C:\win_partition_tests
state: directory
- name: Copy VHDX scripts
win_template:
src: "{{ item.src }}"
dest: C:\win_partition_tests\{{ item.dest }}
loop:
- { src: vhdx_creation_script.j2, dest: vhdx_creation_script.txt }
- { src: vhdx_deletion_script.j2, dest: vhdx_deletion_script.txt }
- name: Create VHD
win_command: diskpart.exe /s C:\win_partition_tests\vhdx_creation_script.txt
- name: Run tests
block:
- include: tests.yml
always:
- name: Detach disk
win_command: diskpart.exe /s C:\win_partition_tests\vhdx_deletion_script.txt
- name: Cleanup files
win_file:
path: C:\win_partition_tests
state: absent

View file

@ -0,0 +1,261 @@
---
- name: Since partition is not present, disk_number is required to create a new partition.
win_partition:
drive_letter: D
register: incorrect_attempt_1
ignore_errors: True
- assert:
that:
- incorrect_attempt_1 is failed
- '"Missing required parameter: disk_number" in incorrect_attempt_1.msg'
- name: Added disk_number but size is still absent
win_partition:
drive_letter: D
disk_number: 0
register: incorrect_attempt_2
ignore_errors: True
- assert:
that:
- incorrect_attempt_2 is failed
- '"Missing required parameter: partition_size" in incorrect_attempt_2.msg'
- name: Added size but the disk we specified earlier doesn't have enough space
win_partition:
drive_letter: D
disk_number: 1
partition_size: 20 GiB
register: incorrect_attempt_3
ignore_errors: True
- assert:
that:
- incorrect_attempt_3 is failed
- '"Partition size is not supported by disk" in incorrect_attempt_3.msg'
- name: Create 1 gib partition using drive_letter and default (huge) mbr type (check mode)
win_partition:
drive_letter: D
state: present
partition_size: 1 GiB
disk_number: 1
active: True
register: create_small_part_check
check_mode: True
- name: Create 1 gib partition using drive_letter and default (huge) mbr type
win_partition:
drive_letter: D
state: present
partition_size: 1 GiB
disk_number: 1
active: True
register: create_small_part
- name: Create 1 gib partition using drive_letter and default (huge) mbr type (idempotence)
win_partition:
drive_letter: D
state: present
partition_size: 1 GiB
disk_number: 1
active: True
register: create_small_part_idempotence
- name: "Check if partition was created successfully"
win_shell: $AnsiPart = Get-Partition -DriveLetter D; "$($AnsiPart.DriveLetter),$($AnsiPart.Size),$($AnsiPart.IsActive),$($AnsiPart.MbrType)"
register: get_small_part
- assert:
that:
- create_small_part_check is changed
- create_small_part is changed
- create_small_part_idempotence is not changed
- get_small_part.stdout | trim == "D,1073741824,True,6"
- name: "Change drive letter, maximize partition size and set partition to read only (check mode)"
win_partition:
drive_letter: E
state: present
partition_size: -1
disk_number: 1
partition_number: 1
read_only: True
register: upgrade_small_part_check
check_mode: True
- name: "Change drive letter, maximize partition size and set partition to read only"
win_partition:
drive_letter: E
state: present
partition_size: -1
disk_number: 1
partition_number: 1
read_only: True
register: upgrade_small_part
- name: "Change drive letter, maximize partition size and set partition to read only (idempotence)"
win_partition:
drive_letter: E
state: present
partition_size: -1
disk_number: 1
partition_number: 1
read_only: True
register: upgrade_small_part_idempotence
- win_shell: $AnsiPart = Get-Partition -DriveLetter E; "$($AnsiPart.DriveLetter),$($AnsiPart.Size),$($AnsiPart.IsReadOnly)"
register: get_max_part
- name: Check if creation and updation were successful
assert:
that:
- upgrade_small_part_check is changed
- upgrade_small_part is changed
- upgrade_small_part_idempotence is not changed
- get_max_part.stdout | trim == "E,2096037888,True"
- name: "Changing size of a read only partition"
win_partition:
drive_letter: E
partition_size: 1 GiB
register: modify_read_only_partition
ignore_errors: True
- assert:
that:
- modify_read_only_partition is failed
- name: "Delete partition (check mode)"
win_partition:
disk_number: 1
partition_number: 1
state: absent
register: delete_partition_check
check_mode: True
- name: "Delete partition"
win_partition:
disk_number: 1
partition_number: 1
state: absent
register: delete_partition
- name: "Delete partition (idempotence)"
win_partition:
disk_number: 1
partition_number: 1
state: absent
register: delete_partition_idempotence
- name: "Confirm that the partition is absent"
win_shell: Get-Partition -DiskNumber 1 -PartitionNumber 1
register: confirm_partition_deletion
ignore_errors: True
- assert:
that:
- delete_partition_check is changed
- delete_partition is changed
- delete_partition_idempotence is not changed
- '"No matching MSFT_Partition objects found" in confirm_partition_deletion.stderr'
- name: "Create new partition without drive letter and ifs mbr type (check mode)"
win_partition:
disk_number: 1
partition_size: -1
mbr_type: IFS
offline: True
register: recreate_partition_check
check_mode: True
- name: "Create new partition without drive letter and ifs mbr type"
win_partition:
disk_number: 1
partition_size: -1
mbr_type: IFS
offline: True
register: recreate_partition
- name: "Create new partition without drive letter and ifs mbr type (idempotence failure)" # Disk is full now; no idempotence without drive letters
win_partition:
disk_number: 1
partition_size: -1
mbr_type: IFS
offline: True
register: recreate_partition_idempotence_failure
ignore_errors: True
- name: "Confirm that new partition is created with maximum size, is offline and is IFS"
win_shell: $AnsiPart = Get-Partition -DiskNumber 1 -PartitionNumber 1; "$($AnsiPart.Size),$($AnsiPart.IsOffline),$($AnsiPart.MbrType)"
register: confirm_recreate_partition
- assert:
that:
- recreate_partition_check is changed
- recreate_partition is changed
- recreate_partition_idempotence_failure is failed
- confirm_recreate_partition.stdout | trim == "2096037888,True,7"
- name: "Adding a drive letter to our partition should bring it back online (check mode)"
win_partition:
drive_letter: D
disk_number: 1
partition_number: 1
register: add_drive_letter_check
ignore_errors: True
check_mode: True
- name: "Adding a drive letter to our partition should bring it back online"
win_partition:
drive_letter: D
disk_number: 1
partition_number: 1
register: add_drive_letter
ignore_errors: True
- name: "Adding a drive letter to our partition should bring it back online (idempotence)"
win_partition:
drive_letter: D
disk_number: 1
partition_number: 1
register: add_drive_letter_idempotence
ignore_errors: True
- name: "Confirm that drive is back online"
win_shell: $AnsiPart = Get-Partition -DiskNumber 1 -PartitionNumber 1; "$($AnsiPart.DriveLetter),$($AnsiPart.IsOffline)"
register: confirm_add_drive_letter
ignore_errors: True
- assert:
that:
- add_drive_letter_check is changed
- add_drive_letter is changed
- add_drive_letter_idempotence is not changed
- confirm_add_drive_letter.stdout | trim == "D,False"
- name: "Remove partition again (check mode)"
win_partition:
drive_letter: D
state: absent
register: delete_partition_again_check
check_mode: True
- name: "Remove partition again"
win_partition:
drive_letter: D
state: absent
register: delete_partition_again
- name: "Remove partition again (idempotence)"
win_partition:
drive_letter: D
state: absent
register: delete_partition_again_idempotence
- assert:
that:
- delete_partition_again_check is changed
- delete_partition_again is changed
- delete_partition_again_idempotence is not changed

View file

@ -0,0 +1,7 @@
create vdisk file="{{ AnsibleVhdx }}" maximum=2000 type=fixed
select vdisk file="{{ AnsibleVhdx }}"
attach vdisk
convert mbr

View file

@ -0,0 +1,3 @@
select vdisk file="{{ AnsibleVhdx }}"
detach vdisk