Guest User

Untitled

a guest
Jul 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.94 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. usage() {
  4.     echo "Usage: $0 config_file vmdk_name"
  5.     echo
  6. }
  7.  
  8. if [ -z "${1}" ]; then
  9.     usage
  10.     echo "ERROR: No config_file specified"
  11.     exit 1
  12. fi
  13. if [ -z "${2}" ]; then
  14.     usage
  15.     echo "ERROR: No vmdk_name specified"
  16.     exit 1
  17. fi
  18.  
  19. if [ -e "${1}" ]; then
  20.     . "${1}"
  21. else
  22.     usage
  23.     echo "ERROR: Could not find ${1}"
  24.     exit 1
  25. fi
  26.  
  27.  
  28. # Check that required variables are defined
  29. if
  30.     ( [ ! $BUILD_IMAGE_NAME ] && echo "VMDK_NAME is undefined!") ||
  31.     ( [ ! $BUILD_WORK_DIR ] && echo "BUILD_WORK_DIR is undefined!") ||
  32.     ( [ ! $DEPLOY_ESX_SERVER ] && echo "DEPLOY_ESX_SERVER is undefined!") ||
  33.     ( [ ! $DEPLOY_ESX_PATH ] && echo "DEPLOY_ESX_PATH is undefined!")
  34. then
  35.     echo "Fatal! A required parameter is not defined."
  36.     exit 1
  37. fi
  38.  
  39. VMDK_NAME=${2}
  40. VMDK_DIR="${BUILD_WORK_DIR}/${VMDK_NAME}"
  41.  
  42. if [ -d "${VMDK_DIR}" ]; then
  43.     usage
  44.     echo "ERROR: ${VMDK_DIR} already exists"
  45.     exit 1
  46. fi
  47.  
  48. mkdir -p ${VMDK_DIR}
  49. if [ $? -ne "0" ]; then
  50.     echo "ERROR: mkdir -p ${VMDK_DIR} failed"
  51.     exit 1
  52. fi
  53.  
  54. # Change directory to BUILD_WORK_DIR
  55. cd ${VMDK_DIR}
  56.  
  57. file -b ${BUILD_IMAGE_NAME} | grep -q "x86 boot sector"
  58. if [ $? -ne "0" ]; then
  59.     echo "ERROR: ${BUILD_IMAGE_NAME} does not appear to be a bootable image file"
  60.     exit 1
  61. fi
  62.  
  63. # Hard link the image into the vmdk directory
  64. ln ${BUILD_IMAGE_NAME} ${VMDK_DIR}/${VMDK_NAME}-000.vmdk
  65.  
  66. # Use the current time to concoct a unique CID value for the vmdk. ghetto fab.
  67. vmdk_cid=`date`
  68. vmdk_cid=`date -j -f "%a %b %d %T %Z %Y" "$vmdk_cid" "+%s"`
  69. vmdk_cid=`echo "obase=16; $vmdk_cid" | bc`
  70.  
  71. # Calculate the number of 512B sectors to allocate
  72. image_size=`stat -f %z ${BUILD_IMAGE_NAME}`
  73. vmdk_sectors_total=`expr ${image_size} / 512`
  74.  
  75. # Cook up the vmdk file
  76. cat <<EOF >> ${VMDK_DIR}/${VMDK_NAME}.vmdk
  77. version=1
  78. CID=$vmdk_cid
  79. parentCID=ffffffff
  80.  
  81. createType="vmfs"
  82. RW $vmdk_sectors_total VMFS "$VMDK_NAME-000.vmdk"
  83. ddb.geometry.cylinders = "`expr $vmdk_sectors_total / 16065`"
  84. ddb.geometry.heads = "255"
  85. ddb.geometry.sectors = "63"
  86. ddb.virtualHWVersion = "7"
  87. ddb.adapterType = "lsilogic"
  88. EOF
  89.  
  90.     # Mint the vmx file
  91. cat <<EOF >> ${VMDK_DIR}/${VMDK_NAME}.vmx
  92. .encoding = "UTF-8"
  93. config.version = "8"
  94. virtualHW.version = "7"
  95. pciBridge0.present = "TRUE"
  96. pciBridge4.present = "TRUE"
  97. pciBridge4.virtualDev = "pcieRootPort"
  98. pciBridge4.functions = "8"
  99. pciBridge5.present = "TRUE"
  100. pciBridge5.virtualDev = "pcieRootPort"
  101. pciBridge5.functions = "8"
  102. pciBridge6.present = "TRUE"
  103. pciBridge6.virtualDev = "pcieRootPort"
  104. pciBridge6.functions = "8"
  105. pciBridge7.present = "TRUE"
  106. pciBridge7.virtualDev = "pcieRootPort"
  107. pciBridge7.functions = "8"
  108. vmci0.present = "TRUE"
  109. nvram = "${VMDK_NAME}.nvram"
  110. virtualHW.productCompatibility = "hosted"
  111. powerType.powerOff = "soft"
  112. powerType.powerOn = "hard"
  113. powerType.suspend = "hard"
  114. powerType.reset = "soft"
  115. displayName = "${VMDK_NAME}"
  116. extendedConfigFile = "${VMDK_NAME}.vmxf"
  117. floppy0.present = "TRUE"
  118. scsi0.present = "TRUE"
  119. scsi0.sharedBus = "none"
  120. scsi0.virtualDev = "lsilogic"
  121. memsize = "${BUILD_MEMSIZE}"
  122. scsi0:0.present = "TRUE"
  123. scsi0:0.fileName = "${VMDK_NAME}.vmdk"
  124. scsi0:0.deviceType = "scsi-hardDisk"
  125. ide1:0.present = "TRUE"
  126. ide1:0.clientDevice = "TRUE"
  127. ide1:0.deviceType = "cdrom-raw"
  128. ide1:0.startConnected = "FALSE"
  129. floppy0.startConnected = "FALSE"
  130. floppy0.fileName = ""
  131. floppy0.clientDevice = "TRUE"
  132. ethernet0.present = "TRUE"
  133. ethernet0.virtualDev = "e1000"
  134. ethernet0.networkName = "VM Network"
  135. ethernet0.addressType = "generated"
  136. guestOS = "freebsd-64"
  137. vmci0.id = "-1777923021"
  138. cleanShutdown = "TRUE"
  139. replay.supported = "FALSE"
  140. replay.filename = ""
  141. scsi0:1.redo = ""
  142. pciBridge0.pciSlotNumber = "17"
  143. pciBridge4.pciSlotNumber = "21"
  144. pciBridge5.pciSlotNumber = "22"
  145. pciBridge6.pciSlotNumber = "23"
  146. pciBridge7.pciSlotNumber = "24"
  147. scsi0.pciSlotNumber = "16"
  148. ethernet0.pciSlotNumber = "32"
  149. vmci0.pciSlotNumber = "33"
  150. vmotion.checkpointFBSize = "16777216"
  151. ethernet0.generatedAddressOffset = "0"
  152. evcCompatibilityMode = "FALSE"
  153. svga.autodetect = "TRUE"
  154. svga.vramSize = "16777216"
  155. EOF
  156.  
  157.  
  158. # Transfer the vmdk to an esx server
  159. echo scp -C -r ${VMDK_DIR} root@${DEPLOY_ESX_SERVER}:${DEPLOY_ESX_PATH}/
  160. scp -C -r ${VMDK_DIR} root@${DEPLOY_ESX_SERVER}:${DEPLOY_ESX_PATH}/
  161. echo ssh root@${DEPLOY_ESX_SERVER} vim-cmd solo/registervm ${DEPLOY_ESX_PATH}/${VMDK_NAME}/${VMDK_NAME}.vmx
  162. VMID=`ssh root@${DEPLOY_ESX_SERVER} vim-cmd solo/registervm ${DEPLOY_ESX_PATH}/${VMDK_NAME}/${VMDK_NAME}.vmx`
  163. if [ ! -n $VMID ]; then
  164.     echo "Error: VMID not set"
  165.     exit
  166. fi
  167. echo ssh root@${DEPLOY_ESX_SERVER} vim-cmd vmsvc/power.on $VMID
  168. ssh root@${DEPLOY_ESX_SERVER} vim-cmd vmsvc/power.on $VMID
  169. echo "VM ${VMDK_NAME} has been started"
  170. echo -n "Waiting for IP address.."
  171. while `ssh root@${DEPLOY_ESX_SERVER} "vim-cmd vmsvc/get.summary $VMID | grep ipAddress | grep -q unset"`; do
  172.     echo -n '.'
  173.     sleep 5
  174. done
  175. echo
  176. ssh root@${DEPLOY_ESX_SERVER} "vim-cmd vmsvc/get.summary $VMID | grep ipAddress"
  177.  
  178. # Clean up
  179. rm -rf ${VMDK_DIR}
Add Comment
Please, Sign In to add comment