Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 75 additions & 9 deletions packages/bsp/common/usr/lib/armbian/armbian-firstrun
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@
. /lib/lsb/init-functions
. /usr/lib/armbian/armbian-common

# Apply a new system hostname once and reconcile the 127.0.1.1/::1 records in
# /etc/hosts written at build time (loopback names, aliases and comments are
# left untouched). No-op only when the target is empty. The /etc/hosts
# reconciliation runs even when new==old (skipping only the hostnamectl call
# itself): a prior run can have set the hostname successfully but failed the
# /etc/hosts edit, and a retry must still repair those records rather than
# short-circuit on "already current".
set_new_hostname() {
local new="$1" old esc_new
old="$(hostname)"
[[ -z "$new" ]] && return 0
if [[ "$new" != "$old" ]]; then
hostnamectl set-hostname "$new" || return 1
fi
esc_new=$(printf '%s' "$new" | sed 's#[\\/&]#\\&#g')
sed -i -E \
-e "s|^(127\.0\.1\.1[[:space:]]+)[^[:space:]#]+|\1${esc_new}|" \
-e "s|^(::1[[:space:]]+localhost[[:space:]]+)[^[:space:]#]+|\1${esc_new}|" \
/etc/hosts
}

case "$1" in
start)

Expand Down Expand Up @@ -129,17 +150,62 @@ case "$1" in
[[ "${LINUXFAMILY}" == meson64 ]] && set_fixed_mac
[[ "${BOARD}" == nanopi-r6* ]] && set_fixed_mac

# adjust hostname for rpi's
# adjust hostname for rpi's: the single arm64 image boots the whole Pi 3B+
# onward lineup, so pick a per-model hostname from the board model string.
# Ordered most-specific-first so e.g. "Model B Plus" wins over "Model B".
if [[ "${BOARD}" == rpi4b && "$(hostname)" == rpi4b ]]; then
BOARD_NAME=$(cat /proc/device-tree/model | tr '\0' '\n' | sed -E 's/ Rev [0-9.]+$//')
[[ "$BOARD_NAME" == *"Zero 2 W"* ]] && NEW_HOSTNAME="rpizero2w"
[[ "$BOARD_NAME" == *"Pi 5 Model B"* ]] && NEW_HOSTNAME="rpi5b"
[[ "$BOARD_NAME" == *"Pi 3 Model B"* ]] && NEW_HOSTNAME="rpi3b"
[[ "$BOARD_NAME" == *"Pi 400"* ]] && NEW_HOSTNAME="rpi400"
# Update /etc/hosts to reflect the new hostname
sed -i "s/$(hostname)/$NEW_HOSTNAME/g" /etc/hosts
# Change the hostname
hostnamectl set-hostname "$NEW_HOSTNAME"
NEW_HOSTNAME=""
# Model strings are the upstream RPi kernel DT values (arm64 / 64-bit
# lineup only). Compute Modules report their carrier-board string, so a
# substring of the SoM name is matched.
case "$BOARD_NAME" in
*"Compute Module 3"*) NEW_HOSTNAME="rpicm3" ;;
*"Compute Module 4S"*) NEW_HOSTNAME="rpicm4s" ;;
*"Compute Module 4"*) NEW_HOSTNAME="rpicm4" ;;
*"Compute Module 5 Lite"*) NEW_HOSTNAME="rpicm5lite" ;;
*"Compute Module 5"*) NEW_HOSTNAME="rpicm5" ;;
*"Zero 2 W"*) NEW_HOSTNAME="rpizero2w" ;;
*"Pi 3 Model A+"*) NEW_HOSTNAME="rpi3aplus" ;;
*"Pi 3 Model B+"*) NEW_HOSTNAME="rpi3bplus" ;;
*"Pi 3 Model B"*) NEW_HOSTNAME="rpi3b" ;;
*"Pi 400"*) NEW_HOSTNAME="rpi400" ;;
*"Pi 4 Model B"*) NEW_HOSTNAME="rpi4b" ;;
*"Pi 500"*) NEW_HOSTNAME="rpi500" ;;
*"Pi 5"*) NEW_HOSTNAME="rpi5b" ;;
esac
# Leaves the default rpi4b hostname in place when no model matched.
# A failure here must not reach the service-disable below - if it did, a
# transient hostnamectl/sed failure would strand the board on the wrong
# hostname forever instead of retrying on the next boot.
set_new_hostname "$NEW_HOSTNAME" || exit 1
fi

# adjust hostname for the FriendlyElec NanoPi 6-series unified image.
# A single image boots on several boards; the unified U-Boot detects the
# model via SARADC and exports nanopi6_model=<model> as its own token on the
# kernel command line, present only on these images. Parse it word-by-word so
# it can never match text embedded inside another kernel argument.
nanopi6_model=""
for arg in $(cat /proc/cmdline); do
case "$arg" in
nanopi6_model=*) nanopi6_model="${arg#nanopi6_model=}"; break ;;
esac
done
if [[ -n "$nanopi6_model" ]]; then
NEW_HOSTNAME=""
case "$nanopi6_model" in
r6s) NEW_HOSTNAME="nanopi-r6s" ;;
r6c) NEW_HOSTNAME="nanopi-r6c" ;;
m6) NEW_HOSTNAME="nanopi-m6" ;;
m6v2) NEW_HOSTNAME="nanopi-m6v2" ;;
t6) NEW_HOSTNAME="nanopct6" ;;
t6-lts) NEW_HOSTNAME="nanopct6-lts" ;;
t6-lts-plus) NEW_HOSTNAME="nanopct6-lts-plus" ;;
esac
# Same reasoning as the rpi4b case above: don't let a failed hostname
# update reach the service-disable, or first-run never gets to retry.
set_new_hostname "$NEW_HOSTNAME" || exit 1
fi

# Remove KDE Neon base files upgrade pin
Expand Down