mirror of
https://gitlab.com/hacklab01/pivilion.git
synced 2026-08-06 16:08:22 +00:00
Fix early-boot hostname application
This commit is contained in:
parent
b5e19f08f6
commit
0f20889948
5 changed files with 130 additions and 4 deletions
|
|
@ -2,6 +2,7 @@
|
|||
set -Eeuo pipefail
|
||||
|
||||
source "${PIVILION_COMMON_PATH:-/usr/local/lib/pivilion/common.sh}"
|
||||
source "${PIVILION_HOSTNAME_LIB_PATH:-$(dirname -- "${BASH_SOURCE[0]}")/hostname.sh}"
|
||||
pivilion_require_root
|
||||
|
||||
BOOT_ROOT=$(pivilion_boot_root)
|
||||
|
|
@ -189,7 +190,10 @@ migrate_paths() {
|
|||
apply_runtime() {
|
||||
local php_version post_mb escaped_ssid
|
||||
migrate_paths
|
||||
if [ "$(hostname -s)" != "$PIVILION_HOSTNAME" ] && command -v hostnamectl >/dev/null 2>&1; then hostnamectl set-hostname "$PIVILION_HOSTNAME"; fi
|
||||
if ! pivilion_apply_hostname "$PIVILION_HOSTNAME"; then
|
||||
fail_boot "Failed to apply hostname '$PIVILION_HOSTNAME' without D-Bus"
|
||||
return "$PIVILION_E_CONFIG"
|
||||
fi
|
||||
command -v iw >/dev/null 2>&1 && iw reg set "$PIVILION_WIFI_COUNTRY" 2>/dev/null || true
|
||||
if [ -n "$PIVILION_WIFI_SSID" ] && [ -n "$PIVILION_WIFI_PSK" ]; then
|
||||
if command -v nmcli >/dev/null 2>&1 && { systemctl is-enabled --quiet NetworkManager 2>/dev/null || systemctl is-active --quiet NetworkManager 2>/dev/null; }; then
|
||||
|
|
|
|||
64
usr/local/lib/pivilion/hostname.sh
Normal file
64
usr/local/lib/pivilion/hostname.sh
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Apply a hostname without systemd-hostnamed. This file is also sourced by the
|
||||
# test suite, so paths and the hostname command may be redirected there.
|
||||
pivilion_apply_hostname() {
|
||||
local hostname=$1
|
||||
local hostname_file=${PIVILION_HOSTNAME_FILE:-/etc/hostname}
|
||||
local hosts_file=${PIVILION_HOSTS_FILE:-/etc/hosts}
|
||||
local hostname_command=${PIVILION_HOSTNAME_COMMAND:-/bin/hostname}
|
||||
local tmp current
|
||||
|
||||
tmp=$(mktemp "$(dirname "$hostname_file")/.pivilion-hostname.XXXXXX") || {
|
||||
echo "Could not create a temporary file for $hostname_file" >&2
|
||||
return 1
|
||||
}
|
||||
printf '%s\n' "$hostname" > "$tmp" && chmod 0644 "$tmp" || {
|
||||
rm -f "$tmp"
|
||||
echo "Could not prepare $hostname_file for hostname '$hostname'" >&2
|
||||
return 1
|
||||
}
|
||||
if [ -r "$hostname_file" ] && cmp -s "$tmp" "$hostname_file"; then
|
||||
rm -f "$tmp"
|
||||
elif ! mv -f "$tmp" "$hostname_file"; then
|
||||
rm -f "$tmp"
|
||||
echo "Could not atomically write hostname '$hostname' to $hostname_file" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
tmp=$(mktemp "$(dirname "$hosts_file")/.pivilion-hosts.XXXXXX") || {
|
||||
echo "Could not create a temporary file for $hosts_file" >&2
|
||||
return 1
|
||||
}
|
||||
awk -v hostname="$hostname" '
|
||||
$1 == "127.0.1.1" { if (!written++) print "127.0.1.1\t" hostname; next }
|
||||
{ print }
|
||||
END { if (!written) print "127.0.1.1\t" hostname }
|
||||
' "$hosts_file" > "$tmp" 2>/dev/null || {
|
||||
rm -f "$tmp"
|
||||
echo "Could not update the 127.0.1.1 entry in $hosts_file" >&2
|
||||
return 1
|
||||
}
|
||||
chmod 0644 "$tmp" || {
|
||||
rm -f "$tmp"
|
||||
echo "Could not prepare the updated $hosts_file" >&2
|
||||
return 1
|
||||
}
|
||||
if [ -r "$hosts_file" ] && cmp -s "$tmp" "$hosts_file"; then
|
||||
rm -f "$tmp"
|
||||
elif ! mv -f "$tmp" "$hosts_file"; then
|
||||
rm -f "$tmp"
|
||||
echo "Could not atomically update the 127.0.1.1 entry in $hosts_file" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
[ -x "$hostname_command" ] || {
|
||||
echo "Cannot set the running hostname: $hostname_command is not executable" >&2
|
||||
return 1
|
||||
}
|
||||
current=$($hostname_command -s 2>/dev/null || true)
|
||||
if [ "$current" != "$hostname" ] && ! "$hostname_command" "$hostname"; then
|
||||
echo "Could not set the running kernel hostname to '$hostname' with $hostname_command" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue