(阿里巴巴)服务器简单磁盘自动化脚本
来源:互联网 发布:哈长城市群知乎 编辑:程序博客网 时间:2024/04/25 07:07
今天学玩文件系统中的LVM逻辑卷后,需要编写脚本进行自动化操作:正好这段时间一致在研究shell脚本编程方向,查看了许多经典的shell编程例子
下面的脚本感觉特别具有代表性,便拿来与大家分享下。
#!/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
>$tmp2
fstab_file=/etc/fstab
#check lock file ,one time only let the script run one time
LOCKfile=/tmp/.$(basename $0)
if [ -f "$LOCKfile" ]
then
echo -e "\033[1;40;31mThe script is already exist,please next time to run this script.\033[0m"
exit
else
echo -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\033[40;37m"
touch $LOCKfile
fi
#check user
if [ $(id -u) != "0" ]
then
echo -e "\033[1;40;31mError: You must be root to run this script, please use root to install this script.\033[0m"
rm -rf $LOCKfile
exit 1
fi
#check disk partition
check_disk()
{
>$LOCKfile
device_list=$(fdisk -l|grep "Disk"|grep "/dev"|awk '{print $2}'|awk -F: '{print $1}'|grep "xv")
for i in `echo $device_list`
do
device_count=$(fdisk -l $i|grep "$i"|awk '{print $2}'|awk -F: '{print $1}'|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 "\033[40;32mThe $i disk is mounted.\033[40;37m"
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 -e "\033[1;40;31mNo free disk need to be fdisk.Exit script.\033[0m"
rm -rf $LOCKfile
exit 0
else
echo -e "\033[40;32mThis system have free disk :\033[40;37m"
for i in `echo $disk_list`
do
echo "$i"
count=$((count+1))
done
fi
}
#fdisk ,formating and create the file system
fdisk_fun()
{
fdisk -S 56 $1 << EOF
n
p
1
wq
EOF
sleep 5
mkfs.ext3 ${1}1
}
#make directory
make_dir()
{
echo -e "\033[40;32mStep 4.Begin to make directory\033[40;37m"
for j in `seq $count`
do
if [ -d "/alidata$j" ]
then
echo -e "\033[1;40;31m/alidata$j is exists.This script will exit,you must to choose a directory for mount.\033[0m"
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`
do
echo -e "\033[40;32mStep 3.Begin to fdisk free disk.\033[40;37m"
fdisk_fun $i
echo "${i}1" >>$tmp2
done
make_dir
>$LOCKfile
paste $tmp2 $tmp1 >$LOCKfile
echo -e "\033[40;32mStep 5.Begin to write configuration to /etc/fstab and mount device.\033[40;37m"
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 -a
}
#=========start script===========
echo -e "\033[40;32mStep 2.Begin to check free disk.\033[40;37m"
check_disk
main
df -h
rm -rf $LOCKfile $tmp1 $tmp2
- (阿里巴巴)服务器简单磁盘自动化脚本
- 服务器自动化安装脚本
- linux简单自动化脚本
- 脚本乐园 磁盘自动化均等分区demo
- 服务器性能自动化测试脚本
- 自动化脚本腾讯云配置集群(一)挂载磁盘
- 自动化脚本登录跳板机和服务器
- 自动化脚本中运到的一些简单算法
- 简单的python http接口自动化脚本
- app应用appium框架简单自动化脚本
- 自动化复制脚本(.bat)
- oracle 巡检脚本(自动化)
- Linux---服务器CPU 内存 磁盘 监控SHELL脚本
- 使用简单的shell脚本进行自动化测试
- 自动化交互脚本Expect简单介绍及应用实例
- 简单的python http 接口自动化测试框架脚本
- 简单的python http 接口自动化测试框架脚本
- 简单测试服务器的磁盘I/O性能
- CBuilder6安装后运行报错,打开工程选项崩溃的处理方法—Win 2008R2或Win7
- centos 桌面没有终端
- 【LeetCode】Distinct Subsequences
- 18个不可不知的有用潜规则
- TYVJ 1044 数字三角形
- (阿里巴巴)服务器简单磁盘自动化脚本
- hdu 2546 dp背包0-1
- su 和 su- 会影响环境变量
- java 线程
- Qt中 QString 和int, char等的“相互”转换
- 不定的工资类
- 解决这九种困扰 可以让你每天精神百倍
- MongoDB学习之旅十三:MongoDB 导入导出
- chrome extension js copy function