Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[solved] Unmount usb drives by non-root user mounted by udev
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
blackst0ne
n00b
n00b


Joined: 14 Jul 2007
Posts: 6
Location: YS, Russia

PostPosted: Mon Apr 02, 2012 11:45 pm    Post subject: [solved] Unmount usb drives by non-root user mounted by udev Reply with quote

Hello!

I used to mount my USB flash drives this way:
Code:

[ blackst0ne at work: ~ ]% cat /etc/udev/rules.d/10-flash-mounts.rules
#! /bin/sh

#######################################
#    USB Flash Drives automounting    #
#######################################

# start at sdb to ignore the system hard 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"

# 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}="flash_drive_%k"

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

# global mount options
ACTION=="add", ENV{mount_options}="users"

# filesystem-specific mount options (777/666 dir/file perms for ntfs/vfat)
ACTION=="add", ENV{ID_FS_TYPE}=="vfat", ENV{mount_options}="$env{mount_options},gid=100,dmask=000,fmask=111,utf8,flush"

# automount ntfs filesystems using ntfs-3g driver
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options} /dev/%k '/mnt/%E{dir_name}'"

# automount all other filesystems
ACTION=="add", ENV{ID_FS_TYPE}!="ntfs", RUN+="/bin/mount -t auto -o %E{mount_options} /dev/%k '/mnt/%E{dir_name}'"

# clean up after device removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/mnt/%E{dir_name}'", RUN+="/bin/rmdir '/mnt/%E{dir_name}'"

# exit
LABEL="exit"



So the question is, how do I unmount such devices by non-root users?

Code:

[ blackst0ne at work: ~ ]% umount /dev/sdb1
umount: /dev/sdb1 is not in the fstab (and you are not root)


Is there a simple way to do that?

Thanks.


Last edited by blackst0ne on Wed Apr 04, 2012 4:05 am; edited 1 time in total
Back to top
View user's profile Send private message
ultraincognito
Guru
Guru


Joined: 03 Jun 2011
Posts: 346
Location: Ukraine

PostPosted: Tue Apr 03, 2012 8:50 am    Post subject: Reply with quote

Make an appropriate script that may be executed with the sudo by you. I did so.
Look on how did I do it if you are interested (in Ukrainian).
Back to top
View user's profile Send private message
blackst0ne
n00b
n00b


Joined: 14 Jul 2007
Posts: 6
Location: YS, Russia

PostPosted: Wed Apr 04, 2012 4:05 am    Post subject: Reply with quote

Okay, I got it.
Here is my /etc/udev/rules.d/10-flash-mounts.rules file:

Code:

[ blackst0ne at home: /etc/udev/rules.d ]% cat ./10-flash-mounts.rules
#! /bin/sh

#######################################
#    USB Flash Drives automounting    #
#######################################

# start at sdb to ignore the system hard 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"

# 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}="flash_drive_%k"

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

# global mount options
#ACTION=="add", ENV{mount_options}="relatime"

# filesystem-specific mount options (777/666 dir/file perms for ntfs/vfat)



########
# vfat #
########

# vfat specific mount options
ACTION=="add", ENV{mount_options_vfat}="gid=100,dmask=000,fmask=111,utf8,flush,rw,noatime,users"

# add device to /etc/fstab
ACTION=="add", ENV{ID_FS_TYPE}=="vfat", RUN+="/bin/sed -i '$a\/dev/%k /mnt/%E{dir_name} vfat %E{mount_options_vfat} 0 0' /etc/fstab"

# mount device
ACTION=="add", ENV{ID_FS_TYPE}=="vfat", RUN+="/bin/mount -t auto -o %E{mount_options_vfat} /dev/%k '/mnt/%E{dir_name}'"



########
# ntfs #
########

# ntfs specific mount options
ACTION=="add", ENV{mount_options_ntfs}="gid=100,dmask=000,fmask=111,utf8,flush,rw,noatime,users"

# add device to /etc/fstab
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/sed -i '$a\/dev/%k /mnt/%E{dir_name} ntfs-3g %E{mount_options_ntfs} 0 0' /etc/fstab"

# mount device
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options_ntfs} /dev/%k '/mnt/%E{dir_name}'"



#####################
# other filesystems #
#####################

# ntfs specific mount options
ACTION=="add", ENV{mount_options_other}="gid=100,dmask=000,fmask=111,utf8,flush,rw,noatime,users"

# automount all other filesystems
ACTION=="add", ENV{ID_FS_TYPE}!="(ntfs|vfat)", RUN+="/bin/mount -t auto -o %E{mount_options_other} /dev/%k '/mnt/%E{dir_name}'"

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

# exit
LABEL="exit"


Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum