2014-09-23 181 views
1

我在筆記本電腦上爲了學習目的而設置了VMWare ESXi。我想自動化的東西,並得到這個非常漂亮的shell腳本。它要求提供諸如cpu內核,iso路徑,ram和磁盤空間等參數。我是一名初學者。我知道這是一個非常基本的問題,但無法在互聯網上找到答案。這是代碼。如何在此shell腳本的命令行中輸入參數?

我不明白如何輸入數值<|c|i|r|s> —所需的語法不清楚,確切地說。

#paratmers: machine name (required), CPU (number of cores), RAM (memory size in MB), HDD Disk size (in GB), ISO (Location of ISO image, optional) 
#default params: CPU: 2, RAM: 4096, DISKSIZE: 20GB, ISO: 'blank' 
- 
phelp() { 
    echo "Script for automatic Virtual Machine creation for ESX" 
    echo "Usage: ./create.sh options: n <|c|i|r|s>" 
    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" 
    echo "Default values are: CPU: 2, RAM: 4096MB, HDD-SIZE: 20GB" 
} 

CPU=2 
RAM=4096 
SIZE=20 
ISO="ISO'S" 
FLAG=true 
ERR=false 
n=vmoo 


while getopts n:c:i:r:s: option 
do 
    case $option in 
      n) 
       NAME=${OPTARG}; 
       FLAG=false; 
       if [ -z $NAME ]; then 
        ERR=true 
        MSG="$MSG | Please make sure to enter a VM name." 
       fi 
       ;; 
      c) 
       CPU=${OPTARG} 
       if [ `echo "$CPU" | egrep "^-?[0-9]+$"` ]; then 
        if [ "$CPU" -le "0" ] || [ "$CPU" -ge "32" ]; then 
         ERR=true 
         MSG="$MSG | The number of cores has to be between 1 and 32." 
        fi 
       else 
        ERR=true 
        MSG="$MSG | The CPU core number has to be an integer." 
       fi 
       ;; 
      i) 
       ISO=${OPTARG} 
       if [ ! `echo "$ISO" | egrep "^.*\.(iso)$"` ]; then 
        ERR=true 
        MSG="$MSG | The extension should be .iso" 
       fi 
       ;; 
      r) 
       RAM=${OPTARG} 
       if [ `echo "$RAM" | egrep "^-?[0-9]+$"` ]; then 
        if [ "$RAM" -le "0" ]; then 
         ERR=true 
         MSG="$MSG | Please assign more than 1MB memory to the VM." 
        fi 
       else 
        ERR=true 
        MSG="$MSG | The RAM size has to be an integer." 
       fi 
       ;; 
      s) 
       SIZE=${OPTARG} 
       if [ `echo "$SIZE" | egrep "^-?[0-9]+$"` ]; then 
        if [ "$SIZE" -le "0" ]; then 
         ERR=true 
         MSG="$MSG | Please assign more than 1GB for the HDD size." 
        fi 
       else 
        ERR=true 
        MSG="$MSG | The HDD size has to be an integer." 
       fi 
       ;; 
      \?) echo "Unknown option: -$OPTARG" >&2; phelp; exit 1;; 
      :) echo "Missing option argument for -$OPTARG" >&2; phelp; exit 1;; 
      *) echo "Unimplimented option: -$OPTARG" >&2; phelp; exit 1;; 
    esac 
done 

if $FLAG; then 
echo "You need to at least specify the name of the machine with the -n parameter." 
exit 1 
fi 

if $ERR; then 
echo $MSG 
exit 1 
fi 

if [ -d "$NAME" ]; then 
echo "Directory - ${NAME} already exists, can't recreate it." 
exit 
fi 


mkdir ${NAME} 


vmkfstools -c "${SIZE}"G -a lsilogic $NAME/$NAME.vmdk 


touch $NAME/$NAME.vmx 


cat <<EOF> $NAME/$NAME.vmx 

config.version = "8" 
virtualHW.version = "7" 
vmci0.present = "TRUE" 
displayName = "${NAME}" 
floppy0.present = "FALSE" 
numvcpus = "${CPU}" 
scsi0.present = "TRUE" 
scsi0.sharedBus = "none" 
scsi0.virtualDev = "lsilogic" 
memsize = "${RAM}" 
scsi0:0.present = "TRUE" 
scsi0:0.fileName = "${NAME}.vmdk" 
scsi0:0.deviceType = "scsi-hardDisk" 
ide1:0.present = "TRUE" 
ide1:0.fileName = "${ISO}" 
ide1:0.deviceType = "cdrom-image" 
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" 
ethernet0.pciSlotNumber = "32" 
ethernet0.present = "TRUE" 
ethernet0.virtualDev = "e1000" 
ethernet0.networkName = "Inside" 
ethernet0.generatedAddressOffset = "0" 
guestOS = "other26xlinux-64" 
EOF 


MYVM=`vim-cmd solo/registervm /vmfs/volumes/datastore1/${NAME}/${NAME}.vmx` 

vim-cmd vmsvc/power.on $MYVM 

echo "The Virtual Machine is now setup & the VM has been started up. Your have the following configuration:" 
echo "Name: ${NAME}" 
echo "CPU: ${CPU}" 
echo "RAM: ${RAM}" 
echo "HDD-size: ${SIZE}" 
if [ -n "$ISO" ]; then 
echo "ISO: ${ISO}" 
else 
echo "No ISO added." 
fi 
echo "Thank you." 
exit 
+1

沒有腳本的實際_parses_參數的部分,我不知道我們應該如何幫助。用法文本的確寫得很模糊。 – 2014-09-23 15:39:01

+1

另外,如果你問*如何使用*腳本,而不是如何寫*腳本,這不是一個真正適合StackOverflow的問題;您可以嘗試使用SuperUser。儘管如此,他們仍然需要整個劇本。 – 2014-09-23 15:39:57

+0

對不起,我編輯有問題,否則我會在開始時這樣做。我認爲我的問題不是一個複雜的問題,它會要求您輸入參數,如CPU核心,ISO路徑,RAM,磁盤大小。 – 2014-09-23 16:06:17

回答

1

輸入他們作爲-k value雙,其中k是用來標識該參數的信。

例如:

./create.sh -n vmname -c 2 -i /path/to/file.iso -s 512 

這將名稱設置爲vmname,CPU數量爲2,ISO文件名/path/to/file.iso

+0

謝謝你。那是我需要的。哈哈這麼傻,我試圖在我的本地機器上測試它,而不是在esxi服務器上執行ssh – 2014-09-23 16:35:54

1

用法消息是關於,你可以爲無益得到並仍然試圖傳達信息。沒有代碼,我不知道該怎麼做。幸運的是,使用shell腳本,你可以看到代碼。用法消息應該讀更多的東西一樣:

{ 
echo "Usage: $0 -n 'vm name' [-c cpus][-i iso-image][-r ram][-s disk]" 
echo " -n 'vm name' Name of VM (required)" 
echo " -c cpus  Number of virtual CPUs" 
echo " -i iso-image Location of an ISO image" 
echo " -r ram  RAM size in MB" 
echo " -s disk  Disk size in GB" 
echo "Default values are: CPU: 2, RAM: 4096MB, HDD-SIZE: 20GB" 
} >&2 

要謹慎:當你寫一個shell腳本,確保使用信息明確記載,無論是在使用信息或者是在腳本中的註釋。你所展示的shell腳本是一個例子,說明如何不做。