前端之家收集整理的这篇文章主要介绍了
Centos-automount,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/bin/bash
#=======================================================================================
#
#FILE:automount
#USAGE:automount[-h][CopyBash_Code_Style]
#DESCRIPTION:Copybashstyleguideandcodingstandard.
#OPTIONS:seefuction'usage'below
#REQUIREMENTS:---
#BUGS:---
#NOTES:---
#AUTHOR:Dr.-Vision.Simple(sp).
#COMPANY:China
#VERSION:1.0
#CREATED:28.09.2016-10:15:30
#REVISION:28.09.2016
#PROJECT:XXX
#COPYRITHT:Copyright(c)2002-2020Shell,AllRightsReserved
#=======================================================================================
#Ifrequired,additionalinformationhastobeadded(e.gcopyrightnote,projectassignment).
TODAY=`date+%Y-%m-%d`
NOW=`date+%H-%M`
BASE=$0
BASEDIR=$(dirname$0)
USAGE=$(echo-e"${BASE%%.*}")
LOG_PATH="${BASEDIR}/proclog/${TODAY}/${USAGE}"
mkdir-p${LOG_PATH}
CMD_LOG="${LOG_PATH}/${NOW}_${USAGE}.cmd"
INFO_LOG="${LOG_PATH}/${NOW}_${USAGE}.info"
ERROR_LOG="${LOG_PATH}/${NOW}_${USAGE}.err"
FSTAB_NAME="/etc/fstab"
BAK_FSTAB="/etc/fstab.bak"
MOUNT_FILE_INFO="/tmp/mount_info"
MOUNT_FILE="/tmp/mount_file"
SFDISK_FILE_INFO="/tmp/sfdisk_info"
DIFF_INFO="/tmp/sfdisk_diff_info"
DIFF_FILE="/tmp/sfdisk_diff_file"
MOUNT_POINT="/data/dev"
MTYPE="ext4"
FLAG=0
exec4>${MOUNT_FILE_INFO}
exec5>${SFDISK_FILE_INFO}
exec6>${DIFF_INFO}
#Functioncomments
#===FUNCTION====================================================================
#NAME:
#DESCRIPTION:Displayusageinformationforthisscript.
#PARAMETER1:---
#=======================================================================================
functionreadme{
echo-e'\033[01;33m'
cat<<EOF
*******************************************
KeepWarnning.
*******************************************
Keys:
1.Check/etc/fstab.bakisexistsandsetupbasedir.
2.read/etc/fstabandsavein/etc/fstab.bak.
3.getdiskdeviceinfo
4.formatandmountdisk
5.write/etc/fstab.
*******************************************
EOF
echo-e'\033[01;0m'
read-p"线上设备已经格式化,确认?(y/n)"answer
case$answerin
y|Y|yes|Yes|YES)
echo""
;;
n|N|no|No|NO)
rm-rf${DIFF_INFO}${MOUNT_FILE_INFO}${SFDISK_FILE_INFO}
echo"退出脚本!"
exit0
;;
*)
echo"退出脚本!"
exit0
;;
esac
}
functioncheck_env{
#>${INFO_LOG}
#FIRSTRUN
iftest!-d"${LOG_PATH}";then
mkdir-p"${LOG_PATH}"
exec2>${ERROR_LOG}
exec8>>${CMD_LOG}
echo-e"################################1.1CheckEnvStart################################\n"1>&8
else
exec2>${ERROR_LOG}
exec8>>${CMD_LOG}
echo-e"################################1.1CheckEnvStart################################\n"1>&8
fi
if[[-e${BAK_FSTAB}]];then
printf"\e[32m#############${NOW}CheckFile/etc/fstab[AlreadyExists]############\e[0m\n"
printf"\e[32m#############${NOW}CheckFile/etc/fstab[AlreadyExists]############\e[0m\n">${INFO_LOG}
echo"rm-rf${DIFF_INFO}${MOUNT_FILE_INFO}${MOUNT_FILE}${SFDISK_FILE_INFO}${SFDISK_FILE}\n"1>&8
rm-rf${DIFF_INFO}${MOUNT_FILE_INFO}${MOUNT_FILE}${SFDISK_FILE_INFO}${SFDISK_FILE}
exit0
else
exec3>${BAK_FSTAB}
FLAG=1
echo"exec3>${BAK_FSTAB}FLAG=1"1>&8
printf"\e[32m################FirstWrite/etc/fstab[Running]##############\e[0m\n"
printf"\e[32m################FirstWrite/etc/fstab[Running]##############\e[0m\n">${INFO_LOG}
fi
echo"################################1.1CheckEnvEnd################################"1>&8
}
functionread_fstab{
echo"################################1.2ReadfstabStart################################"1>&8
whilereadDATA
do
echo"${DATA}"
done<${FSTAB_NAME}>&3
wait;
printf"\e[32m################Read/etc/fstab[OK]##############\e[0m\n"
printf"\e[32m################Read/etc/fstab[OK]##############\e[0m\n">>${INFO_LOG}
echo"################################1.2ReadfstabEnd################################"1>&8
}
functionget_device_info(){
#mount_device_info=`df-Th|tail-n+2|awk'{print$1}'|grep-v"tmpfs"|awk'{printsubstr($0,1,length-1)}'|uniq`
echo"#############################1.3GetDeviceinfoStart#############################"1>&8
mount_device_info=`df-Th|tail-n+2|awk'{print$1}'|grep-v"tmpfs\|sda[0-9]"|uniq`
sfdisk_device_info=`ls/dev/sd*|grep-v"[0-9]\|sda"`
echo"${mount_device_info}">&4
echo"${sfdisk_device_info}">&5
diff_info="`sort${MOUNT_FILE_INFO}${SFDISK_FILE_INFO}|uniq`"
echo"diff_info:sort${MOUNT_FILE_INFO}${SFDISK_FILE_INFO}|uniq"1>&8
if[[${diff_info}!=""]];then
echo"${diff_info}|tr-d\"[0-9]\"|awk'{if(length!=0)print$0}'>&6"1>&8
echo-e"${diff_info}"|tr-d"[0-9]"|awk'{if(length!=0)print$0}'>&6
printf"\e[32m################GetDeviceInfo[Success]##############\e[0m\n"
printf"\e[32m################GetDeviceInfo[Success]##############\e[0m\n">>${INFO_LOG}
else
echo"cat/dev/null>&6"1>&8
cat/dev/null>&6
printf"\e[32m################GetDeviceInfo[Failed]##############\e[0m\n"
printf"\e[32m################GetDeviceInfo[Failed]##############\e[0m\n">>${INFO_LOG}
fi
echo"#############################1.3GetDeviceinfoEnd#############################"1>&8
}
functionwrite_device_info{
echo"#############################1.4PreWritefstabStart#########################"1>&8
echo"#############################1.4PreWritefstabStart#########################">>${INFO_LOG}
DIFF_SIZE=$(du${DIFF_INFO}|awk'{print$1}')
echo-e"################WriteFstab.bakStart-Time:${TODAY}################\n">>${BAK_FSTAB}
if[[-s${DIFF_INFO}&&${DIFF_SIZE}-ge4]];then
devices=$(cat${DIFF_INFO}|wc-l)
awk'$0=""NR""$0'${DIFF_INFO}>${DIFF_FILE}
if[[${DIFF_SIZE}-gt1&&${FLAG}!=0]];then
for((id=1;id<=${devices};id++))
do
eval`awk-vid=$id'{if($1==id)print"name="$2";id="$1}'${DIFF_FILE}`
num=$(expr${id}-1)
#echo"mount-oremount,rw-t${MTYPE}${name}${MOUNT_POINT}${num}"1>&1|tee${MOUNT_FILE}>>${FSTAB_NAME}
remount=`df-Th|grep"${MOUNT_POINT}${num}"|wc-l`
if[[${remount}-eq0]];then
echo"mount-t${MTYPE}${name}${MOUNT_POINT}${num}"1>&8
mount-t${MTYPE}${name}"${MOUNT_POINT}${num}"
else
echo"mount-oremount,rw-t${MTYPE}${name}${MOUNT_POINT}${num}"1>&8
mount-oremount,rw-t${MTYPE}${name}"${MOUNT_POINT}${num}"
fi
if[!-f"${MOUNT_POINT}${num}/${num}"];then
mkdir-p"${MOUNT_POINT}${num}"&&touch"${MOUNT_POINT}${num}/${num}"
echo"mkdir-p${MOUNT_POINT}${num}&&touch${MOUNT_POINT}${num}/${num}"1>&8
echo"${name}\t\t${MOUNT_POINT}${num}\t\t${MTYPE}\tdefaults00>>${BAK_FSTAB}"1>&8
echo-e"${name}\t\t${MOUNT_POINT}${num}\t\t${MTYPE}\tdefaults00">>${BAK_FSTAB}
printf"\e[32m################Disk${name}isAlreadMount[OK]##############\e[0m\n"
printf"\e[32m################Disk${name}isAlreadMount[OK]##############\e[0m\n">>${INFO_LOG}
else
printf"\e[32m################Disk${name}isAlreadMount[OK]##############\e[0m\n">>${INFO_LOG}
printf"\e[32m################Disk${name}isAlreadMount[OK]##############\e[0m\n"
echo"${name}\t\t${MOUNT_POINT}${num}\t\t${MTYPE}\tdefaults00>>${BAK_FSTAB}"1>&8
echo-e"${name}\t\t${MOUNT_POINT}${num}\t\t${MTYPE}\tdefaults00">>${BAK_FSTAB}
fi
done
fi
echo"rm-rf${DIFF_INFO}${MOUNT_FILE_INFO}${MOUNT_FILE}${SFDISK_FILE_INFO}${SFDISK_FILE}"1>&8
rm-rf${DIFF_INFO}${MOUNT_FILE_INFO}${MOUNT_FILE}${SFDISK_FILE_INFO}${SFDISK_FILE}
else
printf"\e[32m################AllDiskisAlreadMount[OK]##############\e[0m\n"
printf"\e[32m################AllDiskisAlreadMount[OK]##############\e[0m\n">>${INFO_LOG}
fi
echo-e"################WriteFstab.bakEnd-Time:${TODAY}################\n">>${BAK_FSTAB}
echo"#############################1.4PreWritefstabEnd#########################"1>&8
echo"#############################1.4PreWritefstabEnd#########################">>${INFO_LOG}
}
functionwrite_fstab{
echo"#############################1.5WritefstabStart#########################"1>&8
if[[-f${BAK_FSTAB}&&-f${FSTAB_NAME}]];then
exportcomm_result="$(comm-3${BAK_FSTAB}${FSTAB_NAME})"
fi
echo-e'\033[01;33m'
cat<<EOF>>${INFO_LOG}
****************************************************************************************
WriteWarnning.
****************************************************************************************
WriteKeys:
1.Writemountdeviceinfoandautomount.
2.result=$(echo-e"$comm_result")
****************************************************************************************
EOF
echo"${comm_result}">>${FSTAB_NAME}
awk'!a[$0]++'${BAK_FSTAB}>${FSTAB_NAME}
echo"#############################1.5WritefstabEnd#########################"1>&8
}
functionmain_func{
readme
check_env
read_fstab
get_device_info
write_device_info
write_fstab
}
#Sectioncomments
#---------------------------------------------------------------------------------------
#definemain.
#---------------------------------------------------------------------------------------
main_func
#lineendcomments
原文链接:https://www.f2er.com/centos/378695.html