在GNU/Linux下使用命令行自动挂载与卸载USB磁盘
在命令行环境下如果每次都是靠手动敲入mount与umount命令来挂载与卸载USB磁盘是件很麻烦的事情。尤其是mount命令的参数非常多。比如,磁盘的分区类型(vfat、ntfs等),挂载的目录节点,以何种用户和组的身份来挂载(uid与gid),挂载后文件与文件夹的权限(umask)等等。于是,自己编写了两个脚本程序来分别实现自动挂载与卸载USB磁盘。现在分别介绍如下。
首先是加载USB磁盘的 auto_mount.sh 脚本,使用它可以自动提取与设置mount命令所需的参数,执行mount命令,检查是否挂载成功:
- 使用 whoami 和 id 命令获取当前登录用户的 uid 和 gid :
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 命令如下,其中参数 $1 为 auto_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
在GNU/Linux下使用命令行自动挂载与卸载USB磁盘的更多相关文章
- 在Linux下使用命令行打印文件
近期需要将数学笔记打印出来复习,才发现Linux KDE环境下的默认PDF软件Okular根本无法将我在GoodNotes B5大小的页面写下的内容自适应地放大到A4纸上,只能以页面的原始尺寸打印.然 ...
- 在linux下用命令行编译 java的eclipse项目
由于jdk的版本问题导致在windows上编译打包好的jar包放在linux服务器上运行的时候出现一点小异常,所以决定在linux上进行一次项目编译,这有两个选择1.在相同的linux环境下安装lin ...
- Linux下基于命令行的抓包方法
大家可能都已经对著名的抓包工具Ethereal比较熟悉了,这里再介绍一种基于命令行的抓包工具tcpdump. 举例:抓本机1813端口上的数据,并将抓包结果保存在test.cap文件中 然后在本地可以 ...
- Linux下使用命令行配置IPMI
ipmitool是什么: 百度百科给的解释已经够用了,简单说就是“IPMI(Intelligent Platform Management Interface)即智能平台管理接口是使硬件管理具备“智能 ...
- Linux下的命令行
一.文件传输(两种方式) 1. 使用CRT传输 1. 一定要修改编码为UTF-8类型 1. 按住alt + p 切换成传输文件的窗口,然后拖拽文件进来即可 2. 使用类似xftp这种软件传输 这种软件 ...
- linux下通过命令行上传文件到百度网盘
一.环境: centos release 6.9 python 2.7.13 二.安装工具bypy sudo pip install bypy 三.使用bypy 3.1 授权 [root@ineedl ...
- Linux下通过命令行mail发送e-mail
找到配置文件/etc/mail.rc添加如下行 # vi /etc/mail.rc set from=@qq.com set smtp=smtp.qq.com set smtp-auth-user= ...
- linux下java命令行引用jar包
一般情况下: 如果java 文件和jar 包在同一目录 poi-3.0-alpha3-20061212.jar testTwo.java 编译: javac -cp poi-3.0-alpha3-2 ...
- linux下通过命令行把文件拷贝到U盘上
常用linux,往U盘拷贝文件是常用的一种方法.下面这个方法是笔者亲测有效,暂时记录下来. 1.插入U盘,fdisk -l查看U盘是哪个设备(比如/dev/sdb1)然后mount /dev/sdb ...
随机推荐
- 不用char*作为hash_map的key
尽量不用char*作为hash_map的key Posted on 2013-09-09 21:21 Springlie 阅读(83) 评论(0) 编辑 收藏 引子: 同事前几天用hash_map时发 ...
- MySQL 的 phpmyadmin上传大小限制(转)以及 MySQL server has gone away 的解决办法
phpmyadmin上传大小限制 原帖地址:http://www.hmidc.com/home/news/?13914.html 时间:2011-6-21 11:17:57 作者:红帽之家 来源: ...
- 内Cool超人
内Cool超人 经过一年时间看到asp.net mvc一直被受微软开发团队的注重.与之相比的silverlight我感觉到有点力不从心.除去silverlight第一次运行要安装Runtime不说,产 ...
- sharepoint 2013 文档库eventhandle权限控制
记录一下如何在sharepoint server 2013文档库中,使用eventhandle控制文档库document library的条目item权限. ///<summary> // ...
- 通过VNC Viewer使用VMware虚拟机的远程桌面连接
本文转自:http://www.14blog.com/archives/185 要在VMware虚拟机中使用远程桌面连接?方法有两个:一种是在虚拟机中做“端口映射”,当然,这个稍显复杂(虚拟机端口映射 ...
- 如何Windows系统中搭建php环境
PHP介绍: PHP 独特的语法混合了C.Java.Perl以及PHP自创的语法.它可以比CGI或者Perl更快速地执行动态网页.用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTM ...
- javaapplicationWeb application setup on Ubuntu VPS
题记:写这篇博客要主是加深自己对javaapplication的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. Now there are many hosting server ...
- Magnum Kuernetes源码分析(二)
Kubernetes Master Stack kubernetes master的stack的resources主要分为三个部分. master wait handle wait handle主要用 ...
- spring mvc后台接收中文乱码
可从如下几方面着手 1.jsp页面编码 2.web.xml配置字符过滤器,该字符过滤器最好放在开头 3.tomcat下server.xml添加URIEncoding="UTF-8" ...
- 总结 React 组件的三种写法 及最佳实践 [涨经验]
React 专注于 view 层,组件化则是 React 的基础,也是其核心理念之一,一个完整的应用将由一个个独立的组件拼装而成. 截至目前 React 已经更新到 v15.4.2,由于 ES6 的普 ...