Xenserver 更改磁盘属性
#!/bin/sh #this script set xenserver vm vdi on-boot mode #send set-vdi-onboot.sh to xenserver use ftp or ssh first ./set-vdi-onboot.sh #version: 0.02 #date: 2017-12-01 #wangminsheng@3tcloud.com get_vm_name(){ xe vm-list params=all |grep name-label |awk -F\: '{print $2}' | sed 's/ //' >vm_name.tmp for((;;)) do read -p "Please input VM Name: " vmname echo -e "You input VM Name: $vmname" >>out.log_"$outlog" if [ `cat vm_name.tmp | sed 's/ /_/g'|grep -wc "$vmname"` -eq 1 ] 2>/dev/null; then echo "$vmname" >vm_name.txt xe vm-list name-label="$vmname" |grep uuid |awk -F\: '{print $2}' |sed 's/ //' >vm_uuid.txt break else echo "VM Name label not exist, Try again " |tee -a out.log_"$outlog" continue fi done } get_vm_name_label(){ if [ "$selectNum1" == 1 ]; then get_vm_name elif [ "$selectNum1" == 2 ]; then #xe vm-list params=all |grep name-label |awk -F\: '{print $2}' | sed 's/ //' >vm_name.txt xe vm-list dom-id=-1 params=uuid |grep -v "^$" |awk -F\: '{print $2}' | sed 's/ //' >vm_uuid.txt fi } #get vm power state halted or running get_vm_pstate(){ p_state=`xe vm-list uuid=$vm_uuid |grep power-state |awk -F\: '{print $2}' |sed 's/ //'` } shutdown_vm(){ echo "Shutdown VM: $vm_uuid now ... Please wait 30S " xe vm-shutdown uuid=$vm_uuid sleep 30s } get_vdi_uuids(){ xe vm-disk-list uuid=$vm_uuid |grep -A1 "VDI:" |grep uuid |awk -F\: '{print $2}' |sed 's/ //' >vdi_uuids.txt } get_vdi_onboot_status(){ vdi_onboot_status=`xe vdi-param-list uuid=$vdiuuid |grep on-boot |awk -F\: '{print $2}' |sed 's/ //'` } reset_vdi_onboot(){ get_vm_pstate if [ "$p_state" != "halted" ];then shutdown_vm xe vdi-param-set uuid=$vdiuuid on-boot=reset else xe vdi-param-set uuid=$vdiuuid on-boot=reset fi } persist_vdi_onboot(){ get_vm_pstate if [ "$p_state" != "halted" ];then shutdown_vm xe vdi-param-set uuid=$vdiuuid on-boot=persist else xe vdi-param-set uuid=$vdiuuid on-boot=persist fi } reset_vmvdi_onboot(){ # get_vm_name_label for vm_uuid in `cat vm_uuid.txt` do echo "VM: $vm_uuid Setting vdi on-boot now: " |tee -a out.log_"$outlog" get_vdi_uuids for vdiuuid in `cat vdi_uuids.txt` do for((;;)) do get_vdi_onboot_status if [ "$vdi_onboot_status" != "reset" ];then reset_vdi_onboot get_vdi_onboot_status if [ "$vdi_onboot_status" == "reset" ];then echo "VM: $vm_uuid Setting VDI [ $vdiuuid ] on-boot : reset sucessfull!" |tee -a out.log_"$outlog" break else echo "VM: $vm_uuid setting VDI [ $vdiuuid ] on-boot : reset FAIL!!!!!!!" |tee -a out.log_"$outlog" get_vdi_onboot_status echo "VM: $vm_uuid VDI [ $vdiuuid ] on-boot: $vdi_onboot_status" |tee -a out.log_"$outlog" break fi else echo "VM: $vm_uuid Setting VDI [ $vdiuuid ] on-boot: reset [ OK ]" |tee -a out.log_"$outlog" break fi done done done } persist_vmvdi_onboot(){ # get_vm_name_label for vm_uuid in `cat vm_uuid.txt` do echo "VM: $vm_uuid Setting vdi on-boot now: " |tee -a out.log_"$outlog" get_vdi_uuids for vdiuuid in `cat vdi_uuids.txt` do for((;;)) do get_vdi_onboot_status if [ "$vdi_onboot_status" != "persist" ];then persist_vdi_onboot get_vdi_onboot_status if [ "$vdi_onboot_status" == "persist" ];then echo "VM: $vm_uuid Setting VDI [ $vdiuuid ] on-boot : persist sucessfull!" |tee -a out.log_"$outlog" break else echo "VM: $vm_uuid setting VDI [ $vdiuuid ] on-boot : persist FAIL!!!!!!!" |tee -a out.log_"$outlog" get_vdi_onboot_status echo "VM: $vm_uuid VDI [ $vdiuuid ] on-boot: $vdi_onboot_status" |tee -a out.log_"$outlog" break fi else echo "VM: $vm_uuid Setting VDI [ $vdiuuid ] on-boot: persist [ OK ]" |tee -a out.log_"$outlog" break fi done done done } selectVMdiskMode(){ echo -e "------------------------" |tee -a out.log_"$outlog" echo -e "\n*1. set VM disk [reset] mode." |tee -a out.log_"$outlog" echo -e "*2. set VM disk [persist] mode." |tee -a out.log_"$outlog" echo -e "*3. exit" |tee -a out.log_"$outlog" echo -e "------------------------" |tee -a out.log_"$outlog" read -p "Please select your operation: " selectNum2 echo -e "Please select your operation: $selectNum2" >>out.log_"$outlog" return $selectNum2 } do_setOneVMdisk(){ for((;;)) do get_vm_name selectVMdiskMode if [ "$selectNum2" == 1 ]; then reset_vmvdi_onboot elif [ "$selectNum2" == 2 ]; then persist_vmvdi_onboot elif [ "$selectNum2" == 3 ]; then break fi done } do_setAllVMdisk(){ for((;;)) do get_vm_name_label selectVMdiskMode if [ "$selectNum2" == 1 ]; then reset_vmvdi_onboot elif [ "$selectNum2" == 2 ]; then persist_vmvdi_onboot elif [ "$selectNum2" == 3 ]; then break fi done } selectOperation(){ echo -e "------------------------" |tee -a out.log_"$outlog" echo -e "\n*1. set one VM disk " |tee -a out.log_"$outlog" echo -e "*2. set all VM disk " |tee -a out.log_"$outlog" echo -e "*3. exit\n" |tee -a out.log_"$outlog" echo -e "------------------------" |tee -a out.log_"$outlog" read -p "Please select your operation: " selectNum1 echo -e "Please select your operation: $selectNum1" >>out.log_"$outlog" return $selectNum1 } main(){ outlog=`date "+%Y-%m-%d_%H:%M:%S"` for((;;)) do selectOperation if [ "$selectNum1" == 1 ]; then do_setOneVMdisk elif [ "$selectNum1" == 2 ]; then do_setAllVMdisk elif [ "$selectNum1" == 3 ]; then break fi done # tar czf outlog_"$outlog".tar.gz ./out.log* vdi_uuids.txt vm_name.tmp vm_uuid.txt mv out.log_"$outlog" /tmp/ rm -rf ./out.log* vdi_uuids.txt vm_name.tmp vm_uuid.txt vm_name.txt } #start: main
脚本使用说明:
- 将set-vdi-onboot_new.sh 上传到xenserver后台
- chmod 777 set-vdi-onboot_new.sh
- 当前目录下执行./set-vdi-onboot_new.sh
一、通过VM标签逐个设置磁盘属性方法如图:
注意: 要退出单台VM磁盘属性方法:先输入一次正确的VM标签,输入两次3即可
一次性更改所有VM磁盘方法如下图:
脚本局限性说明:
- 单个VM设置时不支持带中文或空格的VM标签
- 设置所有的VM时不影响(通过VM UUID实现的)
- 当VM有多个磁盘时,可以支持设置,但不能实现对部分磁盘设置保留,对部分磁盘设置不保留
- 脚本在池环境下未测试
- 用脚本更改虚拟机磁盘属性时,虚拟机最好在关机状态下,虚拟机开机也能改,脚本会自动关虚拟机,每台等30s。