当openstack物理节点意外关机崩溃时,使用该脚本能自动匹配解锁volumes里面的rbd镜像,使意外关闭的虚拟机实例可以在迁移到正常节点启动。
1.引入openstack的账号密码环境变量等
source /root/openrc
2.使用脚本
bash openstack_instance_lockoutv1.sh <虚拟机名称>
如:
bash openstack_instance_lockoutv1.sh test-ops
通过uuid去找实例,并找到对应的卷,解锁对应ceph卷。
提示解锁:按y
注意:如果虚拟机卷已解锁,则查询不到卷信息,不需要解锁,提示时按n。
openstack_instance_lockoutv1.sh
#!/bin/bash
instance_name=$1
if [ -z $instance_name ];then
echo "please input instance name"
exit 1
fi
echo "instance_name: $instance_name"
instance_uuid=`nova list --all | grep -w $instance_name | awk '{print $2}'`
if [ -z $instance_uuid ];then
echo "instance name is not exist"
exit 1
fi
echo "instance_uuid: $instance_uuid"
echo "-----------------------------------"
volume_list=`nova show $instance_uuid | grep -w volumes_attached | awk -F "|" '{print$3}' | sed s/[[:space:]]//g | sed 's/},/} /g' | sed 's/\[//g'| sed 's/]//g'`
for i in $volume_list;
do
volume_name=volume-`echo $i |awk -F ":" '{print$2}' |awk -F "," '{print$1}' | sed 's/"//g'`
echo "volume_name: $volume_name"
volume_lock_info=`rbd lock ls -p volumes $volume_name | awk 'NF>2{print$1,$2,$3}' | grep -v Lock | grep -v There`
#echo $volume_lock_info
ID_ORG=`echo $volume_lock_info | awk '{print$2,$3}'`
ID=\"`echo $volume_lock_info | awk '{print$2,$3}'`\"
#echo $ID
Locker=`echo $volume_lock_info | awk '{print$1}'`
#echo $Locker
set -x
echo "手动执行解锁"
echo "rbd lock rm -p volumes $volume_name $ID $Locker"
echo "-----------------------------------"
while true
do
read -r -p "是否执行解锁命令?(y/n)" input
case $input in
[yY][eE][sS]|[yY])
ID_ORG_1=`echo $volume_lock_info | awk '{print$2}'`
echo $ID_ORG_1
ID_ORG_2=`echo $volume_lock_info | awk '{print$3}'`
echo $ID_ORG_2
rbd lock rm -p volumes $volume_name $ID_ORG_1_Format "${ID_ORG_1} ${ID_ORG_2}" $Locker
if [ $? -eq 0 ];then
echo "解锁磁盘完成"
break
fi
;;
[nN][oO]|[nN])
echo "请手动解锁"
break
;;
*)
echo "Invalid input..."
;;
esac
done
set +x
done