Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- usage() {
- echo "Usage: $0 config_file vmdk_name"
- echo
- }
- if [ -z "${1}" ]; then
- usage
- echo "ERROR: No config_file specified"
- exit 1
- fi
- if [ -z "${2}" ]; then
- usage
- echo "ERROR: No vmdk_name specified"
- exit 1
- fi
- if [ -e "${1}" ]; then
- . "${1}"
- else
- usage
- echo "ERROR: Could not find ${1}"
- exit 1
- fi
- # Check that required variables are defined
- if
- ( [ ! $BUILD_IMAGE_NAME ] && echo "VMDK_NAME is undefined!") ||
- ( [ ! $BUILD_WORK_DIR ] && echo "BUILD_WORK_DIR is undefined!") ||
- ( [ ! $DEPLOY_ESX_SERVER ] && echo "DEPLOY_ESX_SERVER is undefined!") ||
- ( [ ! $DEPLOY_ESX_PATH ] && echo "DEPLOY_ESX_PATH is undefined!")
- then
- echo "Fatal! A required parameter is not defined."
- exit 1
- fi
- VMDK_NAME=${2}
- VMDK_DIR="${BUILD_WORK_DIR}/${VMDK_NAME}"
- if [ -d "${VMDK_DIR}" ]; then
- usage
- echo "ERROR: ${VMDK_DIR} already exists"
- exit 1
- fi
- mkdir -p ${VMDK_DIR}
- if [ $? -ne "0" ]; then
- echo "ERROR: mkdir -p ${VMDK_DIR} failed"
- exit 1
- fi
- # Change directory to BUILD_WORK_DIR
- cd ${VMDK_DIR}
- file -b ${BUILD_IMAGE_NAME} | grep -q "x86 boot sector"
- if [ $? -ne "0" ]; then
- echo "ERROR: ${BUILD_IMAGE_NAME} does not appear to be a bootable image file"
- exit 1
- fi
- # Hard link the image into the vmdk directory
- ln ${BUILD_IMAGE_NAME} ${VMDK_DIR}/${VMDK_NAME}-000.vmdk
- # Use the current time to concoct a unique CID value for the vmdk. ghetto fab.
- vmdk_cid=`date`
- vmdk_cid=`date -j -f "%a %b %d %T %Z %Y" "$vmdk_cid" "+%s"`
- vmdk_cid=`echo "obase=16; $vmdk_cid" | bc`
- # Calculate the number of 512B sectors to allocate
- image_size=`stat -f %z ${BUILD_IMAGE_NAME}`
- vmdk_sectors_total=`expr ${image_size} / 512`
- # Cook up the vmdk file
- cat <<EOF >> ${VMDK_DIR}/${VMDK_NAME}.vmdk
- version=1
- CID=$vmdk_cid
- parentCID=ffffffff
- createType="vmfs"
- RW $vmdk_sectors_total VMFS "$VMDK_NAME-000.vmdk"
- ddb.geometry.cylinders = "`expr $vmdk_sectors_total / 16065`"
- ddb.geometry.heads = "255"
- ddb.geometry.sectors = "63"
- ddb.virtualHWVersion = "7"
- ddb.adapterType = "lsilogic"
- EOF
- # Mint the vmx file
- cat <<EOF >> ${VMDK_DIR}/${VMDK_NAME}.vmx
- .encoding = "UTF-8"
- config.version = "8"
- virtualHW.version = "7"
- pciBridge0.present = "TRUE"
- pciBridge4.present = "TRUE"
- pciBridge4.virtualDev = "pcieRootPort"
- pciBridge4.functions = "8"
- pciBridge5.present = "TRUE"
- pciBridge5.virtualDev = "pcieRootPort"
- pciBridge5.functions = "8"
- pciBridge6.present = "TRUE"
- pciBridge6.virtualDev = "pcieRootPort"
- pciBridge6.functions = "8"
- pciBridge7.present = "TRUE"
- pciBridge7.virtualDev = "pcieRootPort"
- pciBridge7.functions = "8"
- vmci0.present = "TRUE"
- nvram = "${VMDK_NAME}.nvram"
- virtualHW.productCompatibility = "hosted"
- powerType.powerOff = "soft"
- powerType.powerOn = "hard"
- powerType.suspend = "hard"
- powerType.reset = "soft"
- displayName = "${VMDK_NAME}"
- extendedConfigFile = "${VMDK_NAME}.vmxf"
- floppy0.present = "TRUE"
- scsi0.present = "TRUE"
- scsi0.sharedBus = "none"
- scsi0.virtualDev = "lsilogic"
- memsize = "${BUILD_MEMSIZE}"
- scsi0:0.present = "TRUE"
- scsi0:0.fileName = "${VMDK_NAME}.vmdk"
- scsi0:0.deviceType = "scsi-hardDisk"
- ide1:0.present = "TRUE"
- ide1:0.clientDevice = "TRUE"
- ide1:0.deviceType = "cdrom-raw"
- ide1:0.startConnected = "FALSE"
- floppy0.startConnected = "FALSE"
- floppy0.fileName = ""
- floppy0.clientDevice = "TRUE"
- ethernet0.present = "TRUE"
- ethernet0.virtualDev = "e1000"
- ethernet0.networkName = "VM Network"
- ethernet0.addressType = "generated"
- guestOS = "freebsd-64"
- vmci0.id = "-1777923021"
- cleanShutdown = "TRUE"
- replay.supported = "FALSE"
- replay.filename = ""
- scsi0:1.redo = ""
- pciBridge0.pciSlotNumber = "17"
- pciBridge4.pciSlotNumber = "21"
- pciBridge5.pciSlotNumber = "22"
- pciBridge6.pciSlotNumber = "23"
- pciBridge7.pciSlotNumber = "24"
- scsi0.pciSlotNumber = "16"
- ethernet0.pciSlotNumber = "32"
- vmci0.pciSlotNumber = "33"
- vmotion.checkpointFBSize = "16777216"
- ethernet0.generatedAddressOffset = "0"
- evcCompatibilityMode = "FALSE"
- svga.autodetect = "TRUE"
- svga.vramSize = "16777216"
- EOF
- # Transfer the vmdk to an esx server
- echo scp -C -r ${VMDK_DIR} root@${DEPLOY_ESX_SERVER}:${DEPLOY_ESX_PATH}/
- scp -C -r ${VMDK_DIR} root@${DEPLOY_ESX_SERVER}:${DEPLOY_ESX_PATH}/
- echo ssh root@${DEPLOY_ESX_SERVER} vim-cmd solo/registervm ${DEPLOY_ESX_PATH}/${VMDK_NAME}/${VMDK_NAME}.vmx
- VMID=`ssh root@${DEPLOY_ESX_SERVER} vim-cmd solo/registervm ${DEPLOY_ESX_PATH}/${VMDK_NAME}/${VMDK_NAME}.vmx`
- if [ ! -n $VMID ]; then
- echo "Error: VMID not set"
- exit
- fi
- echo ssh root@${DEPLOY_ESX_SERVER} vim-cmd vmsvc/power.on $VMID
- ssh root@${DEPLOY_ESX_SERVER} vim-cmd vmsvc/power.on $VMID
- echo "VM ${VMDK_NAME} has been started"
- echo -n "Waiting for IP address.."
- while `ssh root@${DEPLOY_ESX_SERVER} "vim-cmd vmsvc/get.summary $VMID | grep ipAddress | grep -q unset"`; do
- echo -n '.'
- sleep 5
- done
- echo
- ssh root@${DEPLOY_ESX_SERVER} "vim-cmd vmsvc/get.summary $VMID | grep ipAddress"
- # Clean up
- rm -rf ${VMDK_DIR}
Add Comment
Please, Sign In to add comment