VMware always recommends formatting and creating a new VMFS volume using the vSphere Client as it automatically aligns your VMFS volume. However, if you do not have access to the vSphere Client or you wanted to format additional VMFS volumes via a kickstart, you can do so using the CLI and the partedUtil under /sbin.
~ # /sbin/partedUtil
Not enough arguments
Usage:
Get Partitions : get
Set Partitions : set ["partNum startSector endSector type attr"]*
Delete Partition : delete Resize Partition : resize
Get Partitions : getptbl
Set Partitions : setptbl
With ESXi 5, an MBR (Master Boot Record) partition table is no longer used and has been replaced with a GPT (GUID Partition Table) partition table. There is also only one block size of 1MB versus the 2,4 and 8 that was available in ESX(i) 4.x
We can view the partitions of a device by using the "getptbl" option and ensure we don't have an existing VMFS volume:
~ # /sbin/partedUtil "getptbl" "/vmfs/devices/disks/mpx.vmhba1:C0:T2:L0"
gpt
652 255 63 10485760
Next we will need to create a partition by using the "setptbl" option:
/sbin/partedUtil "setptbl" "/vmfs/devices/disks/mpx.vmhba1:C0:T2:L0" "gpt" "1 2048 10474379 AA31E02A400F11DB9590000C2911D1B8 0"
The "setptbl" accepts 3 arguments:
- diskName
- label
- partitionNumber startSector endSector type/GUID attribute
The diskName in this example is the full path to the device which is /vmfs/devices/disks/mpx.vmhba1:C0:T2:L0
The label will be gpt
The last argument is actually a string comprised of 5 individual parameters:
- partitionNumber - Pretty straight forward
- startSector - This will always be 2048 for 1MB alignment for VMFS5
- endSector - This will need to be calculated based on size of your device
- type/GUID - This is the GUID key for a particular partition type, for VMFS it will always be AA31E02A400F11DB9590000C2911D1B8
To view all GUID types, you can use the "showGuids" option:
~ # /sbin/partedUtil showGuids
Partition Type GUID
vmfs AA31E02A400F11DB9590000C2911D1B8
vmkDiagnostic 9D27538040AD11DBBF97000C2911D1B8
VMware Reserved 9198EFFC31C011DB8F78000C2911D1B8
Basic Data EBD0A0A2B9E5443387C068B6B72699C7
Linux Swap 0657FD6DA4AB43C484E50933C84B4F4F
Linux Lvm E6D6D379F50744C2A23C238F2A3DF928
Linux Raid A19D880F05FC4D3BA006743F0F84911E
Efi System C12A7328F81F11D2BA4B00A0C93EC93B
Microsoft Reserved E3C9E3160B5C4DB8817DF92DF00215AE
Unused Entry 00000000000000000000000000000000
Once you have the 3 arguments specified, we can now create the partition:
~ # /sbin/partedUtil "setptbl" "/vmfs/devices/disks/mpx.vmhba1:C0:T2:L0" "gpt" "1 2048 10474379 AA31E02A400F11DB9590000C2911D1B8 0"
gpt
0 0 0 0
1 2048 10474379 AA31E02A400F11DB9590000C2911D1B8 0
UPDATE (01/15) - Here is a quick shell snippet that you can use to automatically calculate End Sector as well as creating the VMFS5 volume:
partedUtil mklabel ${DEVICE} msdos
END_SECTOR=$(eval expr $(partedUtil getptbl ${DEVICE} | tail -1 | awk '{print $1 " \\* " $2 " \\* " $3}') - 1)
/sbin/partedUtil "setptbl" "${DEVICE}" "gpt" "1 2048 ${END_SECTOR} AA31E02A400F11DB9590000C2911D1B8 0"
/sbin/vmkfstools -C vmfs5 -b 1m -S $(hostname -s)-local-datastore ${DEVICE}:1
Note: You can also use the above to create a VMFS-based datastore running on a USB device, however that is not officially supported by VMware and performance with USB-based device will vary depending on the hardware and the speed of the USB connection.
We can verify by running the "getptbl" option on the device that we formatted:
~ # /sbin/partedUtil "getptbl" "/vmfs/devices/disks/mpx.vmhba1:C0:T2:L0"
gpt
652 255 63 10485760
1 2048 10474379 AA31E02A400F11DB9590000C2911D1B8 vmfs 0
Finally we will now create the VMFS volume using our favorite vmkfstools, the syntax is the same as previous release of ESX(i):
~ # /sbin/vmkfstools -C vmfs5 -b 1m -S himalaya-SSD-storage-3 /vmfs/devices/disks/mpx.vmhba1:C0:T2:L0:1
Checking if remote hosts are using this device as a valid file system. This may take a few seconds...
Creating vmfs5 file system on "mpx.vmhba1:C0:T2:L0:1" with blockSize 1048576 and volume label "himalaya-SSD-storage-3".
Successfully created new volume: 4dfdb7b0-8c0dcdb5-e574-0050568f0111
Now you can refresh the vSphere Client or run vim-cmd hostsvc/datastore/refresh to view the new datastore that was created.
Dredd says
AFAIK the partition for a VMFS volume must end on a cylinder boundary, so selecting the last sector for the partition table needs to be done using the formula endSector = ( C * H * S - 1).
In your example, C=652, H=255, S = 63, taking the product and subtracting one gives 10474379 as shown.
William says
@Dredd,
Thanks for the information, the numbers are actually from a VMware installation and I was looking through the esxi_install.log to get these numbers, I was interested in how this was calculated as well.
monsrud says
Either of you know any way to avoid the check "Checking if remote hosts are using this device as a valid file system. This may take a few seconds..." prior to creating the filesystem? I'd like to measure the time it takes to create filesystems backed by various types of storage. Thanks.
Kiran Basavaraju says
I am getting the following error while executing the command partedUtil "setptbl" /vmfs/devices/disks/mpx.vmhba1:C0:T0:L0 "gpt" "1 2048 10474379 AA31E02A400F11DB9590000C2911D1B8 0"
Error: Read-only file system during write on /dev/disks/mpx.vmhba1:C0:T0:L0
SetPtableGpt: Unable to commit to disk
Anonymous says
Did anyone find a solution to this? We are getting the same message Error: Read-only file system during write on /dev/disks/naa.600508b1001c282ee975ec5ff98a098c
WriteNewPtable: Unable to commit to disk
Anonymous says
Works like a charm. Good stuff!
Anonymous says
Thanks to Willian I've been using this method to kickstart numerous systems and it all worked fine. Today I came across a slightly different system (older) and it was refusing create VMFS partition table with "can not satisfy all constraints" error. Spent some time troubleshooting and one of VMware KBs pointed at partedUtil getUsableSectors. The end sector from getUsableSectors was less than what cyls*heads*sects - 1. And partedUtil was happy to make me a new partition. Just FYI
fishpen0 says
It would appear that this method will not work when attempting to create a datastore on the disk that ESXi is installed on and running from. partedUtil will consistently error out with "Read-only file system during write on /dev/disks/foo.bar". This is frustrating because you CAN edit the datastores on a disk that ESXi is running on from within vSphere itself.
Clearly ESXi is capable of configuring the disk it is running on. I was wondering if there is an alternate set of commands to run that set the disk up for write before running partedUtil.
William Lam says
Have you tried to delete the partition first? Another option would be just to dd the device and then trying again.
fishpen0 says
The partition doesn't exist yet because we use the --novmfsondisk option in kickstart. We recently tried removing that flag and then deleting the partition first, which it will do with no complaints, but then it refuses to remake that partition with the same error as above.
Casper42 says
Did you adjust the start sector value?
Should be obvious, but if you already have ESXi installed on that disk, there is around 1GB of data already on that disk and a 2MB offset alignment isn't going to work. I would probably let the GUI tools do the work and then go back and reverse engineer the start sector to determine the proper value.
Casper42 says
This comment has been removed by the author.
Casper42 says
William , I would love it if you could expand on this topic with the concept of a rolling VMFS 5 upgrade in mine.
Example, youhave 8 1TB DataStores on VMFS3 and using a 4MB Block Size.
You want to upgrade to VMFS 5 now that all your hosts and vCenter and such are upgraded.
The best Practice states you create a new DS thats VMFS5, then SvMotion the VMs off an existing DS over to that one and then delete and re-create the entire LUN and start over.
I would like to see an example of instead of deleting the LUN (and pestering the storage admin), using the above tools you mentioned to document the proper values from the existing DS, and then wipe it and create a fresh DS using VMFS 5 optimized values.
This way we pester the Storage admin once to add another LUN to the cluster, and then after that the VMware admin can do the rest of the rolling upgrade on their own.
NoVA says
I am having similar issues but my error out of the hostd.log shows the following:
Error: Connection timed out during read on /dev/disks/mpx.vmhba1:C0:T1:L0
WriteNewPtable: Unable to commit to disk
Netshark says
This is not a vmware issue; its a RAID/volume/SAN disks problem/incorrect setup. Disks might be presented to multiple hosts causing a write error lock/ The paths may be incorrectly setup /volume may be read only, etc..
Netshark says
"With ESXi 5, an MBR (Master Boot Record) partition table is no longer used and has been replaced with a GPT (GUID Partition Table) partition table."
No entirely true. GPTs are hardware dependable. They are only there, if the disk and the bios support them, regardless of the OS used (ESXi or otherwise).
ESXi 5 in a old supported server with old HDs, will have a nice MBR in it. I know mine do.
James says
Thank you for the clear and concise writeup. I just used this to create a datastore on my home ESXi 5 box because I can't use the vSphere client since I'm on Linux and I can't seem to get it to work under Wine. This was very helpful!
I'd echo the comment about the MBR. When I created the partition, I actually forgot to include the "gpt" parameter. Running getptbl on the new partition returned "msdos"
Also, thanks to the commenter who posted the formula to find the ending sector!
David Lam says
William - There are tons of articles and discussions on the web regarding partition alignment for guests. Here is my $1,000,000.00 question - For a default ESXi 5.1 update 2 and ESXi 5.5 update 2 installation, there is no options to configure the partition during installation. After the installation is complete, reboot. At the ESXi service console, login, run partedUtil, the starting sector is 64 !! Every VM doc on the web says ESXi 5.0 and above creating VMFS5+ creates aligned partitions. Starting sector of 64 is misaligned. What am I doing wrong? Or all this is just in my head? Thanks
gregstreuber says
Great post. Thanks William.
Mars says
Thanks for posting this, William!
Virgilio Fornazin says
There's a way to set a partition type of disk ?
I've splitted my mini 2012 1tb disk in two, and got the following ptbl
gpt
121601 255 63 1953525168
1 40 409639 C12A7328F81F11D2BA4B00A0C93EC93B systemPartition 128
2 409640 952255591 48465300000011AAAA1100306543ECAC unknown 0
3 952255592 953525127 426F6F74000011AAAA1100306543ECAC unknown 0
4 953526272 1953523711 EBD0A0A2B9E5443387C068B6B72699C7 linuxNative 0
how can I change the partition 4 to 0xfb to (UUID AA31E02A400F11DB9590000C2911D1B8) and format disk with vmkfstools ?
"FCHI says
wonderfull guy you are men!!!!! you saved my life- after a new vSphere 6 installation -Smart array P420i on a HP DL380Gen8,
no way to add a datastore using the vClient or web Vcenter server appliance....
but running your script generates error:
ATS on device /dev/disks/naa.600508b1001c8e2d6458b2b5f17bfff0:1: not supported
Thanks A LOT!
Vince says
Hey William, I tried your UPDATE (01/15) section and turned it into a shell script. I execute it and it bombs out.
expr: syntax error
Invalid number of tokens
Invalid partition information: 1 2048 AA31E02A400F11DB9590000C2911D1B8 0
Invalid Partition information
create fs deviceName:':1', fsShortName:'vmfs5', fsName:'BSTNDBYESXI01-local-datastore'
Device path name ":1" is not a valid absolute or relative path
Failed to resolve device name path :1
VmkfslibExtractObjectName failedUsage: vmkfstools -C [vmfs3|vmfs5] /vmfs/devices/disks/vml... or,
vmkfstools -C [vmfs3|vmfs5] /vmfs/devices/disks/naa... or,
vmkfstools -C [vmfs3|vmfs5] /vmfs/devices/disks/mpx.vmhbaA:T:L:P
Error: No such file or directory
William Lam says
Make sure you can manually execute it successfully before turning it into a script. Looks like you might not be passing in something correctly as its pointing to syntax error
qwe says
vmkfstools -C vmfs5 mpx.vmhba32\:C0\:T0\:L0\:1
Error: vmkfstools failed: vmkernel is not loaded or call not implemented.
then i type
tail /var/log/vmkernel.log
and get error
LVM: 7611: LVMProbeDevice failed on (3569532448, mpx.vmhba32:C0:T0:L0:1): Device does not contain a logical volume
2015-10-14T14:32:37.697Z cpu0:33343)usb-storage: detected SCSI revision number 6 on vmhba32
Can u comment this?
qwe says
LUN is present
partedUtil getptbl /dev/disks/mpx.vmhba32\:C0\:T0\:L0
gpt
243197 255 63 3906963456
1 128 3906963422 AA31E02A400F11DB9590000C2911D1B8 vmfs 0
sorry but i cant find any info in google.
Thanx u VERY much for Manual
Adam Bernstein says
Below is a script to create a valid VMFS partition. Afterwards, run this command to format the partition:
/sbin/vmkfstools --createfs vmfs5 --blocksize 1m -S NewDatastoreName ${device}:1
#!/bin/sh
debug=echo
# Fill in "device=" with the disk to partition. Otherwise specify path on command line
#
device="/vmfs/devices/disks/t10.ATA_____TOSHIBA_MD04ACA400______________________________X4CDK6N2FSBA"
if [ " $1" = " -h" ]; then
echo "Usage $0 [-W] /vmfs/devices/disks/NNN"
echo " -W: Write partition table; dry run without -W"
exit 1
elif [ " $1" = " -W" ]; then
unset debug
shift
fi
if [ -n "$1" ]; then
device="$1"
fi
if [ -z "$device" ]; then
echo "ERROR: Must specify valid raw device to partition"
exit 1
fi
lastsector=`partedUtil getUsableSectors $device`
if [ $? -ne 0 ]; then
echo "ERROR: Invalid partition specified."
exit 1
fi
lastsector=`echo $lastsector | awk '{print $2}'`
if [ -n "$debug" ]; then
echo "NOTICE: === This is a dry run ===="
echo "NOTICE: === Use -W option to write partition table ===="
fi
$debug /sbin/partedUtil setptbl $device gpt "1 2048 $lastsector AA31E02A400F11DB9590000C2911D1B8 0"
echo
echo "Partition table information:"
/sbin/partedUtil getptbl $device
Adam Bernstein says
Update: The blog "ate" the backquote characters in the above script. Below is the corrected post.
Below is a script to create a valid VMFS partition. Afterwards, run this command to format the partition:
/sbin/vmkfstools –createfs vmfs5 –blocksize 1m -S NewDatastoreName ${device}:1
#!/bin/sh
debug=echo
# Fill in "device=" with the disk to partition. Otherwise specify path on command line
#
device="/vmfs/devices/disks/t10.ATA_____TOSHIBA_MD04ACA400______________________________X4CDK6N2FSBA"
if [ " $1" = " -h" ]; then
echo "Usage $0 [-W] /vmfs/devices/disks/NNN"
echo " -W: Write partition table; dry run without -W"
exit 1
elif [ " $1" = " -W" ]; then
unset debug
shift
fi
if [ -n "$1" ]; then
device="$1"
fi
if [ -z "$device" ]; then
echo "ERROR: Must specify valid raw device to partition"
exit 1
fi
lastsector=$(partedUtil getUsableSectors $device)
if [ $? -ne 0 ]; then
echo "ERROR: Invalid partition specified."
exit 1
fi
lastsector=$(echo $lastsector | awk '{print $2}')
if [ -n "$debug" ]; then
echo "NOTICE: === This is a dry run ===="
echo "NOTICE: === Use -W option to write partition table ===="
fi
$debug /sbin/partedUtil setptbl $device gpt "1 2048 $lastsector AA31E02A400F11DB9590000C2911D1B8 0"
echo
echo "Partition table information:"
/sbin/partedUtil getptbl $device
Sivakumar says
Can you please provide the command to format and mount a file system in vmware.
Armin says
Don't know to go on:
the Formatting won't do right !
~ # vmkfstools -C vmfs5 -S USB-Backup /vmfs/devices/disks/mpx.vmhba43\:C0\:T0\:L0:1
create fs deviceName:'/vmfs/devices/disks/mpx.vmhba43:C0:T0:L0:1', fsShortName:'vmfs5', fsName:'USB-Backup'
deviceFullPath:/dev/disks/mpx.vmhba43:C0:T0:L0:1 deviceFile:mpx.vmhba43:C0:T0:L0:1
Checking if remote hosts are using this device as a valid file system. This may take a few seconds...
Creating vmfs5 file system on "mpx.vmhba43:C0:T0:L0:1" with blockSize 1048576 and volume label "USB-Backup".
Failed to create VMFS on device mpx.vmhba43:C0:T0:L0:1
Usage: vmkfstools -C [vmfs3|vmfs5] /vmfs/devices/disks/vml... or,
vmkfstools -C [vmfs3|vmfs5] /vmfs/devices/disks/naa... or,
vmkfstools -C [vmfs3|vmfs5] /vmfs/devices/disks/mpx.vmhbaA:T:L:P
Error: vmkfstools failed: vmkernel is not loaded or call not implemented.
Vladimir says
Armin, did you get it to work? I have it working on ESXi 6.5 running on a standard workstation but get the same error on an older Dell R310 running ESXi 5.5 U3.
Tom Otomanski says
If you guys never found a solution I came across this post which helped me fix the issue:
http://www.vbrain.info/2016/09/14/couldnt-format-vmfs-nested-esxi-vsan-datastore/
raj says
Hello William
i am trying to use USB drive as VMFS local drive in esxi 6.7
i followed all the command and i created partition but when i am running
root@localhost:~] vmkfstools -c vmfs6 -S E2USB-ISO-Datastore /dev/disks/t10.SanDisk00Cruzer_Dial00000000004C530001250419112222:1
i am getting
Invalid file length specifier: vmfs6
i dont know what i am missing
these are other command i used first
root@localhost:~] partedUtil setptbl dev/disks/t10.SanDisk00Cruzer_Dial00000000004C530001250419112222 gpt "1 2048 124053929 AA31E02A400F11DB9590000C2911D1B8 0" gpt
0 0 0 0
1 2048 124053929 AA31E02A400F11DB9590000C2911D1B8 0
please advise
Tom Otomanski says
@raj I think your issue is you're using -c instead of -C in your command. The lowercase c option is used for creating vmdk's for VM's.