Mozhartatti
Contributor
Contributor

Attaching existing disks to VM with specific settings

Good day,

I am having trouble comprehending how to add 16 hard disks to two VMs with PowerCLI. These are existing hard disks shared between these two VMs.

I need to add them with the following settings:

VM storage policy: Datastore Default
Type: Thick Provision Eager Zeroed
Sharing: Multi-writer
Disk File: <existing vmdk>
Shares: Default (Normal > 1000)
Disk Mode: Independent - Persistent
Virtual Device Node: Specific SCSI controller (I have multiple SCSI controllers)

I went over this link and as far as I understand I need to create a few new objects?
$disk = New-Object VMware.Vim.VirtualDisk
$back = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

Thanks

0 Kudos
8 Replies
LucD
Leadership
Leadership

Is there a question in here?

Btw, that link you point to is for adding new disks, not adding existing disks


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

Mozhartatti
Contributor
Contributor

My apologies! This is what I get to trying to multitask.

Yes, it is a question on how to add existing disks to a VM with specified hard disk settings. I was using the link as a potential reference that could be useful for my case. I am not very seasoned with PowerCLI so forgive me for miscommunication.

0 Kudos
Mozhartatti
Contributor
Contributor

Hi @LucD . I have narrowed my question a bit. I am wondering how to set a Multiwritter flag for the disk I am attaching.

Looking at this thread, can I do something like this?

 

$vm = Get-VM "myVM"

$vm | New-HardDisk -DiskPath "[Datastore] VM/DISK01_3.vmdk" -Persistence IndependentPersistent -Controller "SCSI controller 2" -OutVariable $hd

$ctrl = Get-ScsiController -HardDisk $hd
Try {
    Get-AdvancedSetting -Entity $vm -Name "scsi$($ctrl.ExtensionData.BusNumber):$($hd.ExtensionData.UnitNumber).sharing" -ErrorAction Stop |
    Set-AdvancedSetting -Value "multi-writer"
}
Catch {
    New-AdvancedSetting -Entity $vm -Name "scsi$($ctrl.ExtensionData.BusNumber):$($hd.ExtensionData.UnitNumber).sharing"
}

 

0 Kudos
Mozhartatti
Contributor
Contributor

I looked at another system, as a reference, that has multi-writer preset for its sharing option for multiple drives but I do not see anything like scsi.sharing listed so perhaps my approach is flawed:

$vm | Get-AdvancedSetting | Select-String -Pattern scsi

scsi0.pciSlotNumber:160
scsi0.sasWWID:50 ...
scsi1.pciSlotNumber:256
scsi2.pciSlotNumber:1184
scsi3.pciSlotNumber:1216
scsi1.sasWWID:50 ...
scsi2.sasWWID:50 ...
scsi3.sasWWID:50 ...
scsi0:1.redo:
scsi0:0.redo:

...

I use ESXi/vCenter 7

0 Kudos
Mozhartatti
Contributor
Contributor

Seems like it is a version issue. Was able to find the answer as to why scsi.sharing isn't showing, in your other post 

Still, would very much appreciate your assistance with setting Multiwriter flag on version 7 (if different for 6.x)🙂

0 Kudos
Mozhartatti
Contributor
Contributor

I have managed to achieve what I wanted (referencing this code) but not sure if this is the correct way of doing this on version 7. Any feedback?:

$vm = Get-VM "MyVM"

$vm | New-HardDisk -DiskPath "[Datastore] VM/DISK01_3.vmdk" `
-Persistence IndependentPersistent `
-Controller "SCSI controller 2" `
-OutVariable hd

$diskDevice = $hd.ExtensionData
$diskDeviceBaking = $hd.ExtensionData.backing

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.deviceChange[0].operation = 'edit'
$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
$spec.deviceChange[0].device = $diskDevice
$spec.DeviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
$spec.DeviceChange[0].device.backing = $diskDeviceBaking
$spec.DeviceChange[0].device.Backing.Sharing = "sharingMultiWriter"


Write-Host "`nEnabling Multiwriter flag on on VMDK:" $hd.Name "for VM:" $vm.name
$task = $vm.ExtensionData.ReconfigVM_Task($spec)
$task1 = Get-Task -Id ("Task-$($task.value)")
$task1 | Wait-Task

0 Kudos
Mozhartatti
Contributor
Contributor

Here is what I come up with to add multiple hard disks to a VM.

settings.csv contains:
vmdk,scsi
[datastore] VM/hd1.vmdk,2
[datastore] VM/hd2.vmdk,3
...

 

$vm = Get-VM "MyVM"
$items = Import-Csv c:\settings.csv

$items | ForEach-Object {
    $vmdk = $_.vmdk
    $scsi_controller = $_.scsi

    $vm | New-HardDisk -DiskPath "$vmdk" `
                       -Persistence IndependentPersistent `
                       -Controller "SCSI controller $($scsi_controller)" `
                       -OutVariable hd

    $diskDevice = $hd.ExtensionData
    $diskDeviceBaking = $hd.ExtensionData.backing

    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
    $spec.deviceChange[0].operation = 'edit'
    $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
    $spec.deviceChange[0].device = $diskDevice
    $spec.DeviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
    $spec.DeviceChange[0].device.backing = $diskDeviceBaking
    $spec.DeviceChange[0].device.Backing.Sharing = "sharingMultiWriter"

    Write-Host "`nEnabling Multiwriter flag on on VMDK:" $hd.Name "for VM:" $vm.name
    $task = $vm.ExtensionData.ReconfigVM_Task($spec)
    $task1 = Get-Task -Id ("Task-$($task.value)")
    $task1 | Wait-Task
}

 

 

This edits a VM twice per hard disk: first time when a hard disk is attached and a second time when "multiwriter" is set. Can anyone please help me minimize the number of calls this does?

0 Kudos
Mozhartatti
Contributor
Contributor

@LucD Hello.

Any suggestions on how to improve my script and perhaps consolidate the calls made to a VM?

Thank you,

0 Kudos