Compile and burn SPI-NOR-Flash V3s core board (5): automatically mount/unmount U disk

In the last article: Compile and burn SPI-NOR-Flash V3s core board (4): USB extension, a topic left over is how to make the system automatically mount the U disk when the USB interface is inserted Partition, when the U disk is pulled out, it will automatically uninstall the partition of the U disk. Here is a brief explanation.

My board uses mdev instead of udev to implement hot plugging of USB interface devices. Don’t talk nonsense, go directly to the final configuration.

Step 1: Log in to the core board

Use the DEBUG port of the core board to connect to the core board. Only in this way can you see the prompt information when the USB device is connected. If you don’t care about these prompts, you can also use telnet or ssh to log in to the core board.

Step 2: Modify the /etc/init.d/rcS file

Add a sentence at the end of the /etc/init.d/rcS file: echo /sbin/mdev > /proc/sys/kernel/hotplug, as shown below

#!/bin/sh


# Start all init scripts in /etc/init.d
# executing them in numerical order.
#
for i in /etc/init.d/S* ;do

     # Ignore dangling symlinks (if any).
     [ ! -f "$i" ] & amp; & amp; continue

     case "$i" in
        *.sh)
            # Source shell script for speed.
            (
                trap-INT QUIT TSTP
                set start
                .$i
            )
            ;;
        *)
            # No sh extension, so fork subprocess.
            $i start
            ;;
    esac
done

echo /sbin/mdev > /proc/sys/kernel/hotplug

In the case of Jiangzi, the operating system can update the /proc/sys/kernel/hotplug file after starting all startup scripts. The /proc directory is in memory and is created when the system starts, so this command in /etc/init.d/rcS makes the hotplug (hot plug) function available.

Step 3: Edit the hotplug script

For a hot-swappable device, regardless of its specific function, there will be two events of insertion (insert) and removal (remove). We now consider the hot-plug response to the U disk, so write the response code for these two events as follows:

File location: /etc/hotplug

# dir /etc/hotplug/*.sh
-rwxr-xr-x 1 root root 303 Jul 28 10:13 /etc/hotplug/insert.sh*
-rwxr-xr-x 1 root root 97 Jul 28 10:23 /etc/hotplug/remove.sh*

/etc/hotplug/insert.sh

#
#
# cat /etc/hotplug/insert.sh
if [ -n "$1" ] ; then
    if [ -b /dev/$1 ]; then
        if [ ! -d /mnt ]; then
            mkdir -p /mnt
        the fi

        if [ ! -d /mnt/$1 ]; then
            mkdir -p /mnt/$1
        the fi

        mount /dev/$1 /mnt/$1

        if [ $? -ne 0 ]; then
            rm -rf /mnt/$1
        the fi
    the fi
the fi

Explanation: This script is actually quite simple. If mdev passes in a parameter, for example: sda1, a mount point will be created for this device. My mount point here is allocated in the /mnt directory, and the sda1 device will be mounted to / mnt/sda1, the sda2 device will be mounted under /mnt/sda2, etc.

/etc/hotplug/remove.sh

#
# cat /etc/hotplug/remove.sh
mntpoint=$(mount | grep $1 | cut -d' ' -f3)
umount $mntpoint
rm -rf $mntpoint
echo "$mntpoint unmounted"
#
#

Explanation: According to the parameters passed in by mdev, this script first umounts, and then deletes the /mnt/ subdirectory allocated by this device.

Please note:

  • There is no #!/bin/sh at the beginning of these two files, and they can be called by mdev without scaring
  • Both files must be chmod + x + x + x to take effect normally, otherwise there will be a Permission denied error

Edit mdev.conf

# find / -name mdev.conf
/etc/mdev.conf
#
#
# cat /etc/mdev.conf
sd[a-z][0-9] 0:0 666 @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
sd[a-z] 0:0 666 $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
ub[a-z][0-9] 0:0 666 @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
ub[a-z] 0:0 666 $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
mmcblk[0-9]p[0-9] 0:0 666 @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
mmcblk[0-9] 0:0 666 $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
#
#

This script uses regular expressions to mount different types of U disks (maybe U disks, SD cards or emmc cards).

Authentication

The figure below is the response of a hot-swapping of the U disk. You can see that the device is mounted normally and can be read and written.

#
#
[24067.423682] usb 1-1: new high-speed USB device number 11 using ehci-platform
[24067.573701] usb 1-1: device descriptor read/64, error -71
[24067.843685] usb 1-1: device descriptor read/64, error -71
[24068.103689] usb 1-1: new high-speed USB device number 12 using ehci-platform
[24068.253681] usb 1-1: device descriptor read/64, error -71
[24068.523678] usb 1-1: device descriptor read/64, error -71
[24068.783680] usb 1-1: new high-speed USB device number 13 using ehci-platform
[24069.233680] usb 1-1: device not accepting address 13, error -71
[24069.593683] usb 1-1: new high-speed USB device number 14 using ehci-platform
[24070.043679] usb 1-1: device not accepting address 14, error -71
[24070.049722] usb usb1-port1: unable to enumerate USB device
[24070.523681] usb 2-1: new full-speed USB device number 8 using ohci-platform
[24070.776720] usb 2-1: not running at top speed; connect to a high speed hub
[24070.794732] usb 2-1: New USB device found, idVendor=05e3, idProduct=0727
[24070.801464] usb 2-1: New USB device strings: Mfr=3, Product=4, SerialNumber=2
[24070.808649] usb 2-1: Product: USB Storage
[24070.812655] usb 2-1: Manufacturer: Generic
[24070.816770] usb 2-1: SerialNumber: 000000000250
[24070.824199] usb-storage 2-1:1.0: USB Mass Storage device detected
[24070.844638] scsi host0: usb-storage 2-1:1.0
[24071.930823] scsi 0:0:0:0: Direct-Access Generic STORAGE DEVICE 0250 PQ: 0 ANSI: 0
[24071.940878] sd 0:0:0:0: Attached scsi generic sg0 type 0
[24072.088785] sd 0:0:0:0: [sda] 15204352 512-byte logical blocks: (7.78 GB/7.25 GiB)
[24072.105821] sd 0:0:0:0: [sda] Write Protect is off
[24072.119793] sd 0:0:0:0: [sda] No Caching mode page found
[24072.125200] sd 0:0:0:0: [sda] Assuming drive cache: write through
[24072.191818] sda: sda1
[24072.227902] sd 0:0:0:0: [sda] Attached SCSI removable disk
[24072.324807] FAT-fs (sda1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

#
#
#
# dir /mnt/sda1
total 12
drwxrwxrwx 4 root root 4096 Jan 1 1970 ./
drwxr-xr-x 3 1000 1000 0 Jul 28 19:04 ../
drwxrwxrwx 2 root root 4096 Jul 25 01:52 System Volume Information/
drwxrwxrwx 2 root root 4096 Jul 25 11:23 ddd/
#
#
# cat /mnt/sda1/ddd/test.txt
Hello world!

#
#
#
# [24102.731126] usb 2-1: USB disconnect, device number 8

#
#
#
# dir /mnt
total 0
drwxr-xr-x 2 1000 1000 0 Jul 28 19:04 ./
drwxr-xr-x 18 root root 0 Jan 1 1970 ../
#
#