perf(misc): optimize network checks, IP detection, and subprocess chains

Files: install.func, alpine-install.func, core.func, api.func

Changes:
- install.func: Cache hostname -I result in setting_up_container() —
  eliminates 3 redundant calls (retry loop + post-check + display).
  Single awk extracts first IP from cached call.

- alpine-install.func: Cache ip addr result in setting_up_container() —
  replaces 3 identical 4-process pipelines (ip|grep|grep|awk|cut) with
  one call using single awk (sub+print in one pass).

- core.func get_lxc_ip(): Merge awk+cut into single awk (sub+print)
  for eth0 IPv4/IPv6 lookups. Replace tr|grep pipeline for IPv6 hostname
  fallback with bash array iteration (read -ra + for loop).

- api.func detect_gpu(): Merge 2x sed + cut into single sed -E + bash
  substring. detect_cpu(): Merge 4x sed + cut into single sed -E + bash
  substring. get_error_text(): Merge 2x sed into single sed -E.
  post_addon_to_api(): Replace 2x grep|cut|tr on /etc/os-release with
  single while-read loop (1 file read instead of 6 subprocess forks).
This commit is contained in:
MickLesk
2026-03-23 20:44:47 +01:00
parent a02bdf083d
commit 2008b1b458
4 changed files with 38 additions and 29 deletions
+5 -5
View File
@@ -67,22 +67,22 @@ EOF
# This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection # This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection
setting_up_container() { setting_up_container() {
msg_info "Setting up Container OS" msg_info "Setting up Container OS"
local _ip=""
while [ $i -gt 0 ]; do while [ $i -gt 0 ]; do
if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" != "" ]; then _ip=$(ip -4 addr show 2>/dev/null | awk '/inet [0-9]/ && !/127\.0\.0\.1/ {sub(/\/.*/, "", $2); print $2; exit}')
break [[ -n "$_ip" ]] && break
fi
echo 1>&2 -en "${CROSS}${RD} No Network! " echo 1>&2 -en "${CROSS}${RD} No Network! "
sleep $RETRY_EVERY sleep $RETRY_EVERY
i=$((i - 1)) i=$((i - 1))
done done
if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" = "" ]; then if [[ -z "$_ip" ]]; then
echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}" echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
echo -e "${NETWORK}Check Network Settings" echo -e "${NETWORK}Check Network Settings"
exit 121 exit 121
fi fi
msg_ok "Set up Container OS" msg_ok "Set up Container OS"
msg_ok "Network Connected: ${BL}$(ip addr show | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1 | tail -n1)${CL}" msg_ok "Network Connected: ${BL}${_ip}${CL}"
post_progress_to_api post_progress_to_api
} }
+13 -6
View File
@@ -401,7 +401,7 @@ get_error_text() {
fi fi
if [[ -n "$logfile" && -s "$logfile" ]]; then if [[ -n "$logfile" && -s "$logfile" ]]; then
tail -n 20 "$logfile" 2>/dev/null | sed 's/\r$//' | sed 's/\x1b\[[0-9;]*[a-zA-Z]//g' tail -n 20 "$logfile" 2>/dev/null | sed -E 's/\r$//; s/\x1b\[[0-9;]*[a-zA-Z]//g'
fi fi
} }
@@ -508,7 +508,8 @@ detect_gpu() {
if [[ -n "$gpu_line" ]]; then if [[ -n "$gpu_line" ]]; then
# Extract model: everything after the colon, clean up # Extract model: everything after the colon, clean up
GPU_MODEL=$(echo "$gpu_line" | sed 's/.*: //' | sed 's/ (rev .*)$//' | cut -c1-64) GPU_MODEL=$(echo "$gpu_line" | sed -E 's/.*: //; s/ \(rev .*\)$//')
GPU_MODEL="${GPU_MODEL:0:64}"
# Detect vendor and passthrough type # Detect vendor and passthrough type
if echo "$gpu_line" | grep -qi "Intel"; then if echo "$gpu_line" | grep -qi "Intel"; then
@@ -557,7 +558,8 @@ detect_cpu() {
esac esac
# Extract model name and clean it up # Extract model name and clean it up
CPU_MODEL=$(grep -m1 "model name" /proc/cpuinfo 2>/dev/null | cut -d: -f2 | sed 's/^ *//' | sed 's/(R)//g' | sed 's/(TM)//g' | sed 's/ */ /g' | cut -c1-64) CPU_MODEL=$(grep -m1 "model name" /proc/cpuinfo 2>/dev/null | cut -d: -f2 | sed -E 's/^ *//; s/\(R\)//g; s/\(TM\)//g; s/ +/ /g')
CPU_MODEL="${CPU_MODEL:0:64}"
fi fi
export CPU_VENDOR CPU_MODEL export CPU_VENDOR CPU_MODEL
@@ -1325,11 +1327,16 @@ post_addon_to_api() {
error_category=$(categorize_error "$exit_code") error_category=$(categorize_error "$exit_code")
fi fi
# Detect OS info # Detect OS info (single read)
local os_type="" os_version="" local os_type="" os_version=""
if [[ -f /etc/os-release ]]; then if [[ -f /etc/os-release ]]; then
os_type=$(grep "^ID=" /etc/os-release | cut -d= -f2 | tr -d '"') while IFS='=' read -r _k _v; do
os_version=$(grep "^VERSION_ID=" /etc/os-release | cut -d= -f2 | tr -d '"') _v="${_v//\"/}"
case "$_k" in
ID) os_type="$_v" ;;
VERSION_ID) os_version="$_v" ;;
esac
done < /etc/os-release
fi fi
local JSON_PAYLOAD local JSON_PAYLOAD
+12 -9
View File
@@ -1644,7 +1644,7 @@ function get_lxc_ip() {
local ip local ip
# Try direct interface lookup for eth0 FIRST (most reliable for LXC) - IPv4 # Try direct interface lookup for eth0 FIRST (most reliable for LXC) - IPv4
ip=$(ip -4 addr show eth0 2>/dev/null | awk '/inet / {print $2}' | cut -d/ -f1 | head -n1) ip=$(ip -4 addr show eth0 2>/dev/null | awk '/inet / {sub(/\/.*/, "", $2); print $2; exit}')
if [[ -n "$ip" && "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then if [[ -n "$ip" && "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "$ip" echo "$ip"
return 0 return 0
@@ -1652,11 +1652,14 @@ function get_lxc_ip() {
# Fallback: Try hostname -I (returns IPv4 first if available) # Fallback: Try hostname -I (returns IPv4 first if available)
if command -v hostname >/dev/null 2>&1; then if command -v hostname >/dev/null 2>&1; then
ip=$(hostname -I 2>/dev/null | awk '{print $1}') local -a _ips
if [[ -n "$ip" && "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then read -ra _ips <<<"$(hostname -I 2>/dev/null)"
for ip in "${_ips[@]}"; do
if [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "$ip" echo "$ip"
return 0 return 0
fi fi
done
fi fi
# Try routing table with IPv4 targets # Try routing table with IPv4 targets
@@ -1674,7 +1677,7 @@ function get_lxc_ip() {
done done
# IPv6 fallback: Try direct interface lookup for eth0 # IPv6 fallback: Try direct interface lookup for eth0
ip=$(ip -6 addr show eth0 scope global 2>/dev/null | awk '/inet6 / {print $2}' | cut -d/ -f1 | head -n1) ip=$(ip -6 addr show eth0 scope global 2>/dev/null | awk '/inet6 / {sub(/\/.*/, "", $2); print $2; exit}')
if [[ -n "$ip" && "$ip" =~ : ]]; then if [[ -n "$ip" && "$ip" =~ : ]]; then
echo "$ip" echo "$ip"
return 0 return 0
@@ -1682,11 +1685,11 @@ function get_lxc_ip() {
# IPv6 fallback: Try hostname -I for IPv6 # IPv6 fallback: Try hostname -I for IPv6
if command -v hostname >/dev/null 2>&1; then if command -v hostname >/dev/null 2>&1; then
ip=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E ':' | head -n1) local -a _ips6
if [[ -n "$ip" && "$ip" =~ : ]]; then read -ra _ips6 <<<"$(hostname -I 2>/dev/null)"
echo "$ip" for ip in "${_ips6[@]}"; do
return 0 [[ "$ip" == *:* ]] && { echo "$ip"; return 0; }
fi done
fi fi
# IPv6 fallback: Use routing table with IPv6 targets # IPv6 fallback: Use routing table with IPv6 targets
+5 -6
View File
@@ -116,14 +116,14 @@ setting_up_container() {
(chown root:root / 2>/dev/null) || true (chown root:root / 2>/dev/null) || true
fi fi
local _host_ip=""
for ((i = RETRY_NUM; i > 0; i--)); do for ((i = RETRY_NUM; i > 0; i--)); do
if [ "$(hostname -I)" != "" ]; then _host_ip=$(hostname -I 2>/dev/null | awk '{print $1}')
break [[ -n "$_host_ip" ]] && break
fi
echo 1>&2 -en "${CROSS}${RD} No Network! " echo 1>&2 -en "${CROSS}${RD} No Network! "
sleep $RETRY_EVERY sleep $RETRY_EVERY
done done
if [ "$(hostname -I)" = "" ]; then if [[ -z "$_host_ip" ]]; then
echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}" echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
echo -e "${NETWORK}Check Network Settings" echo -e "${NETWORK}Check Network Settings"
exit 121 exit 121
@@ -131,8 +131,7 @@ setting_up_container() {
rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED
systemctl disable -q --now systemd-networkd-wait-online.service systemctl disable -q --now systemd-networkd-wait-online.service
msg_ok "Set up Container OS" msg_ok "Set up Container OS"
#msg_custom "${CM}" "${GN}" "Network Connected: ${BL}$(hostname -I)" msg_ok "Network Connected: ${BL}${_host_ip}"
msg_ok "Network Connected: ${BL}$(hostname -I)"
post_progress_to_api post_progress_to_api
} }