VMware vSphere

 View Only
Expand all | Collapse all

SCript for converting RDM from Physical Mode to Virtual

  • 1.  SCript for converting RDM from Physical Mode to Virtual

    Posted Feb 27, 2025 04:06 PM

    We are working on a project to convert all RDMs on VMs to VMDK.

    The first step is to convert the RDM from Physical mode to Virtual mode so that we can then use storage vmotion to convert to VMDK.

    I have a script that I can run against a list of VM that finds all physical mode RDMs attached to a VM and then one by one removes them and adds them back in in Virtual mode.

    The script work well except that now the Hard Disk numbers are all changed. This is fine for the OS running on those VM but is causing problems for VM backups as disk exclusion is based on Hard Disk number.

    Here is the script

    =====================================================

    foreach ( $row in Import-Csv -Path $vmlistfile -UseCulture )
    {
        $VM = Get-VM -Name $row.vmName
        $VmName = $row.vmName
        

        foreach($hd in (Get-HardDisk -VM $VM -DiskType RawPhysical))
        {

            $scsicontroller = Get-ScsiController -HardDisk $hd
            Remove-HardDisk -HardDisk $hd -Confirm:$false

            $lun = Get-ScsiLun -VmHost $VM.VMHost | where{$_.CanonicalName -match $hd.ExtensionData.Backing.LunUuid.substring(10,32)}

            New-HardDisk -VM $VM -DiskType RawVirtual -DeviceName $lun.ConsoleDeviceName -Controller $scsicontroller -Persistence IndependentPersistent

        }

    ==========================================================================

    I would like to preserve the Hard Disk order after the script. One thought I had was to remove and re add all of the virtual disk to ensure the order is kept.

    Unless there is another option that anyone can suggest

    Regards



  • 2.  RE: SCript for converting RDM from Physical Mode to Virtual

    Posted Mar 12, 2025 12:11 AM

    Hi

    Just to follow up I was able to achieve this by removing and adding all of the virtual disks in order, including the VMDK based virtual disk.

    I did have to change the commands for removing the disk as the original way was removing the virtual SCSI controller on occasion.

    An issue I am having now is that after I remove the physical mode RDM and add it back in virtual mode the disk will appear as offline in windows. The disk can be easily brought online in Windows and the VM restarted again to fix it but I would rather not have to do this, especially as we have some VMs with large number of RDMs. Does anyone know of a fix for this?

    Here is my new code

    ============================

    foreach($hd in (Get-HardDisk -VM $VM ))
        {

            if ( $hd.DiskType -eq "RawPhysical" )
            {
                $scsicontroller = Get-ScsiController -HardDisk $hd
                $lun = Get-ScsiLun -VmHost $VM.VMHost | where{$_.CanonicalName -match $hd.ExtensionData.Backing.LunUuid.substring(10,32)}

                $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
                $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
                $dev.Device = $hd.ExtensionData
                $dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove
                $spec.DeviceChange += $dev
                $VM.ExtensionData.ReconfigVM($spec)

              

                # Remove-HardDisk -HardDisk $hd -Confirm:$false

                

                New-HardDisk -VM $VM -DiskType RawVirtual -DeviceName $lun.ConsoleDeviceName -Controller $scsicontroller -Persistence IndependentPersistent
                
                
            }

            if ( $hd.DiskType -eq "RawVirtual" )
            {
                $scsicontroller = Get-ScsiController -HardDisk $hd
                $lun = Get-ScsiLun -VmHost $VM.VMHost | where{$_.CanonicalName -match $hd.ExtensionData.Backing.LunUuid.substring(10,32)}

                $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
                $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
                $dev.Device = $hd.ExtensionData
                $dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove
                $spec.DeviceChange += $dev
                $VM.ExtensionData.ReconfigVM($spec)

                # Remove-HardDisk -HardDisk $hd -Confirm:$false

                

                New-HardDisk -VM $VM -DiskType RawVirtual -DeviceName $lun.ConsoleDeviceName -Controller $scsicontroller -Persistence IndependentPersistent
                
                
            }

            if ( $hd.DiskType -eq "Flat" )
            {
                $scsicontroller = Get-ScsiController -HardDisk $hd
                $filename = $hd.Filename

                $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
                $dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
                $dev.Device = $hd.ExtensionData
                $dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::remove
                $spec.DeviceChange += $dev
                $VM.ExtensionData.ReconfigVM($spec)
               
                # Remove-HardDisk -HardDisk $hd -Confirm:$false

               
              
                New-HardDisk -VM $VM -DiskPath $filename -Controller $scsicontroller
                
                
            }