Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved 10-flash-mounts.rules #1

Open
ghost opened this issue Mar 26, 2016 · 0 comments
Open

Improved 10-flash-mounts.rules #1

ghost opened this issue Mar 26, 2016 · 0 comments

Comments

@ghost
Copy link

ghost commented Mar 26, 2016

Ahoi kamarat,

#! /bin/sh

####################################################
#        USB Flash Drives automounting             #
#    Device without partitions will not be mounted #
#                                                  #
#        parted -a optimal /dev/sdX                #
#        mklabel msdos                             #
#        mkpart primary 1 -1                       #
#        quit                                      #
#        mkfs.TYPE /dev/sdX1                       #
#                                                  #
####################################################

# start at sdb to ignore the system drive
KERNEL!="sd[b-z]*", GOTO="exit"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="exit"

# import some useful filesystem info as variables
IMPORT{program}="/sbin/blkid -o udev -p %N"
#IMPORT{program}="/bin/udevadm info -n %N"

# get the label if present, otherwise assign one based on device/partition
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="USB_%k"
ENV{ID_FS_TYPE}=="", GOTO="exit"


# Your $USER goes here if you want rw access
ENV{MY_UZER}="frost"


# determine whether the FS is vfat or ntfs
# and assign some environment variables to
# make our life easier
ENV{ID_FS_TYPE}!="(vfat|ntfs)", ENV{da_mount_type}="auto"
ENV{ID_FS_TYPE}=="vfat", ENV{da_mount_type}="vfat"
ENV{ID_FS_TYPE}=="ntfs", ENV{da_mount_type}="ntfs-3g"

# create the dir in /media
ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"

ACTION=="add", ENV{mount_optionz}="gid=100,dmask=000,fmask=110,utf8,flush,rw,noauto,discard,noatime,users"
ACTION=="add", ENV{ID_FS_TYPE}!="vfat", ENV{ID_FS_TYPE}!="ntfs", ENV{mount_optionz}="rw,noauto,discard,noatime,users"

# remove existing device and then add it to /etc/fstab
ACTION=="add", RUN+="/bin/sed -i '/\/dev\/%k /d' /etc/fstab"
ACTION=="add", RUN+="/bin/sed -i '$a\/dev/%k /media/%E{dir_name} %E{da_mount_type} %E{mount_optionz} 0 0' /etc/fstab"

# mount device and change ownership to have rw access
ACTION=="add", RUN+="/bin/mount -t %E{da_mount_type} -o %E{mount_optionz} /dev/%k '/media/%E{dir_name}'"
ACTION=="add", RUN+="/bin/chown -R %E{MY_UZER}:users /media/%E{dir_name}"

# clean up after device removal
ACTION=="remove", RUN+="/bin/umount -l '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"
ACTION=="remove", RUN+="/bin/sed -i '/\/dev\/%k /d' /etc/fstab"

# exit
LABEL="exit"

Nashle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants