之前有一篇文章:《ARM-Linux下实现U盘自动挂载》在嵌入式Linux开发板中实现自动挂载U盘的方法。该方法有效,但局限。本人自己测试是在buildroot构建的根文件系统下实现的,可以识别U盘以及读卡器插SD卡实现U盘方式。
现在使用瑞芯微RK3288开发板时,却翻车了。
翻车回放:在/etc/udev/rules.d中新建.rules文件,,设置U盘自动挂载路径为/mnt/usb。但是插上U盘后,U盘自动挂载到/media/root目录下。(跑的ubuntu文件系统,自己新建的udev规则无法生效)。目前不知晓修ubuntu文件系统下的哪个文件去更改挂载位置。
解决方法:
一、可用(有缺陷):
使用systemd-mount、systemd-umount替代mount、umount 亲测有效
直接上解决方案:
1、每次挂载U盘创建挂载文件夹、拔出U盘删除文件夹版本:
vi /etc/udev/rules.d/10-myudisk.rules
添加以下内容:
KERNEL!="sd*", GOTO="media_by_label_auto_mount_end"
SUBSYSTEM!="block",GOTO="media_by_label_auto_mount_end"
IMPORT{program}="/sbin/blkid -o udev -p %N"
ENV{ID_FS_TYPE}=="", GOTO="media_by_label_auto_mount_end"
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="%E{ID_FS_UUID}"
ACTION=="add", ENV{mount_options}="relatime,sync"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="iocharset=utf8,umask=000"
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/usr/bin/systemd-mount -o %E{mount_options} --no-block --automount=yes --collect /dev/%k /media/%E{dir_name}"
ACTION=="remove", ENV{dir_name}!="", RUN+="/usr/bin/systemd-mount --umount /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
LABEL="media_by_label_auto_mount_end"
2、直接挂载到固定文件夹版本:
vi /etc/udev/rules.d/10-myudisk.rules
添加以下内容:
KERNEL!="sd*", GOTO="media_by_label_auto_mount_end"
SUBSYSTEM!="block",GOTO="media_by_label_auto_mount_end"
IMPORT{program}="/sbin/blkid -o udev -p %N"
ENV{ID_FS_TYPE}=="", GOTO="media_by_label_auto_mount_end"
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="%E{ID_FS_UUID}"
ACTION=="add", ENV{mount_options}="relatime,sync"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat", ENV{mount_options}="iocharset=utf8,umask=000"
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", ENV{mount_options}="iocharset=utf8,umask=000"
ACTION=="add", RUN+="/usr/bin/systemd-mount -o %E{mount_options} --no-block --automount=yes --collect /dev/%k /home/zml"
ACTION=="remove", ENV{dir_name}!="", RUN+="/usr/bin/systemd-umount --l /home/zml"
LABEL="media_by_label_auto_mount_end"
注意:目前测试在ubuntu文件系统下,只识别纯正的U盘,不识别读卡器插SD卡的伪U盘方式!!!
本人查看了两者属性及其他信息,基本没区别,但是就是不能加载读卡器插sd卡的伪U盘方式,可以识别到/dev/sda /dev/sda1,但是加载不出伪U盘内的文件。但是可以通过手动挂载的方式实现读取SD内的文件!!!
二、可用,无缺陷:
共三步:
step1:
创建脚本文件:
vi /usr/local/bin/usbstorage-mount.sh
添加以下内容:
1、采用创建文件夹、删除文件夹方式:
#!/bin/bash
# This script is called from our systemd unit file to mount or unmount
# a USB drive.
usage()
{
echo "Usage: $0 {add|remove} device_name (e.g. sdb1)"
exit 1
}
if [[ $# -ne 2 ]]; then
usage
fi
ACTION="$1"
DEVBASE="$2"
DEVICE="/dev/${DEVBASE}"
# See if this drive is already mounted, and if so where
MOUNT_POINT=$(/bin/mount | /bin/grep ${DEVICE} | /usr/bin/awk '{ print $3 }')
do_mount()
{
if [[ -n "${MOUNT_POINT}" ]]; then
echo "Warning: ${DEVICE} is already mounted at ${MOUNT_POINT}"
exit 1
fi
# Get info for this drive: $ID_FS_LABEL, $ID_FS_UUID, and $ID_FS_TYPE
# added some sed's to avoid space issues
eval $(/sbin/blkid -o udev ${DEVICE}|sed 's/=/="/'|sed 's/$/"/')
# Figure out a mount point to use
LABEL="${ID_FS_LABEL}"
if [[ -z "${LABEL}" ]]; then
LABEL="${DEVBASE}"
elif /bin/grep -q " /media/${LABEL} " /etc/mtab; then
# Already in use, make a unique one
LABEL+="-${DEVBASE}"
fi
MOUNT_POINT="/media/${LABEL}"
echo "Mount point: ${MOUNT_POINT}"
/bin/mkdir -p "${MOUNT_POINT}"
# Global mount options
OPTS="rw,relatime"
#added a chmod checker for file systems that don't
#understand allow all to read write
# CHMOD=no
# File system type specific mount options
if [[ ${ID_FS_TYPE} == "vfat" ]]; then
OPTS+=",users,gid=100,iocharset=utf8,umask=000"
# OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"
#added options I wanted on ntfs
elif [[ ${ID_FS_TYPE} == "ntfs" ]]; then
OPTS+=",users,gid=100,iocharset=utf8,umask=000"
# OPTS+=",user,users,umask=000,allow_other"
else
OPTS+=",users,gid=100,iocharset=utf8,umask=000"
# CHMOD=yes
fi
if ! /bin/mount -o "${OPTS}" ${DEVICE} "${MOUNT_POINT}"; then
echo "Error mounting ${DEVICE} (status = $?)"
/bin/rmdir "${MOUNT_POINT}"
exit 1
fi
echo "**** Mounted ${DEVICE} at ${MOUNT_POINT} ****"
# if [ "${CHMOD}" = "yes" ];then
# /usr/bin/find "${MOUNT_POINT}" -type f -exec chmod 0666 {} \;
# /usr/bin/find "${MOUNT_POINT}" -type d -exec chmod 0777 {} \;
# fi
}
do_unmount()
{
if [[ -z ${MOUNT_POINT} ]]; then
echo "Warning: ${DEVICE} is not mounted"
else
/bin/umount -l ${DEVICE}
echo "**** Unmounted ${DEVICE}"
fi
# Delete all empty dirs in /media that aren't being used as mount
# points. This is kind of overkill, but if the drive was unmounted
# prior to removal we no longer know its mount point, and we don't
# want to leave it orphaned...
for f in /media/* ; do
if [[ -n $(/usr/bin/find "$f" -maxdepth 0 -type d -empty) ]]; then
if ! /bin/grep -q " $f " /etc/mtab; then
echo "**** Removing mount point $f"
/bin/rmdir "$f"
fi
fi
done
}
case "${ACTION}" in
add)
do_mount
;;
remove)
do_unmount
;;
*)
usage
;;
esac
2、采用挂载固定文件夹的方式:
#!/bin/bash
# This script is called from our systemd unit file to mount or unmount
# a USB drive.
usage()
{
echo "Usage: $0 {add|remove} device_name (e.g. sdb1)"
exit 1
}
if [[ $# -ne 2 ]]; then
usage
fi
ACTION="$1"
DEVBASE="$2"
DEVICE="/dev/${DEVBASE}"
# See if this drive is already mounted, and if so where
MOUNT_POINT=$(/bin/mount | /bin/grep ${DEVICE} | /usr/bin/awk '{ print $3 }')
do_mount()
{
if [[ -n "${MOUNT_POINT}" ]]; then
echo "Warning: ${DEVICE} is already mounted at ${MOUNT_POINT}"
exit 1
fi
# Get info for this drive: $ID_FS_LABEL, $ID_FS_UUID, and $ID_FS_TYPE
# added some sed's to avoid space issues
eval $(/sbin/blkid -o udev ${DEVICE}|sed 's/=/="/'|sed 's/$/"/')
# Figure out a mount point to use
LABEL="${ID_FS_LABEL}"
if [[ -z "${LABEL}" ]]; then
LABEL="${DEVBASE}"
elif /bin/grep -q "/home/zml/lcdfile" /etc/mtab; then
# Already in use, make a unique one
LABEL+="-${DEVBASE}"
fi
MOUNT_POINT="/home/zml/lcdfile"
echo "Mount point: ${MOUNT_POINT}"
# /bin/mkdir -p "${MOUNT_POINT}"
# Global mount options
OPTS="rw,relatime,sync"
#added a chmod checker for file systems that don't
#understand allow all to read write
# CHMOD=no
# File system type specific mount options
if [[ ${ID_FS_TYPE} == "vfat" ]]; then
OPTS+=",users,gid=100,iocharset=utf8,umask=000"
# OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"
#added options I wanted on ntfs
elif [[ ${ID_FS_TYPE} == "ntfs" ]]; then
OPTS+=",users,gid=100,iocharset=utf8,umask=000"
# OPTS+=",user,users,umask=000,allow_other"
else
OPTS+=",users,gid=100,iocharset=utf8,umask=000"
# CHMOD=yes
fi
if ! /bin/mount -o "${OPTS}" ${DEVICE} "${MOUNT_POINT}"; then
echo "Error mounting ${DEVICE} (status = $?)"
# /bin/rmdir "${MOUNT_POINT}"
exit 1
fi
echo "**** Mounted ${DEVICE} at ${MOUNT_POINT} ****"
# if [ "${CHMOD}" = "yes" ];then
# /usr/bin/find "${MOUNT_POINT}" -type f -exec chmod 0666 {} \;
# /usr/bin/find "${MOUNT_POINT}" -type d -exec chmod 0777 {} \;
# fi
}
do_unmount()
{
if [[ -z ${MOUNT_POINT} ]]; then
echo "Warning: ${DEVICE} is not mounted"
else
/bin/umount -l ${DEVICE}
echo "**** Unmounted ${DEVICE}"
fi
# Delete all empty dirs in /media that aren't being used as mount
# points. This is kind of overkill, but if the drive was unmounted
# prior to removal we no longer know its mount point, and we don't
# want to leave it orphaned...
# for f in /media/* ; do
# if [[ -n $(/usr/bin/find "$f" -maxdepth 0 -type d -empty) ]]; then
# if ! /bin/grep -q " $f " /etc/mtab; then
# echo "**** Removing mount point $f"
# /bin/rmdir "$f"
# fi
# fi
# done
}
case "${ACTION}" in
add)
do_mount
;;
remove)
do_unmount
;;
*)
usage
;;
esac
以上方式根据自己需求选择其一即可。
创建完脚本,需要赋上可执行权限
chmod a+x /usr/local/bin/usbstorage-mount.sh
step2:
vi /lib/systemd/system/usb-mount@.service
添加以下内容:
[Unit]
Description=Mount USB Drive on %i
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/usbstorage-mount.sh add %i
ExecStop=/usr/local/bin/usbstorage-mount.sh remove %i
step3:
创建udev规则:
vi /etc/udev/rules.d/10-myusbstorage.rules
添加以下内容:
KERNEL!="sd*", GOTO="auto_mount_end"
SUBSYSTEM!="block", GOTO="auto_mount_end"
ACTION=="add", RUN+="/bin/systemctl start usb-mount@%k.service"
ACTION=="remove", RUN+="/bin/systemctl stop usb-mount@%k.service"
LABEL="auto_mount_end"
完成以上三步,可以选择重启开发板,或者执行以下命令,即可实现全部功能:
# 重新加载 udev rules 文件
udevadm control --reload-rules
# 重新加载 systemd unit 文件
systemctl daemon-reload
参考文章:(可能需要)
https://serverfault.com/questions/766506/automount-usb-drives-with-systemd