VMware Cloud Community
bvdkolk
Contributor
Contributor
Jump to solution

create new-datastore with specified capacity

Hi all,

I'm doing some research about how to automate the creation of vmfs datastores and specify a certain (custom) size for new datastores.

Some 2 TiB LUN's are presented to my ESX hosts and via the vSphere (Web) Client i'm able to specify a custom space setting for example 1500 GB so 548 GB is still unallocated and (available) for future use on the datastore.

Can this be done via the new-datastore cmdlet? I haven't found out yet..

The command i've used is..

PowerCLI C:\> new-datastore -vmhost esx001.domain.local -name MyDatastore001 -path naa.60001450000000349342ad3282342343 -Vmfs

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

`Try something like this

It will create a VMFS datastore of around 66GB on the LUN defined by the canonicalname.

Note that due to overhead, there will not be exactly the amount of space specified available.

$esxName = 'MyEsx'

$canonicalName = 'naa.60050768018073a21800000000000123'

$sizeGB = 66

$dsName = 'TestDS'

$esx = Get-View -ViewType HostSystem -Filter @{'Name'=$esxName}

$hdSys = Get-View -Id $esx.ConfigManager.DatastoreSystem

$disk = $hdSys.QueryAvailableDisksForVmfs($null) | where{$_.CanonicalName -eq $canonicalName}

$opt = $hdSys.QueryVmfsDatastoreCreateOptions($disk.DevicePath,$null)

$sectorTotal = $sizeGB * 1GB / 512

$spec = New-Object VMware.Vim.VmfsDatastoreCreateSpec

$spec.DiskUuid = $disk.Uuid

$spec.Partition = $opt[0].Spec.Partition

$spec.Partition.Partition[0].EndSector = $spec.Partition.Partition[0].StartSector + $sectorTotal

$spec.Partition.TotalSectors = $sectorTotal

$spec.Vmfs = New-Object VMware.Vim.HostVmfsSpec

$spec.Vmfs.VolumeName = $dsName

$ext = New-Object VMware.Vim.HostScsiDiskPartition

$ext.DiskName = $canonicalName

$ext.Partition = $opt[0].Info.Layout.Partition[0].Partition

$spec.Vmfs.Extent = $ext

$spec.vmfs.MajorVersion = $opt[0].Spec.Vmfs.MajorVersion

$hdSys.CreateVmfsDatastore($spec)


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

No, the New-Datastore cmdlet doesn't offer that functionality.

You will have to fall back to a vSphere method, in this case the CreateVmfsDatastore method.

In the VmfsDatastoreCreateSpec you specify under the Partition property, how many blocks shall be used.

You can have a look how it is done via Onyx, or else let me know and I can give you some sample code.


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

0 Kudos
bvdkolk
Contributor
Contributor
Jump to solution

Hi Luc,

It would be nice if you can get help me out and provide some code to get me started..

Or are there any examples the book you've written "vsphere powercli reference"?? I purchased it some time ago..

Ps. My short-term goal is to automate some processes and the final goal is to re-write the steps in python.

Thanks anyway for your help! 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

`Try something like this

It will create a VMFS datastore of around 66GB on the LUN defined by the canonicalname.

Note that due to overhead, there will not be exactly the amount of space specified available.

$esxName = 'MyEsx'

$canonicalName = 'naa.60050768018073a21800000000000123'

$sizeGB = 66

$dsName = 'TestDS'

$esx = Get-View -ViewType HostSystem -Filter @{'Name'=$esxName}

$hdSys = Get-View -Id $esx.ConfigManager.DatastoreSystem

$disk = $hdSys.QueryAvailableDisksForVmfs($null) | where{$_.CanonicalName -eq $canonicalName}

$opt = $hdSys.QueryVmfsDatastoreCreateOptions($disk.DevicePath,$null)

$sectorTotal = $sizeGB * 1GB / 512

$spec = New-Object VMware.Vim.VmfsDatastoreCreateSpec

$spec.DiskUuid = $disk.Uuid

$spec.Partition = $opt[0].Spec.Partition

$spec.Partition.Partition[0].EndSector = $spec.Partition.Partition[0].StartSector + $sectorTotal

$spec.Partition.TotalSectors = $sectorTotal

$spec.Vmfs = New-Object VMware.Vim.HostVmfsSpec

$spec.Vmfs.VolumeName = $dsName

$ext = New-Object VMware.Vim.HostScsiDiskPartition

$ext.DiskName = $canonicalName

$ext.Partition = $opt[0].Info.Layout.Partition[0].Partition

$spec.Vmfs.Extent = $ext

$spec.vmfs.MajorVersion = $opt[0].Spec.Vmfs.MajorVersion

$hdSys.CreateVmfsDatastore($spec)


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

0 Kudos
bvdkolk
Contributor
Contributor
Jump to solution

Hi Luc,

Many thanks for the sample code. Great job!

0 Kudos