Skip to content

Script to add disk to existing VM on ESXi

by on November 20, 2013

the script below adds a new disk to an existing VMDK

the commands I use are:

  • in the ESX host
    • vim-cmd vmsvc/getallvms – to get the VMID
    • vim-cmd vmsvc/get.datastores – to get the Datastore
    • vim-cmd vmsvc/get.summary – to get the full name for the location of the disks
    • vmkfstools – to actualy create the vmdk
    • vim-cmd vmsvc/device.getdevices – to get the next unit Number for the disk
    • vim-cmd vmsvc/device.diskaddexisting – to add the new created disk to the existing VM
  • in the VM
    • scan_disks.sh – my script to rescan the harddisk within the VM
  1. #!/bin/bash
  2. for host in `ls /sys/class/scsi_host/`
  3. do
  4. echo “- – -” > /sys/class/scsi_host/$host/scan
  5. done
    • fdisk_gen.sh – my script to create partition
  1. #!/bin/bash
  2. function Usage {
  3. echo “$0 <device>”
  4. exit 1
  5. }
  6. fdisk -l | grep $dev
  7. dev=$1
  8. echo “
  9. n
  10. p
  11. 1
  12. w
  13. ” | fdisk $dev
  14. mkfs.ext4 ${dev}1
  15. fdisk -l | grep $dev

and the main script:

  1. #!/bin/bash
  2. #
  3. # Adding a disk to a VM
  4. # Input:
  5. # ESX Host
  6. # disk Size
  7. # VM Name
  8. #
  9. # TODO: add status check after each operation
  10. #
  11. function Usage {
  12. echo “$0 <Disk Size> <Host>”
  13. exit 1
  14. }
  15. ESX=$1
  16. SZ=$2
  17. NM=$3
  18. echo ESX=$ESX SZ=$SZ NM=$NM
  19. [ -z $ESX ] || [ -z $SZ ] || [ -z $NM ] && Usage
  20. fdiskfile=`mktemp -u –suffix fdisk_${NM}`
  21. ssh $NM fdisk -l > ${fdiskfile}.before
  22. VMID=`ssh $ESX vim-cmd vmsvc/getallvms 2>/dev/null | awk ‘/’$NM’/{print $1}’`
  23. echo VMID=$VMID
  24. DS=`ssh $ESX vim-cmd vmsvc/get.datastores $VMID 2>/dev/null | awk ‘/name/{print $2}’`
  25. echo DS=$DS
  26. FULLNM=`ssh $ESX vim-cmd vmsvc/get.summary $VMID |awk ‘/vmPathName/{gsub(“/”,” “);print $4}’`
  27. echo FULLNM=$FULLNM
  28. timestamp=`date +%Y%m%d_%H%M%S`
  29. #
  30. #Creating the actual Virtual Disk file (the HDD) with vmkfstools
  31. #
  32. NEWVMDK=/vmfs/volumes/${DS}/$FULLNM/${FULLNM}_${timestamp}.vmdk
  33. echo ssh $ESX vmkfstools -c “${SZ}”G -a lsilogic $NEWVMDK
  34. ssh $ESX vmkfstools -c “${SZ}”G -a lsilogic $NEWVMDK
  35.  
  36. #
  37. # Get max unitID
  38. #
  39. tmpfile=`mktemp`
  40. ssh $ESX vim-cmd vmsvc/device.getdevices $VMID > $tmpfile
  41. unitNumber=`awk ‘/vim.vm.device.VirtualDisk/{ff=1;next}/unitN/{if(ff==1){gsub(“,”,””);unitN=$3;ff=0}}END{print unitN+1}’ $tmpfile`
  42. echo ssh $ESX vmware-vim-cmd vmsvc/device.diskaddexisting $VMID $NEWVMDK 0 $unitNumber
  43. ssh $ESX vim-cmd vmsvc/device.diskaddexisting $VMID $NEWVMDK 0 $unitNumber
  44. echo sleep 5
  45. sleep 5
  46. echo ssh $NM scan_disks.sh
  47. ssh $NM scan_disks.sh
  48. ssh $NM fdisk -l > ${fdiskfile}.after
  49. newdisk=`diff ${fdiskfile}*|awk ‘/ Disk/{gsub(“:”,””);print $3;exit}’`
  50. echo newdisk=$newdisk
  51. echo ssh $NM fdisk_gen.sh $newdisk
  52. ssh $NM fdisk_gen.sh $newdisk
Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: