Last active
December 23, 2015 15:39
-
-
Save lambdacpp/6656539 to your computer and use it in GitHub Desktop.
使用模板创建虚拟机的脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| template_name="Debian 7 Template" | |
| default_vlan=101 | |
| Usage() | |
| { | |
| echo "$0 new_vm_name [vlan_id]" | |
| echo " vlanid default 101" | |
| } | |
| if [ $# -eq 2 ];then | |
| vm_name=$1 | |
| vlan_id=$2 | |
| elif [ $# -eq 1 ];then | |
| vm_name=$1 | |
| vlan_id=$default_vlan | |
| else | |
| Usage | |
| exit 1 | |
| fi | |
| ## Check VM | |
| uuid=`xe vm-list name-label="$vm_name" --minimal` | |
| if [ "$uuid" ];then | |
| echo "$vm_name is exist." | |
| exit 1 | |
| fi | |
| ## Check VLAN | |
| xe vlan-list params=tag --minimal | grep $vlan_id | |
| if [ $? -ne 0 ];then | |
| echo "$vlan_id is invalid." | |
| exit 1 | |
| fi | |
| echo "$vm_name $vlan_id" | |
| ## Create VM | |
| echo "Creating VM" | |
| vm_uuid=`xe vm-install template="$template_name" new-name-label="$vm_name"` | |
| if [ $? -ne 0 ];then | |
| echo "Create VM fail." | |
| exit 1 | |
| fi | |
| ## Modify VM xvda disk name | |
| #### Get vdi uuid by vm vdb | |
| vdi_uuid=`xe vbd-list params=vdi-uuid device=xvda vm-name-label=$vm_name --minimal` | |
| #### modify | |
| xe vdi-param-set uuid=$vdi_uuid name-label="${vm_name}-root" | |
| if [ $vlan_id -eq $default_vlan ]; then | |
| exit 0 | |
| fi | |
| ## Modify vlan id | |
| vlan_network_uuid=`xe pif-list VLAN=$vlan_id params=network-uuid --minimal` | |
| vm_vif_uuid=`xe vif-list vm-name-label=$vm_name --minimal` | |
| xe vif-destroy uuid=$vm_vif_uuid | |
| xe vif-create vm-uuid=$vm_uuid network-uuid=$vlan_network_uuid mac=random device=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment