LUN juggling in vSphere 5

Buried in the massive amount of new features introduced with vSphere 5 there are several new API methods on the HostStorageSystem managed object.

Two of these API methods will allow you to automate the new Attach/Detach LUN feature from the vSphere Client. It concerns the AttachScsiLun and DetachScsiLun methods. Until this new feature is available natively in PowerCLI, you can use the following functions.

Script

Annotations

Line 3,16: The host where you want to attach/detach the LUN must be provided as a VMHostImpl object

Line 4,17: The LUN you want to attch/detach is identified with it’s Canonicalname

Line 7,20: The AttachScsiLun and DetachScsiLun methods are present on the StorageSystem Managed Object.

Line 8-9,21-22: Both methods want the UUID of the LUN.

Sample run

A very simple example that shows how you can detach a LUN from a host.

There are other new methods on the HostStorageSystem managed object that were introduced in vSphere 5. I’ll handle those in a future post.

Enjoy !

8 Comments

    DanV

    I’m sure that I’m doing something wrong trying to use these for an upcoming datacenter move. SAN replicated disks, need to detach from one, then attach to the other side.
    given :
    $vmhost.Name ustestvm02.ad.hbfuller.com
    $luns[0] naa.600… /vmfs/devices/disks/naa.600… disk 25.000 RoundRobin

    running:
    Attach-Disk $vmhost naa.60060e80132cb60050202cb600000c25

    i get :
    Attach-Disk $vmhost naa.60060e80132cb60050202cb600000c25
    Exception calling “AttachScsiLun” with “1” argument(s): “The operation is not allowed in the current state.”

    what is the expected state, and how do i get my LUNs in that expected state ?
    any help would be greatly appreciated!

    Reginaldo Tunisi

    Great job on the script, LucD!

    I did a few modifications and got large improvements on runtime performance simply removing the Get-scsilun cmdlet (which can be very slow if your system runs with a lot of Luns). The configManager object has a list of all attached luns on the StorageDeviceInfo parameter. You can simply run your where query over there to find the ScsiLun Uuid

    Added valueFromPipeline and mandatory validation for the vmhost parameter

    Function Attach-Disk{
    param(
    [Parameter(Mandatory=$true,
    ValueFromPipeline=$True,
    ValueFromPipelineByPropertyName=$true)]
    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,
    [string]$CanonicalName
    )

    $storSys = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
    $lunUuid = ($storsys.storagedeviceinfo.scsilun | ? CanonicalName -eq $CanonicalName).Uuid

    $storSys.AttachScsiLun($lunUuid)
    }

    function Detach-Disk{
    param(
    [Parameter(Mandatory=$true,
    ValueFromPipeline=$True,
    ValueFromPipelineByPropertyName=$true)]
    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,
    [string]$CanonicalName
    )

    $storSys = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
    $lunUuid = ($storsys.storagedeviceinfo.scsilun | ? CanonicalName -eq $CanonicalName).Uuid

    $storSys.DetachScsiLun($lunUuid)
    }

      LucD

      Wow, thanks !
      That is indeed a great improvement.

    Garth Reid

    sorry for being late on this post but this is a great script. I have a datastore with extent, do you have a script to show the datastore and all naa associated with attached or detached state.

    GregD

    Thanks for your scripts above however i have a question or two. The Detach-disk command, is it only for SCSI attach storage (SCSI attached) or does it work with fiber attached (HBA) storage?
    I run the command against an ESX server with Fiber attached but it does not detach the LUN, and does not give me an error either.

    andre

    i look for a simple script to mount a vmfs datastore

    vSphere PowerCLI> $CanonicalName = “naa.60a980005034472f544a6f416d4a487a”
    vSphere PowerCLI> Write-Host $CanonicalName
    naa.60a980005034472f544a6f416d4a487a
    vSphere PowerCLI> $esx = Get-VMHost
    vSphere PowerCLI> Write-Host $esx
    ls-esxt-01
    vSphere PowerCLI> Attach-Disk -vmhost $esx -CanonicalName $CanonicalName
    Ausnahme beim Aufrufen von “AttachScsiLun” mit 1 Argument(en): “The operation is not allowed in the current state.”
    Bei Zeile:9 Zeichen:27
    + $storSys.AttachScsiLun <<<< ($lunUuid)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

      LucD

      From the message it looks as if the LUN is not in the correct state to perform the operation.
      Could it be that the LUN is already in use ?

      Also have a look at Automating Datastore/Storage Device Detachment in vSphere 5. Alan’s functions also allow you to mount datastores.

        Ben

        Is there a way to do the Detach-Disk Function with Datastore Name instead of CanonicalName?

        function Detach-Disk{
        param(
        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,
        [string]$CanonicalName
        )

        $storSys = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
        $lunUuid = (Get-ScsiLun -VmHost $VMHost |
        where {$_.CanonicalName -eq $CanonicalName}).ExtensionData.Uuid

        $storSys.DetachScsiLun($lunUuid)

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.