Skip navigation
VMware

This Question is Answered (go to answer)

1 "helpful" answer available (6 pts)
2,755 Views 8 Replies Last post: Mar 8, 2011 7:59 AM by Marcel00M RSS
erickmiller Enthusiast 116 posts since
Sep 29, 2003
Currently Being Moderated

Apr 30, 2008 9:04 PM

Adding an existing hard disk

 

Hi,

 

Is there a way to use the toolkit to add an existing hard disk file to an existing virtual machine?  On the opposite end, is there a way to remove a hard disk from a powered-down VM with either deleting or not-deleting the disk files using the toolkit?

 

Thanks!

 

Eric K. Miller, Genesis Hosting Solutions, LLC

http://www.genesishosting.com/ - Lease part of our ESX cluster!

kjb007 Guru vExpert 6,682 posts since
Sep 18, 2006
Currently Being Moderated
1. May 1, 2008 7:51 AM in response to: erickmiller
Re: Adding an existing hard disk

 

There isn't an easy answer here for you, I'm afraid.  The get-harddisk allow you to get a disk object, but it has to exist on a vm, template, or a snapshot.  The new-harddisk allows you to create a new disk and add it to a vm, but again, not an existing that I can see.  You will have to get deeply involved with views, and I'm sure there are others on this forum who can provide the requisite direction for you.  It will depend on how comfortable you are with scripting, to get an involved answer, or  were you looking for a quick answer?

 

 

 

 

 

-KjB

 

 

vExpert/VCP/VCAP vmwise.com / @vmwise -KjB
LucD Guru User Moderators vExpert 8,994 posts since
Oct 31, 2005
Currently Being Moderated
3. May 1, 2008 1:09 PM in response to: erickmiller
Re: Adding an existing hard disk

From our VI Toolkit snippet-vault.

The following 2 functions do probably most of what you want.

 

Function add-HD uses an existing .vmdk file to create a hard disk on a VM.

The calling syntax: add-HD <VM-name> <datastore-name> <path-to-vmdk> <controller-label>

 

An example: add-HD "PC4" "vmfs2" "PC4/PC4.vmdk" "SCSI Controller 0"

 

Function remove-HD removes a hard disk from a VM and optionally deletes the .vmdk file.

The calling syntax: remove-HD <VM-name> <hard-disk-label> <delete-flag>

 

An example: remove-HD "PC4" "Hard Disk 3" $TRUE

 

The code:

 

Get-VIServer -Server <VC-server>

[http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("vmware.vim")

function remove-HD {
  param($VMname, $HDname, $Delflag)
  
  $vm = Get-View (Get-VM $VMname).ID

  foreach($dev in $vm.Config.Hardware.Device){
    if ($dev.DeviceInfo.Label -eq $HDname){
      $key = $dev.Key
       $name = $dev.Backing.FileName
    }
  }

  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  $spec.deviceChange = @()
  $spec.deviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec
  $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDevice
  $spec.deviceChange[0].device.key = $key
  $spec.deviceChange[0].operation = "remove"

  $vm.ReconfigVM_Task($spec)

  if ($Delflag){
    $svcRef = new-object VMware.Vim.ManagedObjectReference
    $svcRef.Type = "ServiceInstance"
    $svcRef.Value = "ServiceInstance"
    $serviceInstance = get-view $svcRef

    $fileMgr = Get-View $serviceInstance.Content.fileManager

    $datacenter = (Get-View (Get-VM $VMname | Get-Datacenter).ID).get_MoRef()
     
    $fileMgr.DeleteDatastoreFile_Task($name, $datacenter)
  }
}

function add-HD {
  param($VMname, $DSname, $Filename, $SCSIcntrl)

  $vm = Get-View (Get-VM $VMname).ID
  $ds = Get-View (Get-Datastore -Name $DSname).ID
  foreach($dev in $vm.config.hardware.device){
    if ($dev.deviceInfo.label -eq $SCSIcntrl){
       $CntrlKey = $dev.key
     }
  }
  $Unitnumber = 0
  $DevKey = 0
  foreach($dev in $vm.config.hardware.device){
    if ($dev.controllerKey -eq $CntrlKey){
       if ($dev.Unitnumber -gt $Unitnumber){$Unitnumber = $dev.Unitnumber}
       if ($dev.key -gt $DevKey) {$DevKey = $dev.key}
     }
  }

  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  $spec.deviceChange = @()
  $spec.deviceChange += New-Object VMware.Vim.VirtualDeviceConfigSpec

  $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk
  $spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
  $spec.deviceChange[0].device.backing.datastore = $ds.MoRef
  $spec.deviceChange[0].device.backing.fileName = "[" + $DSname + "] " + $Filename
  $spec.deviceChange[0].device.backing.diskMode = "independent_persistent"
  $spec.deviceChange[0].device.key = $DevKey + 1
  $spec.deviceChange[0].device.unitnumber = $Unitnumber + 1
  $spec.deviceChange[0].device.controllerKey = $CntrlKey
  $spec.deviceChange[0].operation = "add"

  $vm.ReconfigVM_Task($spec)
}

 

Since the forum SW apparently has a mind of it's own I have attached the code.

 

Note that the functions can definitely be improved.

Currently the fail-safe mechanisms are quit limited.

 

If there is a need to explain parts of the code please let me know.

 

And of course, use in a production environment at your own risk !

Attachments:
Blog: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
halr9000 Master vExpert 1,124 posts since
Jun 7, 2007
Currently Being Moderated
5. Aug 7, 2008 8:31 AM in response to: erickmiller
Re: Adding an existing hard disk

If I'm not mistaken, the [http://Reflection.Assembly|http://Reflection.Assembly]::LoadWithPartialName("vmware.vim") bit is no longer necessary.

 

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
olan025 Enthusiast 48 posts since
Jan 22, 2007
Currently Being Moderated
6. Mar 6, 2009 8:01 AM in response to: LucD
Re: Adding an existing hard disk

Hey LucD or others...

 

I am looking to get the remove-HD function working with physicalmode RDMs.

 

 

It will remove the .vmdk file, but leaves the rdmp.vmdk file, then error out.

 

 

I can attach screen shot tomorrow when I'm back in the office.

 

 

Hopefully someone here might know the answer on that... I will start looking around the Views to see if there's anything I can see.

 

 

Ryan

 

 

_____________________ Created NEW THREAD __________________

 

 

http://communities.vmware.com//thread/198055?tstart=0

Marcel00M Novice 28 posts since
May 19, 2009
Currently Being Moderated
7. Mar 8, 2011 4:51 AM in response to: LucD
Re: Adding an existing hard disk

Hmmm the remove-HD function leave the -flat.vmkd, which is the one that uses the most storage.

ESX and VC Version is 4.0

 

before:

 

total 7926508
-rw------- 1 root root  4294967808 Nov 17 11:19 ersatz01_1-flat.vmdk
-rw------- 1 root root         528 Nov 17 11:16 ersatz01_1.vmdk
-rw------- 1 root root 16106127360 Nov 17 11:19 ersatz01_2-flat.vmdk
-rw------- 1 root root         530 Nov 17 11:16 ersatz01_2.vmdk

-rw------- 1 root root 12884901888 Nov 17 11:22 ersatz01-flat.vmdk
-rw------- 1 root root        8684 Nov 17 11:22 ersatz01.nvram
-rw------- 1 root root         528 Nov 17 11:16 ersatz01.vmdk
-rw------- 1 root root           0 Nov 17 11:15 ersatz01.vmsd
-rwxr-xr-x 1 root root        2641 Nov 17 11:22 ersatz01.vmx
-rw------- 1 root root         263 Nov 17 11:22 ersatz01.vmxf
-rw-r--r-- 1 root root       98363 Nov 17 11:10 vmware-168.log
-rw-r--r-- 1 root root      113461 Nov 17 11:10 vmware-169.log
-rw-r--r-- 1 root root       91517 Nov 17 11:10 vmware-170.log
-rw-r--r-- 1 root root       91240 Nov 17 11:10 vmware-171.log
-rw-r--r-- 1 root root       91037 Nov 17 11:10 vmware-172.log
-rw-r--r-- 1 root root       90557 Nov 17 11:10 vmware-173.log
-rw-r--r-- 1 root root       86224 Nov 17 11:22 vmware.log

 

after:

 

-rw------- 1 root root  4294967808 Nov 17 11:19 ersatz01_1-flat.vmdk
-rw------- 1 root root         528 Nov 17 11:16 ersatz01_1.vmdk
-rw------- 1 root root 16106127360 Nov 17 11:19 ersatz01_2-flat.vmdk
-rw------- 1 root root 12884901888 Nov 17 11:22 ersatz01-flat.vmdk
-rw------- 1 root root        8684 Nov 17 11:22 ersatz01.nvram
-rw------- 1 root root         528 Nov 17 11:16 ersatz01.vmdk
-rw------- 1 root root           0 Nov 17 11:15 ersatz01.vmsd
-rwxr-xr-x 1 root root        2551 Mar  8 13:35 ersatz01.vmx
-rw------- 1 root root         263 Mar  8 13:35 ersatz01.vmxf
-rw-r--r-- 1 root root       98363 Nov 17 11:10 vmware-168.log
-rw-r--r-- 1 root root      113461 Nov 17 11:10 vmware-169.log
-rw-r--r-- 1 root root       91517 Nov 17 11:10 vmware-170.log
-rw-r--r-- 1 root root       91240 Nov 17 11:10 vmware-171.log
-rw-r--r-- 1 root root       91037 Nov 17 11:10 vmware-172.log
-rw-r--r-- 1 root root       90557 Nov 17 11:10 vmware-173.log
-rw-r--r-- 1 root root       86224 Nov 17 11:22 vmware.log

Marcel00M Novice 28 posts since
May 19, 2009
Currently Being Moderated
8. Mar 8, 2011 7:59 AM in response to: Marcel00M
Re: Adding an existing hard disk

Ahhh I should use my eyes

 

You already posted th solution :

 

http://communities.vmware.com/message/1281621#1281621

 

 

-rw------- 1 root root  4294967808 Nov 17 13:17 ersatz02_1-flat.vmdk
-rw------- 1 root root         528 Nov 17 11:23 ersatz02_1.vmdk
-rw------- 1 root root 16106127360 Nov 17 13:17 ersatz02_2-flat.vmdk
-rw------- 1 root root         530 Nov 17 11:23 ersatz02_2.vmdk
-rw------- 1 root root 12884901888 Nov 17 13:17 ersatz02-flat.vmdk

-rw------- 1 root root        8684 Nov 17 13:18 ersatz02.nvram
-rw------- 1 root root         528 Nov 17 11:22 ersatz02.vmdk
-rw------- 1 root root           0 Nov 17 11:22 ersatz02.vmsd
-rwxr-xr-x 1 root root        2641 Nov 17 13:18 ersatz02.vmx
-rw------- 1 root root         263 Nov 17 13:18 ersatz02.vmxf
-rw-r--r-- 1 root root       98363 Nov 17 11:16 vmware-168.log
-rw-r--r-- 1 root root      113461 Nov 17 11:16 vmware-169.log
-rw-r--r-- 1 root root       91517 Nov 17 11:16 vmware-170.log
-rw-r--r-- 1 root root       91240 Nov 17 11:16 vmware-171.log
-rw-r--r-- 1 root root       91037 Nov 17 11:16 vmware-172.log
-rw-r--r-- 1 root root       90557 Nov 17 11:16 vmware-173.log
-rw-r--r-- 1 root root       87101 Nov 17 13:18 vmware.log

-rw------- 1 root root  4294967808 Nov 17 13:17 ersatz02_1-flat.vmdk
-rw------- 1 root root         528 Nov 17 11:23 ersatz02_1.vmdk
-rw------- 1 root root 12884901888 Nov 17 13:17 ersatz02-flat.vmdk
-rw------- 1 root root        8684 Nov 17 13:18 ersatz02.nvram
-rw------- 1 root root         528 Nov 17 11:22 ersatz02.vmdk
-rw------- 1 root root           0 Nov 17 11:22 ersatz02.vmsd
-rwxr-xr-x 1 root root        2551 Mar  8 16:55 ersatz02.vmx
-rw------- 1 root root         263 Mar  8 16:55 ersatz02.vmxf
-rw-r--r-- 1 root root       98363 Nov 17 11:16 vmware-168.log
-rw-r--r-- 1 root root      113461 Nov 17 11:16 vmware-169.log
-rw-r--r-- 1 root root       91517 Nov 17 11:16 vmware-170.log
-rw-r--r-- 1 root root       91240 Nov 17 11:16 vmware-171.log
-rw-r--r-- 1 root root       91037 Nov 17 11:16 vmware-172.log
-rw-r--r-- 1 root root       90557 Nov 17 11:16 vmware-173.log
-rw-r--r-- 1 root root       87101 Nov 17 13:18 vmware.log

Bookmarked By (0)

Share This Page

Communities