├── README.md └── create.sh /README.md: -------------------------------------------------------------------------------- 1 | Automatically Virtual Machine creation on an ESXi server 2 | 3 | Usage: /bin/sh create.sh options: n <|c|i|r|s> 4 | 5 | 6 | Where n: Name of VM (required), c: Number of virtual CPUs, i: location of an ISO image, r: RAM size in MB, s: Disk size in GB 7 | 8 | Default values are: CPU: 2, RAM: 4096MB, HDD-SIZE: 20GB 9 | -------------------------------------------------------------------------------- /create.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #paratmers: machine name (required), CPU (number of cores), RAM (memory size in MB), HDD Disk size (in GB), ISO (Location of ISO image, optional) 4 | #default params: CPU: 2, RAM: 4096, DISKSIZE: 20GB, ISO: 'blank' 5 | 6 | phelp() { 7 | echo "Script for automatic Virtual Machine creation for ESX" 8 | echo "Usage: ./create.sh options: n <|c|i|r|s>" 9 | echo "Where n: Name of VM (required), c: Number of virtual CPUs, i: location of an ISO image, r: RAM size in MB, s: Disk size in GB" 10 | echo "Default values are: CPU: 2, RAM: 4096MB, HDD-SIZE: 20GB" 11 | } 12 | 13 | #Setting up some of the default variables 14 | CPU=2 15 | RAM=4096 16 | SIZE=20 17 | ISO="" 18 | FLAG=true 19 | ERR=false 20 | 21 | #Error checking will take place as well 22 | #the NAME has to be filled out (i.e. the $NAME variable needs to exist) 23 | #The CPU has to be an integer and it has to be between 1 and 32. Modify the if statement if you want to give more than 32 cores to your Virtual Machine, and also email me pls :) 24 | #You need to assign more than 1 MB of ram, and of course RAM has to be an integer as well 25 | #The HDD-size has to be an integer and has to be greater than 0. 26 | #If the ISO parameter is added, we are checking for an actual .iso extension 27 | while getopts n:c:i:r:s: option 28 | do 29 | case $option in 30 | n) 31 | NAME=${OPTARG}; 32 | FLAG=false; 33 | if [ -z $NAME ]; then 34 | ERR=true 35 | MSG="$MSG | Please make sure to enter a VM name." 36 | fi 37 | ;; 38 | c) 39 | CPU=${OPTARG} 40 | if [ `echo "$CPU" | egrep "^-?[0-9]+$"` ]; then 41 | if [ "$CPU" -le "0" ] || [ "$CPU" -ge "32" ]; then 42 | ERR=true 43 | MSG="$MSG | The number of cores has to be between 1 and 32." 44 | fi 45 | else 46 | ERR=true 47 | MSG="$MSG | The CPU core number has to be an integer." 48 | fi 49 | ;; 50 | i) 51 | ISO=${OPTARG} 52 | if [ ! `echo "$ISO" | egrep "^.*\.(iso)$"` ]; then 53 | ERR=true 54 | MSG="$MSG | The extension should be .iso" 55 | fi 56 | ;; 57 | r) 58 | RAM=${OPTARG} 59 | if [ `echo "$RAM" | egrep "^-?[0-9]+$"` ]; then 60 | if [ "$RAM" -le "0" ]; then 61 | ERR=true 62 | MSG="$MSG | Please assign more than 1MB memory to the VM." 63 | fi 64 | else 65 | ERR=true 66 | MSG="$MSG | The RAM size has to be an integer." 67 | fi 68 | ;; 69 | s) 70 | SIZE=${OPTARG} 71 | if [ `echo "$SIZE" | egrep "^-?[0-9]+$"` ]; then 72 | if [ "$SIZE" -le "0" ]; then 73 | ERR=true 74 | MSG="$MSG | Please assign more than 1GB for the HDD size." 75 | fi 76 | else 77 | ERR=true 78 | MSG="$MSG | The HDD size has to be an integer." 79 | fi 80 | ;; 81 | \?) echo "Unknown option: -$OPTARG" >&2; phelp; exit 1;; 82 | :) echo "Missing option argument for -$OPTARG" >&2; phelp; exit 1;; 83 | *) echo "Unimplimented option: -$OPTARG" >&2; phelp; exit 1;; 84 | esac 85 | done 86 | 87 | if $FLAG; then 88 | echo "You need to at least specify the name of the machine with the -n parameter." 89 | exit 1 90 | fi 91 | 92 | if $ERR; then 93 | echo $MSG 94 | exit 1 95 | fi 96 | 97 | if [ -d "$NAME" ]; then 98 | echo "Directory - ${NAME} already exists, can't recreate it." 99 | exit 100 | fi 101 | 102 | #Creating the folder for the Virtual Machine 103 | mkdir ${NAME} 104 | 105 | #Creating the actual Virtual Disk file (the HDD) with vmkfstools 106 | vmkfstools -c "${SIZE}"G -a lsilogic $NAME/$NAME.vmdk 107 | 108 | #Creating the config file 109 | touch $NAME/$NAME.vmx 110 | 111 | #writing information into the configuration file 112 | cat << EOF > $NAME/$NAME.vmx 113 | 114 | config.version = "8" 115 | virtualHW.version = "7" 116 | vmci0.present = "TRUE" 117 | displayName = "${NAME}" 118 | floppy0.present = "FALSE" 119 | numvcpus = "${CPU}" 120 | scsi0.present = "TRUE" 121 | scsi0.sharedBus = "none" 122 | scsi0.virtualDev = "lsilogic" 123 | memsize = "${RAM}" 124 | scsi0:0.present = "TRUE" 125 | scsi0:0.fileName = "${NAME}.vmdk" 126 | scsi0:0.deviceType = "scsi-hardDisk" 127 | ide1:0.present = "TRUE" 128 | ide1:0.fileName = "${ISO}" 129 | ide1:0.deviceType = "cdrom-image" 130 | pciBridge0.present = "TRUE" 131 | pciBridge4.present = "TRUE" 132 | pciBridge4.virtualDev = "pcieRootPort" 133 | pciBridge4.functions = "8" 134 | pciBridge5.present = "TRUE" 135 | pciBridge5.virtualDev = "pcieRootPort" 136 | pciBridge5.functions = "8" 137 | pciBridge6.present = "TRUE" 138 | pciBridge6.virtualDev = "pcieRootPort" 139 | pciBridge6.functions = "8" 140 | pciBridge7.present = "TRUE" 141 | pciBridge7.virtualDev = "pcieRootPort" 142 | pciBridge7.functions = "8" 143 | ethernet0.pciSlotNumber = "32" 144 | ethernet0.present = "TRUE" 145 | ethernet0.virtualDev = "e1000" 146 | ethernet0.networkName = "Inside" 147 | ethernet0.generatedAddressOffset = "0" 148 | guestOS = "other26xlinux-64" 149 | EOF 150 | 151 | #Adding Virtual Machine to VM register - modify your path accordingly!! 152 | MYVM=`vim-cmd solo/registervm /vmfs/volumes/datastore1/${NAME}/${NAME}.vmx` 153 | #Powering up virtual machine: 154 | vim-cmd vmsvc/power.on $MYVM 155 | 156 | echo "The Virtual Machine is now setup & the VM has been started up. Your have the following configuration:" 157 | echo "Name: ${NAME}" 158 | echo "CPU: ${CPU}" 159 | echo "RAM: ${RAM}" 160 | echo "HDD-size: ${SIZE}" 161 | if [ -n "$ISO" ]; then 162 | echo "ISO: ${ISO}" 163 | else 164 | echo "No ISO added." 165 | fi 166 | echo "Thank you." 167 | exit 168 | --------------------------------------------------------------------------------