Skip to content

Instantly share code, notes, and snippets.

@JM2K69
Last active March 9, 2023 02:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JM2K69/625aeb845e828c4bdfcdf5a8649fca93 to your computer and use it in GitHub Desktop.
Save JM2K69/625aeb845e828c4bdfcdf5a8649fca93 to your computer and use it in GitHub Desktop.
TrueNas VMwware Lab
# Author: Jérôme Bezet-Torres
# Twitter: @JM2K69
# Website: jm2k69.github.io
# Network and Domain Data ##
$network = "10.0.0."
$domain = "vexpert.lab"
$VCSASSODomainName = "vsphere.local"
$TrueNasIP = "172.18.0.4"
# vCenter Server vSphere 8 Lab
$VIServer = "vcsa.$Domain"
$VIUsername = "administrator@vsphere.local"
$VIPassword = "VMware123!"
# ESXI informations
$VMPassword = "VMware1!"
$NestedESXiHostnameToIPs = @{
"esxi1.$domain" = "$network`81"
"esxi2.$domain" = "$network`82"
"esxI3.$domain" = "$network`83"
"esxI4.$domain" = "$network`84"
"esxI5.$domain" = "$network`85"
"esxI6.$domain" = "$network`86"
# "esxI7.$domain" = "$network`87"
# "esxI8.$domain" = "$network`88"
# "esxI9.$domain" = "$network`89"
}
# Name of new vSphere Datacenter/Cluster when VCSA is deployed
$NewVCDatacenterName = "Datacenter"
$NewVCSAClusterName = "Cluster"
########### Don't edit ##############################
$StartTime = Get-Date
$vc = Connect-VIServer $VIServer -User "administrator@$VCSASSODomainName" -Password $VIPassword -WarningAction SilentlyContinue
$d = Get-Datacenter -Server $vc $NewVCDatacenterName -ErrorAction Ignore
if( -Not $d) {
New-Datacenter -Server $vc -Name $NewVCDatacenterName -Location (Get-Folder -Type Datacenter -Server $vc)
}
$c = Get-Cluster -Server $vc $NewVCSAClusterName -ErrorAction Ignore
if( -Not $c) {
New-Cluster -Server $vc -Name $NewVCSAClusterName -Location (Get-Datacenter -Name $NewVCDatacenterName -Server $vc) -DrsEnabled -HAEnabled
(Get-Cluster $NewVCSAClusterName) | New-AdvancedSetting -Name "das.ignoreRedundantNetWarning" -Type ClusterHA -Value $true -Confirm:$false
}
$NestedESXiHostnameToIPs.GetEnumerator() | Sort-Object -Property Value | Foreach-Object {
$VMName = $_.Key
$VMIPAddress = $_.Value
$targetVMHost = $VMName
Add-VMHost -Server $vc -Location (Get-Cluster -Name $NewVCSAClusterName) -User "root" -Password $VMPassword -Name $targetVMHost -Force
}
Import-Module TrueNas
Connect-TrueNasServer -Server $TrueNasIP -SkipCertificateCheck
#List all disk
#Get-TrueNasDisk
#List all my disk unsed
Get-TrueNasDiskUnsed
#Create My Storage Pool
New-TrueNasPool -PoolName "Data" -Encryption $false -Deduplication OFF -PoolDesign Data -NbDataDisks 7 -DataVdevType RAIDZ2
#Create my Zvol Volume
New-TrueNasZvol -Name Zvol1 -ZvolName Data -Type VOLUME -Volsize 30 -Unit GiB -Sparse $true -Comment "Zvol1 #VMwareLab" -Compression LZ4
New-TrueNasZvol -Name Zvol2 -ZvolName Data -Type VOLUME -Volsize 15 -Unit GiB -Sparse $true -Comment "Zvol2 #VMwareLab" -Compression LZ4
New-TrueNasZvol -Name Zvol3 -ZvolName Data -Type VOLUME -Volsize 40 -Unit GiB -Sparse $true -Comment "Zvol3 #VMwareLab" -Compression LZ4
New-TrueNasZvol -Name Zvol4 -ZvolName Data -Type VOLUME -Volsize 20 -Unit GiB -Sparse $true -Comment "Zvol4 #VMwareLab" -Compression LZ4
#Get-TrueNasVolume -Type VOLUME
#Configure iSCSI - Portal - Initiator - Target
New-TrueNasIscsiPortal -IpPortal 0.0.0.0 -Port 3260 -Comment "TrueNas #VMwareLab"
New-TrueNasIscsiInitiator -AuthInitiators ALL -AuthNetwork ALL
New-TrueNasIscsiTarget -TargetName LUN1 -TargetAlias lun1 -GroupsPortalId 1 -GroupsInitiatorId 1
New-TrueNasIscsiTarget -TargetName LUN2 -TargetAlias lun2 -GroupsPortalId 1 -GroupsInitiatorId 1
New-TrueNasIscsiTarget -TargetName LUN3 -TargetAlias lun3 -GroupsPortalId 1 -GroupsInitiatorId 1
New-TrueNasIscsiTarget -TargetName LUN4 -TargetAlias lun4 -GroupsPortalId 1 -GroupsInitiatorId 1
New-TrueNasIscsiExtent -ExtentName LUN1 -ExtenType Zvol -ExtentSpeed SSD -ExtendComment "Lun1" -TrueNasPoolName Data -TrueNasZvolName Zvol1
New-TrueNasIscsiExtent -ExtentName LUN2 -ExtenType Zvol -ExtentSpeed SSD -ExtendComment "Lun2" -TrueNasPoolName Data -TrueNasZvolName Zvol2
New-TrueNasIscsiExtent -ExtentName LUN3 -ExtenType Zvol -ExtentSpeed SSD -ExtendComment "Lun3" -TrueNasPoolName Data -TrueNasZvolName Zvol3
New-TrueNasIscsiExtent -ExtentName LUN4 -ExtenType Zvol -ExtentSpeed SSD -ExtendComment "Lun4" -TrueNasPoolName Data -TrueNasZvolName Zvol4
New-TrueNasIscsiAssociat2Extent -TargetId 1 -ExtentId 1
New-TrueNasIscsiAssociat2Extent -TargetId 2 -ExtentId 2
New-TrueNasIscsiAssociat2Extent -TargetId 3 -ExtentId 3
New-TrueNasIscsiAssociat2Extent -TargetId 4 -ExtentId 4
#Configure iSCSI service
Get-TrueNasService | Where-Object {$_.service -eq "iscsitarget"}
Start-TrueNasService -Service iscsitarget
Get-TrueNasService | Where-Object {$_.service -eq "iscsitarget"}
###VMware vsphere #####
$vmhost = Get-VMhost
foreach ($item in $vmhost.Name) {
write-host "Configurartion Host : $item " -ForegroundColor Green
write-host "Create iSCSI vSwitch : $item " -ForegroundColor Blue
$network = Get-VMHostNetwork -VMHost $item
$phNic = $network.PhysicalNic[3].DeviceName
$vswitch = New-VirtualSwitch -VMHost $item -Name iSCSI -Nic $phNic
write-host "Create VMKernel on iSCSI vSwitch : $item " -ForegroundColor Blue
New-VMHostNetworkAdapter -VMHost $item -PortGroup "iSCSI" -virtualSwitch "iSCSI" | Out-Null
write-host "Configuring iSCSI HBA Storage on : $item " -ForegroundColor Blue
Get-VMHostStorage -VMHost $item | Set-VMHostStorage -SoftwareIScsiEnabled $True | Out-Null
Get-VMHostHba -Type iScsi -VMHost $item | Select-Object Name, Status, IScsiName
Get-VMHostHba -Type iScsi -VMHost $Item | New-IScsiHbaTarget -Address $TrueNasIP
Get-VMHostStorage -RescanAllHba -RescanVmfs -VMHost $item
}
function Get-FreeEsxiLUN
{
##############################
#.SYNOPSIS
#Shows free or unassigned SCSI LUNs/Disks on Esxi
#
#.DESCRIPTION
#The Get-FreeEsxiLUNs cmdlet finds free or unassigned SCSI LUNs/Disks on VMWare Esxi server. Free or unassigned disks are unformatted LUNs and need to format VMFS datastore or use as RDM (Raw Device Mapping)
#
#.PARAMETER Esxihost
#This is VMware Esxi host name
#
#.EXAMPLE
#Get-FreeEsxiLUNs -Esxihost Esxi001.vcloud-lab.com
#
#Shows free unassigned storage Luns disks on Esxi host name Esxi001.vcloud-lab.com
#
#.NOTES
#http://vcloud-lab.com
#Written using powershell version 5
#Script code version 1.0
###############################
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $true)]
[System.String]$Esxihost
)
Begin
{
if (-not(Get-Module vmware.vimautomation.core))
{
Import-Module vmware.vimautomation.core
}
#Connect-VIServer | Out-Null
}
Process
{
$VMhost = Get-VMhost $EsxiHost
$AllLUNs = $VMhost | Get-ScsiLun -LunType disk
$Datastores = $VMhost | Get-Datastore
foreach ($lun in $AllLUNs)
{
$Datastore = $Datastores | Where-Object { $_.extensiondata.info.vmfs.extent.Diskname -Match $lun.CanonicalName }
if ($null -eq $Datastore.Name)
{
$lun | Select-Object CanonicalName, CapacityGB, Vendor
}
}
}
End { }
}
for ($i = 1; $i -le $vmhost.count ; $i++) {
if ($i -eq 1) {
$t = $i - 1
$FreeESXILUN = Get-FreeEsxiLUN -Esxihost $vmhost.Name[$t] | Select-Object -Property CanonicalName, CapacityGB, Vendor | Where-Object { $_.Vendor -eq "TrueNas" }
for ($y = 0; $y -lt $FreeESXILUN.Count ; $y++) {
if ($y -eq 0) {
$z = 1
$Name = "LUN"+"$z" +"_"+ $FreeESXILUN.CapacityGB[$y]+"G"
New-Datastore -Name $Name -Path $FreeESXILUN.CanonicalName[$y] -Vmfs -FileSystemVersion 6 -VMHost $vmhost.Name[$y] |Out-Null
}
elseif ($y -ne 0){
$w = $y+1
$Name = "LUN"+"$w" +"_"+ $FreeESXILUN.CapacityGB[$y]+"G"
New-Datastore -Name $Name -Path $FreeESXILUN.CanonicalName[$y] -Vmfs -FileSystemVersion 6 -VMHost $vmhost.Name[$y] |Out-Null
}
}
}
else{
Get-VMHostStorage -RescanAllHba -RescanVmfs -VMHost $vmhost.Name[$t] | Out-Null
}
}
$EndTime = Get-Date
$duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalMinutes,2)
Write-host "vSphere Terraform Lab Deployment Complete!"
Write-host "StartTime: $StartTime"
Write-host " EndTime: $EndTime"
Write-host " Duration: $duration minutes"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment