# udev removing mounted SD-card is only known as change

## kami22

Hi,

i am trying to add and remove a SD-Card from an USB Card Reader. It is working really fine in the udevadm monitor. But if i add this udev.rule to mount and umount the device, it works when i add the card but if i remove the card it is only known as change and not as remove. So the problem is, that after removing the card the device is still mounted.

here is my udev.rule:

```
/etc/udev/rules.d/11-sd-cards-auto-mount.rules

KERNEL!="mmcblk[0-9]p[0-9]", GOTO="sd_cards_auto_mount_end"

# Global mount options

ACTION=="add", ENV{mount_options}="relatime"

# Filesystem specific options

ACTION=="add", IMPORT{program}="/sbin/blkid -o udev -p %N"

ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

ACTION=="add", RUN+="/bin/mkdir -p /media/sd-%k", RUN+="/bin/ln -s /media/sd-%k /mnt/sd-%k", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/sd-%k"

ACTION=="remove", RUN+="/bin/umount -l /media/sd-%k", RUN+="/bin/rmdir /media/sd-%k"

LABEL="sd_cards_auto_mount_end"

```

And here is my udev output at the end you can see that it is only change.

```
udevadm monitor

monitor will print the received events for:

UDEV - the event which udev sends out after rule processing

KERNEL - the kernel uevent

KERNEL[1641.015363] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

KERNEL[1641.020223] remove   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde/sde1 (block)

KERNEL[1641.020294] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

UDEV  [1641.022615] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

UDEV  [1641.028879] remove   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde/sde1 (block)

UDEV  [1641.033845] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

KERNEL[1647.416343] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

KERNEL[1647.427269] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

KERNEL[1647.427623] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde/sde1 (block)

UDEV  [1647.551886] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

UDEV  [1647.679667] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

UDEV  [1648.077400] add      /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde/sde1 (block)

KERNEL[1650.400842] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

UDEV  [1650.406386] change   /devices/pci0000:00/0000:00:1d.7/usb1/1-4/1-4.1/1-4.1:1.0/host6/target6:0:0/6:0:0:2/                                                                                              block/sde (block)

```

Thanks a lot for help.

Cu kami

----------

## ulenrich

The real problem is:

For to have a proper unmount, the kernel needs to have the mounted partition available. Some bits may have to be set to mark the partition proper ...

This seems to be a bug like this famous Gentoo unsolved:

USE=cow emerge bull

You could try a patch like this

if change then sleep 5 and then unmount

which would need to be handled by you removing twice ...

----------

## kami22

Hi,

so if i understand you right, it is a general problem. So is my solution right?

Can i do this. 

If i plug in a card it is mounted with a script. If i remove the device i will only get the change info. But with this change info i can look in "mount" if there is such a device mounted and then umount it? Is this right?

Thanks a lot.

Cu kami

----------

## ulenrich

No, because 

if you unmount a partition the Linux Kernel wants to write some 

"proper" Bits into that partition. You cannot remove before !

It is the same with Microsoft Windows Clean Remove the device. This must also be done before removing the hardware.

You can do this cleanup with Kde Device Notifier right click menu - if you use that DesktopEnvirenment ...

Excuse me, my response was in parts a kind of a joke ....

----------

## kami22

Hi,

thanks a lot for the answer. I know this problem. Normally i will unmount the device before i remove it. But if some just remove the card, the system can not hang  :Smile:  So there should be an alternative  :Smile: 

I will try a script  :Smile: 

Cu kami

----------

