You are not logged in.
Pages: 1
Hi guys, is there a way to:
-automatically mount all /dev in /media/$USER at boot without using fstab
-unmount all previously mounted devices before shutdown or reboot
P.S: i'm running archlinux with i3 window manager.
Thanks, Federico.
Last edited by fede93 (2016-07-20 08:44:50)
Offline
This should already be handled just fine by udisks2. This is also described in the wiki @ https://wiki.archlinux.org/index.php/Udisks
Offline
Why do you want to do this "without using fstab"? That is the simplest method for handling internal drives (simplest meaning requiring no additional tools or background daemons).
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
This should already be handled just fine by udisks2. This is also described in the wiki @ https://wiki.archlinux.org/index.php/Udisks
I tried to use udisks2 and it worked perfectly, but I wanted to recreate its behaviour with my bare hands .
Why do you want to do this "without using fstab"? That is the simplest method for handling internal drives (simplest meaning requiring no additional tools or background daemons).
Because i want something more dynamic .
I've found a way to auto mount/unmount usb drives in /media instead of internal partitions, but the logic is the same.
So, here is what i've come up to:
-In /lib/systemd/system/systemd-udevd.service I've changed the option MountFlags from slave to shared
-I've created a udev rule in /etc/udev/rules.d/ named 99-usb-handler.rules
ATTRS{removable}=="0", GOTO="usb-storage-end"
ENV{ID_FS_TYPE}=="", ENV{ID_USB_DRIVER}!="usb-storage", ENV{ID_BUS}!="usb", ENV{DEVTYPE}!="usb-device", GOTO="usb-storage-end"
ACTION=="add|remove", KERNEL=="s[a-z]*[0-9]", SUBSYSTEM=="block", RUN+="/etc/udev/usb-handler"
LABEL="usb-storage-end"
-I've created a bash script in /etc/udev/ named usb-handler, which handles the mount/unmount operations, and I've made it executable (chmod +x /etc/udev/usb-handler)
#!/bin/bash
function getMountpoint() {
i=0;
dirs=($(ls -S /media | grep -P "^($1)_[1-9]+$|^($1)$"));
while ((i < ${#dirs[@]})) && mountpoint -q /media/${dirs[$i]}; do
i=$(($i+1));
done
if ((${#dirs[@]} == 0)); then
mountpoint=$1;
elif (($i < ${#dirs[@]})); then
mountpoint=${dirs[$i]};
else
lastChar=${dirs[$i-1]##*_};
if [ $lastChar == $1 ]; then
mountpoint=$1_1;
else
mountpoint=${dirs[$i-1]%_*}_$(($lastChar+1));
fi
fi
if mountpoint -q /media/$mountpoint; then
echo $(getMountpoint $mountpoint);
else
echo $mountpoint;
fi
exit 0;
}
log="\n[$(date +%D-%T:%3N)] -> pid $$: waiting for mutex";
#This is from preventing udev to go in race condition during boot
while ! /usr/bin/mutex lock /var/lock/usb-handler-lock $$; do
:
done ;
log+="\n[$(date +%D-%T:%3N)] -> pid $$: done waiting";
WHITESPACES=$(printf "%-23s" " ");
if ! [ -d /media ]; then
mkdir /media;
fi
if [ $ACTION == add ]; then
log+="\n[$(date +%D-%T:%3N)] -> $DEVNAME has been added. Trying to mount it";
if [ ! -z $ID_FS_LABEL ]; then
mountpoint=/media/$(getMountpoint $ID_FS_LABEL);
else
mountpoint=/media/$(getMountpoint $ID_FS_UUID);
fi
if mkdir $mountpoint; then
log+="\n[$(date +%D-%T:%3N)] -> no matching mountpoint found\n[$(date +%D-%T:%3N)] -> creating $mountpoint";
else
log+="\n[$(date +%D-%T:%3N)] -> matching mountpoint found in $mountpoint";
fi
if mount -t $ID_FS_TYPE $DEVNAME $mountpoint; then
log+="\n[$(date +%D-%T:%3N)] -> mounted $DEVNAME to $mountpoint";
else
rmdir $mountpoint;
log+="\n[$(date +%D-%T:%3N)] -> Error: cannot mount $DEVNAME to $mountpoint with $ID_FS_TYPE filesystem";
log+="\n[$(date +%D-%T:%3N)] -> deleted $mountpoint";
echo -e "$log" >> /var/log/usb-handler.log
exit 1;
fi
log+="\n${WHITESPACES// /' '} -> UUID: $ID_FS_UUID, LABEL: $ID_FS_LABEL, FS: $ID_FS_TYPE";
else
log+="\n[$(date +%D-%T:%3N)] -> $DEVNAME has been removed. Trying to unmount it";
mountpoint=$(cat /proc/mounts | grep -w $DEVNAME | awk '{print $2}')
if [ $mountpoint != "" ]; then
umount $mountpoint;
rm -r $mountpoint/;
log+="\n[$(date +%D-%T:%3N)] -> unmounted $DEVNAME from $mountpoint";
log+="\n[$(date +%D-%T:%3N)] -> deleted $mountpoint";
else
log+="\n[$(date +%D-%T:%3N)] -> $DEVNAME is not mounted";
exit 1;
fi
fi
echo -e "$log" >> /var/log/usb-handler.log;
exit 0;
-I've downloaded a bash script for preventing race conditions from http://wiki.grzegorz.wierzowiecki.pl/code:mutex-in-bash, renamed to mutex and placed it in /usr/bin (remember to made it executable)
I'm a lazy human being, so I've written the code only to accomplish the job. I know for sure that in here there are people more talented than me, so i beg you to give me any advice on how i can improve the script or the rule.
Hope it helps.
Regards, Federico.
References:
http://www.reactivated.net/writing_udev_rules.html
https://wiki.archlinux.org/index.php/Udev
http://wiki.grzegorz.wierzowiecki.pl/code:mutex-in-bash
Offline
Wow. There's much simpler means in achieving this if you use the right tools—parsing ls and mount are anything but the right tools. Mounting from udev also wouldn't work with long running process, like mount.ntfs.
Read the following man pages: findmnt, mount, udev, udevadm, lsblk, flock
Note that if you didn't dismiss the fstab idea, you could do automount with the correct user permissions in about 5 lines of bash.
Last edited by Alad (2016-07-20 09:29:18)
Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Offline
Pages: 1