在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 ...
随机推荐
- cocos2d(CCSprite 用贝塞尔做抛物线,足球精灵并且同时做旋转放大效果)
今天刚学到Cocos2d中的动作哪一张,自己做了一个用贝塞尔曲线足球精灵实现同时放大旋转和抛物线动作. 使用 [CCSpawn actions:,,]链接这几个动作,同时做.与CCSequence(一 ...
- springMVC3学习(三)--handlerMapping和handlerAdapter
基本结构和 springMVC3学习(一)--框架搭建 差不多,这里不再用Annotation注解的方式 以下只说明需要修改的部分: 1.在Spring配置文件中配置HandlerMapping.Ha ...
- AOP的成员介绍
AOP(Aspect Oriented Programming)面向切面编程,AOP的作用不过多介绍,本文是主要是介绍AOP的成员,是我在复习的时候记录的一些笔记,方便以后查阅方便一些. JointP ...
- Page_Load接收随机参数放到字典类中
Page_Load接收随机参数放到字典类中,可以用作签名.普通的接收url的参数可以用作下面这种模式: int appid =Convert.ToInt32(param["appid&qu ...
- CentOS_6.5_x64:VNC安装配置
1.安装软件前首先检查下系统是否已经安装了这个软件:rpm -qa tigervnc-server 2.根据前面命令的查询,显示系统还是没有安装VNC服务器端软件,那么我们就使用命令进行安装一下:yu ...
- [每日一题] OCP1z0-047 :2013-07-19 Rules of Precedence――括号的使用
这道题目的意思是你的公司决定给所有呆到5年或5年以上的所有员工每个月加50美元,然后算出总的年薪.每个月薪水:salary,每个月加到:salary+50,总的年薪: (salary+50)*12. ...
- [置顶] 和孩子们一起学Python编程
1. 推荐书名 Computer Programming for Kids and Other Beginners in Python, 4Ed.pdf 中文译名:<和孩子们一起学Pyt ...
- Visual Studio属性配置中使用宏
在学习C语言的时候,我们曾经遇到过一个宏的概念.宏的作用机理本质上是宏的展开,C语言中的宏的用法也有很多种(水其实很深...),不过从感觉上来讲,人们大致上会在以下的场景中,利用宏来解决一些窘境:一是 ...
- 应用Git Flow—Git团队协作最佳实践
规范的Git使用 Git是一个很好的版本管理工具,不过相比于传统的版本管理工具,学习成本比较高. 实际开发中,如果团队成员比较多,开发迭代频繁,对Git的应用比较混乱,会产生很多不必要的冲突或者代码丢 ...
- VS 使用技巧
1.按下alt键,可以做到竖向选择 2.