From 18a9b144be17afca1a5bdb9d1142e6dbf201d8a7 Mon Sep 17 00:00:00 2001 From: zhangqiang Date: Mon, 7 Nov 2022 07:14:11 +0800 Subject: [PATCH] Incorrect processing of empty files for resolv.conf When resolv.conf is empty or does not exist,ifup-post backup files that do not match expectations. --- network-scripts/ifup | 2 ++ network-scripts/network-functions | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/network-scripts/ifup b/network-scripts/ifup index a053077..8871526 100755 --- a/network-scripts/ifup +++ b/network-scripts/ifup @@ -50,6 +50,8 @@ if [ ${UID} != 0 ]; then exit 1 fi +init_resolv_conf + source_config if ! is_true ${DEPRECATION_WARNING_ISSUED}; then diff --git a/network-scripts/network-functions b/network-scripts/network-functions index eed3c91..83e4bea 100644 --- a/network-scripts/network-functions +++ b/network-scripts/network-functions @@ -633,6 +633,29 @@ is_bonding_device () [ -f "/sys/class/net/$1/bonding/slaves" ] } +# Initialize /etc/resolv.conf when the network service starts +init_resolv_conf () +{ + title="# Generated by network" + if [ ! -f /etc/resolv.conf ];then + echo ${title} > /etc/resolv.conf + return 0 + fi + #If the file exists and the content is empty or there are empty lines, the ifup-post condition will be judged wrongly, + #and the backup process will be started. Add a comment at the first line here to solve the problem + if ! grep -E -q "${title}" /etc/resolv.conf;then + if tmp_file=$(mktemp);then + echo ${title} > ${tmp_file} + cat /etc/resolv.conf >> ${tmp_file} + cp -rf ${tmp_file} /etc/resolv.conf + /usr/bin/logger -p local7.notice -t "NET" -i "$0 : updated ${title} to /etc/resolv.conf" + rm -rf ${tmp_file} + else + /usr/bin/logger -p local7.err -t "NET" -i "$0 : /etc/resolv.conf was not updated ${title}: failed to create temporary file" + fi + fi +} + # Invoke this when /etc/resolv.conf has changed: change_resolv_conf () { -- 2.27.0