|
|
|
@ -1,14 +1,22 @@
|
|
|
|
#!/bin/bash
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
|
|
# Script to set the hostname in the k3os config.yaml file.
|
|
|
|
# Script to set the hostname for k3OS by writing directly to the hostname file.
|
|
|
|
# It handles the read-only filesystem constraint of a running k3OS system.
|
|
|
|
# This script requires root privileges and prompts the user for the new hostname.
|
|
|
|
|
|
|
|
|
|
|
|
# Define the path to the config file and the mount point
|
|
|
|
HOSTNAME_FILE="/var/lib/rancher/k3os/hostname"
|
|
|
|
CONFIG_FILE="/k3os/system/config.yaml"
|
|
|
|
|
|
|
|
CONFIG_DIR="/k3os/system"
|
|
|
|
# --- Function to check for root/sudo privileges ---
|
|
|
|
|
|
|
|
check_privileges() {
|
|
|
|
|
|
|
|
if [[ $EUID -ne 0 ]]; then
|
|
|
|
|
|
|
|
echo "Error: This script must be run with root/sudo privileges."
|
|
|
|
|
|
|
|
echo "Please run: sudo $0"
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# --- Function to get user input ---
|
|
|
|
# --- Function to get user input ---
|
|
|
|
get_hostname_input() {
|
|
|
|
get_hostname_input() {
|
|
|
|
|
|
|
|
# Loop until a non-empty hostname is provided
|
|
|
|
while true; do
|
|
|
|
while true; do
|
|
|
|
read -r -p "Enter the desired new hostname: " NEW_HOSTNAME
|
|
|
|
read -r -p "Enter the desired new hostname: " NEW_HOSTNAME
|
|
|
|
if [ -n "$NEW_HOSTNAME" ]; then
|
|
|
|
if [ -n "$NEW_HOSTNAME" ]; then
|
|
|
|
@ -19,61 +27,31 @@ get_hostname_input() {
|
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# --- Function to modify the config file ---
|
|
|
|
# --- Function to write the hostname to the file ---
|
|
|
|
set_hostname() {
|
|
|
|
write_hostname() {
|
|
|
|
local NEW_HOSTNAME="$1"
|
|
|
|
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 "Attempting to write new hostname: **$NEW_HOSTNAME** to **$HOSTNAME_FILE**"
|
|
|
|
echo "Config file location: **$CONFIG_FILE**"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 1. Remount the filesystem as read/write (rw)
|
|
|
|
# Write the new hostname, ensuring no extra whitespace or newlines
|
|
|
|
echo "Attempting to remount $CONFIG_DIR as read/write..."
|
|
|
|
echo -n "$NEW_HOSTNAME" > "$HOSTNAME_FILE"
|
|
|
|
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."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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."
|
|
|
|
|
|
|
|
echo -e "\nk3os:\n$HOSTNAME_ENTRY" >> "$CONFIG_FILE"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif grep -q "^k3os:" "$CONFIG_FILE" && grep -q "^\s\+hostname:" "$CONFIG_FILE"; then
|
|
|
|
# Check if the write operation was successful
|
|
|
|
echo "Existing hostname found. Replacing the entry."
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
sed -i "/^\s*hostname:/c\\$HOSTNAME_ENTRY" "$CONFIG_FILE"
|
|
|
|
echo "Success: The new hostname has been written to $HOSTNAME_FILE."
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
echo "'k3os:' block found, but no hostname entry. Appending the entry."
|
|
|
|
|
|
|
|
sed -i "/^k3os:/a\\$HOSTNAME_ENTRY" "$CONFIG_FILE"
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
echo -e "$HOSTNAME_ENTRY" > /var/lib/rancher/k3os/hostname
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 4. Report status
|
|
|
|
|
|
|
|
if [ $MODIFICATION_STATUS -eq 0 ]; then
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
echo ""
|
|
|
|
echo "Successfully updated the hostname in $CONFIG_FILE."
|
|
|
|
echo "--- Verification ---"
|
|
|
|
echo "The change will take effect after a **reboot**."
|
|
|
|
echo "File content: $(cat "$HOSTNAME_FILE")"
|
|
|
|
echo ""
|
|
|
|
echo ""
|
|
|
|
echo "--- Verification (Hostname line only) ---"
|
|
|
|
echo "Action Required: A **system reboot is necessary** for the new hostname to take effect."
|
|
|
|
grep -E "^\s*hostname:" "$CONFIG_FILE"
|
|
|
|
echo "You can reboot now using the 'reboot' command."
|
|
|
|
else
|
|
|
|
else
|
|
|
|
echo "Error: Failed to modify $CONFIG_FILE during step 2."
|
|
|
|
echo "Error: Failed to write to $HOSTNAME_FILE. Check file permissions or disk status."
|
|
|
|
exit 1
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# --- Main execution ---
|
|
|
|
# --- Main execution ---
|
|
|
|
|
|
|
|
check_privileges
|
|
|
|
get_hostname_input
|
|
|
|
get_hostname_input
|
|
|
|
set_hostname "$NEW_HOSTNAME"
|
|
|
|
write_hostname "$NEW_HOSTNAME"
|