VMware Cloud Community
BenLiebowitz
Expert
Expert
Jump to solution

Remove a datastore that is in use

So, we have some datastores we're looking to retire, however removing them from the web client gives us a "datastore is in use" error.  We worked with VMware support, and they gave us a bunch of partedutil commands, but I'd like to try and automate this to reduce the human error factor. 

The commands to run are:

partedUtil getptbl /vmfs/devices/disks/naa.1234567890

partedUtil delete /vmfs/devices/disks/naa.1234567890

Everything else I can do with get-scsilun

This is what I have so far...

1. connect to vcenter

2. list the clusters

3. list the hosts in the selected cluster

4. list the datastores/luns on the host

5. browse the foor of the datastore selected (to verify nothing is on it and it's the one to be removed)

6. This would be where the partedutil commands go.  Smiley Happy

Thanks in advance!

----------------------

# Define the LunDatastoreName function

New-VIProperty -Name lunDatastoreName -ObjectType ScsiLun -Value {

    param($lun)

    $ds = $lun.VMHost.ExtensionData.Datastore | %{Get-View $_} | `

        where {$_.Summary.Type -eq "VMFS" -and

            ($_.Info.Vmfs.Extent | where {$_.DiskName -eq $lun.CanonicalName})}

    if($ds){

        $ds.Name

    }

} -Force | Out-Null

# connect to selected vCenter

connect-viserver $vcenter

# List Clusters

write-host ""

Write-host "Choose which Cluster where the LUN you want to remove presented:"

write-host "(it may take a few seconds to build the list)"

write-host ""

$ICLUSTER = get-cluster | Select Name | Sort-object Name

$i = 1

$ICLUSTER | %{Write-Host $i":" $_.Name; $i++}

$HCLUSTER = Read-host "Enter the number for the host to Patch."

$SCLUSTER = $ICLUSTER[$HCLUSTER -1].Name

write-host "You have selected $($SCLUSTER)."

start-sleep -s 3

# List hosts to select

write-host ""

Write-host "Choose which vSphere host that has the LUN presented."

write-host "(it may take a few seconds to build the list)"

write-host ""

$IHOST = get-cluster $SCLUSTER | Get-VMhost | Select Name | Sort-object Name

$i = 1

$IHOST | %{Write-Host $i":" $_.Name; $i++}

$DSHost = Read-host "Enter the number for the host to Patch."

$SHOST = $IHOST[$DSHost -1].Name

write-host "you have selected" $SHOST"."

# List all scsi devices

write-host ""

Write-host "Choose which LUN to remove.  *** VERIFY VIA THE vSPHERE CLIENT ***"

write-host "(it may take a minute to build the list)"

write-host ""

$ILUN = get-scsilun -vmhost $SHOST | Select LunDatastoreName, CanonicalName, RuntimeName, CapacityGB | Sort lundatastorename

$i = 1

$ILUN | %{Write-Host $i":" $_.lunDatastoreName, $_.RuntimeName, $_.CapacityGB; $i++}

$DSLUN = Read-host "Enter the number for the LUN to remove."

$SLUN = $ILUN[$DSLUN -1].LunDatastoreName

write-host "you have selected" $SLUN"."

# List the files on the LUN you selected

get-childitem (get-datastore $SLUN).datastorebrowserpath

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That can easily be checked with my function from Test if the datastore can be unmounted


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

View solution in original post

15 Replies
LucD
Leadership
Leadership
Jump to solution

Just wondering, can't you do the delete partition with the UpdateDiskPartitions method.

When you pass an empty Spec it erases all partitions.

Perhaps worth asking Support if that achieves the same thing?


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

ITaaP
Enthusiast
Enthusiast
Jump to solution

I know it is silly to ask, but is the datastore used for heartbeating or do you have storage I/O control enabled?

https://tactsol.com https://vmware.solutions
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That can easily be checked with my function from Test if the datastore can be unmounted


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

BenLiebowitz
Expert
Expert
Jump to solution

Luc,

Thanks, this function is very helpful!

It turns out, these datastores were configured to store local syslog data instead of using the scratch partition.  Once I changed them to scratch, I was able to delete the datastores. Smiley Happy

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos
BenLiebowitz
Expert
Expert
Jump to solution

Is there a way to run the partedutil commands via PowerCLI though?  I'm still getting "resource is in use" errors on two datastores.  I can browse and find nothing on them...  I may need to forcibly remove them. Smiley Sad

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Short of SSH-ing into the ESXi node, I'm afraid not.


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

0 Kudos
BenLiebowitz
Expert
Expert
Jump to solution

invoke-command won't work here, would it? 

Or are there any cmdlets you know of to delete / format the partition, similar to what partedutil is doing?

I hate the human error aspect having to do this at the canonical name level via SSH...  If I can automate it, that'd be much better!

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I guess you mean Invoke-VMScript, but that only works against VMs, not ESXi nodes.

I don't know of any cmdlet that would have the same functionality as the partedutil.

The closest that is available to perform specific functions inside an ESXi node is through the Get-EsxCli cmdlet.

But for commands that are not available in esxcli (or Get-EsxCli for PowerCLI), most of the time one would have to revert to an SSH session.

And execute console commands through something like plink.exe (from the PuTTY Suite).

And I don't remember seeing any API that offer this partedutil functionality.

I was hoping that the UpdateDiskPartitions method might do the trick, but obviously it doesn't.


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

BenLiebowitz
Expert
Expert
Jump to solution

in vSphere 6, they introduced an "erase partition" in the web client.  In the Hosts and Clusters view, go to a host, then Manage -> Storage - Storage Devices.  Find the LUN you want to erase and there's a button.  However, I'm getting the same in use error.  Oh well.

Thanks for your help!

erase_partition.PNG

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Perhaps you have to be at a specific VMFS version for this to work?


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

0 Kudos
BenLiebowitz
Expert
Expert
Jump to solution

Hey Luc,

I decided to find another way to run the partedUtil command...  I'm not getting the same results as I do if I run it via SSH though.  Is there a better way to get the Canonical Name of a LUN?

Thanks!

# connect to selected vCenter

connect-viserver $vcenter

# List Clusters

write-host ""

Write-host "Choose which Cluster where the LUN you want to remove presented:"

write-host "(it may take a few seconds to build the list)"

write-host ""

$ICLUSTER = get-cluster | Select Name | Sort-object Name

$i = 1

$ICLUSTER | %{Write-Host $i":" $_.Name; $i++}

$HCLUSTER = Read-host "Enter the number for the host to Patch."

$SCLUSTER = $ICLUSTER[$HCLUSTER -1].Name

write-host "You have selected $($SCLUSTER)."

start-sleep -s 3

# List hosts to select

write-host ""

Write-host "Choose which vSphere host that has the LUN presented."

write-host "(it may take a few seconds to build the list)"

write-host ""

$IHOST = get-cluster $SCLUSTER | Get-VMhost | Select Name | Sort-object Name

$i = 1

$IHOST | %{Write-Host $i":" $_.Name; $i++}

$DSHost = Read-host "Enter the number for the host to Patch."

$SHOST = $IHOST[$DSHost -1].Name

write-host "you have selected" $SHOST"."

# List all scsi devices

write-host ""

Write-host "Choose which LUN to remove.  *** VERIFY VIA THE vSPHERE CLIENT ***"

write-host "(it may take a minute to build the list)"

write-host ""

$ILUN = get-scsilun -vmhost $SHOST | Select LunDatastoreName, CanonicalName, RuntimeName, CapacityGB | Sort lundatastorename

$i = 1

$ILUN | %{Write-Host $i":" $_.lunDatastoreName, $_.RuntimeName, $_.CapacityGB; $i++}

$DSLUN = Read-host "Enter the number for the LUN to remove."

$SLUN = $ILUN[$DSLUN -1].LunDatastoreName

write-host "you have selected" $SLUN"."

# List the files on the LUN you selected

get-childitem (get-datastore $SLUN).datastorebrowserpath

# Check to see if the datastore is in use

Get-DatastoreUnmountStatus -datastore $SLUN

# Pipe datastore to get-scsilun to get canonicalname

$LUN = Get-Datastore $SLUN | get-scsilun | Select CanonicalName

# Start SSH

$ssh = Get-VMHostService -vmhost $SHOST | Where {$_.Key -eq "TSM-SSH"}

Start-VMHostService -HostService ($ssh) -Confirm:$false

# Remove the 99bnx2fc.sh file.

$User = "root"

$Pswd = "Password1!"

$plink = "c:\plink.exe"

$plinkoptions = " -v -batch -pw $Pswd"

$cmd1 = 'partedUtil getptbl /vmfs/devices/disks/$LUN'

$remoteCommand = '"' + $cmd1 + '"'

$command = $plink + " " + $plinkoptions + " " + $User + "@" + $SHOST + " " + $remoteCommand

$msg = Invoke-Expression -command $command

$msg

# Stop SSH

Stop-VMHostService -HostService ($ssh) -Confirm:$false

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean not using the Get-ScsiLun cmdlet?


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

0 Kudos
BenLiebowitz
Expert
Expert
Jump to solution

Yes.  When I run the command in the script above, it lists the canonical name mulitple times, so when I pipe it into a variable, I'm not sure it's being read properly.

$LUN = Get-Datastore Datastore1 | get-scsilun | Select CanonicalName

When I do $LUN alone, I get this:

2017-07-17 08_49_55-Administrator_ PowerShell NYVMXVCPRD05.png

There are multiple hosts attached, as you can see.

I believe I need it to be just 1 line...

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Got it, try like this

$LUN = Get-Datastore Datastore1 | get-scsilun | Select -First 1 -ExpandProperty CanonicalName

 


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

BenLiebowitz
Expert
Expert
Jump to solution

That got me a single line, thanks! 

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos