在GNU/Linux下使用命令行自动挂载与卸载USB磁盘

时间:2022-04-14 00:22:32

在命令行环境下如果每次都是靠手动敲入mount与umount命令来挂载与卸载USB磁盘是件很麻烦的事情。尤其是mount命令的参数非常多。比如,磁盘的分区类型(vfat、ntfs等),挂载的目录节点,以何种用户和组的身份来挂载(uid与gid),挂载后文件与文件夹的权限(umask)等等。于是,自己编写了两个脚本程序来分别实现自动挂载与卸载USB磁盘。现在分别介绍如下。

首先是加载USB磁盘的 auto_mount.sh 脚本,使用它可以自动提取与设置mount命令所需的参数,执行mount命令,检查是否挂载成功:

  • 使用 whoamiid 命令获取当前登录用户的 uidgid
user_name=`whoami`
user_id=`id -u $user_name`
user_group_id=`id -g $user_name`
  • 使用 udisks 命令获得磁盘分区的名称(volume label)与类型,如果指定分区没有volume label,则自动设为 disk
vol_label=`udisks --show-info "$1" | gawk '/label:.*[[:alnum:]]+/ {print tolower($2); exit}'`
vol_type=`udisks --show-info "$1" | gawk '/type:.*[[:alnum:]]+/ {print $2; exit}'`
  • 调用 mount 命令,将指定的磁盘设备以上述参数挂载到 /media/$vol_label 目录。其中, umask 设为 002 ,即将挂载后的文件与文件夹权限设为 775 。所用的 mount 命令如下,其中参数 $1auto_mount.sh 命令行参数所指定的磁盘设备节点,比如 /dev/sdb1
sudo mount -t $vol_type "$1" "/media/$vol_label" -o uid=$user_id,gid=$user_group_id,umask=002
  • 最后,查看 /etc/mtab 文件中是否已包含目录 /media/$vol_label ,从而验证是否挂载成功。

auto_mount.sh 源代码如下所示:

#!/bin/bash

# Automount a usb disk

script_name="auto_mount.sh"
script_usage=$(cat <<EOF
auto_mount.sh [OPTIONS] [DEVICE FOR DISK]
EOF
)
script_function=$(cat <<EOF
Automatically mount a disk to a folder beneath /media whose name is the volume label of the disk.
EOF
)
script_doc=$(cat <<EOF
-h Display this help.
EOF
)
script_examples=$(cat <<EOF
auto_mount.sh /dev/sdd1
EOF
)
state_prefix="==="
warning_prefix="***"
error_prefix="!!!" function display_help() {
if [ -n "$script_usage" ]; then
echo -e "Usage: $script_usage"
fi if [ -n "$script_function" ]; then
echo -e "$script_function"
fi if [ -n "$script_doc" ] ; then
echo -e "\n$script_doc"
fi if [ -n "$script_examples" ]; then
echo -e "\nExamples"
echo -e "$script_examples"
fi
} # Process command options
while getopts ":h" opt; do
case $opt in
h ) display_help
exit 0 ;;
\? ) display_help
exit 1 ;;
esac
done
shift $(($OPTIND - 1)) if [ $OSTYPE = 'linux-gnu' ]; then
# Default volume label if the mounted disk does not have one.
vol_label_default=disk if [ -n "$1" ]; then
if [ -e "$1" ]; then
user_name=`whoami`
user_id=`id -u $user_name`
user_group_id=`id -g $user_name`
vol_label=`udisks --show-info "$1" | gawk '/label:.*[[:alnum:]]+/ {print tolower($2); exit}'`
vol_type=`udisks --show-info "$1" | gawk '/type:.*[[:alnum:]]+/ {print $2; exit}'`
# Create a directory in /media and chown it into the current user and group
if [ -d "/media/$vol_label" ]; then
echo "$warning_prefix /media/$vol_label already exists!"
if [ -n "`cat /etc/mtab | grep /media/$vol_label`" ]; then
echo "$warning_prefix A device has already been mounted to the path /media/$vol_label!"
exit 0
fi
else
if [ -n "$vol_label" ]; then
echo "$state_prefix Create /media/$vol_label..."
else
echo "$warning_prefix The device $1 has no volume label, a default path /media/$vol_label_default will be used!"
vol_label=$vol_label_default
fi
sudo mkdir "/media/$vol_label"
fi # Mount the disk
sudo mount -t $vol_type "$1" "/media/$vol_label" -o uid=$user_id,gid=$user_group_id,umask=002
if [ -n "`cat /etc/mtab | grep /media/$vol_label`" ]; then
echo "$state_prefix The disk $1 has been successfully mounted to /media/$vol_label!"
else
echo "$error_prefix Failed to mount the disk $1 to /media/$vol_label!"
fi
else
echo "$warning_prefix Please provide a valid device name!"
fi
else
echo "$warning_prefix Please provide a device name!"
fi
else
echo "$warning_prefix Operating system or host name is not supported!"
fi

然后,按如下方式运行 auto_mount.sh 即可自动挂载USB磁盘:

auto_mount.sh /dev/sdb1

接下来要介绍的 auto_umount.sh 脚本就很简单了。只要用户指定磁盘所挂载的文件夹,该脚本会检查该文件夹是否存在于 /etc/mtab 文件。若存在,则调用 umount 命令,然后删除上述文件夹。其源码如下:

#!/bin/bash

# Auto unmount a disk

script_name="auto_umount.sh"
script_usage=$(cat <<EOF
auto_umount.sh [OPTIONS] [MOUNTED PATH OF DISK]
EOF
)
script_function=$(cat <<EOF
Automatically umount a disk which has been mounted on the specified path.
EOF
)
script_doc=$(cat <<EOF
-h Display this help.
EOF
)
script_examples=$(cat <<EOF
auto_umount.sh /media/data
EOF
)
state_prefix="==="
warning_prefix="***"
error_prefix="!!!" function display_help() {
if [ -n "$script_usage" ]; then
echo -e "Usage: $script_usage"
fi if [ -n "$script_function" ]; then
echo -e "$script_function"
fi if [ -n "$script_doc" ] ; then
echo -e "\n$script_doc"
fi if [ -n "$script_examples" ]; then
echo -e "\nExamples"
echo -e "$script_examples"
fi
} # Process command options
while getopts ":h" opt; do
case $opt in
h ) display_help
exit 0 ;;
\? ) display_help
exit 1 ;;
esac
done
shift $(($OPTIND - 1)) if [ $OSTYPE = 'linux-gnu' ]; then
if [ -n "$1" ]; then
if [ -e "$1" ]; then
if [ -n "`cat /etc/mtab | grep ${1%/}`" ]; then
sudo umount "$1"
sudo rmdir "$1"
if [ -n "`cat /etc/mtab | grep ${1%/}`" ]; then
echo "$error_prefix Failed to unmount the device from the path $1!"
else
echo "$state_prefix Device has been successfully umounted from the path $1!"
fi
else
echo "$warning_prefix No device has been mounted to $1!"
fi
else
echo "$warning_prefix Please provide a valid mounted path name!"
fi
else
echo "$warning_prefix Please provide a mounted path name!"
fi
else
echo "$warning_prefix Operating system or host name is not supported!"
fi