|
|
|
|
@ -1,13 +1,14 @@
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
# Script to set the hostname in the k3os config.yaml file using only standard tools (sed, grep).
|
|
|
|
|
# Script to set the hostname in the k3os config.yaml file.
|
|
|
|
|
# It handles the read-only filesystem constraint of a running k3OS system.
|
|
|
|
|
|
|
|
|
|
# Define the path to the config file
|
|
|
|
|
# Define the path to the config file and the mount point
|
|
|
|
|
CONFIG_FILE="/k3os/system/config.yaml"
|
|
|
|
|
CONFIG_DIR="/k3os/system"
|
|
|
|
|
|
|
|
|
|
# --- Function to get user input ---
|
|
|
|
|
get_hostname_input() {
|
|
|
|
|
# Loop until a non-empty hostname is provided
|
|
|
|
|
while true; do
|
|
|
|
|
read -r -p "Enter the desired new hostname: " NEW_HOSTNAME
|
|
|
|
|
if [ -n "$NEW_HOSTNAME" ]; then
|
|
|
|
|
@ -23,41 +24,50 @@ set_hostname() {
|
|
|
|
|
local NEW_HOSTNAME="$1"
|
|
|
|
|
# Note the two spaces for YAML indentation under 'k3os:'
|
|
|
|
|
local HOSTNAME_ENTRY=" hostname: $NEW_HOSTNAME"
|
|
|
|
|
|
|
|
|
|
echo "Attempting to set hostname to: **$NEW_HOSTNAME**"
|
|
|
|
|
echo "Config file location: **$CONFIG_FILE**"
|
|
|
|
|
|
|
|
|
|
# 1. Remount the filesystem as read/write (rw)
|
|
|
|
|
echo "Attempting to remount $CONFIG_DIR as read/write..."
|
|
|
|
|
if ! mount -o remount,rw "$CONFIG_DIR"; then
|
|
|
|
|
echo "Error: Failed to remount $CONFIG_DIR as read/write. Aborting."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
echo "Filesystem successfully remounted as read/write."
|
|
|
|
|
|
|
|
|
|
echo "Attempting to set hostname to: **$NEW_HOSTNAME** in **$CONFIG_FILE**"
|
|
|
|
|
|
|
|
|
|
# 1. Check if the 'k3os:' block exists
|
|
|
|
|
# 2. Modify the config file (same logic as before)
|
|
|
|
|
if ! grep -q "^k3os:" "$CONFIG_FILE"; then
|
|
|
|
|
echo "'k3os:' block not found. Appending the block and hostname."
|
|
|
|
|
# Append the k3os block and the new hostname entry
|
|
|
|
|
echo -e "\nk3os:\n$HOSTNAME_ENTRY" >> "$CONFIG_FILE"
|
|
|
|
|
|
|
|
|
|
# 2. Check if a 'hostname' entry already exists under 'k3os:'
|
|
|
|
|
# Checks for the 'k3os:' block AND an indented 'hostname:' line
|
|
|
|
|
elif grep -q "^k3os:" "$CONFIG_FILE" && grep -q "^\s\+hostname:" "$CONFIG_FILE"; then
|
|
|
|
|
echo "Existing hostname found. Replacing the entry."
|
|
|
|
|
# Use sed to replace the existing hostname line under k3os
|
|
|
|
|
# /^\s*hostname:/ matches any line starting with optional spaces followed by 'hostname:'
|
|
|
|
|
sed -i "/^\s*hostname:/c\\$HOSTNAME_ENTRY" "$CONFIG_FILE"
|
|
|
|
|
|
|
|
|
|
# 3. 'k3os:' exists but 'hostname:' does not.
|
|
|
|
|
else
|
|
|
|
|
echo "'k3os:' block found, but no hostname entry. Appending the entry."
|
|
|
|
|
# Use sed to insert the new hostname entry immediately after the 'k3os:' line
|
|
|
|
|
# The 'a\' command appends text after the matched line
|
|
|
|
|
sed -i "/^k3os:/a\\$HOSTNAME_ENTRY" "$CONFIG_FILE"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check the exit status of the previous command
|
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
|
local MODIFICATION_STATUS=$?
|
|
|
|
|
|
|
|
|
|
# 3. Remount the filesystem back to read-only (ro)
|
|
|
|
|
echo "Remounting $CONFIG_DIR back to read-only..."
|
|
|
|
|
if ! mount -o remount,ro "$CONFIG_DIR"; then
|
|
|
|
|
echo "Warning: Failed to remount $CONFIG_DIR back to read-only. Proceeding..."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 4. Report status
|
|
|
|
|
if [ $MODIFICATION_STATUS -eq 0 ]; then
|
|
|
|
|
echo ""
|
|
|
|
|
echo "Successfully updated the hostname in $CONFIG_FILE."
|
|
|
|
|
echo "The change will take effect after a reboot."
|
|
|
|
|
echo "The change will take effect after a **reboot**."
|
|
|
|
|
echo ""
|
|
|
|
|
echo "--- Verification (Hostname line only) ---"
|
|
|
|
|
# Print the modified line for verification
|
|
|
|
|
grep -E "^\s*hostname:" "$CONFIG_FILE"
|
|
|
|
|
else
|
|
|
|
|
echo "Failed to modify $CONFIG_FILE."
|
|
|
|
|
echo "Error: Failed to modify $CONFIG_FILE during step 2."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|