脚本为学习过程中所写,还有些功能不太完善,共同进步吧,有问题留言
#!/bin/bash
#
mean(){
cat <
1) 创建新的客户机
2) 开启客户机
3) 关闭客户机
4) 增加磁盘
5) 增加网卡
6) 编辑客户机
7) 查看客户机的情况
8) 删除客户机
9) 修改网络模式
q) 退出
=======================
EOF
}
guest_create(){
while true
do
read -p "请输入要创建的虚拟机的个数" num
if echo $num |grep [^0-9]
then
break
fi
read -p "请输入你要创建的虚拟机的名字" name
guest=`virsh list --all|grep "${name}"|awk '{print $2}'|grep "^${name}$" `
if [ -z "$guest" ]
then
for ((i=1;i<=num;i++))
do
qemu-img create -f qcow2 -b /kvm/images/centos7.img /kvm/images/${name}-${i}.img
cp /etc/libvirt/qemu/centos7-bak.xml /etc/libvirt/qemu/${name}-${i}.xml
source_name=`grep "/name" /etc/libvirt/qemu/${name}-${i}.xml | awk -F \> '{print $2}' | awk -F \< '{print $1}'`
sed -i "s#${source_name}#${name}-${i}#g" /etc/libvirt/qemu/${name}-${i}.xml
source_uuid=`grep uuid /etc/libvirt/qemu/${name}-${i}.xml | awk -F \> '{print $2}' | awk -F \< '{print $1}'`
uuid=`uuidgen`
sed -i "/$source_uuid/d" /etc/libvirt/qemu/${name}-${i}.xml
source_mac=`grep "mac address=" /etc/libvirt/qemu/${name}-${i}.xml | awk -F \' '{print $2}'`
limit_mac=`od /dev/urandom -w5 -tx1 -An|sed -e 's/ //' -e 's/ /:/g'|head -n 1`
sed -i "/$source_mac/d" /etc/libvirt/qemu/${name}-${i}.xml
virsh define /etc/libvirt/qemu/${name}-${i}.xml
done
return 0
else
echo "客户机${name}已经存在"
continue
fi
done
}
guest_start(){
start_menu="==================
1.开启一台虚拟机
2.开启一组虚拟机
3.开启所有的虚拟机
=================== "
read -p "$start_menu" key
if [ $key -eq 1 ]
then
read -p "输入虚拟机的名字" name
virsh start $name
elif [ $key -eq 2 ]
then
read -p "输入虚拟机名字的前缀(nginx-1)nginx:" name
for i in `virsh list --all |grep "$name"|awk '{print $2}' `
do
virsh start $i
done
elif [ $key -eq 3 ]
then
for i in `virsh list --all |awk '{print $2}' `
do
virsh start $i
done
else
echo "请输入正确的选择"
fi
}
guest_stop(){
stop_menu="==================
1.关闭一台虚拟机
2.关闭一组虚拟机
3.关闭所有的虚拟机
==================="
read -p "$stop_menu" key
if [ $key -eq 1 ]
then
read -p "输入虚拟机的名字" name
virsh destroy $name
elif [ $key -eq 2 ]
then
read -p "输入虚拟机名字的前缀(nginx-1)nginx:" name
for i in `virsh list --all |grep "$name"|awk '{print $2}' `
do
virsh destroy $i
done
elif [ $key -eq 3 ]
then
for i in `virsh list |awk '{print $2}' `
do
virsh start $i
done
else
echo "请输入正确的选择"
fi
}
guest_add_disk() {
guest=`virsh list --all|grep "${name}"|awk '{print $2}'|grep "^${name}$" `
if [ -z "$guest" ]
then
echo "客户机${name} 不存在"
continue
else
flag=`virsh list|grep "${name}"|awk '{print $2}'|grep "^${name}$" `
if [ -z "$flag" ]
then
echo "客户机域没有运行,请先将客户机的域运行"
continue
else
read -p "你要添加磁盘的大小(单位G):" size
read -p "磁盘的格式(raw qcow2)" kind
read -p "磁盘的名字:" name
qemu-img create -f ${kind} /kvm/${name}.img ${size}G &>/dev/null
virsh attach-disk ${name} /kvm/${name}.img --target vdb --cache writeback --subdriver ${kind}
fi
fi
}
guest_add_net(){
guest=`virsh list --all|grep "${name}"|awk '{print $2}'|grep "^${name}$" `
if [ -z "$guest" ]
then
echo "客户机${name} 不存在"
continue
else
flag=`virsh list|grep "${name}"|awk '{print $2}'|grep "^${name}$" `
if [ -z "$flag" ]
then
echo "客户机域没有运行,请先将客户机的域运行"
continue
else
virsh attach-interface ${name} \
--type network \
--model virtio \
--source default \
--persistent
fi
fi
}
touch_eth0(){
cat <
TYPE="Ethernet"
BOOTPROTO="none"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
PREFIX=24
GATEWAY=192.168.122.1
DNS1=114.114.114.114
EOF
}
touch_hosts(){
cat <
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
EOF
}
guest_edit(){
edit_menu="===============
1.修改一台虚拟机
2.修改一组虚拟机
==============="
read -p "$edit_menu" type
if [ $type -eq 1 ]
then
read -p "请输入你要编辑虚拟机的名字" name
read -p "请输入修改虚拟机的ip地址(192.168.122.0/24)" guest_ip
read -p "请输入修改客户机的hostname:" guest_name
touch_hosts
echo "IPADDR=${guest_ip} " >>ifcfg-eth0
echo "${guest_ip} ${guest_name}" >> hosts
guestmount -a /kvm/images/$name.img -i /www
cp -p /root/hosts /www/etc/
cp -p /root/ifcfg-eth0 /www/etc/sysconfig/network-scripts/
umount /www
echo "修改配置完成"
elif [ $type -eq 2 ]
then
> ip.txt
touch_hosts
read -p "请输入你要编辑虚拟机的名字的前缀(nginx-1)nginx:" name
read -p "请输入起始ip地址(192.168.122.0/24)" guest_ip
hhh=`echo $guest_ip | awk -F \. '{print $1"."$2"."$3}'`
next_ip=`echo $guest_ip | awk -F \. '{print $4}'`
num=`virsh list --all |grep "$name"|wc -l `
for((i=1;i<=num;i++))
do
read -p "请输入第${i}虚拟机的主机名" guest_name
echo "${hhh}.${next_ip}" >>ip.txt
echo "${hhh}.${next_ip} ${guest_name}" >> hosts
let next_ip=next_ip+1
done
echo "请稍等可能需要一点时间......"
for ((i=1;i<=num;i++))
do
touch_eth0
ip=`head -${i} ip.txt|tail -1 `
echo "IPADDR=${ip}">> ifcfg-eth0
guestmount -a /kvm/images/${name}-${i}.img -i /www
cp -p /root/hosts /www/etc/
cp -p /root/ifcfg-eth0 /www/etc/sysconfig/network-scripts/
umount /www
done
else
echo "请输入正确的选择"
fi
}
guest_info(){
guest=`virsh list --all|grep "${name}"|awk '{print $2}'|grep "^${name}$" `
if [ -z "$guest" ]
then
echo "客户机${name} 不存在"
continue
else
echo "${name}客户机的运行状态"
virsh list --all|grep "${name}"
echo "客户机的磁盘数"
virsh domblklist ${name}
echo "客户机的网卡信息"
virsh domiflist ${name}
echo "客户机的镜像信息"
qemu-img info /kvm/images/${name}.img
echo "==============================="
fi
}
guest_delete(){
delete_menu="==================
1.删除一台虚拟机
2.删除一组虚拟机
3.删除所有的虚拟机
==================="
read -p "$delete_menu" key
if [ $key -eq 1 ]
then
read -p "输入虚拟机的名字" name
virsh undefine $name
elif [ $key -eq 2 ]
then
read -p "输入虚拟机名字的前缀(nginx-1)nginx:" name
for i in `virsh list --all |grep "$name"|awk '{print $2}' `
do
virsh undefine $i
done
elif [ $key -eq 3 ]
then
for i in `virsh list |awk '{print $2}' `
do
virsh undefine $i
done
else
echo "请输入正确的选择"
fi
}
guest_net_edit(){
guest=`virsh list --all|grep "${name}"|awk '{print $2}'|grep "^${name}$" `
if [ -z "$guest" ]
then
echo "客户机${name} 不存在"
continue
else
flag=`virsh list|grep "${name}"|awk '{print $2}'|grep "^${name}$" `
if [ -z "$flag" ]
then
read -p "请输入你要修改成的网络模式(1.nat|2.hostonly|3.bridge):" net_type
source_net=`grep "
}
while true
do
mean
read -p "请输入你的选择:" chose
case $chose in
1)
guest_create
;;
2)
guest_start
;;
3)
guest_stop
;;
4)
read -p "请输入你增加磁盘虚拟机名字" name
guest_add_disk
;;
5)
read -p "请输入你增加网卡虚拟机名字" name
guest_add_net
;;
6)
guest_edit
;;
7)
read -p "请输入查看客户机的名字" name
guest_info
;;
8)
guest_delete
;;
9)
read -p "请输入要修改客户机的名字" name
guest_net_edit
;;
q)
exit
;;
*)
echo "请输入正确的选择"
esac
done