把之前在腾讯云上部署环境过程中遇到的可以抽象摘取出来的固定逻辑,稍加整理供大家参考。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #!/bin/bash function Usage (){ cat << EOF Usage: sh $0 sh $0 -d MOUNTDIRECTORY sh $0 -h|--help Options: -d|--MountDirectory {eg:/data}: Mount directory. -h|--help: Show help. EOF } OPTS=$(getopt -o d:h -a --long help ,MountDirectory: -n 'warning' -- "$@ " ) eval set -- "$OPTS " while true ;do case "$1 " in -d|--MountDirectory) MountDirectory=$2 ; shift 2 ;; -h|--help ) Usage; shift ; exit 0 ;; --) shift ; break ;; *) echo -e "\nParameter error.\n" ; Usage; exit 1 ;; esac done function disk_mount (){ if [ -b /dev/vdb ]; then echo -e "\033[32m$(date "+%Y-%m-%d %H:%M:%S" ) \tfdisk /dev/vdb \033[0m" fdisk /dev/vdb 1>/dev/null << EOF n p 1 wq EOF wait if [ -b /dev/vdb1 ]; then echo -e "\033[32m$(date "+%Y-%m-%d %H:%M:%S" ) \t mkfs.ext4 /dev/vdb1 \033[0m" mkfs.ext4 /dev/vdb1 1>/dev/null sleep 3 echo -e "\033[32m$(date "+%Y-%m-%d %H:%M:%S" ) \t mounting -a \033[0m" mkdir -p ${MountDirectory} if ! grep "${MountDirectory} " /etc/fstab >/dev/null 2>&1 ; then echo -e "/dev/disk/by-uuid/$(ls -l /dev/disk/by-uuid/ |grep vdb1 |awk '{print $9}') ${MountDirectory} ext4 defaults,nofail 0 2" >>/etc/fstab && mount -a echo -e "\033[32m$(date "+%Y-%m-%d %H:%M:%S" ) \t Finished. \033[0m" df -h |grep "${MountDirectory} " else echo -e "\033[31m /etc/fstab record has already existed. \033[0m" fi else echo -e "\033[31m fdisk failed. \033[0m" fi else echo -e "\033[31m/dev/vdb does not exist. \033[0m" fi } function main (){ MountDirectory="$(echo "${MountDirectory} " |grep -Po '^/(\w+/) *\w+')" [ -z "${MountDirectory} " ] && MountDirectory=/tencentCloud/data; disk_mount } main