From: Frank Brehm Date: Thu, 23 Nov 2023 15:21:50 +0000 (+0100) Subject: Adding postinstall-scripts/update-networkmanager X-Git-Tag: 1.8.0^2~4 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=429a70b41d453470d312fea39518e9d7f961db57;p=pixelpark%2Fcreate-terraform.git Adding postinstall-scripts/update-networkmanager --- diff --git a/postinstall-scripts/update-networkmanager b/postinstall-scripts/update-networkmanager new file mode 100644 index 0000000..48c91b4 --- /dev/null +++ b/postinstall-scripts/update-networkmanager @@ -0,0 +1,198 @@ +#!/bin/bash + +set -e +set -u + +BASE_NAME="$( basename ${0} )" +MY_REAL_NAME=$( readlink -f $0 ) +BIN_DIR=$( dirname "${MY_REAL_NAME}" ) +BASE_DIR=$( dirname "${BIN_DIR}" ) + +if [[ -f "${BIN_DIR}/functions.rc" ]] ; then + . "${BIN_DIR}/functions.rc" +else + echo "Bash resource file '${BIN_DIR}/functions.rc' not found" >&2 + exit 5 +fi + +FQDN=$( hostname -f ) +NM_CFG_DIR='/etc/NetworkManager/system-connections' + +detect_color + +#------------------------------------------------------------------------------ +DESCRIPTION=$( cat <<-EOF + Generate a valid configuration for the NetworkManager. + + EOF +) + +#------------------------------------------------------------------------------ +usage() { + + cat <<-EOF + Usage: ${BASE_NAME} ${STD_USAGE} + ${BASE_NAME} [-h|--help] + ${BASE_NAME} [-V|--version] + + Options: + EOF + + echo "${STD_USAGE_MSG}" +} + +#------------------------------------------------------------------------------ +get_options() { + + local tmp= + local short_options="${STD_SHORT_OPTIONS}" + local long_options="${STD_LONG_OPTIONS}" + + set +e + tmp=$( getopt -o "${short_options}" --long "${long_options}" -n "${BASE_NAME}" -- "$@" ) + if [[ $? != 0 ]] ; then + echo "" >&2 + usage >&2 + exit 1 + fi + set -e + + # Note the quotes around `$TEMP': they are essential! + eval set -- "${tmp}" + eval_common_options "$@" + if [[ "${DEBUG}" == 'y' ]] ; then + declare -p REMAINING_OPTS + declare -p REMAINING_ARGS + fi + + local len="${#REMAINING_OPTS[*]}" + local i="0" + local j= + local arg= + while [[ "$i" -lt "${len}" ]] ; do + + arg="${REMAINING_OPTS[$i]}" + + case "${arg}" in + *) echo -e "Internal error - option '${RED}${arg}${NORMAL} was wrong!" + exit 1 + ;; + esac + done + +} + +#------------------------------------------------------------------------------ +get_primary_device() { + + local primary_device=$( facter networking.primary ) + + if [[ -z "${primary_device}" ]] ; then + error "Did not found the primary networking device" + exit 5 + fi + + debug "Primary networking device is: '${CYAN}${primary_device}${NORMAL}'." + echo "${primary_device}" + +} + +#------------------------------------------------------------------------------ +generate_nm_file() { + + local primary_device=$( get_primary_device ) + local dev_basename="${primary_device}.nmconnection" + local dev_file="${NM_CFG_DIR}/${dev_basename}" + local if_uuid=$( uuidgen -r ) + local ip_primary=$( ip -br -o address show dev "${primary_device}" | awk '{print $3}' ) + local default_gateway=$( ip -br -o route show | grep ^default | awk '{print $3}' ) + + local -a nameservers=() + local ns + for ns in $(cat /etc/resolv.conf | grep '^nameserver' | awk '{print $2}' ) ; do + nameservers+=( "${ns}" ) + done + local ns_list="" + for ns in "${nameservers[@]}" ; do + ns_list+="${ns};" + done + + local -a search_domains=() + local domain + for domain in $(cat /etc/resolv.conf | grep '^search' | sed -e 's/^search[ ][ ]*//' ) ; do + search_domains+=( "${domain}" ) + done + if [[ "${#search_domains[*]}" == "0" ]] ; then + search_domains=( "pixelpark.net" "pixelpark.com" ) + fi + local domain_list="" + for domain in "${search_domains[@]}" ; do + domain_list+="${domain};" + done + + local dns_options="timeout:1;attempts:1;" + if grep -q '^options' /etc/resolv.conf ; then + dns_options=$( grep '^options' /etc/resolv.conf | \ + sed -e 's/^options[ ][ ]*//' -e 's/[ ][ ]*/;/g' ) + dns_options+=';' + fi + + local file_content=$( cat <<-EOF + [connection] + id=${primary_device} + uuid=${if_uuid} + type=ethernet + interface-name=${primary_device} + + [ethernet] + + [ipv4] + method=manual + address1=${ip_primary},${default_gateway} + dns=${ns_list} + dns-options=${dns_options} + dns-search=${domain_list} + + [ipv6] + addr-gen-mode=eui64 + method=disabled + + [proxy] + EOF + ) + + info "Writing '${CYAN}${dev_file}${NORMAL}' ..." + debug "Generated content of '${CYAN}${dev_file}${NORMAL}':\n${file_content}" + + if [[ "${SIMULATE}" != "y" ]] ; then + echo -n "${file_content}" > "${dev_file}" + fi + +} + +################################################################################ +## +## Main +## +################################################################################ + +#------------------------------------------------------------------------------ +main() { + + get_options "$@" + set_locale "en_US.utf8" + + empty_line + info "Trying to fix ${CYAN}NetworkManager config${NORMAL} ..." + empty_line + + check_for_root + generate_nm_file + +} + +main "$@" + +exit 0 + +# vim: list