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

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

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

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

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

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

脚本内容如下:

#!/bin/bash
# Author:  yeho <lj2007331 AT gmail.com>
# BLOG:  https://blog.linuxeye.com
#
# Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
#
# Project home page:
#       http://oneinstack.com
#       https://github.com/lj2007331/oneinstack

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
printf "
#######################################################################
#       OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+      #
#                             Auto fdisk                              #
#       For more information please visit http://oneinstack.com       #
#######################################################################
"

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"

# Check if user is root
[ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; } 

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 "Disk" | grep "/dev" | awk ‘{print $2}‘ | awk -F: ‘{print $1}‘ | grep "vd"`
    do
        DEVICE_COUNT=$(fdisk -l $i | grep "$i" | awk ‘{print $2}‘ | awk -F: ‘{print $1}‘ | wc -l)
        NEW_MOUNT=$(df -h)
        if [ $DEVICE_COUNT -lt 2 ];then
            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
                echo "${CWARNING}The $i disk is mounted${CEND}"
            else
                echo $i >> $LOCKfile
                echo "You have a free disk, Now will fdisk it and mount it"
            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}"
    [ -d "$MOUNT_DIR" ] && mv ${MOUNT_DIR}{,_bk}
    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 "`df -h | 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
        [ -z "`grep ^${a} $FSTAB_FILE`" -a -z "`grep ${b} $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

原文:https://blog.linuxeye.com/433.html

时间: 2024-10-09 22:29:37

自动分区、格式化、挂载脚本的相关文章

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

#!bin/bash#功能:自动对磁盘分区.格式化.挂载#作者:liusingbon#对虚拟机的 vdb 磁盘进行分区格式化,使用<<将需要的分区指令导入给程序 fdisk,#n(新建分区),p(创建主分区),e(创建扩展分区),1(分区编号为 1,以此类推),w(保存并退出)#注意:1,2,3,4后面的空白行必须存在. 创建3个主分区+1个扩展分区.fdisk /dev/vdb << EOFnp1 +1Gnp2 +2Gnp3 +3Gne4 wEOF#格式化已创建的分区1mkfs.

(转)centos7.4 fdisk磁盘分区 格式化 挂载

centos7.4 fdisk磁盘分区 格式化 挂载 原文:http://blog.csdn.net/capecape/article/details/78499351 1.查看系统中有多少可以识别的硬盘.U盘 [root@localhost ~]# fdisk -l 1 2.使用fdisk命令进行分区 [root@localhost ~]# fdisk /dev/sdb 1  记得输入w保存退出! 3.通知操作系统,分区表已经改变 [root@localhost ~]# partprobe 1

分区.格式化.挂载

分区——>格式化——>挂载 [[email protected] ~]# ls -l /dev/sd* 分区: [[email protected] ~]# fdisk /dev/sdb [[email protected] ~]# ls -l /dev/sdb* [[email protected] ~]# partprobe     (同步分区信息) 格式化: [[email protected] ~]# mkfs 两次Tab键 [[email protected] ~]# mkfs.xf

一键自动创建磁盘分区以及格式化挂载脚本

说明: 1.默认分3个区,其中一个是SWAP分区 2.分区的大小没有写成交互式,直接定义在脚本,需要在脚本修改 3.只是演示测试,sda磁盘没有设置自动分区功能,可把sdb分区代码拷贝到sda位置即可 4.分区前会把分区磁盘的分区表先重置,请提前做好数据备份!! #!/bin/bash #Author: Hill.Mai #Contact: [email protected] #Create time: 2016-08-31 08:43:29 #Description:auto disks pa

Linux磁盘管理----分区格式化挂载fdisk、mkfs、mount

1,磁盘分区命令fdisk [[email protected] ~]# fdisk -l #查看磁盘分区情况 Disk /dev/sda: 10.8 GB, 10837518250 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280bytes (这个硬盘的大小是10.8GB,有255个磁面,63个扇区,1305磁柱(cylinders) 每个cylinder(磁

raid --&gt; 分区--&gt; 格式化 --&gt;挂载

###分区相关   d delete a partition             #注:这是删除一个分区的动作:   l list known partition types      #注:列出分区类型,以供我们设置相应分区的类型:   m print this menu             #注:列出帮助信息:   n add a new partition         #注:添加一个分区:   p print the partition table         #注:列出分

磁盘分区 格式化 挂载

磁盘分区:是使用分区编辑器(partition editor)在磁盘上划分几个逻辑部分.碟片一旦划分成数个分区(Partition),不同类的目录与文件可以存储进不同的分区. 格式化:(高级格式化)又称逻辑格式化,它是指根据用户选定的文件系统(如FAT16.FAT32.NTFS.EXT2.EXT3.EXT4等),在磁盘的特定区域写入特定数据,在分区中划出一片用于存放文件分配表.目录表等用于文件管理的磁盘空间. 挂载:在windows操作系统中, 挂载通常是指给磁盘分区(包括被虚拟出来的磁盘分区)

linux磁盘分区格式化、挂载,文件系统

一.硬盘分区&格式化&挂载 RHEL5强制刷新分区表 partprobe /dev/sdb RHEL6强制刷新分区表 partx -a /dev/sdb 1.创建文件系统:挂载分区&格式化 mkfs.TAB 查看当前系统可创建分区类型 [[email protected] ~]# mkfs. mkfs.cramfs  mkfs.ext3    mkfs.vfat    mkfs.ext2    mkfs.msdos 格式化第一个分区   mkfs.ext3 /dev/sdb1 创建

多磁盘自动分区自动挂载脚本

起源: 准备测试tfs分布式存储,发现一台服务器24块大盘,还有好几台服务器, 想想那么碰分区格式化,UUID自动挂载,还好有自动脚本. 1,yes能在你需要输入y时帮你操作,要不一台服务器不间断24次y还不知道什么时候敲击 2,UUID也能自动获取,这样才能自动挂载且盘符能固定 #!/bin/bash #mkfs.ext4 mkdir /data/disk{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24} yes |