I saw there was just a discussion touching this topic, but I have a more detailed question.
I have an iSCSI server with several iSCSI targets being shared to my ESXi cluster. I need to upgrade the server. I know I could just migrate all the VMs to other iSCSI servers and power off the empty iSCSI server. I don't think that's the proper way. What I want in the end is to be able to add the recreated iSCSI datastores without any previous vCenter/Host settings carrying over. A clean slate.
The process I've come up with is:
- Migrate VMs off the datastore.
- Unmount datastore
- Remove datastore
- Disconnect datastore
- Unmap iSCSI target
Here's the code I've cobbled together from here and other corners of the internet and it works:
#Migrating VMs is some I do more manually.
$iSCSINames = "iSCSI-12-04","iSCSI-12-05","iSCSI-12-06"
$alarm = Get-AlarmDefinition -Entity $cluster -Name "Cannot connect to storage"
Set-AlarmDefinition -AlarmDefinition $alarm -Enabled:$false
foreach ($iSCSIName in $iSCSINames){
$ds = get-datastore -name $iSCSIName
$hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname
if ($ds.ExtensionData.Host) {
$attachedHosts = $ds.ExtensionData.Host
Foreach ($VMHost in $attachedHosts) {
$hostview = Get-View $VMHost.Key
$StorageSys = Get-View $HostView.ConfigManager.StorageSystem
Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..."
$StorageSys.UnmountVmfsVolume($DS.ExtensionData.Info.vmfs.uuid)
}
Write-Host "Removing $($DS.Name) from cluster..."
Remove-Datastore -VMHost $hostview.name -Datastore $ds -ErrorAction SilentlyContinue -Confirm:$false
Start-Sleep 10
Foreach ($VMHost in $attachedHosts) {
$hostview = Get-View $VMHost.Key
$StorageSys = Get-View $HostView.ConfigManager.StorageSystem
$devices = $StorageSys.StorageDeviceInfo.ScsiLun
Foreach ($device in $devices) {
if ($device.canonicalName -eq $hostviewDSDiskName) {
$LunUUID = $Device.Uuid
Write-Host "Detaching LUN $($Device.CanonicalName) from host $($hostview.Name)..."
$StorageSys.DetachScsiLun($LunUUID);
}
}
$storageSys.UpdateViewData("storageDeviceInfo.hostBusAdapter")
$iScsi = $storageSys.StorageDeviceInfo.HostBusAdapter | where { $_.GetType().Name -eq "HostInternetScsiHba" }
$iScsiDevice = $iscsi.Device
$staticTargets = $iScsi | foreach { $_.ConfiguredStaticTarget } | where { $_.IScsiName -like $iSCSIName }
if ($staticTargets -ne $null -and $staticTargets.Count -gt 0) {
$staticTargetsAddress = $staticTargets | foreach {$_.address}
$staticTargetsAddress = $staticTargetsAddress -join ', '
Write-Host "Unmapping iSCSI address(es) $($staticTargetsAddress) from host $($hostview.Name)..."
$storageSys.RemoveInternetScsiStaticTargets($iScsiDevice, $staticTargets)
}
}
}
}
start-sleep 10
Set-AlarmDefinition -AlarmDefinition $alarm -Enabled:$true
Here are the problems I can’t solve. Hopefully someone can help. As the code runs it still generates an alarm "Cannot connect to storage". Even though I’ve disable the alarm. Second, after I reformat the iSCSI server, reinstall the OS, and recreate the iSCSI targets (with the same names it used to have), I scan for new datastores in vCenter. I see several datastores I can format and add to my cluster. When done the hosts still remember and applies all the setting they had from the previous iSCSI targets such as RoundRobin. This tells me I didn't really get the previous iSCSI targets completely removed. Is there something more I could do to so I can add the new ones with a completely clean slate?