# X11 hotplug USB

**URL:** <https://forums.balena.io/t/x11-hotplug-usb/7250>\
**Category:** Product support\
**Created:** [May 3, 2019, 12:52pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250 "2019-05-03T12:52:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [May 3, 2019, 12:52pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/1 "2019-05-03T12:52:58Z")

</div>

Hi all,

I’ve been working with Balena for quite a while know, and I’ve succeeded to get an UI container working with the GPU correct GPU drivers for an UP Squared. So far so good.

The interface requires user interaction, so the user has a keyboard and a mouse/touchscreen. All works fine. However, if I inplug the keyboard USB and/or the touchscreen (the problem occurs with any USB), and plug it back in, the `udev monitor` sees changes inside the UI container, but the X11 session doesn’t listen to udev (I think). The X11 session just doesn’t add the USB’s, and I can’t type or click anywhere.

Some information:  
Hardware: UP Squared (Atom E5 chipset)  
Balena OS: 2.29.0+rev2  
Docker Image used: up-board-debian:latest  
Docker image runs as privileged (UDEV changes are triggered, but X11 doesn’t do anything with them)

Dockerfile:

```auto
FROM node:slim

RUN apt-get update && apt-get -y install --no-install-recommends libgtkextra-dev libgconf2-dev libnss3 libasound2 libxtst-dev libxss1 libgtk-3-0

WORKDIR /usr/src/app

COPY ./app/build/package.json ./

RUN JOBS=MAX npm install --unsafe-perm && npm cache clean --force && \
  rm -rf /tmp/*

COPY ./app/build ./

RUN ["bash", "/usr/src/app/init.sh"]

FROM balenalib/up-board-debian:latest

ENV UDEV=1

RUN apt-get update \
    && apt-get -y install --no-install-recommends \
      fuse \
      xserver-xorg-core xserver-xorg-input-all xserver-xorg-video-intel xorg \
      libnss3 libgtk-3-0 libasound2-dev \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

WORKDIR /usr/src/app

COPY ./app/run/ ./

COPY --from=0 /usr/src/app/dist/kiosk.AppImage /usr/src/app/kiosk.AppImage

CMD ["bash", "/usr/src/app/init.sh"]

```

X11 session start:

```auto
startx /usr/src/app/kiosk.AppImage

```

I hope some of you guys can help me out, because it’s really frustrating that some USB changes just don’t get picked up by the X11 session.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![CameronDiver](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@CameronDiver](https://forums.balena.io/u/CameronDiver)\
**Post date:** [May 3, 2019, 12:58pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/3 "2019-05-03T12:58:56Z")

</div>

I think this is because the dev directory is linked into the container on startup, but it doesn’t reflect any changes. We have plans to support mounting the dev filesystem into containers (with a specific flag) so that these changes are mirrored, for the exact use case of hotplugging etc.

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [May 3, 2019, 1:27pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/4 "2019-05-03T13:27:24Z")

</div>

Hi @CameronDiver,

Even if I add to my docker-compose.yml:

```auto
devices:
- "/dev:/dev"

```

I have the same issues. And I’ve checked if both the HostOS and the container have the same `/dev/bus/usb/*`, and that’s the case. So that can’t be the case?

Also the `/dev/input/` is the same, and `cat /dev/input/event2` (the keyboard) also gives some input when I press some keys.

---

<div class="post-metadata">

**Author:** ![CameronDiver](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@CameronDiver](https://forums.balena.io/u/CameronDiver)\
**Post date:** [May 3, 2019, 1:31pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/5 "2019-05-03T13:31:41Z")

</div>

Do you mean that the events still come through after replugging from within the container? That’s very strange, it might be because the filename of the device doesn’t change. In that case I would have expected the xserver to support this.

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [May 3, 2019, 1:44pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/6 "2019-05-03T13:44:11Z")

</div>

Hi @CameronDiver,

Yes, the events still come through in the container. The only thing that changes with `lsusb` is the device number of the keyboard USB when I replug the USB. So it’s very strange…

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [May 8, 2019, 8:58pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/13 "2019-05-08T20:58:27Z")

</div>

@bversluijs not sure if this will be help, but maybe if you add the mounting of /dev as a devtmpfs into your container, you can see this here: [https://github.com/balena-io-library/base-images/blob/master/scripts/assets/entry.sh#L10](https://github.com/balena-io-library/base-images/blob/master/scripts/assets/entry.sh#L10) which is applied when a balenalib container runs as privileged, but doesn’t apply when unprivileged. I think that will make the /dev refresh when hotplug events happen, but its just a guess 🙂

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [May 9, 2019, 8:46am UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/15 "2019-05-09T08:46:21Z")

</div>

Hi @shaunmulligan,

Thanks for your reply. For testing purposes, this is my `docker-compose.yml` at the moment:

```auto
version: '2'
services:
  kiosk:
    build: ./kiosk
    privileged: true
    restart: always
    environment:
      - URL_LAUNCHER_URL=https://google.com
      - ELECTRON_ENABLE_HW_ACCELERATION=1
    tmpfs:
      - /dev

```

I’ve added the /dev as a `tmpfs`. This is what you mean, right?  
I’ve also changed my Dockerfile `FROM` to `FROM balenalib/up-board-ubuntu:latest`, because the command `startx` didn’t work reliable on the debian stretch image, which was really weird (I had to start and stop startx and then start it again, before I got something to show up on my screen. The first time it started, the screen stayed black and didn’t get any signal. The Ubuntu image just starts like it’s supposed to).

Back to the `devtmpfs`, I got this error after all steps have been completed:

```auto
ERROR: for f7af2ec82207_kiosk_1 Cannot start service kiosk: b'OCI runtime create failed: container_linux.go:296: starting container process caused "process_linux.go:398: container init caused \\"rootfs_linux.go:112: reopening /dev/null inside container caused \\\\\\"Failed to open /dev/null - open /dev/null: permission denied\\\\\\"\\"": unknown'

```

For your information, I’m using the `docker-compose` command to build and start containers on my Balena device, because it’s much much faster than `balena push <ipAddress>`.

I hope you can help me sort out this issue! 🙂

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [May 9, 2019, 10:13am UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/16 "2019-05-09T10:13:51Z")

</div>

hmm, im not sure `tmpfs: - /dev` will work for /dev. could you try running the function i linked to in your entrypoint or start up script?

Also interesting data point on `docker-compose` being faster than `balena push <IP>` perhaps @camerondiver can point to why that would be, it shouldn’t really make much of a difference. Also just be careful if you are developing that way, because it won’t always be 1-to-1 compatible with balena push

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [May 9, 2019, 10:43am UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/18 "2019-05-09T10:43:48Z")

</div>

Hi @shaunmulligan,

Do you mean just copy the `mount_dev` function and running that in my init.sh script? **I’ve done that**  
But this is the output:

```auto
kiosk_1 | mount: /tmp/tmpmount/console: special device /dev/console does not exist.
kiosk_1 | mount: /tmp/tmpmount/console: wrong fs type, bad option, bad superblock on /dev/console, missing codepage or helper program, or other error.
kiosk_1 | umount: /dev: target is busy.
kiosk_1 | mount: /sys/kernel/debug: nodev already mounted on /sys/kernel/debug.

```

And the USB hotplugging still doesn’t work. I’m thinking that the problem is with the X11/xorg session, because when I use `udev monitor` inside the container, every UDEV event is triggered and seems fine. The problem is, I’ve been using debian jessie before and everything worked fine (If I remember correctly), but I want to use the GPU of the UP Squared, and those drivers only work in debian stretch in combination with X11…

And regarding the `balena push <IP>`, it takes long before the push starts and after building, it takes some time before it really shows some logs. However, when I use `docker-compose`, it’s all instant. Also, stuff like `docker-compose down` or `docker-compose kill` aren’t supported (afaik) with `balena push <IP>`. And I know it isn’t 1-to-1 compatible, because I’m missing the balena ENV vars, which is too bad. But I don’t have any problems so far with just using `docker-compose` except for the balena ENV vars and the labels, which is becoming a problem because I need to contact the supervisor in the future. But I’m postponing that because the development time is much longer.

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [May 9, 2019, 11:44am UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/20 "2019-05-09T11:44:52Z")

</div>

Ah, thats weird, especially since the container is running privileged, i wouldn’t expect that to fail. If it would help, I could try reproduce here on my end (although I don’t have an UPboard on hand) if you can create a minimal project that has the issue.

On the `balena push <IP>` side of things, we have been improving it quite a bit and also added a new `--live` flag on the latest version, that will do live reloads, so that might make things faster, would be interested in hearing your take on it 🙂

---

<div class="post-metadata">

**Author:** ![CameronDiver](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@CameronDiver](https://forums.balena.io/u/CameronDiver)\
**Post date:** [May 9, 2019, 11:51am UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/22 "2019-05-09T11:51:30Z")

</div>

Hey, I wrote the `balena push` code so I’ll go through the reasons for this.

> And regarding the `balena push <IP>` , it takes long before the push starts and after building, it takes some time before it really shows some logs. However, when I use `docker-compose` , it’s all instant.

This is more noticeable with larger code repositories, but essentially it boils down to the way that balena push tars the input to stream to the docker daemon. Because of the handling of things like Dockerfile.templates etc, the pipeline has to read the input at least once, transforming it as it moves through. Add in that the node module we use for this has some inherent problems, the performance is definitely not as good as raw docker-compose. Two things I’ve been planning for this, one is to be able to skip the Dockerfile.template et al. resolution, which will improve things, and another is to start working on a tar module (or changing the current) to be more performance aware (for example we don’t need to add files one by one as we do now, if we have the size beforehand, which we do). Unfortunately these just haven’t taken as large a priority as other more fun features (`--live` like [@shaunmulligan](https://forums.balena.io/u/shaunmulligan) mentioned ![:slight_smile:](https://global.discourse-cdn.com/balena/original/2X/c/c9edc9164aa4cd799789705e885cf115f94c42f9.png) )

Another reason for this is that the CLI must talk to the supervisor to negotiate the push, and update the target state. This is necessary, but also can be optimized for fewer calls. Factor in that those calls are often handled by an rpi or similarly low-powered device, the responses can take some time.

Basically, we have this on our radar, it’s just not a huge priority right now.

> Also, stuff like `docker-compose down` or `docker-compose kill` aren’t supported (afaik) with `balena push <IP>` .

Again, these are going to be upcoming, we are making a lot of changes to the CLI right now, and this is definitely earmarked as one of them.

Also, this feedback is fantastic ![:slight_smile:](https://global.discourse-cdn.com/balena/original/2X/c/c9edc9164aa4cd799789705e885cf115f94c42f9.png)

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [May 9, 2019, 12:44pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/23 "2019-05-09T12:44:34Z")

</div>

Hi [@shaunmulligan](https://forums.balena.io/u/shaunmulligan),

I’m going to create a simple public project for you to test it. But it’s made for an UP Squared, so it installs the intel drivers for the GPU and starts the display with the intel drivers. This is quite important for me that these work, because that’s the reason why I’ve updated the kiosk (Also other reasons, like I’m a big fan of using the latest stable Debian release, and the kiosk I’ve used before was 1.1GB and this is around 500MB, which is a big improvement). But nonetheless, probably the issue still occurs when you try it on another device without GPU drivers, because USB-devices don’t use the USB ![:slight_smile:](https://global.discourse-cdn.com/balena/original/2X/c/c9edc9164aa4cd799789705e885cf115f94c42f9.png)

So a big thank you for helping me with this issue! I’m going to create the project right now!

And hi [@CameronDiver](https://forums.balena.io/u/camerondiver),

Thanks for explaining the `balena push` for me. I’m going to try the `--live` option. I prefer to use the `balena push` ofcourse, but it just takes too long to build and start the container when comparing it to the `docker-compose` as I explained. But using the Balena ENV vars and the ability to use the Balena labels for the supervisor is becoming more and more important. As explained in other topics, I want to use the supervisor for the current state, release ID, reboot’s etc.

I was planning on creating a topic to address this “issue”, but it was relevant for this topic too. I understand this isn’t a high priority issue, but the faster the `balena push` command is, the more it’s going to be used and I can imagine it’s also a hugh relieve when developing the command when it’s fast ![:slight_smile:](https://global.discourse-cdn.com/balena/original/2X/c/c9edc9164aa4cd799789705e885cf115f94c42f9.png)

For the record, I’ve been developing on an UP Squared board and a RPI3 with Balena, and there is a significant improvement in speed on the UP Squared board, like expected. But it’s still quite slow. On the RPI 3 I understand it’s slow, `docker-compose` isn’t much faster.

If creating a new topic is best for this “issue”, I’ll do that! Because we must not lose focus on the issue of the X11 hotplug USB ofcourse! And I’m really eager to learn from the Balena team and other users on how they develop for Balena. Because, like I said, on the RPI3 it’s really slow to keep pushing all the code (which is the issue of the RPI3, not Balena ofcourse).

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [May 9, 2019, 12:58pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/24 "2019-05-09T12:58:49Z")

</div>

Great, let us know when you have the example put together, we share your love of staying on latest stable debian and it would be good to get to the bottom of the issue 🙂

For live push and general development, it would be awesome to start a new topic and get feedback and info on how people are developing, so please definitely do that 🙂

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [May 9, 2019, 1:12pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/26 "2019-05-09T13:12:35Z")

</div>

I’ve created a repository to replicate the issue @ [https://github.com/vedicium/balena-X11-hotplug](https://github.com/vedicium/balena-X11-hotplug)  
I hope you can find something!

**Update**  
When using the intel drivers and the Ubuntu image, it isn’t reliable either. Same as the debian image, the X11 session stays blank for the first time…

And I’ll create a topic to share some idea’s about how people are developing on Balena!

Tadaaa [Developing on a Balena device](https://forums.balena.io/t/developing-on-a-balena-device/8134)

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [May 9, 2019, 2:29pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/27 "2019-05-09T14:29:40Z")

</div>

Thanks @bversluijs , I’ll see if we can figure it out using your repro!

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [July 2, 2019, 4:28pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/29 "2019-07-02T16:28:50Z")

</div>

Hi @shaunmulligan,

Did you/someone have had some time to look at this issue?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [August 3, 2019, 1:20pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/30 "2019-08-03T13:20:14Z")

</div>

After a lot of tweaking, I’ve figured it out.  
I’ve added the following code before starting the X11 process:

```
if which udevadm > /dev/null; then
  set +e # Disable exit on error
  udevadm control --reload-rules
  service udev restart
  udevadm trigger
  set -e # Re-enable exit on error
fi

```

I restarted udev and did `udevadm trigger`, and hotplugging USB’s started working. And I’ve combined it with the intel graphics drivers, so the UP Board uses the GPU instead of CPU.

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [August 6, 2019, 6:19pm UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/31 "2019-08-06T18:19:52Z")

</div>

Awesome, @bversluijs thats great new. Sorry I haven’t managed to get you anymore clarity on this. Glad you managed to get there in the end. I’ll ask our base images engineer to have a look at this and see if there is something we can add to our base images to make this work more out of the box.

---

<div class="post-metadata">

**Author:** ![bversluijs](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/bversluijs/32/3954_2.png) [@bversluijs](https://forums.balena.io/u/bversluijs)\
**Post date:** [August 7, 2019, 9:06am UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/33 "2019-08-07T09:06:29Z")

</div>

No problem @shaunmulligan. Let me know if you have any updates from the engineers!

---

<div class="post-metadata">

**Author:** ![skuenstler](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@skuenstler](https://forums.balena.io/u/skuenstler)\
**Post date:** [January 13, 2023, 7:12am UTC](https://forums.balena.io/t/x11-hotplug-usb/7250/34 "2023-01-13T07:12:06Z")

</div>

@[bversluijs](https://forums.balena.io/u/bversluijs): In my case, adding your extra code at the bottom of the start.sh script inside xserver and creating my own custom balena-block from it, did not work.

However, adding your extra code into the correct docker Container finally worked as well.

Adding your code to the start.sh and making sure to execute it from the correct Container finally worked

Thank you very much.

```auto
COPY start.sh /
CMD ["bash", "/start.sh"]

```

[Next page](https://forums.balena.io/t/x11-hotplug-usb/7250.md?page=2)
