RobertAnders
Contributor
Contributor

New-Datastore with max capacity

Hi,

I try to write a script to create a new VMFS Datastore for our environment. By default I map all our LUN's from Datacore with 2 TB LUN size and I create VMFS with 0,5 TB on it. With that I'm sure, for example a snapshot, cannot grow to the full 2 TB. But if I need more capacity I can easy increase the VMFS.

Now the problem: The "New-Datastore" cmdlet has no option to define the capacity of the VMFS like in vSphere Client (see screenshot). Is there another way to define that?

Thx for your ideas!

Robert

Tags (2)
0 Kudos
6 Replies
LucD
Leadership
Leadership

Yes, with the CreateVmfsDatastore method you can specify how many sectors (TotalSectors property) from each extent should be used for the datastore.


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

Grzesiekk
Expert
Expert

Hi there,

just blind guess, maybe you could use the  CreateVmfsDatastore method in order to put the size .

http://pubs.vmware.com/vsphere-50/topic/com.vmware.wssdk.apiref.doc_50/vim.host.VmfsDatastoreCreateS...

I haven't tried this before. But what you could do is to download Onyx from vmware

http://communities.vmware.com/community/vmtn/server/vsphere/automationtools/onyx

and then create a datastore for 50 GB for example, and see the output of this action , if you would do this you could paste the output here and i could try to help you from there.

Greg

--- @blog https://grzegorzkulikowski.info
RobertAnders
Contributor
Contributor

Hi,

thanks a lot for show me this option. But I never worked with these "Views", so I'm stuck a bit. I found this script, where I got "the big picture" how this work. And now I have the following "beta"-script:

$esxihost = Get-VMHost -Name "hostname"
$lunid = "17"
$VmfsName = "017VMFS"
$hostview = $esxihost | Get-View
$storMgr = Get-View $hostview.ConfigManager.DatastoreSystem
# Search for LUN with given LUN ID
$searchterm = ":L" + $lunid + "$"
$targetLun = Get-ScsiLun -VMHost $esxihost | Where-Object {$_.RuntimeName -Match $searchterm}
# Search for Disk with same Canonical Name of LUN (because I didn't find in $storMgr.QueryAvailableDisksForVmfs($null) a property with LunID)
$targetDisk = $storMgr.QueryAvailableDisksForVmfs($null) | Where-Object {$_.CanonicalName -eq $targetLun.CanonicalName}
$dsOpt = $storMgr.QueryVmfsDatastoreCreateOptions($targetDisk.DeviceName,$null)
$spec = $dsOpt.Spec
$spec.Vmfs.VolumeName = $VmfsName
$spec.Vmfs.BlockSizeMb = 8
# $spec.Partition.TotalSectors was "4261412864", the LUN has 2032 GB -> 4261412864 / 2032 * 512 = 1073741824
$spec.Partition.TotalSectors = 1073741824
$spec.Extent = $spec.Vmfs.Extent
$storMgr.CreateVmfsDatastore($spec)

My Problem: The VMFS created by this script is takes also the hole capacity of the volume and not only 512 GB. I'm afraid that I not really got it, how to create the spec.

Can anyone give me a tip, what I do wrong?

Thanks a lot.

0 Kudos
Grzesiekk
Expert
Expert

Hi there,

  i will put it this way. I have done it, but i totally spent too less time on this to fully understand it. I think Luc is the person here who can explain in further detail if needed. What i basically did is that i have modified his script in the way as he told in his first post replay so here it is:

http://www.lucd.info/2010/06/26/create-vmfs-datastores-on-a-free-space-partition/

function Get-ScsiFreePartition{
param (
[parameter(ValueFromPipeline = $true,Position=1)]
[ValidateNotNullOrEmpty()]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]
$VMHost
)

process{
$esx = $VMHost | Get-View
$storMgr = Get-View $esx.ConfigManager.DatastoreSystem
$storSys = Get-View $esx.ConfigManager.StorageSystem

$lunExt = $storMgr.QueryAvailableDisksForVmfs($null)
foreach($lun in $lunExt){
$info = $storMgr.QueryVmfsDatastoreCreateOptions($lun.DevicePath,$null)
foreach($dsOpt in $info){
$info2 = $storSys.ComputeDiskPartitionInfo($lun.DevicePath,$dsOpt.Info.Layout,$null)
$info2.Layout.Partition | where {$_.Type -eq "vmfs"} | %{
New-Object PSObject -Property @{
DeviceName = $lun.DeviceName
DeviceSizeMB = $lun.Capacity.block * $lun.Capacity.BlockSize / 1MB
Partition = $_.Partition
PartitionSizeMB = ($_.End.block - $_.Start.Block) * $_.Start.BlockSize / 1MB
FullDisk = &{if($_.Partition -eq 1){$true}else{$false}}
}
}
}
}
}
}

function New-PartitionDatastore{
param (
[parameter(ValueFromPipeline = $true,Position=1)]
[ValidateNotNullOrEmpty()]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]
$VMHost,
[parameter(Position=2)]
[ValidateNotNullOrEmpty()]
[PSObject]
$Partition,
[parameter(Position=3)]
[ValidateNotNullOrEmpty()]
[String]
$Name
)

process{
$esx = $VMHost | Get-View
$storMgr = Get-View $esx.ConfigManager.DatastoreSystem

$lunExt = $storMgr.QueryAvailableDisksForVmfs($null)
$device = $lunExt | where {$_.DeviceName -eq $Partition.DeviceName}
$dsOpt = $storMgr.QueryVmfsDatastoreCreateOptions($Partition.DeviceName,$null) | where {$_.Info.VmfsExtent.Partition -eq $Partition.Partition}

$spec = $dsOpt.Spec
$spec.Vmfs.VolumeName = $Name
$spec.Extent += $spec.Vmfs.Extent

#calculate sectors that you want
$spec.partition.totalsectors=5242880
$spec.partition
#read-host
$dsMoRef = $storMgr.CreateVmfsDatastore($spec)
Get-Datastore (Get-View $dsMoRef).Name
}
}

And after this like he has described in his post

$partition = get-vmhost 'your_host'| Get-ScsiFreePartition    # i had only 1 free

#23:07:57> get-vmhost | Get-ScsiFreePartition


DeviceSizeMB    : 5120
DeviceName      : /vmfs/devices/disks/mpx.vmhba1:C0:T1:L0
Partition       : 1
PartitionSizeMB : 5106.59912109375
FullDisk        : True

and after this

get-vmhost 'your_host'| New-PartitionDatastore -Partition $partition -Name "MyDS"

Since you have hardcoded the totalsectors number, the size will be applied regarding that number.

I wish i had more time to really understand this process, give it a try in lab maybe first as i don't; want to be responsible for crashing or someting like that Smiley Wink i did it in my lab and this has worked.

Greg

Btw, i got confused now. That does not matter if you would create a ds using the script or vsphere client.

I have added 5 gb disk to esxi 5. Then created ds on 2 gb from that. Since this moment, i can not add more to this although it shows that i got 5 gb and 2 gb are used. No clue 😕

--- @blog https://grzegorzkulikowski.info
0 Kudos
RobertAnders
Contributor
Contributor

Hi Greg,

thanks for your help, but it doesn't work for me. Perhaps it's because I'm still on vSphere v4.1? That's also the reason I cannot try Onyx. I took your tested Script:

function Get-ScsiFreePartition{
param (
[parameter(ValueFromPipeline = $true,Position=1)]
[ValidateNotNullOrEmpty()]
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]
$VMHost
)
process{
$esx = $VMHost | Get-View
$storMgr = Get-View $esx.ConfigManager.DatastoreSystem
$storSys = Get-View $esx.ConfigManager.StorageSystem
$lunExt = $storMgr.QueryAvailableDisksForVmfs($null)
foreach($lun in $lunExt){
$info = $storMgr.QueryVmfsDatastoreCreateOptions($lun.DevicePath,$null)
foreach($dsOpt in $info){
$info2 = $storSys.ComputeDiskPartitionInfo($lun.DevicePath,$dsOpt.Info.Layout,$null)
$info2.Layout.Partition | where {$_.Type -eq "vmfs"} | %{
New-Object PSObject -Property @{
DeviceName = $lun.DeviceName
CanonicalName = $lun.CanonicalName
DeviceSizeMB = $lun.Capacity.block * $lun.Capacity.BlockSize / 1MB
Partition = $_.Partition
PartitionSizeMB = ($_.End.block - $_.Start.Block) * $_.Start.BlockSize / 1MB
FullDisk = &{if($_.Partition -eq 1){$true}else{$false}}
}
}
}
}
}
}
function New-PartitionDatastore{ param ( [parameter(ValueFromPipeline = $true,Position=1)] [ValidateNotNullOrEmpty()] [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl] $VMHost, [parameter(Position=2)] [ValidateNotNullOrEmpty()] [PSObject] $Partition, [parameter(Position=3)] [ValidateNotNullOrEmpty()] [String] $Name ) process{ $esx = $VMHost | Get-View $storMgr = Get-View $esx.ConfigManager.DatastoreSystem $lunExt = $storMgr.QueryAvailableDisksForVmfs($null) $device = $lunExt | where {$_.DeviceName -eq $Partition.DeviceName} $dsOpt = $storMgr.QueryVmfsDatastoreCreateOptions($Partition.DeviceName,$null) | where {$_.Info.VmfsExtent.Partition -eq $Partition.Partition} $spec = $dsOpt.Spec $spec.Vmfs.VolumeName = $Name $spec.Extent += $spec.Vmfs.Extent #calculate sectors that you want $spec.partition.totalsectors=1073741824 $spec.partition #read-host $dsMoRef = $storMgr.CreateVmfsDatastore($spec) Get-Datastore (Get-View $dsMoRef).Name } }

The only thing I changed was to TotalSectors to get 512 GB. First I call the Get-ScsiFreePartition.

$x = Get-ScsiFreePartition(Get-VMHost -Name "hostname")

And then (because I have at the moment serveral LUNs available) I call New-PartitionDatastore with a manual selected partition.

Get-VMHost -Name "hostname" | New-PartitionDatastore -Partition $x[9] -Name "VMFS9"

Like you can see in the output the partition size is correct. But the Datastore size is again the full available 2032 GB (aka 2080512 MB).

PartitionFormat : Chs             : VMware.Vim.HostDiskDimensionsChs TotalSectors    : 1073741824 Partition       : {} DynamicType     : DynamicProperty : FileSystemVersion              : 3.46 DatacenterId                   : Datacenter-datacenter-2 Datacenter                     : yyyyy ParentFolderId                 : Folder-group-s8580 ParentFolder                   : datastore DatastoreBrowserPath           : vmstores:\xxxxxxxxxx@443\yyyyy\VMFS9 FreeSpaceMB                    : 2079941 CapacityMB                     : 2080512 Accessible                     : True Type                           : VMFS StorageIOControlEnabled        : False CongestionThresholdMillisecond : 30 Name                           : VMF9 ExtensionData                  : VMware.Vim.Datastore CapacityGB                     : 2031,1923828125 FreeSpaceGB                    : 2031,75 Id                             : Datastore-datastore-12484 Uid                            : /VIServer=@xxxxxxxxxx:443/Datastore=Datastore-datastore-12484/ Client                         : VMware.VimAutomation.ViCore.Impl.V1.VimClient

Anyone an idea, what's wrong here?

Robert

0 Kudos
RobertAnders
Contributor
Contributor

Hi,

I installed two lab ESXi, one with v4.1U2 and one with v5.1, both with one 8GB disk. I run on both the script to create a 2GB VMFS. On the v4.1 ESXi the VMFS took the hole disk size of 8GB. On the v5.1 ESXi the VMFS took the given size of 2GB. It seems, that this solution only work on v5.x. Is that correct or is there another way for v4.1?

Regards!

Robert

0 Kudos