How to recover an accidentally deleted ESXi Datastore in vSphere 6.7, 6.5 , 6.0

Here I’m gonna explain how to recover a datastore that has been deleted by accident.

In order to simulate the senario I delete my datastore that contains some VM files:

Now just enable the ssh service on our ESXi host:

And connect to the one of esxi host that already connected to that Datastore

Go to the vSphere client and write down the location for the deleted datastore or storage device

If we need to see a summary about our storage devices in CLI mode we can use :

offset="128 2048"; for dev in `esxcfg-scsidevs -l | grep "Console Device:" | awk {'print $3'}`; do disk=$dev; echo $disk; 
partedUtil getptbl $disk; { for i in `echo $offset`; do echo "Checking offset found at $i:"; hexdump -n4 -s $((0x100000+(512*$i))) $disk; hexdump -n4 -s $((0x1300000+(512*$i))) $disk; hexdump -C -n 128 -s $((0x130001d + (512*$i))) $disk; done; } | grep -B 1 -A 5 d00d; echo "---------------------"; done

Now we need to get some info about the our deleted storage device partition table:

partedUtil getptbl /vmfs/devices/disks/{Device identifier}

In next step we should define what is the begining and end of block in VMFS partition by issuing this command:

partedUtil getUsableSectors /vmfs/devices/disks/{Device identifier}

Now it’s time to set the a lable for the partition table:

partedUtil mklabel /vmfs/devices/disks/{Device identifier} gpt

Then we need to create the partition table, but before that we need to get the GUID of a VMFS partition because we lost a VMFS partition, if it was a vSAN partition we would use a vSAN GUID from table below, we can get this info with this command:

partedUtil showGuids

Now we create the partition table :

partedUtil setptbl /vmfs/devices/disks/{Device identifier} gpt "1 2048 {end block in VMFS partition} {Partition GUID} 0"

Now we can run the vmkfstool command to mount the deleted Datastore, and rescan all the storage devices

vmkfstools -V
esxcli storage core adapter rescan --all

Now we should check in vSphere client whether the datastore is back or not

the Datastore is back but the size is not correct, for solving this issue we need to check the vmkernel log:

(it is better to open another ssh console and use tail command as follow and on the first console use vmksfstools -V to see the logs

Now we just add 2047 to the highlighted number and try to create the partition table with new end block :

167770079 + 2047 = 167772126

And check again the datastore if the datastore is inaccessible still we can mount it

Now we have access to the data

Leave a Reply

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

20 − = 11