how linux handles sd card remove from usb card reader
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how linux handles sd card remove from usb card reader
I am looking for some technical information. I have USB SD card reader connected linux system. when i connect sd card to USB card reader, "add" event is triggered and i can see sd card is mounted. i am checking on udevadm monitor. When i remove sd card from USB card reader ( not removing USB card reader), i see only "change" event triggered from kernel. No "remove" event. And mounted filesystem is still there and did not unmount. So second time when i connect i dont see any add event.
So i want to know how to unmount filesystem when only sd card is removed from card reader.
As i am new to linux detailed information on this will help me understand better.
Distribution: Cinnamon Mint 20.1 (Laptop) and 20.2 (Desktop)
Posts: 1,673
Rep:
Welcome to Linux Questions!
Like Windows, you shouldn't really remove a USB storage device just by pulling it out. You don't say which distribution you are using but normally you will see the card as a mounted file system on your desktop and also via the window managers view of the /home folder.
Right click and use "safely remove device" or "eject" to unmount it (it will disappear from the desktop) then it's safe to remove. From the command line it would be something like
umount -a [-dflnrv] [-t vfstype] [-O options]
umount [-dflnrv] {dir|device}...
DESCRIPTION
The umount command detaches the file system(s) mentioned from the file hierarchy. A file system is specified by giving the directory where it has
been mounted. Giving the special device on which the file system lives may also work, but is obsolete, mainly because it will fail in case this
device was mounted on more than one directory.
Note that a file system cannot be unmounted when it is `busy' - for example, when there are open files on it, or when some process has its working
directory there, or when a swap file on it is in use. The offending process could even be umount itself - it opens libc, and libc in its turn may
open for example locale files. A lazy unmount avoids this problem.
All,
Thank you for your responses, but i am looking for a solution where i can unmount ( actual command umount ) automatically.
my udev rule for usb automatically mounts the sd card when i connect usb card reader ( with sd card inserted).
udev rule for "remove" event actually unmounts the files system, but what i observe is that "remove event is not triggered when i remove sd card from card reader. I see only change event.
udevadm monitor output when i remove sd card
KERNEL[48.611865] change /devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4.1/1-4.1.1/1-4.1.1:1.0/host3/target3:0:0/3:0:0:0/block/sdb (block)
UDEV [48.613628] change /devices/pci0000:00/0000:00:14.0/usb1/1-4/1-4.1/1-4.1.1/1-4.1.1:1.0/host3/target3:0:0/3:0:0:0/block/sdb (block)
As there is no remove event, udev doesnt automatically unmounts.
So i want to know, what do i do to unmount it automatically when sd card is removed.
Yes i can do that, but my requirement is to handle sd card removal from usb card reader and system should automatically unmount it.
Either udev rule or some module which can detect sd card removal.
After removing the card itself from the reader the umount and/or eject command have no any meaning. Umount will prepare the system to safely remove the card. Without umounting the content of the card may be damaged.
Thats correct, umount has no meaning after sd card removed.
But currently without umount mounted entry points remains, so that is the problem, with umount atleast that entry goes off.
For eg, sd card is mounted to /media/usb-sda1-xx
but even after removing i can see this path and access. I want to remove that path with umount command.
umount may or may not work.
You may try the following: save a huge file on the card, remove the card without umount/eject/sync and finally try to umount.
Distribution: Cinnamon Mint 20.1 (Laptop) and 20.2 (Desktop)
Posts: 1,673
Rep:
As I understand it, in a Unix/Linux system, when the umount command is issued, the OS ensures that the data held on the storage device is synchronised with anything relevant still held in main memory or cache. If the device is just removed, the OS is obviously unable to complete this synchronisation.
As an example, in the "Bad old days" you used to stop a Unix system with the commands
Code:
#sync
#sync
#halt
which "manually" flushed cache, etc, when closing the whole system down. (OK, so you didn't really need to sync twice... Just belt and Braces! )
Play Bonny!
Last edited by Soadyheid; 09-09-2014 at 04:56 AM.
Reason: Additional stuff
I agree with 'Soadyheid'. If you want to manually remove the SD then do from cli as root;
Code:
buck1:~#sync
buck1:~# umount /dev/your_device
By using the 'sync' command you are insuring everything is completed to the SD filesystem then enter the 'umount /dev/your_device'. That way no corruption of the filesystem. If you just pull the SD without insuring everything is completed then a potential corrupt filesystem.
I suggest that you do 'man sync' & 'man umount' to understand the commands.
Hope this helps.
Have fun & enjoy!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.