用cloudmonkey批量创建虚拟机
需求:
1.root磁盘120G(这个在做镜像的时候已经做好)
2.需要用到share网络
3.添加500G磁盘并且挂载早虚拟机上面
#!/bin/bash
zone_id=d530fee4-413a-463b-abf5-4fdcf523077b
network_id=96eb15a6-ccd3-4e33-adf0-b5f3e63e2466
serviceofferingid='b1fb445d-2571-4803-a3f5-93f0ad81b245'
templateid='6d2a206a-eecb-4ba2-b6ab-7db8f38a7b00'
diskOffering_id='049de7c5-48db-4f10-83fe-be54ebb61cc7'
centos_ips="172.18.3.114..115,"
cli='cloudmonkey'
function parseIps()
{
ips=`echo $* | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(\.\.[0-9]{1,3})?"`
#TODO: needs validate ip
for i in `echo $ips`
do
ipPre=`echo $i | cut -d '.' -f -3`
ipStart=`echo $i | cut -d '.' -f 4`
# if $i.contain("..")
if [[ $i == *..* ]]; then
ipEnd=`echo $i | cut -d '.' -f 6`
for ((j=$ipStart; j<=$ipEnd; j++))
do
echo $ipPre.$j
done
# else !$i.contain("..")
else
ipEnd="$ipStart"
echo $ipPre.$ipStart
fi
done
}
function create_vm()
{
centos_iplist=`parseIps $centos_ips`
echo "starting to create centos vm:"
for ip in $centos_iplist
do
a=`echo $ip | cut -d '.' -f 3`
b=`echo $ip | cut -d '.' -f 4`
echo "create haha $a-$b..."
$cli deploy virtualmachine zoneid=$zone_id templateid=$templateid serviceofferingid=$serviceofferingid displayname=haha$a-$b ipaddress=$ip networkids=$network_id
$cli create volume name=haha$a-$b zoneId=$zone_id diskOfferingId=$diskOffering_id
#todo: to attach volume to the vm
volume_id=`$cli list volumes|grep -B1 "name = haha$a-$b" |awk 'NR==1{print }'|awk '{print $3}'`
#echo $volume_id
vm_id=`$cli list virtualmachines|grep "displayname = haha$a-$b" -B15|awk '/^id = /{print}'|awk '{print $3}'`
echo $vm_id
$cli attach volume id=$volume_id virtualmachineid=$vm_id
done
}
create_vm