Skip to content

Commit

Permalink
[installer] Fix variable inside machine.conf caused install.sh error (#…
Browse files Browse the repository at this point in the history
…6600)

Encounter error when install SONiC image if there are some onie_discovery variables assigned with multiple values inside machine.conf

- How I did it

Replace original ". /machine.conf" method and add another function to do the same thing.

- How to verify it

Add a item inside /host/machine.conf like onie_disco_ntpsrv=10.254.141.10 10.254.141.131
Do sonic_installer install to check if any error occurs
  • Loading branch information
kuanyu99 authored Feb 4, 2021
1 parent 62a599a commit cb70c66
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
26 changes: 23 additions & 3 deletions installer/x86_64/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ _trap_push() {
}
_trap_push true

read_conf_file() {
local conf_file=$1
while IFS='=' read -r var value || [ -n "$var" ]
do
# remove newline character
var=$(echo $var | tr -d '\r\n')
value=$(echo $value | tr -d '\r\n')
# remove comment string
var=${var%#*}
value=${value%#*}
# skip blank line
[ -z "$var" ] && continue
# remove double quote in the beginning
tmp_val=${value#\"}
# remove double quote in the end
value=${tmp_val%\"}
eval "$var=\"$value\""
done < "$conf_file"
}

# Main
set -e
cd $(dirname $0)
Expand All @@ -37,7 +57,7 @@ else
fi

if [ -r ./machine.conf ]; then
. ./machine.conf
read_conf_file "./machine.conf"
fi

if [ -r ./onie-image.conf ]; then
Expand All @@ -54,9 +74,9 @@ fi

# get running machine from conf file
if [ -r /etc/machine.conf ]; then
. /etc/machine.conf
read_conf_file "/etc/machine.conf"
elif [ -r /host/machine.conf ]; then
. /host/machine.conf
read_conf_file "/host/machine.conf"
elif [ "$install_env" != "build" ]; then
echo "cannot find machine.conf"
exit 1
Expand Down
16 changes: 16 additions & 0 deletions installer/x86_64/tests/sample_machine.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# sample_machine.conf for onie
# A space in front of comment line
# One blank line below

onie_machine_rev=0
onie_arch=x86_64# some comment after declaration
# no value declaration
onie_config_version=
onie_build_date="2021-02-03T01:50+0800"
onie_partition_type=gpt
onie_disco_ntpsrv=192.168.0.1 192.168.0.2
onie_firmware=auto
# another blank line below

onie_skip_ethmgmt_macs=no
onie_grub_image_name=grubx64.efi
61 changes: 61 additions & 0 deletions installer/x86_64/tests/test_read_conf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
# This is a standalone test file to test read_conf_file function for
# some types of machine.conf file.
# The read_conf_file function is copy from the install.sh

MACHINE_CONF="sample_machine.conf"

read_conf_file() {
local conf_file=$1
while IFS='=' read -r var value || [ -n "$var" ]
do
# remove newline character
var=$(echo $var | tr -d '\r\n')
value=$(echo $value | tr -d '\r\n')
# remove comment string
var=${var%#*}
value=${value%#*}
# skip blank line
[ -z "$var" ] && continue
# remove double quote in the beginning
tmp_val=${value#\"}
# remove double quote in the end
value=${tmp_val%\"}
eval "$var=\"$value\""
done < "$conf_file"
}

TEST_CONF() {
input_value=$1
exp_value=$2
if [ "$input_value" != "$exp_value" ]; then
echo "[ERR] Expect value($exp_value) is not equal to input value($input_value)"
exit 1
fi
}

# define the expected variable value
exp_onie_machine_rev="0"
exp_onie_arch="x86_64"
exp_onie_build_date="2021-02-03T01:50+0800"
exp_onie_partition_type="gpt"
exp_onie_disco_ntpsrv="192.168.0.1 192.168.0.2"
exp_onie_firmware="auto"
exp_onie_skip_ethmgmt_macs="no"
exp_onie_grub_image_name="grubx64.efi"

# read the sample conf file
read_conf_file $MACHINE_CONF

# check each variable and its expected value
TEST_CONF "$onie_machine_rev" "$exp_onie_machine_rev"
TEST_CONF "$onie_arch" "$exp_onie_arch"
TEST_CONF "$onie_build_date" "$exp_onie_build_date"
TEST_CONF "$onie_partition_type" "$exp_onie_partition_type"
TEST_CONF "$onie_disco_ntpsrv" "$exp_onie_disco_ntpsrv"
TEST_CONF "$onie_firmware" "$exp_onie_firmware"
TEST_CONF "$onie_skip_ethmgmt_macs" "$exp_onie_skip_ethmgmt_macs"
TEST_CONF "$onie_grub_image_name" "$exp_onie_grub_image_name"

echo "PASS!!"
exit 0

1 comment on commit cb70c66

@Eric-Zaluzec-Vertiv
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I encountered same issue when onie_disco_dns in /host/machine.conf has multiple variables. Does this fix apply to all fields in /host/machine.conf?
Thanks.

sudo sonic_installer install /home/admin/updateimage/sonic-broadcom-20210102.bin -y

    Installing image SONiC-OS-master.541-dirty-20210102.080340 and setting it as default...
    Command: bash /home/admin/updateimage/sonic-broadcom-20210102.bin
    /tmp/tmp.CsIEe09dQd/installer/install.sh: 11: /host/machine.conf: 10.146.3.200: not found
onie_disco_dns=10.207.15.201 10.146.3.200 10.200.203.200

Please sign in to comment.