Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[installer] Fix variable inside machine.conf caused install.sh error #6600

Merged
merged 4 commits into from
Feb 4, 2021

Conversation

kuanyu99
Copy link
Contributor

- Why I did it

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

  1. Add a item inside /host/machine.conf like onie_disco_ntpsrv=10.254.141.10 10.254.141.131
  2. Do sonic_installer install to check if any error occurs

- Which release branch to backport (provide reason below if selected)

  • 201811
  • 201911
  • 202006
  • 202012

- Description for the changelog

Add a new function 'read_conf_file' to read the settings from machine.conf when the value in machine.conf are separated by space without double quotes between them.

- A picture of a cute animal (not mandatory but encouraged)

* When the value contains space in machine.conf and does not have
  double quote, "install.sh" can't correctly read it.

* "install.sh" uses dot command(i.e. source) to apply the settings
  in machine.conf. If the value in machine.conf are separated by space
  without double quotes between them, it will return failure when
  install.sh try to source machine.conf.

```
machine.conf example:
  ...
  onie_disco_ntpsrv=10.254.141.10 10.254.141.131
  ...

install.sh returns:
  ...
  /tmp/tmp.A7wHuAMSQV/installer/install.sh: 11: /host/machine.conf: 10.254.141.131: not found
```

* Add a new function 'read_conf_file' to read the settings from
  machine.conf when the value in machine.conf are separated by space
  without double quotes between them.
* Originally, the script will failed if the line of machine.conf
  is comment line or blank line. After this patch, it can correctly
  handle these type of content.
Copy link
Collaborator

@qiluo-msft qiluo-msft left a comment

Choose a reason for hiding this comment

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

As comments

local conf_file=$1
while IFS='=' read -r var value
do
f_char=$(echo $var | cut -c1)
Copy link
Collaborator

@qiluo-msft qiluo-msft Feb 1, 2021

Choose a reason for hiding this comment

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

Do you assume the # appear as first char of a line? If yes, can you assume it could appear anywhere on a line? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your suggestion is helpful. I modify this part to remove the string behind #.

tmp_val=${value#\"}
# remove double quote in the end
value=${tmp_val%\"}
eval "$var=\"$value\""
Copy link
Collaborator

@qiluo-msft qiluo-msft Feb 1, 2021

Choose a reason for hiding this comment

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

Is it possible to use declare instead of eval ? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not familiar with declare. What is the advantage to use declare instead of eval?

Copy link
Collaborator

Choose a reason for hiding this comment

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

declare is safer than eval. I know it in bash, not sure about this environment (sh).


In reply to: 568218112 [](ancestors = 568218112)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried it in sh environment. There is no declare command.

Handle following types of line
* Whole comment line
* Comment string behind declaration
* Comment string behind some spaces
* Blank line
@@ -20,6 +20,25 @@ _trap_push() {
}
_trap_push true

read_conf_file() {
Copy link
Collaborator

@qiluo-msft qiluo-msft Feb 2, 2021

Choose a reason for hiding this comment

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

read_conf_file [](start = 0, length = 14)

Could you write a unit test to cover old cases and your new feature? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. But what kind of the test will be better and suitable?
Add a test inside sonic-mgmt and be independent from building. Or, add additional process to test this during building.
I haven't found similar case of this type of unit test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

read_conf_file() is complex. I expect you have tested it locally. Could you just automate the test in another shell script or a python test runner.
In future we may either improve the code or the testcases.


In reply to: 568342160 [](ancestors = 568342160)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just copied the function to another script and test it with a sample machine.conf.
The test can only be triggered manually for now.

* To run this test, `cd` into the tests directory and `./test_read_conf.sh`

  If pass, it will return 0 and show "PASS".
  If fail, it will return 1 and show "[ERR] Expect value(x86_64) is not equal
  to input value()".
@lguohan lguohan merged commit cb70c66 into sonic-net:master Feb 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants