Add a Virtual Disk to Virtual Machine from command line

0

Today we will be looking at how to add Virtual Disk to Virtual Machine from command line.

If you are following along, you must have noticed that I am trying and exploring most of the activities that can be performed from the command line instead of the vSphere Client or Web Client.

The use case of this activity was one of the users had a standalone ESXi host and the size of the disk that was to be added was more than 4 TB.

There is currently a limitation on the vSphere Client when you try to add an existing Virtual Disk which is more than 4 TB, it will error out.

So we will take a look at two different options, wherein we will add an existing Virtual Disk to a Virtual Machine.

And the second method, we will create a new Virtual Disk and to the Virtual Machine.

For this complete activity, make use of the vim-cmd namespace and associated commands with it.

The below command will be used to list the existing VMs that are registered on the ESXi host.

vim-cmd vmsvc/getallvms

Add a Virtual Disk to Virtual Machine from command line

The most important thing that we are interested in is the vmid of the Virtual Machine.

Let us say we have an existing vmdk called TestVMDK.vmdk that we would like to add to the VM.

The syntax to add an existing disk is as seen below.

vim-cmd vmsvc/device.diskaddexisting vmid disk_file controller_number unit_number

Let us make use of the existing SCSI controller 0 and the unit number as 1 since I already I have a device at unit 0.

vim-cmd vmsvc/device.diskaddexisting 10 /vmfs/volumes/iSCSI-1/TestDSL/TestVMDK.vmdk 0 1

Let us now look at the second example of creating the disk and assigning to the Virtual machine.

The syntax to add a new device would be as seen below.

vim-cmd vmsvc/device.diskadd vmid size controller_number unit_number datastore

So the command to add a new disk would be

vim-cmd vmsvc/device.diskadd 10 1G 0 2 iSCSI-1

We were successfully able to see both the scenarios wherein we added an existing disk and also created a new disk and attached to the Virtual machine.

We can verify by running the below command.

grep vmdk *.vmx

Add a Virtual Disk to Virtual Machine from command line

I hope this has been informative and thank you for reading!

Share.

About Author

I am Adil Arif, working as a Senior Technical Support Engineer at Rubrik as well as an independent blogger and founder of Enterprise Daddy. In my current role, I am supporting infrastructure related to Windows and VMware datacenters.

Leave A Reply