1#!/bin/sh 2#- 3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4# 5# Copyright (c) 2010 iXsystems, Inc. All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28# $FreeBSD$ 29 30# functions.sh 31# Library of functions which pc-sysinstall may call upon for parsing the config 32 33# which gets the value of a setting in the provided line 34get_value_from_string() 35{ 36 if [ -n "${1}" ] 37 then 38 export VAL="`echo ${1} | cut -d '=' -f 2-`" 39 else 40 echo "Error: Did we forgot to supply a string to parse?" 41 exit 1 42 fi 43}; 44 45# Get the value from the cfg file including spaces 46get_value_from_cfg_with_spaces() 47{ 48 if [ -n "${1}" ] 49 then 50 export VAL="`grep ^${1}= ${CFGF} | head -n 1 | cut -d '=' -f 2-`" 51 else 52 exit_err "Error: Did we forgot to supply a setting to grab?" 53 fi 54}; 55 56 57# Get the value from the cfg file 58get_value_from_cfg() 59{ 60 if [ -n "${1}" ] 61 then 62 export VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '` 63 else 64 exit_err "Error: Did we forgot to supply a setting to grab?" 65 fi 66}; 67 68# Checks the value of a setting in the provided line with supplied possibilities 69# 1 = setting we are checking, 2 = list of valid values 70if_check_value_exists() 71{ 72 if [ -n "${1}" -a -n "${2}" ] 73 then 74 # Get the first occurrence of the setting from the config, strip out whitespace 75 76 VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '` 77 if [ -z "${VAL}" ] 78 then 79 # This value doesn't exist, lets return 80 return 0 81 fi 82 83 84 VALID="1" 85 for i in ${2} 86 do 87 VAL=`echo "$VAL"|tr A-Z a-z` 88 if [ "$VAL" = "${i}" ] 89 then 90 VALID="0" 91 fi 92 done 93 if [ "$VALID" = "1" ] 94 then 95 exit_err "Error: ${1} is set to unknown value $VAL" 96 fi 97 else 98 exit_err "Error: Did we forgot to supply a string to parse and setting to grab?" 99 fi 100}; 101 102# Checks the value of a setting in the provided line with supplied possibilities 103# 1 = setting we are checking, 2 = list of valid values 104check_value() 105{ 106 if [ -n "${1}" -a -n "${2}" ] 107 then 108 # Get the first occurrence of the setting from the config, strip out whitespace 109 VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '` 110 VALID="1" 111 for i in ${2} 112 do 113 if [ "$VAL" = "${i}" ] 114 then 115 VALID="0" 116 fi 117 done 118 if [ "$VALID" = "1" ] 119 then 120 exit_err "Error: ${1} is set to unknown value $VAL" 121 fi 122 else 123 exit_err "Error: Did we forgot to supply a string to parse and setting to grab?" 124 fi 125}; 126 127# Checks for the presence of the supplied arguments in the config file 128# 1 = values to confirm exist 129file_sanity_check() 130{ 131 if [ -n "$CFGF" -a -n "$1" ] 132 then 133 for i in $1 134 do 135 grep -q "^${i}=" $CFGF 2>/dev/null 136 if [ $? -eq 0 ] 137 then 138 LN=`grep "^${i}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '` 139 if [ -z "${LN}" ] 140 then 141 echo "Error: Config fails sanity test! ${i}= is empty" 142 exit 1 143 fi 144 else 145 echo "Error: Config fails sanity test! Missing ${i}=" 146 exit 1 147 fi 148 done 149 else 150 echo "Error: Missing config file, and / or values to sanity check for!" 151 exit 1 152 fi 153}; 154 155 156# Function which merges the contents of a new config into the specified old one 157# Only works with <val>= type configurations 158merge_config() 159{ 160 OLDCFG="${1}" 161 NEWCFG="${2}" 162 FINALCFG="${3}" 163 164 # Copy our oldcfg to the new one, which will be used as basis 165 cp ${OLDCFG} ${FINALCFG} 166 167 # Remove blank lines from new file 168 cat ${NEWCFG} | sed '/^$/d' > ${FINALCFG}.tmp 169 170 # Set our marker if we've found any 171 FOUNDMERGE="NO" 172 173 while read newline 174 do 175 echo ${newline} | grep -q "^#" 2>/dev/null 176 if [ $? -ne 0 ] ; then 177 VAL="`echo ${newline} | cut -d '=' -f 1`" 178 cat ${OLDCFG} | grep -q ${VAL} 2>/dev/null 179 if [ $? -ne 0 ] ; then 180 if [ "${FOUNDMERGE}" = "NO" ] ; then 181 echo "" >> ${FINALCFG} 182 echo "# Auto-merged values from newer ${NEWCFG}" >> ${FINALCFG} 183 FOUNDMERGE="YES" 184 fi 185 echo "${newline}" >> ${FINALCFG} 186 fi 187 fi 188 done < ${FINALCFG}.tmp 189 rm ${FINALCFG}.tmp 190 191}; 192 193# Loop to check for a specified mount-point in a list 194check_for_mount() 195{ 196 MNTS="${1}" 197 FINDMNT="${2}" 198 199 # Check if we found a valid root partition 200 for CHECKMNT in `echo ${MNTS} | sed 's|,| |g'` 201 do 202 if [ "${CHECKMNT}" = "${FINDMNT}" ] ; then 203 return 0 204 fi 205 done 206 207 return 1 208}; 209 210# Function which returns the next line in the specified config file 211get_next_cfg_line() 212{ 213 CURFILE="$1" 214 CURLINE="$2" 215 216 FOUND="1" 217 218 while read line 219 do 220 if [ "$FOUND" = "0" ] ; then 221 export VAL="$line" 222 return 223 fi 224 if [ "$line" = "${CURLINE}" ] ; then 225 FOUND="0" 226 fi 227 done <${CURFILE} 228 229 # Got here, couldn't find this line or at end of file, set VAL to "" 230 export VAL="" 231}; 232