Linux System Mount data disk demo with one-click Mount Data Disk Script

Source: Internet
Author: User
Applicable system: Linux (Redhat, Centos,debian,ubuntu)

* Linux Server data disk is not partitioned and formatted, you can follow the following steps to partition and format the operation.

The following operation will divide the data disk into one partition to use.

1, view the data disk

You cannot see the data disk until you have partitioned and formatted the data disk, and you can view it using the "fdisk-l" command by using the "df–h" command. The following figure:

Tips: If you execute the fdisk-l command, you find that there is no/dev/xvdb indicating that your service has no data disk, then you do not need to mount, this tutorial does not apply to you

2. Partition the data disk

Execute the "fdisk-s 56/dev/xvdb" command to partition the data disk;

According to the prompts, enter "n", "P" "1", two times carriage return, "Wq", the partition begins, will soon complete.

3, view the new partition

Using the "fdisk-l" command, you can see that the new partition XVDB1 has been built.

4, format the new partition

The new partition is formatted using the "MKFS.EXT3/DEV/XVDB1" command, and the formatted time varies depending on the size of the hard disk.

(also can decide to choose EXT4 format freely)

5, add the partition information

Write new partition information using the "Echo '/dev/xvdb1/mnt ext3 defaults 0 0′>>/etc/fstab" (without quotes) command.
Then use the "cat/etc/fstab" command to view that the following information indicates a write success.

* If you need to mount the data disk to a separate folder, such as the separate use of the Web page, you can modify the/mnt section of the above command

6. Mount New Partition

Use the "mount-a" command to mount a new partition, and then use the "df-h" command to view it, with the following information stating that the mount is successful and you can start using the new partition.

When you get here, you're done with the data-disk mount.

Share a script, a script that mounts the data disk, just run the script to mount the data disk, and the data disk mount path is/a

Count=0
Tmp1=/tmp/.tmp1
Tmp2=/tmp/.tmp2
> $tmp 1
> $tmp 2
Fstab_file=/etc/fstab

#check lock file, one time is let the script run one time
lockfile=/tmp/.$ (basename $)
If [-F "$LOCKfile]"
Then
Echo-e "33[1;40;31mthe script is already exist,please next time to run this script.33[0m"
Exit
Else
Echo-e "33[40;32mstep 1.No lock File,begin to create lock file and continue.33[40;37m"
Touch $LOCKfile
Fi

#check User
If [$ (id-u)!= "0"]
Then
Echo-e "33[1;40;31merror:you must is root to run this script, please use the root to install this script.33[0m"
RM-RF $LOCKfile
Exit 1
Fi

#check disk partition
Check_disk ()
{
> $LOCKfile
device_list=$ (fdisk-l|grep "Disk" |grep "/dev" |awk ' {print $} ' |awk-f: ' {print $} ' |grep ' XV ')
For i in ' echo $device _list '
Todo
device_count=$ (fdisk-l $i |grep "$i" |awk ' {print $} ' |awk-f: ' {print $} ' |wc-l)
Echo
If [$device _count-lt 2]
Then
now_mount=$ (df-h)
If Echo $now _mount|grep-w "$i" >/dev/null 2>&1
Then
ECHO-E "33[40;32mthe $i disk is mounted.33[40;37m"
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-E "33[1;40;31mno free disk need to be fdisk. Exit script.33[0m "
RM-RF $LOCKfile
Exit 0
Else
ECHO-E "33[40;32mthis system have free disk:33[40;37m"
For i in ' echo $disk _list '
Todo
echo "$i"
count=$ ((count+1))
Done
Fi
}

#fdisk, formating and create the file system
Fdisk_fun ()
{
Fdisk-s << EOF
N
P
1


Wq
Eof

Sleep 5
Mkfs.ext3 ${1}1
}

#make Directory
Make_dir ()
{
Echo-e "33[40;32mstep 4.Begin to make directory33[40;37m"
For j in ' Seq $count '
Todo
If [-D "/a"]
Then
Echo-e "33[1;40;31m/a is exists. This script would exit,you must to choose a directory for mount.33[0m
RM-RF $LOCKfile $tmp 2
Exit
Else
echo "/A" >> $tmp 1
mkdir/a
Fi
Done
}

#config/etc/fstab and Mount device
Main ()
{
For i in ' echo $disk _list '
Todo
Echo-e "33[40;32mstep 3.Begin to fdisk free disk.33[40;37m"
Fdisk_fun $i
echo "${i}1" >> $tmp 2
Done
Make_dir
> $LOCKfile
Paste $tmp 2 $tmp 1 > $LOCKfile
Echo-e "33[40;32mstep 5.Begin to write configuration to/etc/fstab and Mount Device.33[40;37m"
While read a B
Todo
If Grep-v ^# $fstab _file|grep ${a} >/dev/null
Then
Sed-i "s=${a}*=#&=" $fstab _file
Fi
echo "${a} $b ext3 defaults 0 0" >> $fstab _file
Done < $LOCKfile
Mount-a
}

#=========start script===========
Echo-e "33[40;32mstep 2.Begin to check free disk.33[40;37m"
Check_disk
Main
Df-h
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.