All I am pulling my hair out trying to get an interactive portion only for the hostname in kickstart. I do not want to hardcode it via a directive and am trying %pre and %post things and they are not working… latest try is
# Pre-installation script to prompt for network settings
%pre --interpreter=/bin/bash
LOGFILE=/tmp/ks-pre.log
exec < /dev/tty6 > /dev/tty6 2> /dev/tty6
chvt 6
echo
echo "###################################"
echo "# Running Pre Setup Configuration #"
echo "###################################"
echo
(
# Predefined DNS servers (replace with actual values)
NAMESERVER="8.8.8.8,1.1.1.1"
# Simple validation function for IP addresses
validate_ip() {
local ip=$1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
return 0
else
return 1
fi
}
CONFIRM="no"
while [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; do
echo -n "Hostname (or leave blank for DHCP): "
read HOSTNAME
if [ -z "$HOSTNAME" ]; then
HOSTLINE="network --device=eth0 --bootproto=dhcp"
echo -e -n "\\e[00;31mConfigure OS to use DHCP? (y/n): \\e[00m"
read CONFIRM
else
# Prompt for static IP details
while true; do
echo -n "IP Address: "
read IPADDRESS
if validate_ip "$IPADDRESS"; then
break
else
echo "Invalid IP address. Please try again."
fi
done
while true; do
echo -n "Netmask: "
read NETMASK
if validate_ip "$NETMASK"; then
break
else
echo "Invalid netmask. Please try again."
fi
done
while true; do
echo -n "Gateway: "
read GATEWAY
if validate_ip "$GATEWAY"; then
break
else
echo "Invalid gateway. Please try again."
fi
done
echo -e "Hostname: \\e[01;36m$HOSTNAME\\e[00m"
echo -e "IP Address: \\e[01;36m$IPADDRESS\\e[00m"
echo -e "Netmask: \\e[01;36m$NETMASK\\e[00m"
echo -e "Gateway: \\e[01;36m$GATEWAY\\e[00m"
HOSTLINE="network --device=eth0 --bootproto=static --ip=$IPADDRESS --netmask=$NETMASK --gateway=$GATEWAY --nameserver=$NAMESERVER --hostname=$HOSTNAME"
echo -n "Is the above configuration correct? (y/n): "
read CONFIRM
fi
done
# Save the network configuration
echo "$HOSTLINE" > /tmp/network.ks
hostname "$HOSTNAME" 2>/dev/null || true
# Inform user and resume
echo "Network configuration saved. Resuming automated installation..."
sleep 2
) 2>&1 | /usr/bin/tee $LOGFILE
# Switch back to the main installer terminal
chvt 1
exec < /dev/tty1 > /dev/tty1 2> /dev/tty1
%end
# Include the dynamically generated network configuration
%include /tmp/network.ks
# Post-installation script to persist network settings
%post --interpreter=/bin/bash
# Read the HOSTLINE to determine if DHCP or static
HOSTLINE=$(cat /tmp/network.ks)
# Persist hostname
if [[ "$HOSTLINE" =~ --hostname=([^ ]+) ]]; then
HOSTNAME=${BASH_REMATCH[1]}
cat > /etc/sysconfig/network <<EOF
NETWORKING=yes
HOSTNAME=$HOSTNAME
EOF
fi
# Persist interface settings
if [[ "$HOSTLINE" =~ --bootproto=dhcp ]]; then
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
EOF
else
# Extract static IP details from HOSTLINE
if [[ "$HOSTLINE" =~ --ip=([^ ]+) ]]; then IPADDRESS=${BASH_REMATCH[1]}; fi
if [[ "$HOSTLINE" =~ --netmask=([^ ]+) ]]; then NETMASK=${BASH_REMATCH[1]}; fi
if [[ "$HOSTLINE" =~ --gateway=([^ ]+) ]]; then GATEWAY=${BASH_REMATCH[1]}; fi
if [[ "$HOSTLINE" =~ --nameserver=([^ ]+) ]]; then NAMESERVER=${BASH_REMATCH[1]}; fi
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
IPADDR=$IPADDRESS
NETMASK=$NETMASK
GATEWAY=$GATEWAY
DNS1=${NAMESERVER%%,*} # Use the first DNS server
EOF
fi
%end
What I am finding is that when you press the first key to input any hostname, it continues the automated install with kickstart…