How do I detect SD card insertion (just the SD card, not the card reader) in Linux?

18,234

Solution 1

Here's how I've solved this problem:

  1. Install the udisks package
  2. Run udisks --poll-for-media

Via a udev rule I then trigger a script that takes care of downloading the data from my CF card automatically.

Solution 2

I suspect that polling for block devices are disabled.

check this value

cat /sys/module/block/parameters/events_dfl_poll_msecs

is its 0, make it 2000

echo 2000 > /sys/module/block/parameters/events_dfl_poll_msecs

Now run time detection should work.

Usually, the udev rules contain attributes to write 2000 at this node. So if that udev package is missing, install it.

Share:
18,234

Related videos on Youtube

WoJ
Author by

WoJ

Updated on September 18, 2022

Comments

  • WoJ
    WoJ almost 2 years

    I have a headless debian (3.2.0-3-686-pae) to which I wanted to attach a standalone card reader to automatically process my pictures. The idea is that the reader will be plugged in all the time and I would just insert the SD card.

    Unfortunately the card is not detected when I insert it.

    • the card reader is correctly recognized
    • when I connect the card reader with the SD card already inserted both are correctly recognized and the card is automatically mounted.

    Therefore this is the action of inserting the card into a working card reader which is not recognized. There are no messages in /var/log/messages (nor anywhere else). When the card reader is inserted the messages are correct:

    Aug  4 14:25:03 server kernel: [711743.411320] usb 1-6: USB disconnect, device number 5
    Aug  4 14:25:12 server kernel: [711751.964021] usb 1-6: new high-speed USB device number 6 using ehci_hcd
    Aug  4 14:25:12 server kernel: [711752.097364] usb 1-6: New USB device found, idVendor=058f, idProduct=6366
    Aug  4 14:25:12 server kernel: [711752.097369] usb 1-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    Aug  4 14:25:12 server kernel: [711752.097373] usb 1-6: Product: Mass Storage Device
    Aug  4 14:25:12 server kernel: [711752.097375] usb 1-6: Manufacturer: Generic
    Aug  4 14:25:12 server kernel: [711752.097377] usb 1-6: SerialNumber: 058F63666433
    Aug  4 14:25:12 server kernel: [711752.098062] scsi4 : usb-storage 1-6:1.0
    Aug  4 14:25:13 server kernel: [711753.224612] scsi 4:0:0:0: Direct-Access     Multiple Card  Reader     1.00 PQ: 0 ANSI: 0
    Aug  4 14:25:14 server kernel: [711753.810820] sd 4:0:0:0: [sdc] 1984000 512-byte logical blocks: (1.01 GB/968 MiB)
    Aug  4 14:25:14 server kernel: [711753.811559] sd 4:0:0:0: [sdc] Write Protect is off
    Aug  4 14:25:14 server kernel: [711753.829719]  sdc: sdc1
    Aug  4 14:25:14 server kernel: [711753.840025] sd 4:0:0:0: [sdc] Attached SCSI removable disk
    

    I was wondering if I am missing something somewhere (the behavior above is out of the box - I did not need to configure anything) -or- that the insertion of a card into an already-connected card reader is simply not recognized by design.


    EDIT: While fora discussions claim that the lack of message is normal, it obviously works for some (thanks Piskvor nad darnir for the comments). I will leave the question unanswered and file a bug with Debian - updating this post if there is any progress.

    EDIT: filed Bug#684306 (bugs.debian.org)

    • ganesh
      ganesh almost 12 years
      I can't find the original mails atm, but a year ago there was a discussion about this on a the FreeBSD mailing lists. The answer was that insertion can not be detected by the hardware. (or as you write: not recognized by design). The only workaround was to poll the device.
    • Piskvor left the building
      Piskvor left the building almost 12 years
      Hmmm...the card insertion event generates a "detected capacity change from 0 to 123456789" line in syslog for me (where 123456789 is card capacity in bytes, IIRC); not sure what is generating this (logged as "kernel").
    • WoJ
      WoJ almost 12 years
      @Piskvor: ah this is interesting. Which kernel are you on? I log everything and do not see this kind of message
    • Piskvor left the building
      Piskvor left the building almost 12 years
      @WoJ: 3.2.0-27; I currently don't have a SD card to try this and post the exact message.
  • darnir
    darnir almost 12 years
    I tried it on Arch and I have an entry in dmesg on card insertion/removal with the reader already plugged in. So, it is possible, out-of-the-box on the vanilla kernel.
  • WoJ
    WoJ almost 12 years
    @darnir: what message do you get? Something similar to Piskvor's one (above)
  • darnir
    darnir almost 12 years
    dmesg output: [ 4366.893775] r592: IRQ: card removed [ 4375.765801] r592: IRQ: card added [ 4376.519845] memstick0: switching to 4-bit parallel mode [ 4376.523049] mspblk0: p1