Skip to content

Commit

Permalink
adding ability to add shared storage to cluster, and an example
Browse files Browse the repository at this point in the history
  • Loading branch information
jpomfret committed Aug 16, 2020
1 parent 8aec2dd commit 9122252
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 45 deletions.
109 changes: 64 additions & 45 deletions AutomatedLab/AutomatedLabFailover.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function Install-LabFailoverCluster

Install-LabWindowsFeature -ComputerName $failoverNodes -FeatureName Failover-Clustering, RSAT-Clustering -IncludeAllSubFeature

Write-ScreenInfo -Message 'Restart post FCI Install'
Restart-LabVM $failoverNodes -Wait

if (Get-LabVm -Role FailoverStorage)
{
Write-ScreenInfo -Message 'Waiting for failover storage server to complete installation'
Expand Down Expand Up @@ -64,7 +67,11 @@ function Install-LabFailoverCluster
Get-Disk | Where-Object -Property OperationalStatus -eq Offline | Set-Disk -IsOffline $false
}
}
else

$storageNode = Get-LabVm -Role FailoverStorage -ErrorAction SilentlyContinue
$role = $storageNode.Roles | Where-Object Name -eq FailoverStorage

if((-not $useDiskWitness) -or ($storageNode.Disks.Count -gt 1))
{
Invoke-LabCommand -ComputerName $firstNode -ActivityName 'Preparing cluster storage' -ScriptBlock {
$diskpartCmd = 'LIST DISK'
Expand Down Expand Up @@ -157,7 +164,7 @@ function Install-LabFailoverCluster

if ($useDiskWitness)
{
$clusterDisk = Get-ClusterResource -Cluster $clusterName -ErrorAction SilentlyContinue | Where-object -Property ResourceType -eq 'Physical Disk'
$clusterDisk = Get-ClusterResource -Cluster $clusterName -ErrorAction SilentlyContinue | Where-object -Property ResourceType -eq 'Physical Disk' | Select -First 1

if ($clusterDisk)
{
Expand Down Expand Up @@ -197,8 +204,6 @@ function Install-LabFailoverStorage
$clusters[$name] += $_.Name
}

$lunDrive = $role.Properties['LunDrive'][0] # Select drive letter only

foreach ($cluster in $clusters.Clone().GetEnumerator())
{
$machines = $cluster.Value
Expand All @@ -211,59 +216,73 @@ function Install-LabFailoverStorage

$clusters[$clusterName] = $initiatorIds
}

Install-LabWindowsFeature -ComputerName $storageNode -FeatureName FS-iSCSITarget-Server

Invoke-LabCommand -ActivityName 'Creating iSCSI target' -ComputerName $storageNode -ScriptBlock {
if (-not $lunDrive)
{
$lunDrive = $env:SystemDrive[0]
}

$driveInfo = [System.IO.DriveInfo] [string] $lunDrive
foreach ($disk in $storageNode.Disks)
{
Write-ScreenInfo "Working on $($disk.name)"
#$lunDrive = $role.Properties['LunDrive'][0] # Select drive letter only
$driveLetter = $disk.DriveLetter

if (-not (Test-Path $driveInfo))
{
$offlineDisk = Get-Disk | Where-Object -Property OperationalStatus -eq Offline | Select-Object -First 1
if ($offlineDisk)
Invoke-LabCommand -ActivityName "Creating iSCSI target for $($disk.name)" -ComputerName $storageNode -ScriptBlock {
# assign drive letter if not provided
if (-not $driveLetter)
{
$offlineDisk | Set-Disk -IsOffline $false
$offlineDisk | Set-Disk -IsReadOnly $false
# http://vcloud-lab.com/entries/windows-2016-server-r2/find-next-available-free-drive-letter-using-powershell-
#$driveLetter = (68..90 | % {$L = [char]$_; if ((gdr).Name -notContains $L) {$L}})[0]
$driveLetter = $env:SystemDrive[0]
}

if (-not ($offlineDisk | Get-Partition | Get-Volume))
$driveInfo = [System.IO.DriveInfo] [string] $driveLetter

if (-not (Test-Path $driveInfo))
{
$offlineDisk | New-Volume -FriendlyName Luns -FileSystem ReFS -DriveLetter $lunDrive
$offlineDisk = Get-Disk | Where-Object -Property OperationalStatus -eq Offline | Select-Object -First 1
if ($offlineDisk)
{
$offlineDisk | Set-Disk -IsOffline $false
$offlineDisk | Set-Disk -IsReadOnly $false
}

if (-not ($offlineDisk | Get-Partition | Get-Volume))
{
$offlineDisk | New-Volume -FriendlyName $disk -FileSystem ReFS -DriveLetter $driveLetter
}
}
}

$lunFolder = New-Item -ItemType Directory -Path (Join-Path -Path $driveInfo -ChildPath LUNs) -ErrorAction SilentlyContinue
$lunFolder = Get-Item -Path (Join-Path -Path $driveInfo -ChildPath LUNs) -ErrorAction Stop
$folder = New-Item -ItemType Directory -Path (Join-Path -Path $driveInfo -ChildPath $($disk.name)) -ErrorAction SilentlyContinue
$folder = Get-Item -Path (Join-Path -Path $driveInfo -ChildPath $($disk.name)) -ErrorAction Stop

foreach ($clu in $clusters.GetEnumerator())
{
New-IscsiServerTarget -TargetName $clu.Key -InitiatorIds $clu.Value
$diskTarget = (Join-Path -Path $lunFolder.FullName -ChildPath "$($clu.Key).vhdx")
New-IscsiVirtualDisk -Path $diskTarget -Size 1GB
Add-IscsiVirtualDiskTargetMapping -TargetName $clu.Key -Path $diskTarget
}
foreach ($clu in $clusters.GetEnumerator())
{
if (-not (Get-IscsiServerTarget -TargetName $clu.Key -ErrorAction SilentlyContinue))
{
New-IscsiServerTarget -TargetName $clu.Key -InitiatorIds $clu.Value
}
$diskTarget = (Join-Path -Path $folder.FullName -ChildPath "$($disk.name).vhdx")
$diskSize = [uint64]$disk.DiskSize*1GB
New-IscsiVirtualDisk -Path $diskTarget -Size $diskSize
Add-IscsiVirtualDiskTargetMapping -TargetName $clu.Key -Path $diskTarget
}
} -Variable (Get-Variable -Name clusters, disk, driveletter) -ErrorAction Stop

} -Variable (Get-Variable -Name clusters, lunDrive) -ErrorAction Stop
$targetAddress = $storageNode.IpV4Address

$targetAddress = $storageNode.IpV4Address
Invoke-LabCommand -ActivityName 'Connecting iSCSI target' -ComputerName (Get-LabVm -Role FailoverNode) -ScriptBlock {
if (-not (Get-Command New-IscsiTargetPortal -ErrorAction SilentlyContinue))
{
iscsicli.exe QAddTargetPortal $targetAddress
$target = ((iscsicli.exe ListTargets) -match 'iqn.+target')[0].Trim()
iscsicli.exe QLoginTarget $target
}
else
{
New-IscsiTargetPortal -TargetPortalAddress $targetAddress
Get-IscsiTarget | Where-Object {-not $PSItem.IsConnected} | Connect-IscsiTarget -IsPersistent $true
}

Invoke-LabCommand -ActivityName 'Connecting iSCSI target' -ComputerName (Get-LabVm -Role FailoverNode) -ScriptBlock {
if (-not (Get-Command New-IscsiTargetPortal -ErrorAction SilentlyContinue))
{
iscsicli.exe QAddTargetPortal $targetAddress
$target = ((iscsicli.exe ListTargets) -match 'iqn.+target')[0].Trim()
iscsicli.exe QLoginTarget $target
}
else
{
New-IscsiTargetPortal -TargetPortalAddress $targetAddress
Get-IscsiTarget | Where-Object {-not $PSItem.IsConnected} | Connect-IscsiTarget -IsPersistent $true
}
} -Variable (Get-Variable targetAddress) -ErrorAction Stop

} -Variable (Get-Variable targetAddress) -ErrorAction Stop
}
}
#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<#
This demo creates a failover cluster with 2 nodes, and 2 shared disks
that can be added to cluster roles.
#>

$labname = 'FailOverLab1'
New-LabDefinition -Name $labname -DefaultVirtualizationEngine HyperV

Add-LabDomainDefinition -Name contoso.com -AdminUser Install -AdminPassword Somepass1

Set-LabInstallationCredential -Username Install -Password Somepass1

Add-LabVirtualNetworkDefinition -Name $labname -AddressSpace 192.168.50.0/24

$PSDefaultParameterValues = @{
'Add-LabMachineDefinition:OperatingSystem' = 'Windows Server 2019 Datacenter (Desktop Experience)'
'Add-LabMachineDefinition:Network' = $labname
'Add-LabMachineDefinition:DomainName' = 'contoso.com'
'Add-LabMachineDefinition:Memory' = 1GB
}

Add-LabMachineDefinition -Name foDC1 -Roles RootDC

# Integrate an iSCSI Target into your machines
$storageRole = Get-LabMachineRoleDefinition -Role FailoverStorage
Add-LabDiskDefinition -Name LunDrive -DiskSizeInGb 26 -DriveLetter D
Add-LabDiskDefinition -Name SqlDataDrive -DiskSizeInGb 10 -DriveLetter E
Add-LabDiskDefinition -Name SqlLogDrive -DiskSizeInGb 10 -DriveLetter F
Add-LabMachineDefinition -Name foCLS1 -Roles $storageRole -DiskName LunDrive, SqlDataDrive, SqlLogDrive

# create a cluster role
$cluster1 = Get-LabMachineRoleDefinition -Role FailoverNode -Properties @{ ClusterName = 'Clu1'; ClusterIp = '192.168.50.111' }

# add two nodes for the cluster
Add-LabMachineDefinition -name foCLN1 -Roles $cluster1
Add-LabMachineDefinition -name foCLN2 -Roles $cluster1

Install-Lab

Show-LabDeploymentSummary

0 comments on commit 9122252

Please sign in to comment.