2

I've made a MP3 player for the Pi using a 16x2 LCD display and 5 buttons; what I would like to do next is to add the ability to automount any USB flash inserted and then tell my program (all written in C btw) so it can add any music on the flash to it's linked list / playlist. As I won't be changing anything on the flash, I'd just want to mount it ro mode (plus to avoid any corruption when the flash is removed)

1
  • 1
    What have you tried? There seem to be plenty of hits for linux mount drive read only. Did none of them work?
    – joan
    Nov 27, 2014 at 9:54

1 Answer 1

3

Under /etc/udev/rules.d , create a file named like usb-ro.rules, with the following content.

KERNEL=="sd*", SUBSYSTEMS=="usb", MODE="0555"

This will mount all USB devices as read-only.

If you have something mounted on USB, unmount it all and issue command

sudo /etc/init.d/udev restart

I think to get RW access you need to unmount it while plugged in then remount it with mount. I am sure you can add that into your program to do this via the interface and calling bash command directly as su


If you want to be able to "automount" any kind of USB device on any port with FAT32/NTFS/EXT it will require a bit more work.

You can simplify the /dev/sd location by always mounting any USB device to the same device namespace location by using udev again..(restart required also)

SUBSYSTEM=="usb", SYMLINK+="Playlist%n"

This will yield (pure example with multiple partitions)

ls -l /dev/Playlist*
lrwxrwxrwx 1 root root  3 2014-02-23 12:36 /dev/Playlist -> sdc
lrwxrwxrwx 1 root root 12 2014-02-23 12:36 /dev/Playlist0 -> bsg/14:0:0:0
lrwxrwxrwx 1 root root  4 2014-02-23 12:36 /dev/Playlist1 -> sdc1
lrwxrwxrwx 1 root root  3 2014-02-23 12:36 /dev/Playlist3 -> sg3

You can then install autofs which will detect the filesytem and mount it where you need.

apt-get install autofs

In the masterfile add this

/media/   /etc/auto.ext-usb --timeout=10,defaults,user,exec,uid=1000

Edit /etc/auto.ext-usb and add this (this will auto mount any 1st partition)

Playlist            -fstype=auto           :/dev/Playlist1

Restart

/etc/init.d/autofs restart

You still need to check if a USB was plugged in or not with your software.

/etc/init.d/autofs status

And if there is something to mount its as simple as command

mount
2
  • The problem is, I need this to work without anyone logged in. On boot up, I have my Pi start up my mp3 player (from /etc/rc.local) as root. I may just add a 6th button that toggles mounting/unmounting /dev/sda1 and throw in some sanity checks.
    – linuxgnuru
    Nov 29, 2014 at 6:59
  • OK, I see like autoplay in Windows. That is possible but requires a bit more scripting.
    – Piotr Kula
    Nov 29, 2014 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.