1. 程式人生 > >linux 自動掛載U盤之udev

linux 自動掛載U盤之udev

cd /etc/udev/rules.d/

建立一個檔案 10_jixiuf_usb.rules

內容如下

KERNEL!="sd[b-z][0-9]", GOTO="media_by_label_auto_mount_end"

# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"

# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usb-%k"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
# 如果是vfat 或者ntfs 系統,則設定mount_options 的選項如下
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=000"

# Mount the device
#  如果檔案系統不是ntfs ,意味著是vfat
# 同時掛載到/media/sd[b-z][0-9] 的形式
ACTION=="add",ENV{ID_FS_TYPE}!="ntfs", RUN+="/bin/mkdir -p /mnt/usb-%k", RUN+="/bin/mount -o $env{mount_options},rw /dev/%k /mnt/usb-%k"

#ntfs
#  如果檔案系統是ntfs ,以ntfs-3g 的形式掛載,實現可讀寫
# 同時掛載到/media/sd[b-z][0-9] 的形式

ACTION=="add",ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mkdir -p /mnt/usb-%k", RUN+="/bin/mount -t ntfs-3g -o $env{mount_options},rw /dev/%k /mnt/usb-%k"
# Clean up after removal
#
ACTION=="remove", ENV{dir_name}!="",  RUN+="/bin/umount -l /mnt/usb-%k",  RUN+="/bin/rmdir /mnt/usb-%k" ,  RUN+="/bin/rm /mnt/usb-%k"


# Exit
LABEL="media_by_label_auto_mount_end"