prenone / platformio-vscode-devcontainer

PlatformIO Development Container for Visual Studio Code
MIT License
19 stars 10 forks source link

usb-serial device permission denied #9

Open kolod opened 5 months ago

kolod commented 5 months ago
  1. What is the sense of commands:
    RUN curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/system/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules
    RUN service udev restart

    if udev does not support containers and you mount /dev from the parent os or wsl?

Running the postAttachCommand from devcontainer.json...

[55441 ms] Start: Run in container: /bin/sh -c sudo service udev restart
udev does not support containers, not started ... (warning).
  1. If idev not working inside container why you install it?

    RUN sudo apt-get -y install --no-install-recommends clang python3-venv udev
  2. For my project I use two containers: one for firmware & one for UI.

docker-compose.yml

version: '3'
services:
  firmware:
    build: ./.devcontainer/firmware-container
    volumes:
      - .:/workspace:cached
    command: sleep infinity
    links:
      - frontend
  frontend:
    image: mcr.microsoft.com/devcontainers/typescript-node:1-20-bookworm
    volumes:
      - .:/workspace:cached
    command: sleep infinity

.devcontainer/firmware-container/devcontainer.json

{
    "name": "firmware",
    "dockerComposeFile": ["../../docker-compose.yml"],
    "service": "firmware",
    "shutdownAction": "none",
    "workspaceFolder": "/workspace/firmware",
    "customizations": {
        "vscode": {
            "extensions": [
                "ms-vscode.cpptools-extension-pack",
                "platformio.platformio-ide",
                "ms-python.python",
                "ms-python.debugpy"
            ]
        }
    },
    "postAttachCommand": "sudo service udev restart",
    "mounts": [
        "source=/dev/,target=/dev/,type=bind,consistency=consistent"
    ],
    "forwardPorts": [
        8008
    ],
    "privileged": false
}

.devcontainer/firmware-container/Dockerfile

FROM mcr.microsoft.com/vscode/devcontainers/base:bookworm

# Install clang, python & udev
RUN sudo apt-get update
RUN export DEBIAN_FRONTEND=noninteractive
RUN sudo apt-get -y install --no-install-recommends clang python3-venv udev

## Set up udev rules for PlatformIO
RUN curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/system/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules
RUN service udev restart
RUN usermod -a -G dialout vscode
RUN usermod -a -G plugdev vscode

# Install PlatformIO CLI
USER vscode
RUN curl -fsSL -o /tmp/get-platformio.py https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py
RUN python3 /tmp/get-platformio.py
RUN echo 'export PATH="$PATH:$HOME/.platformio/penv/bin"' | tee -a /home/vscode/.bashrc
RUN echo 'export PATH="$PATH:$HOME/.platformio/penv/bin"' | sudo tee -a /root/.bashrc

PowerShell:

PS C:\Users\user> usbipd list
Connected:
BUSID  VID:PID    DEVICE                                                        STATE
1-2    1462:7e06  USB Input Device                                              Not shared
1-8    1a86:55d3  USB-Enhanced-SERIAL CH343 (COM3)                              Attached
1-14   8087:0033  Intel(R) Wireless Bluetooth(R)                                Not shared
1-19   090c:1000  USB Mass Storage Device                                       Not shared
3-3    06da:ffff  USB Input Device                                              Not shared
6-1    046d:c31c  USB Input Device                                              Not shared
6-3    1532:0084  Razer DeathAdder V2, USB Input Device                         Not shared

Persisted:
GUID                                  DEVICE

PS C:\Users\user>

Debian WSL:

user@pc:~$ groups
user adm cdrom sudo dip plugdev users
user@pc:~$ ls -lah /dev | grep ttyA
crw-------  1 root root 166,   0 Apr  7 10:04 ttyACM0

Inside container

vscode ➜ /workspace/firmware $ groups vscode
vscode : vscode dialout plugdev
vscode ➜ /workspace/firmware $ ls -lah /dev | grep ttyA
crw-------  1 root root 166,   0 Apr  7 07:04 ttyACM0
vscode ➜ /workspace/firmware $ pio device list
/dev/ttyACM0
------------
Hardware ID: USB VID:PID=1A86:55D3 SER=5764044367 LOCATION=1-1:1.0
Description: USB Single Serial

Upload log

Uploading .pio/build/nodemcuv2/firmware.bin
esptool.py v3.0
Traceback (most recent call last):
Serial port /dev/ttyACM0
  File "/home/vscode/.platformio/penv/lib/python3.11/site-packages/serial/serialposix.py", line 322, in open
    self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '/dev/ttyACM0'
prenone commented 2 days ago

Hello, sorry for getting back to you so late. I do not have much experience with WSL so I hope someone more qualified can step in eventually. Anyway, I don't really get what you mean with

if udev does not support containers and you mount /dev from the parent os or wsl?

and

If idev not working inside container why you install it?

It seems to me that udev works (on Linux, no idea on WSL, but as stated in the README I only guarantee support for Linux) and that is why I install it.

The only things that seems odd to me is that you set "privileged": false in your devcontainer.json. If you look at the code in this repository you can see that the container runs as privileged https://github.com/prenone/platformio-vscode-devcontainer/blob/f11ca7008793c26b54457225d8a2e4c60008bbb0/.devcontainer/devcontainer.json#L28C1-L30C4.

The code you use seems different from the current one in the repository, can you try to git clone this repo and upload a simple code to your device just to verify? Please reply with the errors you get and I will be more than happy to help you

kolod commented 1 day ago

It seems to me that udev works (on Linux, no idea on WSL, but as stated in the README I only guarantee support for Linux) and that is why I install it.

It works because you use /dev from host system.

kolod commented 1 day ago

I solved the problem as follows:

  1. Run wsl --list --all in PowerShell. If docker-desktop is set as default change it to Debian/Ubuntu by wsl --setdefault Debian or wsl --setdefault Ubuntu. If linux distro not installed install it from Microsoft Store.
  2. Install usbipd.
  3. Run usbipd bind --hardware-id 1a86:55d3 in PowerShell console as Administrator.
  4. Run usbipd attach --wsl --auto-attach --hardware-id 1a86:55d3 in PowerShell console as normal user. WSL must be running.
kolod commented 1 day ago

I am currently using my own container based on mcr.microsoft.com/vscode/devcontainers/base:bookworm.

prenone commented 1 day ago

I see... I do not have the time to implement any change nor research it right now. But it seems like you have quite some experience, if you can put together a pull request I can try to test it in the next week and merge😁