VDP fix (POWERCLI): Virtual machine disks consolidation is needed - Unable to Access file since it is locked

VDP fix (POWERCLI): Virtual machine disks consolidation is needed - Unable to Access file since it is locked

Hi everyone!..

Saturday at morning, I woke up... I see the email in my phone.. and I have a lot of alarms from vcenter server such as "Virtual machine disks consolidation is needed y Unable to Access file <unspecified filename> since it is locked".

Seriously I dont know why VMWARE does not fix this kind of problems xD

I made a simple script to create a easy workarround this Vsphere data protection very common problem.

This script need a few imput data to work well.

- At first place, download the code, paste into Powershell ISE and complete the variable declaration data like snmp/from/to email address.

- Then, you need to run this script from powershell prompt, when script starts you need to provide a vcenter server IP address and the vsphere data protection appliance name.

This step its very important because the vsphere data protection appliance name must be the SAME appliance name thats figure in vcenter server. (IE: "vsphere data protection 6.1")

- Finally, the script will ask a virtual machine name thats have the locked file in data protection appliance.

Running script:

STEP 1: Verifying existence of virtual machine in your provided vcenter server. (if not exists... continue)

STEP 2: Verifying existence of virtual machine snapshots. (if not exists... continue)

STEP 3:Verifying existence of locked disk in vmware sphere data protection appliance. (if not exists... continue)

STEP 4: Consolidate virtual machine. (Mandatory)

If any of these steps fail, the script send you a email with error report.

thank you all, I hope it is useful.

Import-Module VMware.VimAutomation.Core -InformationAction SilentlyContinue

clear

#///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////PASO 1

Write-Host "This is a script to fix locks on VDP appliance" -ForegroundColor Yellow -BackgroundColor Black

sleep 3

$smtp = "your SMTP server"

$from = "vdp_fix@yourdomain.com"

$to = "user@yourdomain.com"

$Error.Clear()

$vcenterserver = Read-Host "VCenter Server IP Address"

$testconn = Test-Connection $vcenterserver -Count 1 -ErrorAction SilentlyContinue

if($testconn){Write-Host "OK: Task ended without errors." -ForegroundColor DarkCyan

Connect-VIServer $vcenterserver -InformationAction SilentlyContinue

}else{Write-Host "ERROR: No VM found" -ForegroundColor Red -BackgroundColor DarkRed; exit}

sleep 1

$VDPname = Read-Host "vSphere Data Protection appliance name"

Write-Host "Verifying existence of appliance..." -ForegroundColor DarkCyan

if(get-vm $VDPname -ErrorAction SilentlyContinue){Write-Host "OK: Task ended without errors." -ForegroundColor DarkCyan}else{Write-Host "ERROR: No VM found, exiting." -ForegroundColor Red -BackgroundColor DarkRed ; sleep 3 ; exit}

if($Error.Count -eq "0"){Write-Host "Starting." -ForegroundColor DarkCyan}else{

Write-Host "ERROR: Process finalized with errors, sending email report." -ForegroundColor Red -BackgroundColor DarkRed

$subject = "[ERROR] | Fix VDPLOCK | Input errors"

send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$Error" -BodyAsHtml

exit}

#///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STEP 1

#VM NAME INPUT

write-host "STEP 1"  -ForegroundColor Cyan

$vmname = Read-Host "Write VM name"

Write-Host "Verifying existence of Virtual Machine..." -ForegroundColor DarkCyan

if(get-vm $vmname -ErrorAction SilentlyContinue){Write-Host "OK: VM exist." -ForegroundColor DarkCyan}else{Write-Host "ERROR: VM does not exist, exiting." -ForegroundColor Red -BackgroundColor DarkRed ; sleep 3 ; exit}

#///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STEP 2

#VERIFYING EXISTENCE OF SNAPSHOTS

write-host "STEP 2"  -ForegroundColor Cyan

Write-Host "Verifying existence of Snapshot..." -ForegroundColor DarkCyan

$Error.Clear()

if(get-vm $vmname | Get-Snapshot | Select-Object name -ExpandProperty name -ErrorAction SilentlyContinue)

{Write-Host "OK: Snapshot exist." -ForegroundColor DarkCyan

$ans = Read-Host "Do you want to delete this Snapshot? (Y/N)"

if($ans -ne "Y" -and $ans -ne "N"){Write-Host "ERROR: Invalid option, exiting." -ForegroundColor red -BackgroundColor DarkRed; sleep 3 ;exit}else{

if($ans -eq "N"){Write-Host "Exiting." -ForegroundColor Red -BackgroundColor DarkRed; sleep 3; exit}else{Write-Host "OK: Your selection is 'Y'." -ForegroundColor DarkCyan}}

Write-Host "Deleting Snapshot." -ForegroundColor Yellow

#DELETE SNAPSHOT

$Error.Clear()

$vmsnapshots = Get-Snapshot -VM $vmname | Remove-Snapshot -Confirm:$false -ErrorAction SilentlyContinue

if($Error.Count -eq "0"){Write-Host "OK: Task ended without errors." -ForegroundColor DarkCyan}else{

Write-Host "ERROR: Process finalized with errors, sending email report." -ForegroundColor Red -BackgroundColor DarkRed

$subject = "[ERROR] | Fix VDPLOCK | Error removing snapshot"

send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$Error" -BodyAsHtml}

$Error.Clear()}

else{Write-Host "No snapshot found." -ForegroundColor Red -BackgroundColor DarkRed ; sleep 3}

#///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STEP 3

#VERIFYING DISKS BLOCKS ON VDP 6.1

write-host "STEP 3"  -ForegroundColor Cyan

write-host "Verifying existence of locked disk..."  -ForegroundColor DarkCyan

$Error.Clear()

if($disk = Get-VM $VDPname | Get-HardDisk | where {($_.Filename -notmatch $VDPname) -and ($_.Filename -match "$vmname")})

{Write-Host "OK: External disk found in VDP" -ForegroundColor DarkCyan

$ans = Read-Host "Do you want to detach disk? (Y/N)"

if($ans -ne "Y" -and $ans -ne "N"){Write-Host "ERROR: Invalid option." -ForegroundColor red -BackgroundColor DarkRed; exit}else{

if($ans -eq "N"){Write-Host "Exiting." -ForegroundColor Red -BackgroundColor DarkRed; sleep 3; exit}else{Write-Host "OK: Your selection is 'Y'." -ForegroundColor DarkCyan}}

#UNLOCKING DISK

Write-Host "Unlocking disk." -ForegroundColor Yellow

$Error.Clear()

Get-VM $VDPname | Get-HardDisk | where {($_.Filename -notmatch $VDPname) -and ($_.Filename -match "$vmname")} | Remove-HardDisk -Confirm:$false -ErrorAction SilentlyContinue

if($Error.Count -eq "0"){Write-Host "OK: Task ended without errors." -ForegroundColor DarkCyan}else{

Write-Host "ERROR: Process finalized with errors, sending email report." -ForegroundColor Red -BackgroundColor DarkRed

$subject = "[ERROR] | Fix VDPLOCK | Error removing disk locked on VDP 6.1"

send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$Error" -BodyAsHtml}

$Error.Clear()}

else{Write-Host "No disk found." -ForegroundColor Red -BackgroundColor DarkRed; sleep 3}

#///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STEP 4

#VM CONSOLIDATION

write-host "STEP 4"  -ForegroundColor Cyan

write-host "Consolidating VM..."  -ForegroundColor DarkCyan

sleep 6

$Error.Clear()

(Get-VM $vmname).ExtensionData.ConsolidateVMDisks()

if($Error.Count -eq "0"){Write-Host "OK: Task ended without errors." -ForegroundColor Green}else{

Write-Host "ERROR: Process finalized with errors, sending email report." -ForegroundColor Red -BackgroundColor DarkRed

$subject = "[ERROR] | Fix VDPLOCK | Error consolidating VM"

send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$Error" -BodyAsHtml}

$Error.Clear()

write-host "Exiting."  -ForegroundColor yellow

Disconnect-VIServer -Force -Confirm:$false -ErrorAction SilentlyContinue

exit

Attachments
Comments

I have found that for those VMs that report "Disk consolidation needed" after vSphere Data Protection appliance backup, if you check the VDP appliance hardware configuration you may see that there are extra VM's disk files attached to the appliance.

It looks like the VDP backup appliance takes a snapshot, attaches the target VM disk to itself *temporarily* and then makes the backup.

After the backup it is supposed to release/dismount the VM disk and consolidate the snapshot since it was backing up a running VM, but sometimes it fails to release/unmounts the VM disk file so the VM disk remains locked in a snapshot mode.

Attempting to do a manual snapshot consolidation fails because the VDP appliance has an open file handle of the VMDK - very frustrating!!!!

When I look at my own VDP backup appliance in vCenter, I have seen sometimes up to two extra VM Hard disks mounted.

The solution is easy: Just click the Remove button to dismount them.

I can then manually do a snapshot consolidation procedure to clear the VM warning message about "Disk consolidation needed" it usually finishes in less than a minute.

Version history
Revision #:
1 of 1
Last update:
‎02-20-2017 11:33 AM
Updated by: