Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
Cannot Mount SD card after it is removed without Umount

Good afternoon,

I have a project where the SD card may become momentarily disconnected due to a weak contact on the SD Detect pin. I want to be able to check if it has been unmounted and remount it in software.

My SD card partition is /dev/mmcblk1p1 It is originally mounted automatically when Linux boots up

If I execute a umount /mnt/storage command, then remove the SD card, I can re-insert it, and perform a mount /dev/mmcblk1p1 /mnt/storage without issues

However, if I remove the SD card first, without umount, executing mount /dev/mmcblk1p1 /mnt/storage results in

mounting /dev/mmcblk1p1 on /mnt/storage failed: No such device or address

I tried running

umount /mnt/storage
rmdir /mnt/storage
mkdir /mnt/storage

This error always comes up when the SD card is removed without unmounting

I'm using a Yocto distribution running on Imx6.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

If the SD card is written to by a C++ application, it can be forcefully removed, but it cannot be umount(ed) until that application closes the file descriptors. The application itself does not need to close.

This solution is for mechanical applications, where the SD card (or other flash media) may become momentary disconnected, and needs to be resumed writing to by an application:

Answer:

  1. Forcefully remove the SD card

  2. Detect that the SD card is disconnected

Simplest way to do this is check the SD cards' mount size

read file "/sys/block/mmcblk1/size" and check it's > 0
  1. Close all file descriptors that are writing to files on the SD card.

Note that it's not necessary to close these before the card is removed. Linux is robust enough to write to a non-existing file. But this needs to be done before the SD card is umount'ed, otherwise umount will fail, and this is what was causing my problem. Umount also cannot be ran when the SD card is already re-inserted.

  1. Umount the SD card

    system("umount /mnt/storage");'

  2. Now re-insert the SD card

  3. Detect the Card has been reinserted

  4. Mount the card again

    system("mount /dev/mmcblk1p1 /mnt/storage")

over 4 years ago · Santiago Trujillo Report

0

If you have udev running on your system you can let udev's mount script auto-unmount your devices if they are removed. Therefore add something like the following in /etc/udev/scripts/mount.sh:

UMOUNT="/bin/umount"

if [ "$ACTION" = "remove" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
        for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `; do
                $UMOUNT $mnt
        done
fi

Furthermore you have to tell udev to run that mount script on added and removed devices. Therefore I've created /etc/udev/rules.d/autounmount.rules:

SUBSYSTEM=="block", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh"

This works fine for me using Yocto "Daisy" on an armv7 platform.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!