其他分享
首页 > 其他分享> > kvm 克隆批处理

kvm 克隆批处理

作者:互联网

kvm 指克隆脚本,仅做参考
[root@localhost kvm]# cat kvm.sh 
#! /bin/bash

imgdir="/home"
moban_disk="moban.img"
moban_name="moban"
declare -A XJ
random(){
STR="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890:><+="
for((i=1;i<=8;i++))
do
eval A$i=$(echo $[$RANDOM%67+1])
eval T$i=$(echo $STR|eval cut -c \$A$i)
done

echo -n $T1$T2$T3$T4$T5$T6$T7$T8
echo ""
}
#ABC=$(random)
#echo $ABC

read -p "input the virtual host count" NUM
for((i=1;i<=$NUM;i++))
do
xuniji=$(random)
password=$(random)
     qemu-img create -f qcow2 -b ${imgdir}/${moban_disk} ${imgdir}/disk.${xuniji}
     virsh dumpxml ${moban_name}  > /etc/libvirt/qemu/${xuniju}.xml
     sed -r -i "s/(<name>).*(<\/name>)/\1${xuniji}\2/" /etc/libvirt/qemu/${xuniji}.xml
     sed -i '/mac address/d' /etc/libvirt/qemu/${xuniji}.xml
     sed -i '/<uuid>/d' /etc/libvirt/qemu/${xuniji}.xml
     sed -r -i "/source file/c<source file='/home/disk.${xuniji}'/>" /etc/libvirt/qemu/${xuniji}.xml
     virsh define /etc/libvirt/qemu/${xuniji}.xml
#output name
     XJ[$xuniji]=[$password-192.168.1.$i]

done

#mkdir /tmp/tmpmount/
for xjname in ${!XJ[@]}
do
guestmount -i -a ${imgdir}/disk.${xjname} /tmp/tmpmount &

wait $!
sed -i "/HOSTNAME/cHOSTNAME=${xjname}" /tmp/tmpmount/etc/sysconfig/network
pass=$(openssl passwd -1 -salt "$(random)" "$(echo ${XJ[$xjname]} | cut -d "-" -f 1)")
sed -r -i "s/(root:)[^:]+(:.*)/\1$pass\2/" /tmp/tmpmount/etc/shadow
sed -i -e '/HWADDR/d' -e '/UUID/d' /tmp/tmpmount/etc/sysconfig/network-script/ifcfg-eth0
sed -i '/BOOTPROTO/cBOOTPROTO=static' /tmp/tmpmount/etc/sysconfig/network-script/ifcfg-eth0
sed -i "$aIPADDR=$(echo ${XJ[$xjname]} | cut -d "-" -f 2)\nPREFIX=24" /tmp/tmpmount/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '1,$d' /tmp/tmpmount/etc/udev/rules.d/70-persistent-net.rules
umount /tmp/tmp/mount
done

for name in ${!XJ[@]}
do
echo -n "virtual-host name:${XJ[$name]}  password:$(echo ${XJ[$name]}| cut -d "-" -f1) ip:$(echo ${XJ[$name]}| cut -d "-" -f2)"
 &>> /root/xunijie.file 
done

标签:tmp,XJ,克隆,批处理,kvm,etc,tmpmount,sed,name
来源: https://blog.51cto.com/keep11/2830619