首页 > 其他分享 >分区脚本

分区脚本

时间:2022-11-04 11:59:34浏览次数:43  
标签:脚本 grep 分区 dev echo release LOCKfile CEND

#!/bin/bash


export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export LANG=en_US.UTF-8
clear
printf "
#######################################################################
#       OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+      #
#                             Auto fdisk                              #
#######################################################################
"
# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }

oneinstack_dir=$(dirname "`readlink -f $0`")
pushd ${oneinstack_dir} > /dev/null

echo=echo
for cmd in echo /bin/echo; do
  $cmd >/dev/null 2>&1 || continue
  if ! $cmd -e "" | grep -qE '^-e'; then
    echo=$cmd
    break
  fi
done
CSI=$($echo -e "\033[")
CEND="${CSI}0m"
CDGREEN="${CSI}32m"
CRED="${CSI}1;31m"
CGREEN="${CSI}1;32m"
CYELLOW="${CSI}1;33m"
CBLUE="${CSI}1;34m"
CMAGENTA="${CSI}1;35m"
CCYAN="${CSI}1;36m"
CSUCCESS="$CDGREEN"
CFAILURE="$CRED"
CQUESTION="$CMAGENTA"
CWARNING="$CYELLOW"
CMSG="$CCYAN"

get_char() {
  SAVEDSTTY=`stty -g`
  stty -echo
  stty cbreak
  dd if=/dev/tty bs=1 count=1 2> /dev/null
  stty -raw
  stty echo
  stty $SAVEDSTTY
}

MOUNT_DIR=/data
FSTAB_FILE=/etc/fstab

count=0
TMP1=/tmp/.tmp1
TMP2=/tmp/.tmp2

> $TMP1
> $TMP2

# check lock file, one time only let the script run one time 
LOCKfile=/tmp/.$(basename $0)
if [ -f "$LOCKfile" ];then
  echo
  echo "${CWARNING}The script is already exist, please next time to run this script${CEND}"
  echo
  exit
else
  echo
  echo "${CMSG}Step 1.No lock file, begin to create lock file and continue${CEND}"
  echo
  touch $LOCKfile
fi

# check disk partition
check_disk() {
  > $LOCKfile
  for i in `fdisk -l | grep -oE 'Disk /dev/x?[sv]d[b-z]' | awk '{print $2}'`
  do
    if [ -z "$(blkid | grep -v 'PTTYPE="dos"' | grep -w "$i")" ];then
      if [ -z "$(mount | grep "$i")" -a ! -e "${i}1" ]; then
        echo $i >> $LOCKfile
        echo "You have a free disk, Now will fdisk it and mount it"
      fi
      if [ -z "$(mount | grep "$i")" -a -e "${i}1" ]; then
         echo "${CWARNING}The $i has been partitioned! ${CEND}" 
         while :; do echo
           read -p "Do you want to reformat the partition? [y/n]: " reformat_yn
           if [[ ! ${reformat_yn} =~ ^[y,n]$ ]]; then
             echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
           else
             if [ "${reformat_yn}" == 'y' ]; then
               echo "${CWARNING}You will format $i, Please backup the data in ${i}! ${CEND}"
               echo "Press Ctrl+c to cancel or Press any key to continue..."
               char=$(get_char)
               dd if=/dev/zero of=$i bs=512 count=1 &>/dev/null
               echo $i >> $LOCKfile
             fi
             break
           fi
         done
      fi
    fi
  done
  DISK_LIST=$(cat $LOCKfile)
  if [ "X$DISK_LIST" == "X" ];then
    echo
    echo "${CWARNING}No free disk need to be fdisk. Exit script${CEND}"
    echo
    rm -rf $LOCKfile
    exit 0
  else
    echo "${CMSG}This system have free disk :${CEND}"
    for i in `echo $DISK_LIST`
    do
      echo "$i"
      count=$((count+1))
    done
    [ $count -gt 1 ] && { echo "${CWARNING}This system has at least two free disk, You must manually mount it${CEND}"; exit 0; } 
  fi
}

# check os
check_os() {
  os_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null)
  os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null)
  if [ "$os_release" ] && [ "$os_release_2" ];then
    if echo "$os_release" | grep "release 5" >/dev/null 2>&1;then
      os_release=aliyun5
      modify_env
    fi
  fi
}

# install ext4
modify_env() {
  modprobe ext4
  yum -y install e4fsprogs
}

# fdisk ,formating and create the file system
fdisk_fun() {
fdisk -S 56 $1 << EOF
n
p
1


wq
EOF

sleep 5
mkfs.ext4 ${1}1
}

# make directory
make_dir() {
  echo "${CMSG}Step 4.Begin to make directory${CEND}"
  while :; do echo
    read -p "Please enter a location to mount (Default directory: /data): " MOUNT_DIR 
    [ -z "$MOUNT_DIR" ] && MOUNT_DIR=/data
    if [ -z "`echo $MOUNT_DIR | grep '^/'`" ]; then
      echo "${CWARNING}input error! The directory must begin with / ${CEND}"
    else
      break
    fi
  done
  [ ! -e "$MOUNT_DIR" ] && mkdir -p $MOUNT_DIR
  mkdir -p $MOUNT_DIR
  echo "$MOUNT_DIR" >> $TMP1
}

# config /etc/fstab and mount device
main() {
  for i in `echo $DISK_LIST`
  do
    echo
    echo "${CMSG}Step 3.Begin to fdisk free disk${CEND}"
    [ -n "`mount | grep ${i}1`" ] && { echo "${CFAILURE}The ${i}1 already mount${CEND}"; echo; exit 0; }
    fdisk_fun $i > /dev/null 2>&1
    echo
    echo "${i}1" >> $TMP2
  done
  make_dir
  > $LOCKfile
  paste $TMP2 $TMP1 > $LOCKfile
  echo
  echo "${CMSG}Step 5.Begin to write configuration to /etc/fstab and mount device${CEND}"
  while read a b
  do
    Dline=`grep -n ^${a} /etc/fstab | awk -F: '{print $1}'`
    [ -n "$Dline" ] && sed -i "${Dline}d" $FSTAB_FILE
    echo "${a}	$b	ext4	defaults	0 0" >> $FSTAB_FILE
  done < $LOCKfile
  mount -a
  echo
}

# start script
echo "${CMSG}Step 2.Begin to check free disk${CEND}"
#service mysqld stop
#mv /data /root
check_os
check_disk
main
df -h
#mv /root/data/* /data
#service mysqld start
rm -rf $LOCKfile $TMP1 $TMP2

标签:脚本,grep,分区,dev,echo,release,LOCKfile,CEND
From: https://www.cnblogs.com/newsea/p/16857236.html

相关文章

  • s-sgdisk源码分析 “--set-alignment=value分区对齐参数”
    文章目录​​边界对齐子命令使用​​​​源码分析​​​​sgdisk.ccmain函数入口​​​​gptcl.ccDoOptions解析并执行具体命令函数​​​​gpt.ccCreatePartition创建分......
  • t-testdisk 分区恢复
    安装配置好yum源,直接使用yum​​yuminstalltestdisk-y​​​安装好之后直接运行​​testdisk​​或者​​testdisk/dev/sdx​​进入如下界面,选择​​create​​进行日......
  • A01.shell脚本在子 shell和当前shell执行的区别
    简单测试脚本如下#!/bin/bashcd/homels执行顺序如子shell命令自行的方式:[root@pythonshell]#shsubshell.shwuziqicli[root@pythonshell]#bashsubshell.sh......
  • Win10系统设置在开机,关机,登录时执行脚本
    Win10系统设置在开机,关机,登录时执行脚本有时为了特定需要Win10在开机,关机,登录时等情况下执行某个脚本,那么需要如何操作呢?我们可以利用组策略管理功能,分别在计算机配置......
  • gimp脚本.scm一些心得记录
    gimp的脚本scm一些心得记录GIMP2.10图像处理脚本推荐GIMP使用脚本使得图像处理的功能更加强大,对于一些常见操作可以通过脚本快速实现。GIMP也出来2.10版本了,比前面的2.8......
  • samba服务部署+脚本一键部署
    @目录samba文件服务器windows测试centos测试挂载ubuntu脚本一键部署服务端部署环境:Ubuntu22客户端环境;centos7samba文件服务器安装samba服务aptinstall-ysamba......
  • shell结合expect 脚本自动备份华为交换机配置文件
    最近看了点expect的知识点,就产生了个利用它来自动备份交换机配置的想法。于是就做了下尝试。来看看如何实现。Expect是一个用来实现自动交互功能的软件套件,是基于TCL的脚......
  • Android分区简述
    Android分区在Android目录下,总是能够看到system,data等文件夹,这里简单对Android分区的概念进行简单的整理。在PMS进行初始化的时候,在扫描阶段会去创建分区的子目录,例如:/......
  • cloud stream分区分析
    上一篇博客从使用的角度了解了springcloudstream。现在得知道为什么其可以生效。我们针对分区做了下面的配置:#cloudstream相关spring.cloud.stream.bindings.input.de......
  • 通过Jenkins,实现远程服务器的脚本自动执行
    通过Jenkins,可自动执行写好的应用更新部署脚本,实现测试环境应用的自动定时更新。1.安装SSH插件进入【系统管理】-【插件管理】-【可用插件】,搜索PublishOverSSH......