VMware Cloud Community
MarcBouchard
Enthusiast
Enthusiast
Jump to solution

Create an unattached VMDK in a specific path

Is there a way to create a VMDK file like you can do with VMKFSTools, i.e. simply create the container without having a VM yet?

The purpose is to create a VMDK disk in a specific folder that will be attached in multiwriter mode later on to two VMs.

I want to automate the process we have now which is:

- Create the VMDK disk attached to one of the VMs

- Detach the VMDK disk

- Move it to the shared folder location

- Reattach the VMDK disk to both VMs

Thanks,

Marc

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can do something like this.

Note that the folder (MyFolder) on the datastore needs to be there.

$dsName = 'MyDS'

$dsFolder = 'MyFolder'

$vmdkName = 'NewDisk.vmdk'

$vmdkSizeKB = 5 * 1GB / 1KB

$ds = Get-Datastore -Name $dsName

$vmdkPath = "https://$($global:DefaultVIServer.Id.Split('@')[1].split(':')[0])/folder/$($dsFolder)/$($vmdkName)?dcPath=$($ds.Datacenter.Name)&dsName=$($ds.Name)"

$si = Get-View serviceInstance

$vmdkMgr = Get-View -Id $si.Content.VirtualDiskManager

$spec = New-Object VMware.Vim.FileBackedVirtualDiskSpec

$spec.AdapterType = [VMware.Vim.VirtualDiskAdapterType]::lsiLogic

$spec.DiskType = [VMware.Vim.VirtualDiskType]::thin

$spec.CapacityKb = $vmdkSizeKB

$vmdkMgr.CreateVirtualDisk($vmdkPath,$ds.Datacenter.ExtensionData.MoRef,$spec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You can do something like this.

Note that the folder (MyFolder) on the datastore needs to be there.

$dsName = 'MyDS'

$dsFolder = 'MyFolder'

$vmdkName = 'NewDisk.vmdk'

$vmdkSizeKB = 5 * 1GB / 1KB

$ds = Get-Datastore -Name $dsName

$vmdkPath = "https://$($global:DefaultVIServer.Id.Split('@')[1].split(':')[0])/folder/$($dsFolder)/$($vmdkName)?dcPath=$($ds.Datacenter.Name)&dsName=$($ds.Name)"

$si = Get-View serviceInstance

$vmdkMgr = Get-View -Id $si.Content.VirtualDiskManager

$spec = New-Object VMware.Vim.FileBackedVirtualDiskSpec

$spec.AdapterType = [VMware.Vim.VirtualDiskAdapterType]::lsiLogic

$spec.DiskType = [VMware.Vim.VirtualDiskType]::thin

$spec.CapacityKb = $vmdkSizeKB

$vmdkMgr.CreateVirtualDisk($vmdkPath,$ds.Datacenter.ExtensionData.MoRef,$spec)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MarcBouchard
Enthusiast
Enthusiast
Jump to solution

Thanks Luc, will test this today. Much appreciated!

Reply
0 Kudos
MarcBouchard
Enthusiast
Enthusiast
Jump to solution

Exception calling "CreateVirtualDisk" with "3" argument(s): ""

At line:1 char:1

+ $vmdkMgr.CreateVirtualDisk($vmdkPath,$ds.Datacenter.ExtensionData.MoR ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

It fails, and the error message doesn't help me much.

Reply
0 Kudos
MarcBouchard
Enthusiast
Enthusiast
Jump to solution

Nevermind, it works. I have multiple linked vCenters and I usually connect to all. The $global:DefaultVIServer was a remote one instead of the one where the datastore is hosted.

Thank you very much Luc!

Reply
0 Kudos