Automatic disk partitioning

Source: Internet
Author: User

#! /Bin/bash
######################################## #
# Function: auto fdisk
# Usage: Bash auto_fdisk.sh
# Author: customer service department
# Company: Alibaba Cloud computing
# Version: 2.0
######################################## #

Count = 0
Tmp1 =/tmp/. tmp1
Tmp2 =/tmp/. tmp2
> $ Tmp1
If the/tmp/. tmp1 file does not exist, create
If yes, and the size is not 0, the file is cleared.
[[Email protected] lianxi] # Aa = Meng
[[Email protected] lianxi] #> $ AA> is followed by a variable
[[Email protected] lianxi] # ls
Meng
> $ Tmp2
Fstab_file =/etc/fstab: copy the absolute path of the file to fstab_file.

# Check lock file, one time only let the script run one time
Lockfile =/tmp/. $ (basename $0) $0 is the first parameter when the script is executed./auto_fdisk02.sh
Basename./auto_fdisk02.sh
Auto_fdisk02.sh
If [-F "$ lockfile"]
Then
Echo-e "\ 033 [1; 40; 31 MThe script is runing, Please next time to run this script. \ 033 [0 m"
Exit
Else
Echo-e "\ 033 [40; 32 mstep 1.no lock file, begin to create lock file and continue. \ 033 [40; 37 m"
Touch $ lockfile
Fi

# Check user
If [$ (ID-u )! = "0"] view the ID of the current user
Then
Echo-e "\ 033 [1; 40; 31 merror: You must be root to run this script, please use root to install this script. \ 033 [0 m"
Rm-RF $ lockfile
Exit 1 0 indicates success (zero-success)
Non-0 indicates failure (Non-zero-failure)
2 indicates improper usage (incorrect usage)
127 indicates that the command is not found (command not found)
126 indicates that it is not executable (not an executable)
> = 128 signal generation

Fi

# Check disk partition
Check_disk ()
{
> $ Lockfile: Create a lockfile
Device_list = $ (fdisk-L | grep "disk" | grep "/dev" | awk '{print $2}' | awk-F: '{print $1}' | grep "XV") xxx = $ (XX) Assign XX to xxx
/Dev/SDA
/Dev/SDB
/Dev/mapper/vg_lvmraid-lv_root
/Dev/mapper/vg_lvmraid-lv_swap
For I in 'echo $ device_list'
Do
Device_count = $ (fdisk-L $ I | grep "$ I" | awk '{print $2}' | awk-F: '{print $1}' | WC-l)
/Dev/SDA
*
64
Echo
If [$ device_count-lt 2] is less than, this condition means that the disk is not partitioned
Then
Now_mount = $ (DF-h)
If echo $ now_mount | grep-W "$ I">/dev/null 2> & 1
Then
Echo-e "\ 033 [40; 32 MThe $ I disk is mounted. \ 033 [40; 37 m"
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"] This variable is a Null String
Then
Echo-e "\ 033 [1; 40; 31mno free disk need to be fdisk. Exit script. \ 033 [0 m"
Rm-RF $ lockfile
Exit 0
Else
Echo-e "\ 033 [40; 32 mThis system have free disk: \ 033 [40; 37 m"
For I in 'echo $ disk_list'
Do
Echo "$ I"
Count = $ (count + 1) [[email protected] ~] # Count = $ (count + 1)
-Bash: Count + 1: Command not found
[[Email protected] ~] # Count = $ (count + 1 ))

Done
Fi
}

# Fdisk, formating and create the File System
Fdisk_fun ()
{$1 Location Parameter refers to disk
Fdisk-s 56 $1 <EOF-s specifies the number of sectors per track
N
P
1


WQ
EOF

Sleep 5
Mkfs. ext4 $ {1} 1 the first "1" is a variable, and the second "1" is output as a string
}

# Make directory
Make_dir ()
{
Echo-e "\ 033 [40; 32 mstep 4. Begin to make directory \ 033 [40; 37 m"
For J in 'seq $ count'
Do
If [-d "/alidata $ J"]
Then
Echo-e "\ 033 [1; 40; 31 m/alidata $ J is exists. This script will exit, you must to choose a directory for Mount. \ 033 [0 m"
Rm-RF $ lockfile $ tmp2
Exit
Else
Echo "/alidata $ J" >>$ tmp1
Mkdir/alidata $ J
Fi
Done
}

# Config/etc/fstab and mount device
Main ()
{
For I in 'echo $ disk_list 'unpartitioned Disk
Do
Echo-e "\ 033 [40; 32 mstep 3. Begin to fdisk free disk. \ 033 [40; 37 m"
Fdisk_fun $ I
Echo "$ {I} 1" >>$ tmp2
Done
Make_dir
> $ Lockfile
Paste $ tmp2 $ tmp1> $ lockfile
Echo-e "\ 033 [40; 32 mstep 5. Begin to write configuration to/etc/fstab and mount device. \ 033 [40; 37 m"
While read a B
Do
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-
}

#========= Start script ================
Echo-e "\ 033 [40; 32 mstep 2. Begin to check free disk. \ 033 [40; 37 m"
Check_disk
Main
DF-H
Rm-RF $ lockfile $ tmp1 $ tmp2
====
Disk_list disks with no partitions
Lockfile stores disks that are not partitioned and not mounted
Count indicates the number of unpartitioned disks.
/Tmp2 stores the new partition name
/Tmp1 stores the new directory path
Device_list: assign a value to the disk device.
Make_dir create a mount directory

This article from the "Love armed for a lifetime" blog, please be sure to keep this source http://menglinux.blog.51cto.com/8733806/1437320

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.