Issue with Multiple USB Mount Points on Toradex iMX7D

Hi,

We are developing an application on a Toradex iMX7D SoM running TorizonCore with Weston, using Visual Studio Code with the Torizon IDE. Our application operates inside a Docker container.

Currently, we are focusing on USB device management (utilizing three USB ports), and our goal is to automatically mount USB devices to specific mount points. According to the Toradex documentation, all USB mass storage devices are mounted in /var/rootdirs/media/ with their respective UUID or LABEL in the directory name.

We have added this path /var/rootdirs/media/ to the volumes in order to access it through the container. However, we face a challenge: we cannot access directories beyond this path, which prevents us from reading or writing files to the USB devices.

We attempted to update the fstab file with specific mount points, but this approach has not yielded the desired results due to the multiple connections of USB devices, which create numerous mount points (e.g., /dev/sda1, /dev/sdb1, /dev/sdc1, …, /dev/sdh1).

Additionally, we tried updating the udev rules to handle the addition and removal of USB devices by using systemd-mount. However, this script creates duplicate mounting directories such as /media/usb0, /media/usb1, /media/usb2, etc. Furthermore, it fails to identify the mount points for removing the directories after unmounting, resulting in errors.

Our Query…

  • Is there a direct option to overwrite the rules for USB mounting so that the mount points are named sequentially (i.e., usb0, usb1, usb2, …) instead of using UUID or LABEL?
  • Is there any way for the container to access any directory of the USB mount to read and write data?

Our main requirement is to access the three USB ports with their respective mount points effectively.

Software summary

Bootloader: U-Boot
Kernel version: 6.1.71-6.5.0+git.38fb82ecd144 #1-TorizonCore SMP Tue Dec 19 14:52:07 UTC 2023
Kernel command line: root=LABEL=otaroot rootfstype=ext4 quiet logo.nologo vt.global_cursor_default=0 plymouth.ignore-serial-consoles splash fbcon=map:3 ostree=/ostree/boot.1/torizon/b9a2dcf7fbf43a6103fa51110ba4e2541289013d869f8e87182cf862a2f2bedb/0 console=null
Distro name: NAME=“TorizonCore Upstream”
Distro version: VERSION_ID=6.5.0-build.8
Distro variant: VARIANT=“Docker”
Hostname: colibri-imx7-emmc-14952875

Hardware info

HW model: Toradex Colibri iMX7D 1GB (eMMC) on Colibri Evaluation Board V3
Toradex version: 0039 V1.1B
Serial number: 14952875
Processor arch: armv7l

Regards,
Saikrishna

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Based on the context provided, here are some suggestions to address your queries:

  1. Overwriting USB Mount Rules for Sequential Naming:

    • The default behavior of mounting USB devices using UUID or LABEL is managed by the udev rules and systemd services. To achieve sequential naming like usb0, usb1, usb2, you would need to create custom udev rules. However, this can be complex due to the dynamic nature of USB device connections and disconnections.
    • You can try creating a custom udev rule that triggers a script to mount the devices at your desired mount points. This script can maintain a mapping of connected devices to sequential mount points. However, this approach requires careful handling of device disconnections to avoid stale mount points.
  2. Accessing USB Mount Points from a Container:

    • To allow a container to access USB mount points, you need to ensure that the mount points are shared with the container. You mentioned adding /var/rootdirs/media/ to the container volumes, but you might need to ensure that the specific subdirectories (e.g., /media/usb0, /media/usb1) are also accessible.
    • You can achieve this by using Docker’s --mount option to bind mount the specific directories into the container. For example:
      docker run --rm -it --mount type=bind,source=/var/rootdirs/media/usb0,target=/mnt/usb0 my-container
      
    • Ensure that the user inside the container has the necessary permissions to read and write to these directories. If using the torizon user, it should have the appropriate group memberships to access the USB devices.

If these suggestions do not fully resolve your issue, you may need to explore more advanced udev scripting or consider alternative approaches to manage USB devices dynamically.

Please, also check if these links can help you solve your problem:

Container access USB device.

Greetings @Saikrishna,

Is there a direct option to overwrite the rules for USB mounting so that the mount points are named sequentially (i.e., usb0, usb1, usb2, …) instead of using UUID or LABEL?

I don’t believe there is an easy way to do this. Usually UUID/LABEL is used for auto-mounting purposes since it’s generally more unique of an identifier than other properties of USB devices. You would probably need to create your own unique auto-mounting program to get the exact behavior you want. Since you said “sequentially”, but that can mean different things and open a lot of questions on how mounting should behave that would need to be accounted for.

Is there any way for the container to access any directory of the USB mount to read and write data?

If you’re adding /var/rootdirs/media as a volume and you want to detect usb devices mounted under this directory in the container you should use bind-mount propagation: Bind mounts | Docker Docs

So for example if I do the following:

$ docker run --rm -it -v /media:/test:shared torizon/debian:3-bookworm

I start the container without my USB plugged in. In the container I see nothing in /test this is expected. Once i attach my USB I can see the contents in it from the container.

$ ls test/USB/
 Pictures                     hello_world.bin      torizon-core-docker-apalis-imx8-Tezi_6.8.0-devel-20240924175424+build.0
'System Volume Information'   shared-data.tar.gz   update

I can even write a file to it:

$ echo hi >> /test/USB/test.txt
$ cat test/USB/test.txt
hi

This seems like the behavior you want correct?

Best Regards,
Jeremias

Hi @jeremias.tx

This seems like the behavior you want correct?

Yes

We tested the same as above example mentioned, its working for us. But when it comes to the application which we are developing through torizon in VS code, how to add it in configurations?

We tried by adding in volumes with shared option, it is not accepting the format and also tried by adding in build commands still not working.

Please let us know if any further information is needed to help resolve this issue.

Regards,
Saikrishna

To add volume options you need to modify the corresponding docker-compose file in your project. Something like this:

- bind:
        propagation: shared
      source: <path on host os>
      target: <path in container>
      type: bind

If you’re unsure you can always reference the docker compose file documentation: Compose file reference | Docker Docs

Best Regards,
Jeremias

Hi @jeremias.tx,

We added bind propagation into our docker-compose file as you suggested and tested through container, we are able to read the files and directory, but getting permission denied error for writing/creating the files.

extraparms:
    common:
        mounts: '[{ type: "bind", source: "/media/", target: "/var/rootdirs/home/torizon/test/", propagation: "shared" }]'

Actually, by default USB block devices mounting in 0755 permissions.

torizon@colibri-imx7-emmc-14952875:~$ ls -l /media/
total 32
drwxr-xr-x 6 root root 32768 Jan  1  1970 SAI_DRIVE02
torizon@colibri-imx7-emmc-14952875:/proc$ vi mounts
/dev/sda1 /var/rootdirs/media/SAI_DRIVE02 vfat rw,nosuid,nodev,relatime,**fmask=0022,dmask=0022**,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro 0 0

So, we tried couple of things like adding **privileged: True** and using fstab and custom udev rules for mounting, by making the mounting directory as 0777 mode and group as torizon. Even though we are getting permission denied error with bind propagation.

torizon@colibri-imx7-emmc-14952875:~$ vi /etc/fstab
/dev/sda1            /media/usb0          auto       defaults,sync,auto,nofail,uid=1000,gid=1000,umask=000  0  0

Can you please check and let us know where we are missing the configuration setting.
Sorry for the late reply.

Merry Christmas :smiling_face:.

Regards,
Saikrishna
Sr. R&D Engineer
BPL Medical Technologies Pvt Ltd

Well you didn’t show me what you did in your docker-compose file so I can’t really comment on what you did.

The following docker-compose file was tested and works for me:

services:
  test:
    image: torizon/debian:3
    # This line just makes the container run without exiting
    command: tail -F anything
    volumes:
      - type: bind
        source: /media
        target: /media
        bind:
          propagation: shared

Once this container is up and running I can go into it with docker exec, once inside I attach my USB driver and can see it like so:

root@5ddcabd1bc4d:/# ls -l /media/card/
total 96
drwxr-xr-x 5 torizon torizon 16384 Oct 24 13:39  BluetoothTorizonTest
drwxr-xr-x 2 torizon torizon 32768 Apr  3  2024  Pictures
drwxr-xr-x 2 torizon torizon 16384 Apr  3  2024 'System Volume Information'
-rwxr-xr-x 1 torizon torizon  4473 Sep 24 16:14  shared-data.tar.gz
drwxr-xr-x 2 torizon torizon 16384 Dec 11 16:36  torizon-docker-apalis-imx8-Tezi_7.1.0-devel-20241212001350+build.0

I can write to it just fine:

root@5ddcabd1bc4d:/# touch /media/card/test
root@5ddcabd1bc4d:/# ls -l /media/card/test
-rwxr-xr-x 1 torizon torizon 0 Dec 20 19:57 /media/card/test

Though this was as the root user inside the container. If you want to write things as a non-root user inside the container than the files/directories for the USB drive must be owned by the user who will do the writing. For example the torizon user. This can be done with an entry in /etc/fstab:

# 1000 corresponds to the torizon user
/dev/sda1       /media/card          auto       defaults,sync,auto,nofail,uid=1000,gid=1000  0  0

With that you can even create files on the USB as a non-root user:

torizon@5ddcabd1bc4d:/$ touch /media/card/test
torizon@5ddcabd1bc4d:/$ ls -l /media/card/test
-rwxr-xr-x 1 torizon torizon 0 Dec 20 19:59 /media/card/test

Best Regards,
Jeremias

Hi @jeremias.tx,

Well you didn’t show me what you did in your docker-compose file so I can’t really comment on what you did.

docker-compose file attached.
docker-compose.yml (1.0 KB)

Though this was as the root user inside the container. If you want to write things as a non-root user inside the container than the files/directories for the USB drive must be owned by the user who will do the writing. For example the torizon user. This can be done with an entry in /etc/fstab :

We tried updating fstab file also as you mentioned, but even though we are getting permission denied error with bind propagation.

Note: We are connecting the USB Pen drive after container starts, if it is connecting before container start then it will work.

Regards,
Saikrishna
Sr. R&D Engineer
BPL Medical Technologies Pvt Ltd