一、场景描述
在我们的业务系统中,数据的安全、以及数据的持久化保留是至
二、脚本内容
一些Shell小工具、脚本已经整合至个人的GitHub仓库,有兴趣的小伙伴可以查看下载。
GitHub:https://github.com/dqzboy/ShellProject
#!/usr/bin/env bash
#===============================================================================
#
# FILE: Disk_Initialize.sh
#
# USAGE: ./Disk_Initialize.sh
#
# DESCRIPTION: Linux CentOS Disk免交互格式化分区、挂载
#
# ORGANIZATION: DingQz dqzboy.com
# CREATED: 2021
#===============================================================================
# 安装软件
EXPECT=`yum list installed | grep -w expect`
if [ $? -eq 0 ];then
echo
echo
echo "--------------------------------------------------------------------------"
echo " Yum Install expect Skip..."
echo "--------------------------------------------------------------------------"
echo
echo
else
yum -y install expect
echo
echo
echo "--------------------------------------------------------------------------"
echo " Yum Install expect Done..."
echo "--------------------------------------------------------------------------"
echo
echo
fi
# 解锁文件
chattr -i /etc/fstab
OS_CN() {
# 注意修改磁盘名称前缀,例如这里的 /sd
Dsk=`lsblk -r --output NAME,MOUNTPOINT | awk -F \/ '/sd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }'`
Disk="/dev/${Dsk}"
DiskP="${Disk}1"
if [ ! -z "${Dsk}" ];then
expect -c "
spawn fdisk ${Disk}
expect {
-re \"(.*)三思(.*)(\n*)(.*)获取帮助(.*)\" { send \"n\r\";exp_continue }
\"default p\" { send \"\r\";exp_continue }
\"分区号\" { send \"\r\";exp_continue }
\"起始 扇区\" { send \"\r\";exp_continue }
\"Last 扇区\" { send \"\r\";exp_continue }
\"命令\" { send \"wq\r\";exp_continue }
}
"
else
echo
echo
echo "--------------------------------------------------------------------------"
echo " The new disk was not found. Exit..."
echo "--------------------------------------------------------------------------"
echo
exit 1
fi
}
OS_EN() {
Dsk=`lsblk -r --output NAME,MOUNTPOINT | awk -F \/ '/sd/ { dsk=substr($1,1,3);dsks[dsk]+=1 } END { for ( i in dsks ) { if (dsks[i]==1) print i } }'`
Disk="/dev/${Dsk}"
DiskP="${Disk}1"
if [ ! -z "${Dsk}" ];then
expect -c "
spawn fdisk ${Disk}
expect {
-re \"(.*)careful(.*)(\n*)(.*)help(.*)\" { send \"n\r\";exp_continue }
\"default p\" { send \"\r\";exp_continue }
\"Partition number\" { send \"\r\";exp_continue }
\"First sector\" { send \"\r\";exp_continue }
\"Last sector\" {send \"\r\";exp_continue }
\"Command\" { send \"wq\r\";exp_continue }
}
"
else
echo
echo
echo "--------------------------------------------------------------------------"
echo " The new disk was not found. Exit..."
echo "--------------------------------------------------------------------------"
echo
exit 1
fi
}
Mkfs_Disk() {
sleep 3
# 格式化分区
Partitions=`blkid | awk -F ":" '{print $1}'| grep "${DiskP}"`
if [ $? -eq 0 ];then
echo
echo
echo "--------------------------------------------------------------------------"
echo " Formatted Done"
echo "--------------------------------------------------------------------------"
echo
echo
else
mkfs.xfs -f -n ftype=1 ${DiskP}
fi
if ! grep "${DiskP}" /etc/fstab; then
cat >> /etc/fstab <<EOF
${DiskP} /data xfs defaults 1 0
EOF
echo "--------------------------------------------------------------------------"
fi
# 创建挂载目录
mkdir -p /data
mount ${DiskP} /data
# 非ROOT用户不允许编辑以下文件
chattr +i /etc/fstab
}
# 判断系统语言
OS_LANG=`echo $LANG | awk -F "." '{print $1}'`
if [ "${OS_LANG}" == "en_US" ];then
OS_EN
Mkfs_Disk
elif [ "${OS_LANG}" == "zh_CN" ];then
OS_CN
Mkfs_Disk
else
echo "Sorry script does not support this system."
fi
三、脚本分析
主要是对脚本中使用到的expect工具的几个参数进行介绍说明:
expect -c:-c 标志在脚本中的任何命令之前添加要执行的命令。 该命令应该被引用以防止被shell分解spawn:命令激活一个Unix程序来进行交互式的运行;交互程序开始后面跟命令或者指定程序expect:获文章来源(Source):浅时光博客 取匹配信息匹配成功则执行expect后面的程序动作send:用于发送指定的字符串信息exp_continue:在expect中多次匹配时,继续执行下面的匹配

必须 注册 为本站用户, 登录 后才可以发表评论!