3 # Copyright (c) 2010 iXsystems, Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
8 # 1. Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 # Library of functions which pc-sysinstall may call upon for parsing the config
31 # which gets the value of a setting in the provided line
32 get_value_from_string()
36 export VAL="`echo ${1} | cut -d '=' -f 2-`"
38 echo "Error: Did we forgot to supply a string to parse?"
43 # Get the value from the cfg file including spaces
44 get_value_from_cfg_with_spaces()
48 export VAL="`grep ^${1}= ${CFGF} | head -n 1 | cut -d '=' -f 2-`"
50 exit_err "Error: Did we forgot to supply a setting to grab?"
55 # Get the value from the cfg file
60 export VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
62 exit_err "Error: Did we forgot to supply a setting to grab?"
66 # Checks the value of a setting in the provided line with supplied possibilities
67 # 1 = setting we are checking, 2 = list of valid values
68 if_check_value_exists()
70 if [ -n "${1}" -a -n "${2}" ]
72 # Get the first occurrence of the setting from the config, strip out whitespace
74 VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
77 # This value doesn't exist, lets return
85 VAL=`echo "$VAL"|tr A-Z a-z`
86 if [ "$VAL" = "${i}" ]
93 exit_err "Error: ${1} is set to unknown value $VAL"
96 exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
100 # Checks the value of a setting in the provided line with supplied possibilities
101 # 1 = setting we are checking, 2 = list of valid values
104 if [ -n "${1}" -a -n "${2}" ]
106 # Get the first occurrence of the setting from the config, strip out whitespace
107 VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
111 if [ "$VAL" = "${i}" ]
116 if [ "$VALID" = "1" ]
118 exit_err "Error: ${1} is set to unknown value $VAL"
121 exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
125 # Checks for the presence of the supplied arguments in the config file
126 # 1 = values to confirm exist
129 if [ -n "$CFGF" -a -n "$1" ]
133 grep -q "^${i}=" $CFGF 2>/dev/null
136 LN=`grep "^${i}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
139 echo "Error: Config fails sanity test! ${i}= is empty"
143 echo "Error: Config fails sanity test! Missing ${i}="
148 echo "Error: Missing config file, and / or values to sanity check for!"
154 # Function which merges the contents of a new config into the specified old one
155 # Only works with <val>= type configurations
162 # Copy our oldcfg to the new one, which will be used as basis
163 cp ${OLDCFG} ${FINALCFG}
165 # Remove blank lines from new file
166 cat ${NEWCFG} | sed '/^$/d' > ${FINALCFG}.tmp
168 # Set our marker if we've found any
173 echo ${newline} | grep -q "^#" 2>/dev/null
174 if [ $? -ne 0 ] ; then
175 VAL="`echo ${newline} | cut -d '=' -f 1`"
176 cat ${OLDCFG} | grep -q ${VAL} 2>/dev/null
177 if [ $? -ne 0 ] ; then
178 if [ "${FOUNDMERGE}" = "NO" ] ; then
179 echo "" >> ${FINALCFG}
180 echo "# Auto-merged values from newer ${NEWCFG}" >> ${FINALCFG}
183 echo "${newline}" >> ${FINALCFG}
186 done < ${FINALCFG}.tmp
191 # Loop to check for a specified mount-point in a list
197 # Check if we found a valid root partition
198 for CHECKMNT in `echo ${MNTS} | sed 's|,| |g'`
200 if [ "${CHECKMNT}" = "${FINDMNT}" ] ; then
208 # Function which returns the next line in the specified config file
218 if [ "$FOUND" = "0" ] ; then
222 if [ "$line" = "${CURLINE}" ] ; then
227 # Got here, couldn't find this line or at end of file, set VAL to ""