Skip to content
Snippets Groups Projects
Verified Commit 48d308b8 authored by Clayton Craft's avatar Clayton Craft :speech_balloon:
Browse files

main/bootmac: support systemd

This adds a -systemd subpkg to install a unit file (for bluetooth right
now). This also includes some patches that were sent upstream to bootmac
project, which can be dropped if/when it's accepted in the bootmac
project and a new release is made.

The patches here have been sent upstream:
postmarketOS/bootmac!10

openrc installs will still use the udev rule to run bootmac for
bluetooth, since that seems to work reliably (and there's no concept of
service targets or anything that would make this easy to implement on
openrc, but I digress).

Both systemd and openrc installs will continue to use udev rules for
wifi mac setting, I don't have a way to test that this works, sd unit
file can be created later if it improves the situation there.
parent 8dfe9245
No related branches found
No related tags found
No related merge requests found
Pipeline #213833 passed
From 3bfb183fc5062a5c202c0a356d615605d032078a Mon Sep 17 00:00:00 2001
From: Adam Honse <calcprogrammer1@gmail.com>
Date: Thu, 18 Jul 2024 11:22:44 -0500
Subject: [PATCH 1/4] Add 1 second delay before setting Bluetooth MAC, fixes #4
---
bootmac | 3 +++
1 file changed, 3 insertions(+)
diff --git a/bootmac b/bootmac
index e65354f..c21290d 100755
--- a/bootmac
+++ b/bootmac
@@ -141,6 +141,9 @@ mac_generate() {
}
mac_bluetooth() {
+ # Some Bluetooth adapters take time before they can be configured
+ sleep 1
+
log "setting Bluetooth MAC to $BT_MAC"
# Check if the Bluetooth interface is up
--
2.48.1
From 476c11251853221d9edc378f80f18a780b17ebf2 Mon Sep 17 00:00:00 2001
From: Clayton Craft <clayton@craftyguy.net>
Date: Tue, 14 Jan 2025 14:09:07 -0800
Subject: [PATCH 2/4] udev: split wifi and bt rules into two separate files
The udev rule for bluetooth doesn't work on systemd[1], by splitting the
rule into two separate rules, distros can leave this bluetooth rule out
when packaging for systemd.
Distros packaging bootmac will have to make note of the new rule
files/names.
1. https://github.com/systemd/systemd/issues/35003
---
bootmac.rules => bootmac-bluetooth.rules | 5 ++---
bootmac-wifi.rules | 7 +++++++
2 files changed, 9 insertions(+), 3 deletions(-)
rename bootmac.rules => bootmac-bluetooth.rules (71%)
create mode 100644 bootmac-wifi.rules
diff --git a/bootmac.rules b/bootmac-bluetooth.rules
similarity index 71%
rename from bootmac.rules
rename to bootmac-bluetooth.rules
index 20829b6..f106d87 100644
--- a/bootmac.rules
+++ b/bootmac-bluetooth.rules
@@ -1,8 +1,7 @@
-#
+#
# Configure the MAC addresses of the WLAN and Bluetooth interfaces when they appear.
# Copyright (c) Dylan Van Assche (2022)
# SPDX-License-Identifier: GPL-3.0-or-later
#
-ACTION=="add", SUBSYSTEM=="net", KERNEL=="wlan0", RUN+="/usr/bin/bootmac"
-ACTION=="add", SUBSYSTEM=="bluetooth", KERNEL=="hci0", RUN+="/usr/bin/bootmac"
+ACTION=="add", SUBSYSTEM=="bluetooth", KERNEL=="hci0", RUN+="/usr/bin/bootmac --bluetooth"
diff --git a/bootmac-wifi.rules b/bootmac-wifi.rules
new file mode 100644
index 0000000..133b8ee
--- /dev/null
+++ b/bootmac-wifi.rules
@@ -0,0 +1,7 @@
+#
+# Configure the MAC addresses of the WLAN and Bluetooth interfaces when they appear.
+# Copyright (c) Dylan Van Assche (2022)
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+
+ACTION=="add", SUBSYSTEM=="net", KERNEL=="wlan0", RUN+="/usr/bin/bootmac --wlan"
--
2.48.1
From 3ff0f370cf12061344d2dc7fc84f1054b453f959 Mon Sep 17 00:00:00 2001
From: Clayton Craft <clayton@craftyguy.net>
Date: Tue, 14 Jan 2025 14:11:10 -0800
Subject: [PATCH 3/4] Add systemd unit file
Being part of the bluetooth.target means that this service (if
enabled) is started automatically every time the bluetooth service is
(re)started.
---
bootmac-bluetooth.service | 11 +++++++++++
1 file changed, 11 insertions(+)
create mode 100644 bootmac-bluetooth.service
diff --git a/bootmac-bluetooth.service b/bootmac-bluetooth.service
new file mode 100644
index 0000000..ed6ac96
--- /dev/null
+++ b/bootmac-bluetooth.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Set Mac Address
+PartOf=bluetooth.target
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/usr/bin/bootmac --bluetooth
+
+[Install]
+WantedBy=bluetooth.target
--
2.48.1
From 93e075377460464c22e7471bd32c37a359a3ed87 Mon Sep 17 00:00:00 2001
From: Clayton Craft <clayton@craftyguy.net>
Date: Fri, 17 Jan 2025 10:01:56 -0800
Subject: [PATCH 4/4] Add retry mechnism for setting BT MAC addr
This replaces 3bfb183fc506 with a retry mechanism, where the BT mac is
(re)tried every 1 second for up to 5 seconds (configurable).
If this doesn't play nicely with udev rules when using RUN, BT_TIMEOUT
can be set in the env.
---
bootmac | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/bootmac b/bootmac
index c21290d..44a54c4 100755
--- a/bootmac
+++ b/bootmac
@@ -16,6 +16,7 @@ WLAN_INTERFACE="wlan0"
BT_INTERFACE="hci0"
# Default MAC prefix
MAC_PREFIX="0200"
+BT_TIMEOUT=${BT_TIMEOUT:-5} # seconds
log() {
echo "$@" | logger -t "bootmac"
@@ -141,9 +142,6 @@ mac_generate() {
}
mac_bluetooth() {
- # Some Bluetooth adapters take time before they can be configured
- sleep 1
-
log "setting Bluetooth MAC to $BT_MAC"
# Check if the Bluetooth interface is up
@@ -162,7 +160,24 @@ mac_bluetooth() {
# The 'yes | btmgmt xxx' workaround is needed for proper working of btmgmt
# as noted in https://gitlab.com/postmarketOS/bootmac/-/issues/3
yes | btmgmt -i "$BT_INTERFACE" power off
- yes | btmgmt -i "$BT_INTERFACE" public-addr "$BT_MAC"
+
+ # Setting the mac addr might fail if the device/driver hasn't finished
+ # initializing, so retry
+ timeout=0
+ while [ $timeout -lt 5 ]; do
+ if yes | btmgmt -i "$BT_INTERFACE" public-addr "$BT_MAC"; then
+ break
+ fi
+ echo "Unable to set Bluetooth MAC, retrying after 1 second..."
+ sleep 1
+ timeout=$((timeout + 1))
+ done
+
+ if [ $timeout -ge 5 ]; then
+ echo "Failed to set Bluetooth MAC address, command timed out after $BT_TIMEOUT seconds"
+ return 1
+ fi
+
if [ -n "$BT_RFKILL_UNBLOCKED" ]; then
log "restoring Bluetooth rfkill state to unblocked"
rfkill unblock bluetooth
--
2.48.1
# Maintainer: Dylan Van Assche <me@dylanvanassche.be>
pkgname=bootmac
pkgver=0.5.0
pkgrel=2
pkgrel=3
pkgdesc="Configure MAC addresses at boot"
url="https://gitlab.postmarketos.org/postmarketOS/bootmac"
arch="all"
license="GPLv3-or-later"
source="https://gitlab.postmarketos.org/postmarketOS/bootmac/-/archive/v$pkgver/bootmac-v$pkgver.tar.gz"
source="
https://gitlab.postmarketos.org/postmarketOS/bootmac/-/archive/v$pkgver/bootmac-v$pkgver.tar.gz
0001-Add-1-second-delay-before-setting-Bluetooth-MAC-fixe.patch
0002-udev-split-wifi-and-bt-rules-into-two-separate-files.patch
0003-Add-systemd-unit-file.patch
0004-Add-retry-mechnism-for-setting-BT-MAC-addr.patch
"
depends="bluez-btmgmt bluez-deprecated"
options="!check"
subpackages="$pkgname-openrc $pkgname-systemd"
builddir="$srcdir/$pkgname-v$pkgver"
package() {
install -Dm644 "$builddir"/bootmac.rules \
"$pkgdir"/usr/lib/udev/rules.d/90-bootmac.rules
install -Dm644 "$builddir"/bootmac-wifi.rules \
"$pkgdir"/usr/lib/udev/rules.d/90-bootmac-wifi.rules
install -Dm644 "$builddir"/bootmac-wifi.rules \
"$pkgdir"/usr/lib/udev/rules.d/90-bootmac-bluetooth.rules
install -Dm755 "$builddir"/bootmac \
"$pkgdir"/usr/bin/bootmac
install -Dm644 "$builddir"/bootmac-bluetooth.service \
-t "$pkgdir"/usr/lib/systemd/system/
}
openrc() {
default_openrc
amove usr/lib/udev/rules.d/90-bootmac-bluetooth.rules
}
systemd() {
install="$subpkgname.post-install $subpkgname.pre-deinstall"
default_systemd
}
sha512sums="
32409d0ad978aa92cde061b69071e01bb6de9f7e8d33e4d7f67f2bddc192a8ea710b8c3e01b9618768c3a8b1d297a6fd717fc4dad3c47e7bec508f2d5f198dda bootmac-v0.5.0.tar.gz
7c86c1fe4310601010028b45ee92224f69493258cff78a621705236a681319d16ce65bae6317b7105ceafa42387a4afcdfd90f0aacc772df67ed10e9e23a3498 0001-Add-1-second-delay-before-setting-Bluetooth-MAC-fixe.patch
5ed5f36d78024df30d85b01e41007771d291ac1b7bd787774a6e4b319c970c4095344d2dd6117b6ab87da785070a19c215cc00d0272dc5c5e766a85394ed0d9a 0002-udev-split-wifi-and-bt-rules-into-two-separate-files.patch
639d5e0d74faa226b7d6339209090cb5ee6aa389d460b1b2383ee23c930423a5f9ed4b0f07126924f03fb93165cffb037fe2a83e19cdbcf042bb2e20799dc855 0003-Add-systemd-unit-file.patch
8783837b91504e41be4f899ccdcee091142551f5b20510c9b05c19e8a3909ffe403032a4ad3eb497a1719f45441653f50d47341cf648c0d52b779a50e270d9ab 0004-Add-retry-mechnism-for-setting-BT-MAC-addr.patch
"
#!/bin/sh
. /usr/lib/systemd/systemd-apk-macros.sh
systemd_service_post_install system bootmac-bluetooth.service
#!/bin/sh
. /usr/lib/systemd/systemd-apk-macros.sh
systemd_service_pre_deinstall system bootmac-bluetooth.service
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment