Automatic partitioning, formatting, and mounting of shell scripts in Linux

Source: Internet
Author: User
Tags centos aliyun

Function: Automatically detect if there are not partitioned data disks, format new data disks and mount

Solved what problem: one-click to detect whether there are not partitioned data disks, and can be formatted and automatically mounted, eliminating complex commands and steps

Execution method: Execute command as Root

The code is as follows Copy Code

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

Results:

The script reads as follows:

The code is as follows Copy Code

#!/bin/bash
# author:  Yeho <lj2007331 at Gmail.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 visit http://oneinstack.com        #
#######################################################################

Echo=echo
for CMD in Echo/bin/echo; Todo
$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 is root to run this script${cend}"; exit 1;}

Mount_dir=/data
Fstab_file=/etc/fstab

Count=0
Tmp1=/tmp/.tmp1
Tmp2=/tmp/.tmp2

> $TMP 1
> $TMP 2

# Check lock file, one time ' let the ' script run one time
lockfile=/tmp/.$ (basename $)
If [f "$LOCKfile"];then
Echo
echo "${cwarning}the 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 $} ' | Awk-f: ' {print $} ' | grep "VD"
Todo
device_count=$ (fdisk-l $i | grep "$i" | awk ' {print $} ' | awk-f: ' {print $} ' | 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 $} ') ' = = = '/'-O ' $ (grep-v ' ^# ' $FSTAB _file | Grep-v ^$ | awk ' {print $1,$2,$3} ' | Grep-w "$i" | awk ' {print $} ') ' = = ' swap '];then
echo "${cwarning}the $i disk is mounted${cend}"
Else
echo $i >> $LOCKfile
echo "You have a free disk, now'll 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 '
Todo
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 << 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" >> $TMP 1
}

# Config/etc/fstab and Mount device
Main () {
For i in ' echo $DISK _list '
Todo
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" >> $TMP 2
Done
Make_dir
> $LOCKfile
Paste $TMP 2 $TMP 1 > $LOCKfile
Echo
echo "${cmsg}step 5.Begin to write configuration to/etc/fstab and Mount Device${cend}"
While read a B
Todo
[-Z ' grep ^${a} $FSTAB _file '-a-z ' grep ${b} $FSTAB _file '] && echo ' ${a} $b ext4 defaults 0 0 ' >> $FS Tab_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 $TMP 1 $TMP 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.