Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor device enumeration for unprivileged containers #11900

Merged
merged 1 commit into from
Oct 2, 2023

Conversation

ciandonovan
Copy link
Contributor

There are currently two solutions I'm aware of to run librealsense in a Podman/Docker OCI container.

The first is to run the container with the --privileged flag, giving it access to all the host's devices. But this also disables beneficial security features, and weakens the sandbox boundary.

The second is to use the --device-cgroup-rule "c 81:* rmw" flag to allow access to the USB, but this is still far from ideal as the all USB devices are exposed.

#3849
IntelRealSense/realsense-ros#1104
https://github.com/IntelRealSense/librealsense/blob/master/scripts/Docker/readme.md
https://www.matrix-vision.com/manuals/mvBlueFOX3/UseCases_section_mvBF3_in_docker.html

Ideally it would be possible to pass through to the container only the devices we need with the --device flag, in this case a subset of the /dev/video* devices. This is especially useful for systems with multiple RealSense cameras attached, where one container is setup to handle each camera, and each camera is passed through to each container individually.

However, this doesn't work currently, because in enumerating devices, librealsense naively assumes that the sysfs entries /sys/class/video4linux/video* map directly to their corresponding /dev/video* devices. In most cases this is the correct assumption, but this doesn't hold for unprivileged rootless containers. In these containers, devices passed through must be named explicitly on container creation, and don't necessarily match the same name as on the host. Depending on how many cameras are plugged into the host, a particular device's video node numbers will be different, but they'll still have the same fixed number in the container.

librealsense simply lobs off the /sys/class/video4linux/ part of the path and replaces it with the string /dev/, leading to it trying to access a device not present in the container's mount namespace and failing to discover the camera that's attached.

My pull request modifies this behaviour, introducing a new function std::string get_devname_from_video_path(const std::string& video_path) to perform the task of correctly matching video4linux sysfs entries with their corresponding devfs devices.

This is done by first parsing the sysfs uevent file and extracting the device major and minor number, then searching each file in /dev to see if there's a match. If so, the string of the device name is returned, if not, and empty string is returned and that sysfs entry is ignored.

I've confirmed that this works with and without udev and with the official Intel ROS2 packages.

  • RealSense ROS v4.51.1
  • Running with LibRealSense v2.55.0
  • Device Name: Intel RealSense D435I
  • Device FW version: 5.13.0.50
  • Device Product ID: 0x0B3A

I have not tested with any other Intel RealSense products as I don't have access to any, especially MIPI based ones, would appreciate if someone could test that too.

I also haven't covered any iio:device devices, but from a brief reading of src/linux/backend-hid.cpp it looks like they're accessed exclusively through the devfs and so simply passing them through to the container with the --device flag should suffice. If someone has a test application that makes use of them, or the HID-SENSOR-2000e1.5.auto, I can test to ensure they still work too.

@ciandonovan
Copy link
Contributor Author

The link to the CI build doesn't seem to be working.

Screenshot from 2023-06-10 16-41-57

Got this email where the subject line says the build failed, but from the body it looks like all the checks passed?

@Nir-Az Nir-Az requested a review from dmipx June 10, 2023 16:42
@Nir-Az
Copy link
Collaborator

Nir-Az commented Jun 10, 2023

The link to the CI build doesn't seem to be working.

Screenshot from 2023-06-10 16-41-57

Got this email where the subject line says the build failed, but from the body it looks like all the checks passed?

Hi @ciandonovan
Windows build seems to be missing, probably a CI machine error.
We will double check soon.

Thanks for the PR.

@Tamir91 Tamir91 requested review from SamerKhshiboun and removed request for dmipx June 15, 2023 08:50
@ciandonovan
Copy link
Contributor Author

Is there anything I can do on my end to help fix that failing check? Unfortunately the link still isn't working so can't see exactly what's wrong.

@Nir-Az Nir-Az requested review from dmipx and removed request for SamerKhshiboun June 19, 2023 13:00
@Nir-Az
Copy link
Collaborator

Nir-Az commented Jun 19, 2023

Is there anything I can do on my end to help fix that failing check? Unfortunately the link still isn't working so can't see exactly what's wrong.

Hi,

Please allow some time, once we can make it we will review this PR, thanks!

@dmipx
Copy link
Contributor

dmipx commented Sep 20, 2023

hi @ciandonovan
Thank you for PR.
We review it.
Can you squash all commits?
Thanks.

@ciandonovan
Copy link
Contributor Author

Hi @dmipx

Will I rebase onto the head of the development branch too? Has been a while and is quite behind in terms of upstream commits by now.

src/linux/backend-v4l2.cpp Outdated Show resolved Hide resolved
@ciandonovan
Copy link
Contributor Author

Removed the part affecting the MIPI handling and squashed the remaining commits

src/linux/backend-v4l2.cpp Outdated Show resolved Hide resolved
src/linux/backend-v4l2.cpp Outdated Show resolved Hide resolved
@ciandonovan
Copy link
Contributor Author

ciandonovan commented Sep 20, 2023

I changed all the appropriate LOG_INFO to LOG_ERROR.

I now also always explicitly check the return code of get_devname_from_video_path to avoid TOCTOU issues, and take the appropriate action at that point to either throw an error or ignore if that can be done safely.

@ciandonovan
Copy link
Contributor Author

Commits squashed and ready for review

@Nir-Az Nir-Az requested a review from dmipx September 22, 2023 08:29
src/linux/backend-v4l2.cpp Outdated Show resolved Hide resolved
@ciandonovan
Copy link
Contributor Author

Fixed the get_devname_from_video_path() to return false when no devices are found

Copy link
Contributor

@dmipx dmipx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ciandonovan ciandonovan requested a review from Nir-Az September 28, 2023 10:39
@Nir-Az
Copy link
Collaborator

Nir-Az commented Sep 28, 2023

@ciandonovan Thanks again for this PR.
Since this is a core change in our SDK, we will have to run several internal ci processes.
Please allow us some time to validate before we merge.
Thanks 😀

@dmipx
Copy link
Contributor

dmipx commented Oct 2, 2023

@ciandonovan
Hi.
We are moved further with development branch.
Can you rebase your PR on development? This will help us to validate your changes.
Your changes have no conflict.
Thanks.

…major and minor devices numbers rathers than video4linux sysfs directory names
@ciandonovan ciandonovan requested a review from dmipx October 2, 2023 11:18
Copy link
Contributor

@dmipx dmipx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified.

@Nir-Az Nir-Az closed this Oct 2, 2023
@Nir-Az Nir-Az reopened this Oct 2, 2023
@Nir-Az
Copy link
Collaborator

Nir-Az commented Oct 2, 2023

Close & reopened to trigger CI, last run had a failure, let's see it it's persistent.

@ciandonovan
Copy link
Contributor Author

LRS_libci_trigger still failing, but the link isn't working so can't check why myself :(

@Nir-Az
Copy link
Collaborator

Nir-Az commented Oct 2, 2023

LRS_libci_trigger still failing, but the link isn't working so can't check why myself :(

Yes, this is an internal CI, I will update once I get the results

@Nir-Az Nir-Az merged commit 6e56dbf into IntelRealSense:development Oct 2, 2023
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants