forked from chromecs/auto_fdisk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_fdisk.sh
More file actions
220 lines (202 loc) · 5.51 KB
/
Copy pathauto_fdisk.sh
File metadata and controls
220 lines (202 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
# Author: yeho <lj2007331 AT gmail.com>
# BLOG: https://blog.linuxeye.cn
#
# Notes: OneinStack for CentOS/RedHat 6+ Debian 6+ and Ubuntu 12+
#
# Project home page:
# https://oneinstack.com
# https://github.com/lj2007331/oneinstack
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/RedHat 5+ Debian 6+ and Ubuntu 12+ #
# Auto fdisk #
# For more information please visit http://oneinstack.com #
#######################################################################
"
# 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
reformat_yn=y
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
MOUNT_DIR=/data
[ -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