Hi,
I am trying to mount a USB drive to given symlink and mount it to a given mount point. Mount is working but when the device removed, I can still see the mount
cat /etc/udev/rules.d/81-hnt-usb-rule.rules ACTION=="add", KERNEL=="sd?1", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", \ SYMLINK+="hanthana%n", ENV{SYSTEMD_WANTS}="hnt-usb-rule.service"
ACTION=="remove", RUN+="/usr/bin/umount $(/usr/bin/mount | grep backup | /usr/bin/awk '{print $1}')"
------------------------------------------------------------------------------------------------ [danishka@hanthana ~]$ cat /etc/systemd/system/hnt-usb-rule.service [Unit] Description=USB Mount
[Service] Type=oneshot ExecStart=/root/hnt.sh
[Install] WantedBy=default.target ---------------------------------------------------------------------------------------------------------- cat /root/hnt.sh #!/bin/bash mkdir /backup lsblk > /var/log/hnt.log /usr/bin/mount /dev/hanthana1 /backup
------------------------------------------------------------------------------------------------
ACTION=="remove", RUN+="/usr/bin/umount $(/usr/bin/mount | grep backup | /usr/bin/awk '{print $1}')"
[root@hanthana ~]# echo $(/usr/bin/mount | grep backup | /usr/bin/awk '{print $1}') /dev/sda1
Even though you can't access /backup after removing the USB, mount command shows following results (After plugin the usb and remove several times). I wonder why it automatically remove these entries as fedora normally mount USBs on default location.
/dev/sda1 on /backup type ext4 (rw, relatime, seclabel) /dev/sdb1 on /backup type ext4 (rw, relatime, seclabel) /dev/sdc1 on /backup type ext4 (rw, relatime, seclabel) /dev/sdd1 on /backup type ext4 (rw, relatime, seclabel)
I was testing on both Fedora 28 Gnome and LXDE.
Regards,
Here's what I use for my automated backup script (see https://gitlab.com/slackermedia/attachup for context):
If you adapt this, you'll need to update the path (I use /media), you'll need to update the UUID, and you'll probably need to verify that your drive has a label, because this rule uses the UUID and label to identify the drive.
I save this as 80.local-rules in /etc/udev/rules.d Supposedly you can do a `udevadm control reload` to force the rules to be reloaded, but I usually reboot just to make sure udev is on top of its game.
KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end" IMPORT{program}="/sbin/blkid -o udev -p %N" # Get label or create one ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}" ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k" # mount ACTION=="add", ENV{mount_options}="relatime" ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002" # Clean up after removal ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}" # Exit LABEL="media_by_label_auto_mount_end" #mount ACTION=="add",ENV{ID_FS_UUID}=="165f84bc-1d70-47a4-9fdc-7a588fa457c4",RUN+="/bin/mkdir -p /media/%E{dir_name}",RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}",MODE="0770",GROUP="users"
On Wed, May 15, 2019 at 11:37 PM Danishka Navin danishka@gmail.com wrote:
Hi,
I am trying to mount a USB drive to given symlink and mount it to a given mount point. Mount is working but when the device removed, I can still see the mount
cat /etc/udev/rules.d/81-hnt-usb-rule.rules ACTION=="add", KERNEL=="sd?1", SUBSYSTEM=="block", ENV{ID_BUS}=="usb", \ SYMLINK+="hanthana%n", ENV{SYSTEMD_WANTS}="hnt-usb-rule.service"
ACTION=="remove", RUN+="/usr/bin/umount $(/usr/bin/mount | grep backup | /usr/bin/awk '{print $1}')"
[danishka@hanthana ~]$ cat /etc/systemd/system/hnt-usb-rule.service [Unit] Description=USB Mount
[Service] Type=oneshot ExecStart=/root/hnt.sh
[Install] WantedBy=default.target
cat /root/hnt.sh #!/bin/bash mkdir /backup lsblk > /var/log/hnt.log /usr/bin/mount /dev/hanthana1 /backup
ACTION=="remove", RUN+="/usr/bin/umount $(/usr/bin/mount | grep backup | /usr/bin/awk '{print $1}')"
[root@hanthana ~]# echo $(/usr/bin/mount | grep backup | /usr/bin/awk '{print $1}') /dev/sda1
Even though you can't access /backup after removing the USB, mount command shows following results (After plugin the usb and remove several times). I wonder why it automatically remove these entries as fedora normally mount USBs on default location.
/dev/sda1 on /backup type ext4 (rw, relatime, seclabel) /dev/sdb1 on /backup type ext4 (rw, relatime, seclabel) /dev/sdc1 on /backup type ext4 (rw, relatime, seclabel) /dev/sdd1 on /backup type ext4 (rw, relatime, seclabel)
I was testing on both Fedora 28 Gnome and LXDE.
Regards,
Danishka Navin
users mailing list -- users@lists.fedoraproject.org To unsubscribe send an email to users-leave@lists.fedoraproject.org Fedora Code of Conduct: https://getfedora.org/code-of-conduct.html List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines List Archives: https://lists.fedoraproject.org/archives/list/users@lists.fedoraproject.org
Hi Seth,
On Thu, May 16, 2019 at 3:23 AM Seth Kenlon skenlon@redhat.com wrote:
Here's what I use for my automated backup script (see https://gitlab.com/slackermedia/attachup for context):
If you adapt this, you'll need to update the path (I use /media), you'll need to update the UUID, and you'll probably need to verify that your drive has a label, because this rule uses the UUID and label to identify the drive.
I save this as 80.local-rules in /etc/udev/rules.d Supposedly you can do a `udevadm control reload` to force the rules to be reloaded, but I usually reboot just to make sure udev is on top of its game.
KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end" IMPORT{program}="/sbin/blkid -o udev -p %N" # Get label or create one ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}" ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k" # mount ACTION=="add", ENV{mount_options}="relatime" ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002" # Clean up after removal ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}" # Exit LABEL="media_by_label_auto_mount_end" #mount
ACTION=="add",ENV{ID_FS_UUID}=="165f84bc-1d70-47a4-9fdc-7a588fa457c4",RUN+="/bin/mkdir -p /media/%E{dir_name}",RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}",MODE="0770",GROUP="users"
I tried with your udev rule but no luck.
[root@hanthana ~]# cat /etc/udev/rules.d/81-hnt-usb-rule.rules KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end" IMPORT{program}="/sbin/blkid -o udev -p %N"
# Get label or create one ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}" ENV{ID_FS_LABEL}=="", ENV{dir_name}="backup-%k" # mount ACTION=="add", ENV{mount_options}="relatime" ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs|ext4", ENV{mount_options}="$env{mount_options},utf8,umask=002" # Clean up after removal ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}" # Exit LABEL="media_by_label_auto_mount_end" #mount ACTION=="add",ENV{ID_FS_UUID}=="7720-13E1",RUN+="/bin/mkdir -p /media/%E{dir_name}",RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}",MODE="0770" [root@hanthana ~]# [root@hanthana ~]# [root@hanthana ~]# udevadm control --reload [root@hanthana ~]#
I noticed that udev rule create the given directory but it won't mount the device under given directory but usual direcotry /run/meda/<user>/<LABEL>. [root@hanthana ~]# ls /media/backup/ [root@hanthana ~]# [root@hanthana ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 16G 5.5G 9.7G 37% /run/media/danishka/LIVE
I did test the rule.
run: '/bin/mkdir -p /media/backup' run: '/bin/mount -o relatime,utf8,umask=002 /dev/sdb1 /media/backup' Unload module index Unloaded link configuration context.