Jump to content
Welcome to our new Citrix community!
  • 0

Where are the VM's stored?

NIcklas Ryden1709161360




I want to keep track in where the VM's are stored, to count them so that I will get a even distribution across al SR.


This returns storage utilization. 

Get-XenSR | where {$_.type -eq "gfs2"} | select name_label, type, physical_size, physical_utilisation, virtual_allocation | ft



Getting the number of VM's on each store?

or where are each VM stored?


How to?


Kind Regards


Link to comment

5 answers to this question

Recommended Posts

  • 0

Hi Tobias,


Sure, i'm not uses to shell-scripting, but i've made a few :)


anyway... after a few hours of google, and own code i came up with this:

Connect-XenServer -Url https://xxx.xxx.xxx.xxx  

#get all pdbs linked pointing to iscsi datastores
$colpbd = Get-Xenpbd | where {$_.device_config.provider -like "iscsi"}
$reports = @()
foreach ($pbd in $colpbd){
	#get all SRs pointing to iscsi pbds
	$colsr = Get-XenSR -opaque_ref $pbd.SR.opaque_ref
	foreach ($sr in $colsr) {
		#get all VDIs on SR
		$colvdi = Get-XenVDI | where { $_.SR.opaque_ref -like $sr.opaque_ref}
		foreach ($vdi in $colvdi){
            #get vbd
            $colvbd = Get-XenVBD | where { $_.VDI.opaque_ref -like $vdi.opaque_ref}
            foreach ($vbd in $colvbd){
                #Show VM VDI Path
                $vm = Get-XenVM -Ref $vbd.vm.opaque_ref
     $report = "" | Select vm_name_label, power_state, vdi_name_label, sr_name_label
     $report.vm_name_label            = $vm.name_label
     $report.power_state              = $vm.power_state
     $report.vdi_name_label           = $vdi.name_label
     $report.sr_name_label            = $sr.name_label
     $reports += $report

#sort by  vm_name_label
$reportall = $reports | sort  vm_name_label

#create a list with unique vm_name_label for counting.
$reports = $reports | sort vm_name_label | select -Unique vm_name_label,power_state, vdi_name_label, sr_name_label

$sr_counts = @()
$sr_uniqe = ($reports.sr_name_label | select -Unique | sort )
foreach ($sr_name_label in $sr_uniqe) {

$sr_count = "" | Select sr_name_label, count
$sr_count.sr_name_label    = $sr_name_label
$sr_count.count            = ($reports.sr_name_label | where {$_ -like $sr_name_label}).count
$sr_counts += $sr_count


#write all vm's and disk to screen

#write all sr and the number of vm's of each to screen



The result is fine for me, I get the labels of each SR and the number of VM's on each.


Kind regards.



Link to comment
  • 0

I was able to solve my powershell-script need very well:


Connect-XenServer -Url https://xxx.xxx.xxx.xxx
$LUNs = Get-XenSR | Where-Object {$_.type -ne "iso" -and $_.shared -eq "true" } | Sort-Object name_label
            $StorageInfo = @()
            foreach ($LUN in $LUNs) {
                $LUNInfo = New-Object psobject
                $LUNInfo | Add-Member -MemberType NoteProperty -Name "Name" -Value $LUN.name_label
                $LUNInfo | Add-Member -MemberType NoteProperty -Name "virtual_allocation" -Value ([math]::round($LUN.virtual_allocation / 1Gb, 0))
                $LUNInfo | Add-Member -MemberType NoteProperty -Name "physical_utilisation" -Value ([math]::round($LUN.physical_utilisation / 1Gb, 0))
                $LUNInfo | Add-Member -MemberType NoteProperty -Name "physical_size" -Value ([math]::round($LUN.physical_size / 1Gb, 0))
                $LUNInfo | Add-Member -MemberType NoteProperty -Name "percent_full" -Value ($LUN.physical_utilisation / $LUN.physical_size).tostring("P")
                $LUNInfo | Add-Member -MemberType NoteProperty -Name "vm_count" -Value (($LUN | Select-Object -ExpandProperty VDIs | Get-XenVDI | Select-Object -ExpandProperty VBDs | Get-XenVBD | Where-Object {$_.type -eq "Disk"} | Select-Object -ExpandProperty VM | Get-XenVM | Where-Object {$_.is_a_template -eq $False -and $_.is_a_snapshot -eq $False -and $_.domid -ne 0} | Select-Object name_label -Unique).name_label).count
                $LUNInfo | Add-Member -MemberType NoteProperty -Name "type" -Value ($LUN.type)
                $StorageInfo += $LUNInfo

#Skriver ut hur det ser ut i storage
$StorageInfo | Out-GridView


The thing that solved counting the number if VM's per SR was:

(($LUN | Select-Object -ExpandProperty VDIs | Get-XenVDI | Select-Object -ExpandProperty VBDs | Get-XenVBD | Where-Object {$_.type -eq "Disk"} | Select-Object -ExpandProperty VM | Get-XenVM | Where-Object {$_.is_a_template -eq $False -and $_.is_a_snapshot -eq $False -and $_.domid -ne 0} | Select-Object name_label -Unique).name_label).count



(via the $LUN | first expand the VDIs | Get the VDI | Expand the VDBs | Get the VDB | Expand the VM's | Get the VMs (that were not template or dom0 | select the unique name_labels of the VM's) and count them.


With this report i will be able to keep track of my team-members not putting to many VM's on each iscsi-gfs2-disk.



Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...