Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSC_Disk: Add dev drive creation support to disk resource #278

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Updated DSC_Disk to allow volumes to be formatted as Dev Drives: Fixes #276

## [5.1.0] - 2023-02-22

### Changed
Expand Down
87 changes: 83 additions & 4 deletions source/DSCResources/DSC_Disk/DSC_Disk.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ function Get-TargetResource
.PARAMETER ClearDisk
Specifies if the disks partition schema should be removed entirely, even if data and OEM
partitions are present. Only possible with AllowDestructive enabled.

.PARAMETER DevDrive
Specifies if the volume should be formatted as a Dev Drive.

.PARAMETER UseUnallocatedSpace
Specifies that a new partition and volume should be formatted onto unallocated space on the disk.
#>
function Set-TargetResource
{
Expand Down Expand Up @@ -222,7 +228,15 @@ function Set-TargetResource

[Parameter()]
[System.Boolean]
$ClearDisk
$ClearDisk,

[Parameter()]
[System.Boolean]
$DevDrive,

[Parameter()]
[System.Boolean]
$UseUnallocatedSpace
)

Write-Verbose -Message ( @(
Expand Down Expand Up @@ -395,8 +409,13 @@ function Set-TargetResource
} # if
} # if

# Do we need to create a new partition?
if (-not $partition)
<#
There are two instances when we attempt to create a new partition:
1. When there are no partitions matching the drive letter the user entered.
2. When the user has advised us that they want to create a new partition on the disk's
unallocated space, regardless of whether there is one that matches the $size parameter already.
#>
if (-not $partition -bor $UseUnallocatedSpace)
{
# Attempt to create a new partition
$partitionParams = @{
Expand Down Expand Up @@ -560,6 +579,11 @@ function Set-TargetResource
$($script:localizedData.FormattingVolumeMessage -f $formatVolumeParameters.FileSystem)
) -join '' )

if ($DevDrive)
{
$formatVolumeParameters['DevDrive'] = $DevDrive
}

# Format the volume
$volume = $partition | Format-Volume @formatVolumeParameters
}
Expand Down Expand Up @@ -597,6 +621,11 @@ function Set-TargetResource
$formatParam.Add('AllocationUnitSize', $AllocationUnitSize)
}

if ($PSBoundParameters.ContainsKey('DevDrive'))
{
$formatParam.Add('DevDrive', $DevDrive)
}

$Volume | Format-Volume @formatParam
}
} # if
Expand Down Expand Up @@ -671,6 +700,12 @@ function Set-TargetResource
.PARAMETER ClearDisk
Specifies if the disks partition schema should be removed entirely, even if data and OEM
partitions are present. Only possible with AllowDestructive enabled.

.PARAMETER DevDrive
Specifies if the volume should be formatted as a Dev Drive.

.PARAMETER UseUnallocatedSpace
Specifies that a new partition and volume should be formatted onto unallocated space on the disk.
#>
function Test-TargetResource
{
Expand Down Expand Up @@ -719,7 +754,15 @@ function Test-TargetResource

[Parameter()]
[System.Boolean]
$ClearDisk
$ClearDisk,

[Parameter()]
[System.Boolean]
$DevDrive,

[Parameter()]
[System.Boolean]
$UseUnallocatedSpace
)

Write-Verbose -Message ( @(
Expand Down Expand Up @@ -750,6 +793,42 @@ function Test-TargetResource
return $false
} # if

# Check Dev Drive feature is enabled and that the user inputted ReFS as the file system.
if ($PSBoundParameters.ContainsKey('DevDrive'))
{
Assert-DevDriveFeatureAvailable
Assert-DevDriveFormatOnReFsFileSystemOnly -FSFormat $FSFormat

$tempPartition = Get-Partition `
-DriveLetter $DriveLetter `
-ErrorAction SilentlyContinue | Select-Object -First 1

# For unintialized disks, the largest free extent is the size of the disk.
if ($disk.PartitionStyle -ne 'RAW' )
{
$currentDiskFreeSpace = $disk.LargestFreeExtent
}
else
{
$currentDiskFreeSpace = $disk.Size
}

# User is attempting to create a new Dev Drive volume in a new partition
if ($null -eq $tempPartition)
{
<#
The Dev Drive will be created within the largest block of continguous unallocated space
available. So we need to check that the space is big enough to create the Dev Drive in.
#>
Assert-DiskHasEnoughSpaceToCreateDevDrive `
-UserDesiredSize $Size `
-CurrentDiskFreeSpace $currentDiskFreeSpace `
-DiskNumber $Disk.Number
}

Assert-DevDriveSizeMeetsMinimumRequirement -UserDesiredSize $Size
}

if ($disk.IsOffline)
{
Write-Verbose -Message ( @(
Expand Down
2 changes: 2 additions & 0 deletions source/DSCResources/DSC_Disk/DSC_Disk.schema.mof
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ class DSC_Disk : OMI_BaseResource
[Write, Description("Specifies the file system format of the new volume."), ValueMap{"NTFS","ReFS"}, Values{"NTFS","ReFS"}] String FSFormat;
[Write, Description("Specifies if potentially destructive operations may occur.")] Boolean AllowDestructive;
[Write, Description("Specifies if the disks partition schema should be removed entirely, even if data and OEM partitions are present. Only possible with AllowDestructive enabled.")] Boolean ClearDisk;
[Write, Description("Specifies if the volume should be formatted as a Dev Drive.")] Boolean DevDrive;
[Write, Description("Specifies that a new partition and volume should be formatted onto unallocated space in the disk.")] Boolean UseUnallocatedSpace;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<#PSScriptInfo
.VERSION 1.0.0
.GUID 3f629ab7-358f-4d82-8c0a-556e32514e3e
.AUTHOR DSC Community
.COMPANYNAME DSC Community
.COPYRIGHT Copyright the DSC Community contributors. All rights reserved.
.TAGS DSCConfiguration
.LICENSEURI https://github.com/dsccommunity/StorageDsc/blob/main/LICENSE
.PROJECTURI https://github.com/dsccommunity/StorageDsc
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES First version.
.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core
#>

#Requires -module StorageDsc

<#
.DESCRIPTION
This configuration will wait for disk 2 to become available, and then make the disk available as
two new Dev Drive volumes, 'E' and 'F', with 'F' using all available space after 'E' has been
created.
#>
Configuration Disk_InitializeDiskWithADevDrive
{
Import-DSCResource -ModuleName StorageDsc

Node localhost
{
WaitForDisk Disk2
{
DiskId = '5E1E50A401000000001517FFFF0AEB84' # Disk 2
DiskIdType = 'UniqueId'
RetryIntervalSec = 60
RetryCount = 60
}

# Will create a Dev Drive of 50Gb requiring the disk to have 50Gb of unallocated space.
Disk DevDriveVolume1
{
DiskId = '5E1E50A401000000001517FFFF0AEB84'
DiskIdType = 'UniqueId'
DriveLetter = 'E'
FSFormat = 'ReFS'
FSLabel = 'DevDrive'
DevDrive = $true
Size = 50Gb
UseUnallocatedSpace = $true
DependsOn = '[WaitForDisk]Disk2'
}

<#
Will attempt to create a Dev Drive volume using the rest of the space on the disk assuming
that the rest of the space is greater than the minimum size for Dev Drive volumes (50Gb).
#>
Disk DevDriveVolume2
{
DiskId = '5E1E50A401000000001517FFFF0AEB84'
DiskIdType = 'UniqueId'
DriveLetter = 'F'
FSFormat = 'ReFS'
FSLabel = 'DevDrive'
DevDrive = $true
DependsOn = '[Disk]DevDriveVolume1'
}
}
}
Loading