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

WSL2 Kernel Add Bluetooth Support to microsoft-standard-WSL2 #12234

Open
yaplej opened this issue Nov 4, 2024 · 9 comments
Open

WSL2 Kernel Add Bluetooth Support to microsoft-standard-WSL2 #12234

yaplej opened this issue Nov 4, 2024 · 9 comments
Labels
emailed-logs Logs have been emailed feature

Comments

@yaplej
Copy link

yaplej commented Nov 4, 2024

I am trying to use the development environment for Home Assistant in VS Code using Docker Desktop. The Integration I am working on requires bluetooth connection to a piece of hardware. So the easiest way to get that working would be to pass a USB Bluetooth adapter to WSL.

That much works but once the USB bluetooth adapter is recognized by the container it wont load the bluetooth drivers/module because the WSL kernel does not have those enabled.

Once the USB bluetooth device is passed to the WSL container it should be able to use it.

Tried to build a custom kernel for using with WSL & Docker but didn't get it working.

(docker/for-win#14284)

@yaplej yaplej added the feature label Nov 4, 2024
Copy link

github-actions bot commented Nov 4, 2024

Logs are required for review from WSL team

If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'.
Otherwise please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.

How to collect WSL logs

Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:

Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1

The script will output the path of the log file once done.

If this is a networking issue, please use collect-networking-logs.ps1, following the instructions here

Once completed please upload the output files to this Github issue.

Click here for more info on logging
If you choose to email these logs instead of attaching to the bug, please send them to [email protected] with the number of the github issue in the subject, and in the message a link to your comment in the github issue and reply with '/emailed-logs'.

View similar issues

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@yaplej
Copy link
Author

yaplej commented Nov 4, 2024

/feature

Copy link

github-actions bot commented Nov 4, 2024

Diagnostic information
Found '/feature', adding tag 'feature'

@yaplej yaplej changed the title WSL2 Kernel Add Bluetooth Support to microsoft-standard-WSL2 kernel WSL2 Kernel Add Bluetooth Support to microsoft-standard-WSL2 Nov 4, 2024
Copy link

github-actions bot commented Nov 4, 2024

Logs are required for review from WSL team

If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'.
Otherwise please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.

How to collect WSL logs

Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:

Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1

The script will output the path of the log file once done.

If this is a networking issue, please use collect-networking-logs.ps1, following the instructions here

Once completed please upload the output files to this Github issue.

Click here for more info on logging
If you choose to email these logs instead of attaching to the bug, please send them to [email protected] with the number of the github issue in the subject, and in the message a link to your comment in the github issue and reply with '/emailed-logs'.

@yaplej
Copy link
Author

yaplej commented Nov 5, 2024

/emailed-logs

Copy link

github-actions bot commented Nov 5, 2024

Diagnostic information
Found '/emailed-logs', adding tag 'emailed-logs'

@github-actions github-actions bot added the emailed-logs Logs have been emailed label Nov 5, 2024
@borjamunozf
Copy link

borjamunozf commented Nov 8, 2024

Did you build a custom kernel with this config?

CONFIG_BT=y

which Bluetooh adapter/brand is it?

Build custom kernel:

sudo apt-get update
sudo apt-get install -y build-essential flex bison dwarves libssl-dev libelf-dev bc

git clone --branch linux-msft-wsl-5.15.167.4 https://github.com/microsoft/WSL2-Linux-Kernel.git --progress --depth 1

cd WSL2-Linux-Kernel

vim Microsoft/config-wsl
  • Find CONFIG_BT, should say CONFIG_BT is undefined.
    CONFIG_BT=y

Finally:

echo n | make KCONFIG_CONFIG=Microsoft/config-wsl -j$(nproc) bzImage
  • You'll get the kernel in arch/x86/kernel/bzImage.
  • Copy this file to your /mnt/C/Users/yourUser/
  • Modify the kernel line in .wslconfig to refer to the absolute path of this kernel.
  • Do a wsl --shutdown

Check again.

@adamscybot
Copy link

adamscybot commented Nov 16, 2024

You could try this ad-hoc community project. Instructions in readme. This is a fork of the official kernal repo with necessary edits to enable building a BT enabled kernal. It is itself meant to be forked by yourself as per the instructions. This enables you to kick off a GitHub action for your target kernel that will build that kernel with BT support and spit it out in the GithHub artifacts for the run.

I don't necessarily condone downloading precompiled kernels from random sources....but if you happen to be running WSL 5.15.153.1 (I believe the latest stable. You can check via wsl --version on windows terminal) you can skip the fork step and download the already-compiled BT enabled kernel for it here https://github.com/dathpo/wsl2-linux-kernel/actions/runs/11166047151/artifacts/2012024924 and set that in the WSL config as per the readme.

I yolo'ed this and have working BT in WSL, even inside a devcontainer (docker wrapper) running on top of WSL. And I am also doing home assistant dev!

I did have some issues getting the container part to work properly. The kernel alone is not enough. You need this in your .devcontainer/devcontainer.json:

  "mounts": [
    "source=\\\\wsl.localhost\\Ubuntu\\run\\dbus,target=/var/run/dbus,type=bind"
  ],

Your container needs to be bound to dbus properly for BT to work inside of it, as well as having the BT-enabled kernel. Here, I have a reference to Ubuntu, which is what I'm using, so change as necessary. Otherwise, even if WSL can see Bluetooth, the container won't. You are battling two issues: WSL BT support and docker configuration -- and I can at least say that I personally got into a state of confusion about which one of these I was dealing with at a given time.

It was very finicky trying to find the working mount configuration, and the ones I found online did not work. It only worked for me with the fully qualified \\wsl.localhost... path like the above. I suspect it possibly uses the default docker distro incorrectly if you don't fully specify this. And also, of course, you will need to make sure dbus is installed within that WSL distro and that you used usbipd to attach the Bluetooth device to WSL (it seems you did this already, but adding for others).

After all of this, I am able to consistently get a dev instance of home assistant running via devcontainer-on-wsl that has full access to the BT hardware, and demonstrably works in discovering and connecting to them via home assistant integrations.

All that said, whilst there is support for building a special kernel, do we have any indication as to why it's not enabled by default and if that default will change in the future? There's a lot of headbanging around this topic, which I also went through, so it seems like there is a problem & community need for a more straightforward route.

I think a lot of that demand is coming from the fact that the reality of the BT space is many projects and libs rely on dbus (e.g. Bluez), which is Linux only. So anyone touching dev around BT on Windows may opt for WSL, and that's when the headaches start. This is a small group of people but one with a clear and concentrated need. It would be great if there was a way to make the path easier!

This thread comes up for people searching around similar issues so please feel free to ask me any questions about how I got it working.

4-FLOSS-Free-Libre-Open-Source-Software added a commit to 4-FLOSS-Free-Libre-Open-Source-Software/WSL2-Linux-Kernel-xanmod that referenced this issue Nov 20, 2024
The default WSL2 Linux kernel comes without Bluetooth support. Provide a kernel with Bluetooth support.

Reference implementations:

- https://github.com/dathpo/wsl2-linux-kernel/tree/develop-5.15.y?tab=readme-ov-file#readme-fork


Upstream Issue:

- microsoft/WSL/issues/12234

Signed-off-by: Bernd Eichelberger <46166740+4-FLOSS-Free-Libre-Open-Source-Software@users.noreply.github.com>
Locietta pushed a commit to 4-FLOSS-Free-Libre-Open-Source-Software/WSL2-Linux-Kernel-xanmod that referenced this issue Nov 21, 2024
The default WSL2 Linux kernel comes without Bluetooth support. Provide a kernel with Bluetooth support.

Reference implementations:

- https://github.com/dathpo/wsl2-linux-kernel/tree/develop-5.15.y?tab=readme-ov-file#readme-fork


Upstream Issue:

- microsoft/WSL/issues/12234

Signed-off-by: Bernd Eichelberger <46166740+4-FLOSS-Free-Libre-Open-Source-Software@users.noreply.github.com>
Locietta pushed a commit to Locietta/xanmod-kernel-WSL2 that referenced this issue Nov 21, 2024
Fixes #82

The default WSL2 Linux kernel comes without Bluetooth support. Provide a kernel with Bluetooth support.

Reference implementations:

- https://github.com/dathpo/wsl2-linux-kernel/tree/develop-5.15.y?tab=readme-ov-file#readme-fork

Upstream Issue:

- microsoft/WSL/issues/12234

Signed-off-by: Bernd Eichelberger <46166740+4-FLOSS-Free-Libre-Open-Source-Software@users.noreply.github.com>
@realJoshByrnes
Copy link

realJoshByrnes commented Jan 5, 2025

sudo apt-get install -y build-essential flex bison dwarves libssl-dev libelf-dev bc

You'll also need the git (to get) and python3 (to compile) packages.

  • You'll get the kernel in arch/x86/kernel/bzImage.

This is (currently) arch/x86/boot/bzImage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emailed-logs Logs have been emailed feature
Projects
None yet
Development

No branches or pull requests

4 participants