一键挂载linux数据盘脚本

编程入门 行业动态 更新时间:2024-10-28 21:16:38

<a href=https://www.elefans.com/category/jswz/34/1767003.html style=一键挂载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数据盘脚本

本文发布于:2024-02-11 17:54:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1682421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:一键   脚本   数据   linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!