自动分区、格式化、挂载脚本


#1

功能: 自动检测是否有尚未分区的数据盘,格式化新的数据盘并自动挂载

解决了什么问题: 一键式检测是否有尚未分区的数据盘,并能对其格式化和自动挂载,省去了复杂的命令和步骤

执行方法: 以root身份执行命令

  1. wget http://mirrors.linuxeye.com/scripts/auto_fdisk.sh
  2. chmod +x auto_fdisk.sh
  3. ./auto_fdisk.sh

结果: 出现如下即自动分区、格式化、挂载成功:

脚本内容如下:

  1. #!/bin/bash
  2. Author: yeho <lj2007331 AT gmail.com>

  3. BLOG: https://linuxeye.com

  4. Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+

  5. Project home page:

  6. http://oneinstack.com

  7. https://github.com/lj2007331/oneinstack

  8. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  9. clear
  10. printf "
  11. #######################################################################
  12. OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+

  13. Auto fdisk

  14. For more information please visit http://oneinstack.com

  15. #######################################################################
  16. "
  17. echo=echo
  18. for cmd in echo /bin/echo; do
  19. $cmd >/dev/null 2>&1 || continue
  20. if ! $cmd -e “” | grep -qE ‘^-e’; then
  21. echo=$cmd
  22. break
  23. fi
  24. done
  25. CSI=$($echo -e “\033[”)
  26. CEND="${CSI}0m"
  27. CDGREEN="${CSI}32m"
  28. CRED="${CSI}1;31m"
  29. CGREEN="${CSI}1;32m"
  30. CYELLOW="${CSI}1;33m"
  31. CBLUE="${CSI}1;34m"
  32. CMAGENTA="${CSI}1;35m"
  33. CCYAN="${CSI}1;36m"
  34. CSUCCESS="$CDGREEN"
  35. CFAILURE="$CRED"
  36. CQUESTION="$CMAGENTA"
  37. CWARNING="$CYELLOW"
  38. CMSG="$CCYAN"
  39. Check if user is root

  40. [ $(id -u) != “0” ] && { echo “${CFAILURE}Error: You must be root to run this script${CEND}”; exit 1; }
  41. MOUNT_DIR=/data
  42. FSTAB_FILE=/etc/fstab
  43. count=0
  44. TMP1=/tmp/.tmp1
  45. TMP2=/tmp/.tmp2
  46. > $TMP1
  47. > $TMP2
  48. check lock file, one time only let the script run one time

  49. LOCKfile=/tmp/.$(basename $0)
  50. if [ -f “$LOCKfile” ];then
  51. echo
  52. echo “${CWARNING}The script is already exist, please next time to run this script${CEND}”
  53. echo
  54. exit
  55. else
  56. echo
  57. echo “${CMSG}Step 1.No lock file, begin to create lock file and continue${CEND}”
  58. echo
  59. touch $LOCKfile
  60. fi
  61. check disk partition

  62. check_disk() {
  63. > $LOCKfile
  64. for i in fdisk -l | grep "Disk" | grep "/dev" | awk '{print $2}' | awk -F: '{print $1}' | grep "vd"
  65. do
  66. DEVICE_COUNT=$(fdisk -l $i | grep “$i” | awk ‘{print $2}’ | awk -F: ‘{print $1}’ | wc -l)
  67. NEW_MOUNT=$(df -h)
  68. if [ $DEVICE_COUNT -lt 2 ];then
  69. if [ -n “$(echo $NEW_MOUNT | grep -w “$i”)” -o “$(grep -v ‘^#’ $FSTAB_FILE | grep -v ^$ | awk ‘{print $1,$2,$3}’ | grep -w “$i” | awk ‘{print $2}’)” == ‘/’ -o “$(grep -v ‘^#’ $FSTAB_FILE | grep -v ^$ | awk ‘{print $1,$2,$3}’ | grep -w “$i” | awk ‘{print $3}’)” == ‘swap’ ];then
  70. echo “${CWARNING}The $i disk is mounted${CEND}”
  71. else
  72. echo $i >> $LOCKfile
  73. echo “You have a free disk, Now will fdisk it and mount it”
  74. fi
  75. fi
  76. done
  77. DISK_LIST=$(cat $LOCKfile)
  78. if [ “X$DISK_LIST” == “X” ];then
  79. echo
  80. echo “${CWARNING}No free disk need to be fdisk. Exit script${CEND}”
  81. echo
  82. rm -rf $LOCKfile
  83. exit 0
  84. else
  85. echo “${CMSG}This system have free disk :${CEND}”
  86. for i in echo $DISK_LIST
  87. do
  88. echo “$i”
  89. count=$((count+1))
  90. done
  91. [ $count -gt 1 ] && { echo “${CWARNING}This system has at least two free disk, You must manually mount it${CEND}”; exit 0; }
  92. fi
  93. }
  94. check os

  95. check_os() {
  96. os_release=$(grep “Aliyun Linux release” /etc/issue 2>/dev/null)
  97. os_release_2=$(grep “Aliyun Linux release” /etc/aliyun-release 2>/dev/null)
  98. if [ “$os_release” ] && [ “$os_release_2” ];then
  99. if echo “$os_release” | grep “release 5” >/dev/null 2>&1;then
  100. os_release=aliyun5
  101. modify_env
  102. fi
  103. fi
  104. }
  105. install ext4

  106. modify_env() {
  107. modprobe ext4
  108. yum -y install e4fsprogs
  109. }
  110. fdisk ,formating and create the file system

  111. fdisk_fun() {
  112. fdisk -S 56 $1 << EOF
  113. n
  114. p
  115. 1
  116. wq
  117. EOF
  118. sleep 5
  119. mkfs.ext4 ${1}1
  120. }
  121. make directory

  122. make_dir() {
  123. echo “${CMSG}Step 4.Begin to make directory${CEND}”
  124. [ -d “$MOUNT_DIR” ] && mv ${MOUNT_DIR}{,_bk}
  125. mkdir -p $MOUNT_DIR
  126. echo “$MOUNT_DIR” >> $TMP1
  127. }
  128. config /etc/fstab and mount device

  129. main() {
  130. for i in echo $DISK_LIST
  131. do
  132. echo
  133. echo “${CMSG}Step 3.Begin to fdisk free disk${CEND}”
  134. [ -n “df -h | grep ${i}1” ] && { echo “${CFAILURE}The ${i}1 already mount${CEND}”; echo; exit 0; }
  135. fdisk_fun $i > /dev/null 2>&1
  136. echo
  137. echo “${i}1” >> $TMP2
  138. done
  139. make_dir
  140. > $LOCKfile
  141. paste $TMP2 $TMP1 > $LOCKfile
  142. echo
  143. echo “${CMSG}Step 5.Begin to write configuration to /etc/fstab and mount device${CEND}”
  144. while read a b
  145. do
  146. [ -z “grep ^${a} $FSTAB_FILE” -a -z “grep ${b} $FSTAB_FILE” ] && echo “${a} $b ext4 defaults 0 0” >> $FSTAB_FILE
  147. done < $LOCKfile
  148. mount -a
  149. echo
  150. }
  151. start script

  152. echo “${CMSG}Step 2.Begin to check free disk${CEND}”
  153. #service mysqld stop
  154. #mv /data /root
  155. check_os
  156. check_disk
  157. main
  158. df -h
  159. #mv /root/data/* /data
  160. #service mysqld start
  161. rm -rf $LOCKfile $TMP1 $TMP2