问题
I have an issue that has me stumped and I can't figure out how to work around it. Here is my dilemma...I am testing a storage array. For each test, I create a set of volumes and them present them to my ESXi hosts. Each host has 2 to 4 VMs and each VM needs 4 to 8 RDMs attached. I have written a script to attach the RDMs and it works just fine with 4 RDMs. Here is the problem...when mapping 8 RDMs, I need the devices evenly distributed across the virtual SCSI adapters. Ideally I would like the following:
Hard disk 1 on SCSI 0:1
Hard disk 2 on SCSI 0:2
Hard disk 3 on SCSI 1:1
Hard disk 4 on SCSI 1:2
Hard disk 5 on SCSI 2:1
Hard disk 6 on SCSI 2:2
Hard disk 7 on SCSI 3:1
Hard disk 8 on SCSI 3:2
I have tried using the new-harddisk command to create the new devices. For each hard disk I create I increment a flag by 1. This works for putting HD 1 on 0:1. Then if the flag is > 0 it creates the disk then executes the new-scsicontroller command. This puts HD 2, 3 and 4 onto 1:2, 2:2 and 3:2 respectively. Not exactly what I want by I can live with it. The remaining 4 HDs, 5-8 end up on bus 0 since the max new controllers I can add is 4. Here's my code:
$devicePointer = 0
foreach($vmhost in $vmhosts){
$localVMs = Get-VM -location $vmhost -name $vmNamingPattern | Sort-Object name
$devices = Get-ScsiLun -VmHost $vmhost -CanonicalName $deviceNamePattern | Sort-Object id
foreach ($localVM in $localVMs){
$flag = 0
for($i=$devicePointer;$i -lt $devicePointer+$devicesPerVM; $i++){
$disk = New-HardDisk -VM $localVM -DeviceName $devices[$i].ConsoleDeviceName -DiskType RawPhysical
if ($flag -ne 0) {
New-ScsiController -HardDisk $disk -BusSharingMode NoSharing -Type ParaVirtual
}
$flag+=1
write-host $localVM $disk
}
$devicePointer = $devicePointer+$devicesPerVM
}
}
I tried another methodology of only adding a new scsi controller for the flag value of 1, 2 or 3. Then I ran the following:
Get-HardDisk -VM $localVM | Where {$_.Name -eq "Hard disk 7" | Set-HardDisk -Controller "SCSI controller 1" -Confirm:$false
This resulted in the unit number incrementing by 1. If it was on 0:7 it would move to 0:8 or 2:0 or whatever. Basically it was completely contrary to the value that was specified.
回答1:
$consdevnam = "/vmfs/devices/disks/"
$devicePointer = 0
foreach ($global:Vmhost in $global:my_VM_hosts){
foreach ($localVM in $global:my_VMs){
$global:scsi_ctrl = $null
$global:scsi_ctrl = @()
$flag = 0
for($i=$devicePointer;$i -lt $devicePointer+$devicesPerVM; $i++){
if ($flag -eq 0 -or ($flag % 4) -eq 0){
$disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -DiskType RawPhysical -WarningAction SilentlyContinue
}
if ($flag -ne 0 -and $flag -lt 4) {
$disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -DiskType RawPhysical -WarningAction SilentlyContinue
$global:scsi_ctrl += New-ScsiController -HardDisk $disk -BusSharingMode NoSharing -Type ParaVirtual -WarningAction SilentlyContinue
}
elseif ($flag -ne 0 -and ($flag % 4) -lt 4) {
$f = ($flag % 4)
if ($f -eq 1){
$disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -Controller ($global:scsi_ctrl[0]).name -DiskType RawPhysical –Confirm:$false -WarningAction SilentlyContinue
}
if ($f -eq 2){
$disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -Controller ($global:scsi_ctrl[1]).name -DiskType RawPhysical –Confirm:$false -WarningAction SilentlyContinue
}
if ($f -eq 3){
$disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -Controller ($global:scsi_ctrl[2]).name -DiskType RawPhysical –Confirm:$false -WarningAction SilentlyContinue
}
}
$flag+=1
write-host "Added $disk to $localVM"
}
$devicePointer = $devicePointer+$devicesPerVM
}
}
来源:https://stackoverflow.com/questions/35196740/distributing-rdms-across-scsi-controllers-when-adding-hard-disks-via-powercli