-
Notifications
You must be signed in to change notification settings - Fork 1
add edgeos-hostname service using device UUID for mDNS #39
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
Merged
mihai-chiorean
merged 1 commit into
edgeengineer:main
from
StefanaHanc:feature/edgeos-hostname
Oct 3, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| enable edgeos-hostname.service |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 82 additions & 64 deletions
146
meta-edgeos/recipes-connectivity/avahi/files/generate-hostname.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,118 +1,136 @@ | ||
| #!/bin/bash | ||
| # | ||
| # EdgeOS Hostname Generation Script | ||
| # Generates a unique hostname based on device serial number | ||
| # Generates a unique hostname based on device UUID (fallback to serial/MAC) | ||
| # | ||
|
|
||
| set -e | ||
| set -Eeuo pipefail | ||
|
|
||
| UUID_FILE="/etc/edgeos/device-uuid" | ||
| PREFIX="edgeos" | ||
| STATE_DIR="/etc/edgeos" | ||
| STATE_HOSTNAME_FILE="${STATE_DIR}/hostname" | ||
|
|
||
| # Logging | ||
| log() { | ||
| echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | ||
| logger -t edgeos-hostname "$*" | ||
| logger -t edgeos-hostname "$*" || true | ||
| } | ||
|
|
||
| # Validate UUID (accepts with/without dashes, case-insensitive) | ||
| is_valid_uuid() { | ||
| local v="${1,,}" | ||
| [[ "$v" =~ ^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$ ]] | ||
| } | ||
|
|
||
| # Primary source: device UUID | ||
| get_device_uuid() { | ||
| local uuid="" | ||
| if [ -r "$UUID_FILE" ]; then | ||
| uuid="$(tr -d '[:space:]' < "$UUID_FILE" 2>/dev/null || true)" | ||
| fi | ||
| echo "$uuid" | ||
| } | ||
|
|
||
| # Get device serial number or unique identifier | ||
| get_device_id() { | ||
| # Fallback legacy ID (serial/MAC/machine-id) – used only if UUID is missing/invalid | ||
| get_legacy_id() { | ||
| local device_id="" | ||
|
|
||
| # Try to get Raspberry Pi serial | ||
| # Raspberry Pi serial | ||
| if [ -f /proc/cpuinfo ]; then | ||
| device_id=$(grep Serial /proc/cpuinfo 2>/dev/null | cut -d':' -f2 | tr -d ' ' || true) | ||
| device_id=$(grep -m1 '^Serial' /proc/cpuinfo 2>/dev/null | cut -d':' -f2 | tr -d ' ' || true) | ||
| fi | ||
|
|
||
| # If no serial found, try machine-id | ||
| # machine-id (partial) | ||
| if [ -z "${device_id}" ] && [ -f /etc/machine-id ]; then | ||
| device_id=$(cat /etc/machine-id | head -c 16) | ||
| device_id=$(head -c 16 /etc/machine-id || true) | ||
| fi | ||
|
|
||
| # If still no ID, generate one | ||
| # first MAC | ||
| if [ -z "${device_id}" ]; then | ||
| device_id=$(ip link show | grep ether | head -1 | awk '{print $2}' | tr -d ':' | head -c 16) | ||
| device_id=$(ip link show | awk '/ether/ {gsub(":","",$2); print $2; exit}') | ||
| fi | ||
|
|
||
| # Fallback to random | ||
| # random fallback | ||
| if [ -z "${device_id}" ]; then | ||
| device_id=$(tr -dc 'a-f0-9' < /dev/urandom | head -c 16) | ||
| fi | ||
|
|
||
| echo "${device_id}" | ||
| echo "$device_id" | ||
| } | ||
|
|
||
| # Generate hostname | ||
| # Generate hostname from UUID (preferred) or legacy ID (fallback) | ||
| generate_hostname() { | ||
| local device_id=$(get_device_id) | ||
|
|
||
| # Take last 8 characters for readability | ||
| local short_id="${device_id: -8}" | ||
|
|
||
| # Convert to lowercase | ||
| short_id=$(echo "${short_id}" | tr '[:upper:]' '[:lower:]') | ||
|
|
||
| # Create hostname | ||
| local hostname="edgeos-${short_id}" | ||
|
|
||
| echo "${hostname}" | ||
| local uuid short_id legacy | ||
| uuid="$(get_device_uuid)" | ||
|
|
||
| if [ -n "$uuid" ] && is_valid_uuid "$uuid"; then | ||
| uuid="${uuid//-/}" | ||
| uuid="${uuid,,}" | ||
| short_id="${uuid: -8}" | ||
| else | ||
| legacy="$(get_legacy_id)" | ||
| legacy="${legacy//-/}" | ||
| legacy="${legacy,,}" | ||
| short_id="${legacy: -8}" | ||
| fi | ||
|
|
||
| echo "${PREFIX}-${short_id}" | ||
| } | ||
|
|
||
| # Set hostname | ||
| set_hostname() { | ||
| local new_hostname="$1" | ||
| local current_hostname=$(hostname) | ||
|
|
||
| local current_hostname | ||
| current_hostname=$(hostname || echo "") | ||
|
|
||
| if [ "${current_hostname}" = "${new_hostname}" ]; then | ||
| log "Hostname already set to ${new_hostname}" | ||
| return 0 | ||
| fi | ||
|
|
||
| log "Setting hostname to ${new_hostname}" | ||
|
|
||
| # Set hostname using hostnamectl if available | ||
|
|
||
| if command -v hostnamectl >/dev/null 2>&1; then | ||
| hostnamectl set-hostname "${new_hostname}" | ||
| echo "${new_hostname}" > /etc/hostname # menține sincron/compat | ||
| else | ||
| # Fallback to traditional method | ||
| echo "${new_hostname}" > /etc/hostname | ||
| hostname "${new_hostname}" | ||
| fi | ||
|
|
||
| # Update /etc/hosts | ||
| if ! grep -q "${new_hostname}" /etc/hosts; then | ||
| # Remove old edgeos entries | ||
| sed -i '/edgeos-/d' /etc/hosts | ||
|
|
||
| # Add new entry | ||
| echo "127.0.1.1 ${new_hostname} ${new_hostname}.local" >> /etc/hosts | ||
|
|
||
| # Update /etc/hosts (idempotent) | ||
| if [ -f /etc/hosts ]; then | ||
| grep -q "${new_hostname}" /etc/hosts 2>/dev/null || { | ||
| sed -i '/edgeos-/d' /etc/hosts 2>/dev/null || true | ||
| echo "127.0.1.1 ${new_hostname} ${new_hostname}.local" >> /etc/hosts | ||
| } | ||
| else | ||
| echo "127.0.1.1 ${new_hostname} ${new_hostname}.local" > /etc/hosts | ||
| fi | ||
|
|
||
| log "Hostname set successfully to ${new_hostname}" | ||
| } | ||
|
|
||
| # Main execution | ||
| main() { | ||
| log "Starting EdgeOS hostname generation" | ||
| # Check if we should skip (for development or custom setups) | ||
|
|
||
| # Allow opt-out | ||
| if [ -f /etc/edgeos-hostname-override ]; then | ||
| log "Hostname override found, skipping automatic generation" | ||
| exit 0 | ||
| fi | ||
|
|
||
| # Generate and set hostname | ||
| local hostname=$(generate_hostname) | ||
| set_hostname "${hostname}" | ||
|
|
||
| # Store the generated hostname for reference | ||
| echo "${hostname}" > /etc/edgeos-hostname | ||
|
|
||
| # Restart avahi-daemon if it's running to pick up new hostname | ||
| if systemctl is-active --quiet avahi-daemon.service; then | ||
|
|
||
| mkdir -p "${STATE_DIR}" | ||
|
|
||
| local new | ||
| new="$(generate_hostname)" | ||
| set_hostname "${new}" | ||
|
|
||
| echo "${new}" > "${STATE_HOSTNAME_FILE}" | ||
|
|
||
| # Restart avahi-daemon to broadcast the new name via mDNS (if present) | ||
| if command -v systemctl >/dev/null 2>&1 && systemctl is-active --quiet avahi-daemon.service; then | ||
| log "Restarting avahi-daemon to pick up new hostname" | ||
| systemctl restart avahi-daemon.service | ||
| systemctl restart avahi-daemon.service || true | ||
| fi | ||
| log "EdgeOS hostname generation completed" | ||
|
|
||
| log "EdgeOS hostname generation completed: ${new}" | ||
| } | ||
|
|
||
| # Run main function | ||
| main "$@" | ||
| main "$@" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,3 +39,9 @@ BUILDCFG_VARS += " \ | |
|
|
||
| # Disable WIC's automatic fstab updates | ||
| WIC_CREATE_EXTRA_ARGS = "--no-fstab-update" | ||
|
|
||
| # Provider for 'hostname' required by avahi-daemon | ||
| IMAGE_INSTALL:append = " inetutils-hostname" | ||
|
|
||
| # Avahi + the sub-package with the custom script/service | ||
| IMAGE_INSTALL:append = " avahi-daemon avahi-edgeos-hostname" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought avahi daemon was already installed somewhere. not a problem. I'm sure the build system will figure it out. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice