From fdb3736502137e5088d9fca12e4f88839ea15ce5 Mon Sep 17 00:00:00 2001 From: mirkoCrobu Date: Fri, 7 Nov 2025 14:49:13 +0100 Subject: [PATCH 1/6] set serial number for network discovery at post install time --- debian/arduino-app-cli/DEBIAN/postinst | 1 + .../system/arduino-avahi-serial.service | 17 +++++++ .../usr/sbin/arduino-avahi-serial.sh | 46 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service create mode 100755 debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh diff --git a/debian/arduino-app-cli/DEBIAN/postinst b/debian/arduino-app-cli/DEBIAN/postinst index 8dbe7413..da060a89 100755 --- a/debian/arduino-app-cli/DEBIAN/postinst +++ b/debian/arduino-app-cli/DEBIAN/postinst @@ -4,3 +4,4 @@ chown -R arduino:arduino /home/arduino/.local/share/arduino-app-cli systemctl enable arduino-app-cli systemctl enable arduino-burn-bootloader +systemctl enable arduino-avahi-serial.service diff --git a/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service new file mode 100644 index 00000000..66525b77 --- /dev/null +++ b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service @@ -0,0 +1,17 @@ +[Unit] +Description=Configure Avahi with board serial number +After=avahi-daemon.service +ConditionPathExists=!/var/lib/arduino/avahi_serial_configured.flag + +[Service] +Type=oneshot +RemainAfterExit=true +ExecStart=/usr/sbin/arduino-avahi-serial.sh +ExecStartPost=/bin/mkdir -p /var/lib/arduino +ExecStartPost=/bin/touch /var/lib/arduino/avahi_serial_configured.flag + +StandardOutput=journal +StandardError=journal + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh b/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh new file mode 100755 index 00000000..bcb0616b --- /dev/null +++ b/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# +# Configure Avahi with the serial number. +# This operation is non-blocking: if it fails, +# the script will exit with success in order to +#not to interrupt the post-install process. +# + +TARGET_FILE="/etc/avahi/services/arduino.service" +MARKER_LINE="" +SERIAL_NUMBER_PATH="/sys/devices/soc0/serial_number" + +echo "Configuring Avahi with serial number for network discovery..." + +if [ ! -f "$SERIAL_NUMBER_PATH" ]; then + echo "Warning: Serial number path not found at $SERIAL_NUMBER_PATH. Skipping." >&2 + exit 0 +fi + + +if [ ! -w "$TARGET_FILE" ]; then + echo "Warning: Target file $TARGET_FILE not found or not writable. Skipping." >&2 + exit 0 +fi + +SERIAL_NUMBER=$(cat "$SERIAL_NUMBER_PATH") + +if [ -z "$SERIAL_NUMBER" ]; then + echo "Warning: Serial number file is empty. Skipping." >&2 + exit 0 +fi + +if grep -q "serial_number=${SERIAL_NUMBER}" "$TARGET_FILE"; then + echo "Serial number ($SERIAL_NUMBER) already configured. Skipping." + exit 0 +fi + +SERIAL_NUMBER_ESCAPED=$(echo "$SERIAL_NUMBER" | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/\&/\\\&/g') +NEW_LINE=" serial_number=${SERIAL_NUMBER_ESCAPED}" + +echo "Adding serial number to $TARGET_FILE..." + +sed -i "\#${MARKER_LINE}#i ${NEW_LINE}" "$TARGET_FILE" + +echo "Avahi configuration attempt finished." +exit 0 \ No newline at end of file From a9ddaa2d8d078eac58ffab799635e511e67f749c Mon Sep 17 00:00:00 2001 From: mirkoCrobu Date: Fri, 7 Nov 2025 15:18:06 +0100 Subject: [PATCH 2/6] adds space in a comment --- debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh b/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh index bcb0616b..89f89fb1 100755 --- a/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh +++ b/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh @@ -3,7 +3,7 @@ # Configure Avahi with the serial number. # This operation is non-blocking: if it fails, # the script will exit with success in order to -#not to interrupt the post-install process. +# not to interrupt the post-install process. # TARGET_FILE="/etc/avahi/services/arduino.service" From c52f211a152e7816b9ec95cafd112c00d6e3e915 Mon Sep 17 00:00:00 2001 From: mirkoCrobu Date: Fri, 7 Nov 2025 16:11:14 +0100 Subject: [PATCH 3/6] set script running before Avahi service --- .../etc/systemd/system/arduino-avahi-serial.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service index 66525b77..17a4f327 100644 --- a/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service +++ b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service @@ -1,6 +1,6 @@ [Unit] Description=Configure Avahi with board serial number -After=avahi-daemon.service +Before=avahi-daemon.service ConditionPathExists=!/var/lib/arduino/avahi_serial_configured.flag [Service] From 3076350e86ab0f47c0e027b235b7bd4d2a558591 Mon Sep 17 00:00:00 2001 From: mirkoCrobu Date: Mon, 10 Nov 2025 09:36:14 +0100 Subject: [PATCH 4/6] code review fix --- .../usr/sbin/arduino-avahi-serial.sh | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh b/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh index 89f89fb1..0b088c68 100755 --- a/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh +++ b/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh @@ -1,10 +1,6 @@ #!/bin/sh -# # Configure Avahi with the serial number. -# This operation is non-blocking: if it fails, -# the script will exit with success in order to -# not to interrupt the post-install process. -# + TARGET_FILE="/etc/avahi/services/arduino.service" MARKER_LINE="" @@ -13,21 +9,21 @@ SERIAL_NUMBER_PATH="/sys/devices/soc0/serial_number" echo "Configuring Avahi with serial number for network discovery..." if [ ! -f "$SERIAL_NUMBER_PATH" ]; then - echo "Warning: Serial number path not found at $SERIAL_NUMBER_PATH. Skipping." >&2 - exit 0 + echo "Error: Serial number path not found at $SERIAL_NUMBER_PATH." >&2 + exit 1 fi if [ ! -w "$TARGET_FILE" ]; then - echo "Warning: Target file $TARGET_FILE not found or not writable. Skipping." >&2 - exit 0 + echo "Error: Target file $TARGET_FILE not found or not writable." >&2 + exit 1 fi SERIAL_NUMBER=$(cat "$SERIAL_NUMBER_PATH") if [ -z "$SERIAL_NUMBER" ]; then - echo "Warning: Serial number file is empty. Skipping." >&2 - exit 0 + echo "Error: Serial number file is empty." >&2 + exit 1 fi if grep -q "serial_number=${SERIAL_NUMBER}" "$TARGET_FILE"; then @@ -35,8 +31,7 @@ if grep -q "serial_number=${SERIAL_NUMBER}" "$TARGET_FILE"; then exit 0 fi -SERIAL_NUMBER_ESCAPED=$(echo "$SERIAL_NUMBER" | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/\&/\\\&/g') -NEW_LINE=" serial_number=${SERIAL_NUMBER_ESCAPED}" +NEW_LINE=" serial_number=${SERIAL_NUMBER}" echo "Adding serial number to $TARGET_FILE..." From c1eb54f7896d9b3020b2430b1fc823b2de34b06d Mon Sep 17 00:00:00 2001 From: mirkoCrobu Date: Mon, 10 Nov 2025 15:23:28 +0100 Subject: [PATCH 5/6] code review fix --- debian/arduino-app-cli/DEBIAN/prerm | 1 + .../etc/systemd/system/arduino-avahi-serial.service | 8 ++++---- .../usr/{sbin => local/bin}/arduino-avahi-serial.sh | 7 ++----- 3 files changed, 7 insertions(+), 9 deletions(-) rename debian/arduino-app-cli/usr/{sbin => local/bin}/arduino-avahi-serial.sh (81%) diff --git a/debian/arduino-app-cli/DEBIAN/prerm b/debian/arduino-app-cli/DEBIAN/prerm index 44e8a76c..6d35c65c 100755 --- a/debian/arduino-app-cli/DEBIAN/prerm +++ b/debian/arduino-app-cli/DEBIAN/prerm @@ -2,3 +2,4 @@ systemctl disable arduino-app-cli systemctl disable arduino-burn-bootloader +systemctl disable arduino-avahi-serial.service diff --git a/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service index 17a4f327..61c1287b 100644 --- a/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service +++ b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service @@ -1,14 +1,14 @@ [Unit] Description=Configure Avahi with board serial number Before=avahi-daemon.service -ConditionPathExists=!/var/lib/arduino/avahi_serial_configured.flag +ConditionPathExists=!/var/lib/arduino-app-cli/avahi_serial_configured.flag [Service] Type=oneshot RemainAfterExit=true -ExecStart=/usr/sbin/arduino-avahi-serial.sh -ExecStartPost=/bin/mkdir -p /var/lib/arduino -ExecStartPost=/bin/touch /var/lib/arduino/avahi_serial_configured.flag +ExecStart=/usr/local/bin/arduino-avahi-serial.sh +ExecStartPost=/bin/mkdir -p /var/lib/arduino-app-cli +ExecStartPost=/bin/touch /var/lib/arduino-app-cli/avahi_serial_configured.flag StandardOutput=journal StandardError=journal diff --git a/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh b/debian/arduino-app-cli/usr/local/bin/arduino-avahi-serial.sh similarity index 81% rename from debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh rename to debian/arduino-app-cli/usr/local/bin/arduino-avahi-serial.sh index 0b088c68..07ffdb62 100755 --- a/debian/arduino-app-cli/usr/sbin/arduino-avahi-serial.sh +++ b/debian/arduino-app-cli/usr/local/bin/arduino-avahi-serial.sh @@ -26,16 +26,13 @@ if [ -z "$SERIAL_NUMBER" ]; then exit 1 fi -if grep -q "serial_number=${SERIAL_NUMBER}" "$TARGET_FILE"; then +if grep -q "serial_number=" "$TARGET_FILE"; then echo "Serial number ($SERIAL_NUMBER) already configured. Skipping." exit 0 fi -NEW_LINE=" serial_number=${SERIAL_NUMBER}" - echo "Adding serial number to $TARGET_FILE..." - -sed -i "\#${MARKER_LINE}#i ${NEW_LINE}" "$TARGET_FILE" +sed -i "/<\/service>/i serial_number=${SERIAL_NUMBER}<\/txt-record>" "$TARGET_FILE" echo "Avahi configuration attempt finished." exit 0 \ No newline at end of file From 088a594b589b349acb1477e134d064e222803b5f Mon Sep 17 00:00:00 2001 From: mirkoCrobu Date: Tue, 11 Nov 2025 10:53:38 +0100 Subject: [PATCH 6/6] code review fix --- .../etc/systemd/system/arduino-avahi-serial.service | 2 +- .../bin => var/lib/arduino-app-cli}/arduino-avahi-serial.sh | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) rename debian/arduino-app-cli/{usr/local/bin => var/lib/arduino-app-cli}/arduino-avahi-serial.sh (90%) diff --git a/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service index 61c1287b..f25ef825 100644 --- a/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service +++ b/debian/arduino-app-cli/etc/systemd/system/arduino-avahi-serial.service @@ -6,7 +6,7 @@ ConditionPathExists=!/var/lib/arduino-app-cli/avahi_serial_configured.flag [Service] Type=oneshot RemainAfterExit=true -ExecStart=/usr/local/bin/arduino-avahi-serial.sh +ExecStart=/var/lib/arduino-app-cli/arduino-avahi-serial.sh ExecStartPost=/bin/mkdir -p /var/lib/arduino-app-cli ExecStartPost=/bin/touch /var/lib/arduino-app-cli/avahi_serial_configured.flag diff --git a/debian/arduino-app-cli/usr/local/bin/arduino-avahi-serial.sh b/debian/arduino-app-cli/var/lib/arduino-app-cli/arduino-avahi-serial.sh similarity index 90% rename from debian/arduino-app-cli/usr/local/bin/arduino-avahi-serial.sh rename to debian/arduino-app-cli/var/lib/arduino-app-cli/arduino-avahi-serial.sh index 07ffdb62..77b5a81b 100755 --- a/debian/arduino-app-cli/usr/local/bin/arduino-avahi-serial.sh +++ b/debian/arduino-app-cli/var/lib/arduino-app-cli/arduino-avahi-serial.sh @@ -3,7 +3,6 @@ TARGET_FILE="/etc/avahi/services/arduino.service" -MARKER_LINE="" SERIAL_NUMBER_PATH="/sys/devices/soc0/serial_number" echo "Configuring Avahi with serial number for network discovery..." @@ -27,7 +26,7 @@ if [ -z "$SERIAL_NUMBER" ]; then fi if grep -q "serial_number=" "$TARGET_FILE"; then - echo "Serial number ($SERIAL_NUMBER) already configured. Skipping." + echo "Serial number ($SERIAL_NUMBER) already configured." exit 0 fi