VMware Cloud Community
svv3ns
Contributor
Contributor

PowerCLI to remove a LUN kicks out an alert

My goal is to present new LUN snap-copies to an ESXi host every night, and mount them to a linux VM for backup. I have everything working, except for the LUN removal. I can remove the disks from the VM. But when I try to remove them from the ESXi host, it triggers an alert that the path to that LUN is down.
If I detach the LUN from the ESXi, and then un-present it at the array-level, it kicks out an error.
If I detach the LUN, then issue an esxcli command to remove it, the state goes back to "on". And, of course, it gets an error when I try to remove it.
What am I missing?
Here is my code. The variable "$naa04" has the value of the "naa" ID: naa.60002ac0000000000000034400028c3d
# # # #
$esxhost = "esxihostname"
$esx = Get-VMHost -Name $esxhost
$esxcli = Get-EsxCli -VMHost $esx -v2
$storsys = Get-View $esx.Extensiondata.ConfigManager.StorageSystem
$lunUuid = (Get-ScsiLun -VmHost $esx | where {$_.CanonicalName -eq $naa04}).ExtensionData.Uuid
$storSys.DetachScsiLun($lunUuid)
$esxcli.storage.core.device.detached.list.Invoke() |where {$_.DeviceUID -eq $naa04} | %{ $detach = @{ device = $_.DeviceUID }}
$esxcli.storage.core.device.detached.remove.Invoke($detach)

0 Kudos
5 Replies
LucD
Leadership
Leadership

Shouldn't you rescan storage at some point?

Get-VMHostStorage -VMHost $esx -RescanAllHba


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
svv3ns
Contributor
Contributor

I do, but that's during the re-add of the LUNs.  The PowerShell commands I gave are part of a larger process:

1.) Check the backup and unmount filesystems, 2.) Delete disks from the VM and remove the LUNs from ESX host, 3.) Snap the production LUNs, 4.) present the snaps to ESX host, 5.) re-scan the storage and present the disks to the VM.

Deleting the disks from the VM works just fine.  (Remove-HardDisk -DeletePermanently -Confirm:$false $rmdisk04)  And the command:  "$storSys.DetachScsiLun($lunUuid)" does successfully change the LUN state to "off." 

If, at this point, I un-present the LUNs from the array-side, vSphere sends an alert.

So I added the command:  "$esxcli.storage.core.device.detached.remove.Invoke($detach)"  Instead of removing the LUN, it changes the state back to "on".  I can see this status with: "$esxcli.storage.core.device.list.invoke() |select device,status"

My only other thought is maybe there's an esxcli command to remove the vmdk. 

0 Kudos
svv3ns
Contributor
Contributor

So if I re-scan before I un-present, the LUNs will just come back.

I've been searching esxcli commands, and the VMware PowerCLI reference, and there doesn't appear to be a specific command for deleting the vmdk file.  (Probably the Remove-HardDisk does that, which I'm already doing). 

 

0 Kudos
LucD
Leadership
Leadership

Could it be that the VM still has a lock on the LUN?
Check with

$esxcli.storage.core.device.world.list.Invoke(@{device=$lunUuid})


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
svv3ns
Contributor
Contributor

Thanks LucD, but it didn't have a lock.  I found this:

https://kb.vmware.com/s/article/2004605

It says that if the LUN isn't un-presented after the detach, then when you do the remove, it will go back to "on".  (Which is a ridiculous thing to do.)

The order should be:  detach, un-present the lun, then remove.  What it doesn't mention is that this order will still kick out an alert.  I had hoped to not have to turn off and on alerts every time we do a backup, but seems like there is no way around it.

0 Kudos