一键挂载linux数据盘脚本"/>
一键挂载linux数据盘脚本
一键挂载linux脚本
如果你有一个数据盘,但是懒得一步步去挂载。可以使用下边脚本部署
```#!/bin/bash
############################################
# Function : 磁盘挂载工具脚本
# Author : tang
#
# Usage: sh diskmount.sh /dev/sdb /data
#
############################################# 使用说明
print_usage() {echo ""echo -e "Usage : sh $0 [device_name] [mount_path]"echo -e "example : sh $0 /dev/sdb /data"echo -e "\t device_name \t -- device name, example: /dev/sdb "echo -e "\t mount_path \t -- directory for mount, example: /data"
}# 需要至少2个输入参数
if [ "$#" -lt "2" ]; thenif [ "$#" == "0" ]; thenecho "[ERROR]: no [device_name] and [mount_path] parameter supplied!!!"elseecho "[ERROR]: no [mount_path] parameter supplied!!!"fiprint_usageexit 1
fi# 磁盘设备名称
device_name=$1
# 挂载目录位置
mount_path=$2
# 文件系统格式
fs_type=xfsecho "[INFO]: available device information list follows:"
lsblk# 检查设备是否存在
echo "[INFO]: check device [$device_name] exits status ."
fdisk -l $device_name
if [ $? -ne 0 ]; thenecho "[ERROR]: disk device [ $device_name ] not found ."exit 1
fi# 卸载挂载点,以防重复操作
umount "$device_name"1 2>&1 >/dev/null# 磁盘分区操作
echo "[INFO]: partation for device [$device_name] ."
fdisk $device_name <<EOF
n
p
1w
EOF
if [ $? -ne 0 ]; thenexit 1
fi# 磁盘格式化操作
echo "[INFO]: file system format for device $device_name"1
mkfs -t $fs_type -f "$device_name"1
if [ $? -ne 0 ]; thenexit 1
fi# 检查并创建挂载目录
if [ ! -d "$mount_path" ]; thenecho "[INFO]: directory $mount_path not exit, create it now!"mkdir -p $mount_path
fi# 分区挂载操作
echo "[INFO]: mount device $device_name to directory: $mount_path"
mount "$device_name"1 $mount_path
if [ $? -ne 0 ]; thenexit 1
fi# 配置启动挂载
exist=$(grep "$device_name""1" /etc/fstab)
if [ ! -n "$exist" ]; thenecho "[INFO]: add....."echo "$device_name"1 "$mount_path $fs_type defaults 1 2" >>/etc/fstab
elseecho "[INFO]: update....."sed -i "s|^$device_name"1".*$|$device_name"1" $mount_path $fs_type defaults 1 2|g" /etc/fstab
fi# 输出挂载后的文件设备信息
df -h
echo "[INFO]: success ."
更多推荐
一键挂载linux数据盘脚本
发布评论