Remounting USB/SD Card

      Remounting USB/SD Card

      Hi,

      in my software I'm checking for USB Memory or SD Card by opening /dev/sda1 or /dev/mmcblk0p1. If one of these files exists I'm mounting by

      Source Code

      1. mount -t vfat /dev/sda1 /media/usb
      2. mount -t vfat /dev/mmcblk0p1 /media/sd

      Now I can read/write the media/usb or /media/sd directory. If the user wants to remove the devices he will tell my software an I'm unmounting by

      Source Code

      1. umount /media/usb
      2. umount /media/sd

      Now the USB or SD Card can be removed. When I'm trying to re-enter the USB or SD Card they are mapped to another device (/dev/sda2 and /dev/mmcblk1p1) later (/dev/sda3 and /dev/mmcblk2p1). So it's hard for me to find out if an USB Memory or SD Card was plugged in. How is it possible to use /dev/sda1 and /dev/mmcblk0p1 every time?

      Tanks and kind regards
      RSchubert

      Re: Remounting USB/SD Card

      We use mdev. Here we find the device always on the same node.

      Note: If you switch your configuration to mdev you need to do a clean build.
      F&S Elektronik Systeme GmbH
      As this is an international forum, please try to post in English.
      Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

      Re: Remounting USB/SD Card

      "RSchubert" wrote:

      When I'm trying to re-enter the USB or SD Card they are mapped to another device (/dev/sda2 and /dev/mmcblk1p1) later (/dev/sda3 and /dev/mmcblk2p1). So it's hard for me to find out if an USB Memory or SD Card was plugged in. How is it possible to use /dev/sda1 and /dev/mmcblk0p1 every time?

      It seems as if the old devices are not really freed if you call umount. Can it be that you still have some reference to the device, for example a current directory pointing to it? How do you call these system commands? Do you by any chance start a subshell that continues to run after the mount/umount command is executed? Does it help if you call the C library functions mount() and umount() instead of the system programs mount and umount?

      In our mdev environment I get this behavior if I add a second device (e.g. a second USB stick connected via a hub) while the first is still active. Then the device numbers increase (of course). But when I unplug a device, the corresponding device name is actually removed from the /dev directory by mdev and if I insert it again later, it will re-use the lower number again.

      However in general it is a rather unsafe system to rely on the device number alone. For example if you have inserted two USB sticks while the system boots, it is not defined which of them becomes sda and which of them sdb. It is purely random because of the parallel multitasking character of Linux and only depends on the boot sequence of the kernel threads and the response times of the sticks, *not* on the USB port. So you should have some other item to identify your device, for example some special file structure or file names to be present on the stick. Or some special label or UUID of the device. Current Linux distributions (e.g. Fedora) use different naming schemes /dev/disk/by-id, /dev/disk/by-path and /dev/disk/by-uuid to allow identifying the disks independent from the sd device name they get. But this is beyond the scope of our small BuildRoot distribution and depends on whether you use SystemV start sequence, upstart or systemd to start your system. The BuildRoot/Busybox environment uses a very restricted and minimized starting scheme that resembles a little bit the SystemV start sequence.
      F&S Elektronik Systeme GmbH
      As this is an international forum, please try to post in English.
      Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.