Fix first boot static IP netmask handling

This commit is contained in:
Jino Jose 2026-06-30 16:08:49 +05:30
parent 910e5d1499
commit b1b5abc442

View File

@ -20,6 +20,30 @@ detect_iface() {
IFACE="$(detect_iface)"
IFACE="${IFACE:-$(ip -o link show | awk -F': ' '$2 !~ /lo|docker|br-|veth/ {print $2; exit}')}"
netmask_to_prefix() {
case "$1" in
32|31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|9|8|7|6|5|4|3|2|1) echo "$1" ;;
255.255.255.255) echo 32 ;;
255.255.255.254) echo 31 ;;
255.255.255.252) echo 30 ;;
255.255.255.248) echo 29 ;;
255.255.255.240) echo 28 ;;
255.255.255.224) echo 27 ;;
255.255.255.192) echo 26 ;;
255.255.255.128) echo 25 ;;
255.255.255.0) echo 24 ;;
255.255.254.0) echo 23 ;;
255.255.252.0) echo 22 ;;
255.255.248.0) echo 21 ;;
255.255.240.0) echo 20 ;;
255.255.224.0) echo 19 ;;
255.255.192.0) echo 18 ;;
255.255.128.0) echo 17 ;;
255.255.0.0) echo 16 ;;
*) return 1 ;;
esac
}
# ── Colors / terminal setup ────────────────────────────────
export NEWT_COLORS='
root=,black
@ -66,9 +90,16 @@ if [ "$NET_MODE" = "Static" ]; then
$H $W "" 3>&1 1>&2 2>&3)
NETMASK=$(whiptail --title "$TITLE" \
--inputbox "\nEnter subnet mask (CIDR prefix):\n(Example: 24 means 255.255.255.0)" \
--inputbox "\nEnter subnet mask / CIDR prefix:\n(Examples: 24 or 255.255.255.0)" \
$H $W "24" 3>&1 1>&2 2>&3)
PREFIX="$(netmask_to_prefix "$NETMASK")" || {
whiptail --title "$TITLE" \
--msgbox "\nInvalid subnet mask or CIDR prefix:\n ${NETMASK}\n\nUse a CIDR prefix such as 24, or a mask such as 255.255.255.0." \
$H $W
exit 1
}
GATEWAY=$(whiptail --title "$TITLE" \
--inputbox "\nEnter default gateway:\n(Example: 192.168.1.1)" \
$H $W "" 3>&1 1>&2 2>&3)
@ -85,7 +116,7 @@ network:
${IFACE}:
dhcp4: false
addresses:
- ${IP_ADDR}/${NETMASK}
- ${IP_ADDR}/${PREFIX}
routes:
- to: default
via: ${GATEWAY}
@ -93,17 +124,22 @@ network:
addresses: [${DNS}]
EOF
netplan apply 2>/dev/null || true
if ! netplan apply; then
whiptail --title "$TITLE" \
--msgbox "\nFailed to apply network settings.\n\nIP: ${IP_ADDR}/${PREFIX}\nGateway: ${GATEWAY}\nDNS: ${DNS}\n\nPlease verify the address, gateway, and subnet." \
$H $W
exit 1
fi
sleep 3
# Verify connectivity
if ! ping -c 2 -W 3 8.8.8.8 &>/dev/null; then
whiptail --title "$TITLE" \
--msgbox "\nWARNING: Cannot reach internet with these settings.\n\nIP: ${IP_ADDR}/${NETMASK}\nGateway: ${GATEWAY}\nDNS: ${DNS}\n\nPlease verify your network settings. The install requires internet access." \
--msgbox "\nWARNING: Cannot reach internet with these settings.\n\nIP: ${IP_ADDR}/${PREFIX}\nGateway: ${GATEWAY}\nDNS: ${DNS}\n\nPlease verify your network settings. The install requires internet access." \
$H $W
else
whiptail --title "$TITLE" \
--msgbox "\nNetwork configured successfully!\n\nIP: ${IP_ADDR}/${NETMASK}\nGateway: ${GATEWAY}\nDNS: ${DNS}" \
--msgbox "\nNetwork configured successfully!\n\nIP: ${IP_ADDR}/${PREFIX}\nGateway: ${GATEWAY}\nDNS: ${DNS}" \
$H $W
fi