In an effort to consolidate my homelab, I recently deployed an ESXi host to my main server. There’s been a bit of a learning curve but overall it’s been a pleasant experience. In this tutorial, I will show you how to add a new hard disk to an ESXi virtual machine. (If you're looking for hard drives, I usually use WD Reds that I shuck from a WD Easystore or Elements drive. They're high capacity, well-priced, and reliable.)
Now, theoretically, this should be an easy task where you can just edit the VM and click the Add hard disk > New standard hard disk, specify the hard disk location and be done. Unfortunately, though, there’s a bug in the ESXi host web client and so instead this will be a tutorial on adding an additional hard disk to an ESXi guest via the command line (esxcli).
Starting with the bug in the ESXi host web client, if you add a new hard disk to a VM, and the primary datastore where the VM is located is full, you’ll be greeted with the following message and the save button will gray out:
The disk size specified is greater than the amount available in the datastore. You cannot overcommit disk space when using a thick provisioned disk. You must increase the datastore capacity, or reduce the disk size before proceeding.
Now, that’s all fine and good, but the problem is, the “Save” button stays that way even when you choose a datastore that does have space. Heck, the save button even remains disabled when you remove the new hard disk! It appears that the web client only evaluates once, cripples the save button, and never re-evaluates the settings ever again.
Another thing I’ve learned along the way is that vSphere != ESXi host web client and unfortunately almost all of VMware’s documentation refers explicitly to vSphere, not the ESXi web client, so in this tutorial I’ll be showing you how to use the command line if you don’t have vSphere set up.
Creating the Virtual Disk via the Command Line (esxcli and vmkfstools):
1. Enable SSH on the ESXi host by right-clicking on the host in the web client > Services > Enable Secure Shell:
2. SSH into ESXi host. If you're using Ubuntu, open a terminal and type the following:
ssh root@<insert IP address of ESXi host here>
3. Find the location of your datastore with the following command:
esxcli storage filesystem list
4. Change directory into that location with the following command:
cd <copy Mount Point from above here>
5. Create your hard disk with the following command:
vmkfstools --createvirtualdisk 256G --diskformat thin <nameYourDriveHere>.vmdk
Note that the above creates a 256 GB virtual drive. If you want a different size, just change it.
Adding the New Virtual Disk to Your VM:
At this point, we've created our virtual disk, but it currently only exists in isolation in the datastore. It isn't connected to any VM so we need to add it to a VM.
6. Add the hard disk to your VM:
Here is the "choose your own adventure" part of the guide. I originally assumed that the majority of readers will want to add the virtual disk to their VM using the ESXi host's GUI. That's fine since this part of the ESXi Embedded Host Client isn't broken. If that's you, use the steps outlined in 6a.
However, based on reader feedback, I have learned that some readers are interested in also adding the new hard disk to the VM via the command line, so that both disk creation and hot-adding the disk to the VM are 100% command line. If that's you, refer to 6b.
6a. Add the hard disk to your VM via the ESXi Embedded Host Client (i.e. the ESXi web client GUI):
We're done with the command line now. Go back to your ESXi web client and edit your VM. Now click "Add a hard disk" but this time instead of a new hard disk, we're going to select "Existing hard disk" and navigate to the .vmdk you created in step 5:
Defaults are fine. Don't panic if your new hard disk shows 0 GB. The virtual disk just hasn't been formatted yet. Save and boot up your VM. As you can see I'm using a Windows VM here.
6b. Hot Adding the Disk to the VM via the Command Line:
To hot-add our new virtual disk to the VM via the command line, we first need to find the ID of our VM. To do so, still SSH'd into the ESXi host, we issue the following command:
I want to add this to my Windows VM, so I want Vmid 1. Now to hot-add our new virtual disk, we simply issue the following command:
vim-cmd vmsvc/device.diskaddexisting <vmid> </vmfs/volumes/pathToDisk.vmdk> 0 1
Replace <vmid> and </vmfs/volumes/pathToDisk.vmdk> with the Vmid and path to your new virtual disk, respectively. The path has to be the full path to the disk you created in steps 3/4/5 above. The 0 at the end refers to your SCSI controller (typically 0) and the 1 refers to your SCSI target number (the 0 slot is occupied by the primary virtual disk your VM is using, so 1 is just the next available target. If you already have a second hard disk on your VM, you would increment this number and use 2 instead. You get the idea). In my case, the command would look something like this:
vim-cmd vmsvc/device.diskaddexisting 1 /vmfs/volumes/5d5d560e-962037c1-16f8-fcaa142fc77d/Windows10Storage/win10aux.vmdk 0 1
7. Add the Hard Drive to the OS:
When you boot up your VM and navigate to "This PC" in File Explorer, you still won't see the hard drive. Again, don't panic, this is just because we haven't added the drive in Windows yet.
Open up Disk Management (you can search for it in Cortana) and you should see your new drive, albeit unformatted. Right-click on the hard drive and select "New Simple Volume":
Follow the wizard and when you go back to "This PC", voila, your new hard disk appears!