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

Read /usr/lib/os-release as fallback for /etc/os-release #351

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

hyperupcall
Copy link
Contributor

Closes #345

@hyperupcall hyperupcall changed the title Fallback to reading os-release in /usr/lib if missing one in /etc Read /usr/lib/os-release as fallback for /etc/os-release Oct 14, 2023
Copy link
Collaborator

@evelikov evelikov left a comment

Choose a reason for hiding this comment

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

Welcome and thank you for the MR. Can you please add a small test or two in run_test.sh?

  • move /etc/os-release -> dkms should still work - restore file
  • move both files -> dkms should error -> restore both

Thanks again

dkms.in Outdated Show resolved Hide resolved
@hyperupcall
Copy link
Contributor Author

hyperupcall commented Oct 16, 2023

Sounds good! I'll add the tests and other changes within the next few days

@hyperupcall hyperupcall force-pushed the hyperupcall-use-usrlib-fallback branch 2 times, most recently from 838142e to c8547d0 Compare October 23, 2023 12:59
@hyperupcall hyperupcall force-pushed the hyperupcall-use-usrlib-fallback branch 7 times, most recently from 2020525 to 4ffa230 Compare October 23, 2023 13:31
@hyperupcall
Copy link
Contributor Author

Added the tests and fixed the nit!

run_test.sh Show resolved Hide resolved
run_test.sh Outdated Show resolved Hide resolved
run_test.sh Outdated
EOF

echo "Creating /etc/os-release"
cat > /etc/os-release <<< "$osrelease_contents"
Copy link
Collaborator

@evelikov evelikov Oct 24, 2023

Choose a reason for hiding this comment

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

I don't fully follow why we need the osrelease_contents. Can't we just mv cp the file back?
The trap is already smart enough to handle that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right! After all these years, my misunderstanding of cp was: if it encountered a symlink to copy, it would copy the symlink, not the backing target the symlink resolves to. I have change it to use cp :)

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 have pushed changes that fixes this. It would be nice to either just copy _etc-os-release or _usrlib-os-release instead of creating a new, _os-release file, but if /etc/os-release and /usr/lib/os-release are both relative symlinks, then this will fail. Because at the point where we need to copy, say, _etc-os-release back to /etc/os-release, the symlink will no longer properly resolve because it is in a different directory.

run_test.sh Outdated Show resolved Hide resolved
@@ -2320,7 +2325,7 @@ unset CC CXX CFLAGS CXXFLAGS LDFLAGS
# Set important variables
current_kernel=$(uname -r)
current_os=$(uname -s)
running_distribution=$(distro_version)
running_distribution=$(distro_version) || exit
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems quite odd, why do we need it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

distro_version has code that is responsible for exiting the program on error (die 4 ...). Beacuse this code is running in a subshell, the die does not exit the program, but only exits the subshell. This means that the command running_distribution=$(distro_version) only yields a non-zero exit code. Since errexit is not set, this means that the next commands are ran (not exiting).

This is not what we want because we wanted the die to actually exit the program, not just the subshell. So we add the || exit to ensure our program is actually exited. (we want to run the exit out of the subshell)

Copy link
Contributor Author

@hyperupcall hyperupcall Nov 3, 2023

Choose a reason for hiding this comment

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

It seems this wasn't done before, which means that if /etc/os-release wasn't previously present, then the error would show up on screen, and the rest of the script would run, perhaps doing mysterious and myschevious things to the user's system.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can i request splitting the change into separate commit (as part of this PR) and adding some/all of the above analysis in the commit message.

@evelikov
Copy link
Collaborator

Apart from the comments (most notably the || true one), the test looks good. Please squash the second "nit" commit into the original.

@evelikov
Copy link
Collaborator

@hyperupcall did you have the chance to respin this PR and address the comments?

@hyperupcall
Copy link
Contributor Author

hyperupcall commented Dec 1, 2023

Yes! I'll have this done within the next several hours. My apologies for the wait

@hyperupcall hyperupcall force-pushed the hyperupcall-use-usrlib-fallback branch from c880984 to 155a850 Compare December 2, 2023 02:34
`distro_version` contains code that can run `exit`, which is supposed to
exit the whole process. However, `distro_version` runs in a subshell.
By default in Bash, this means that `exit` will only exit the subshell, and set `?`
to that exit code. The rest of script will execute, despite the error.

There are two ways to fix this:
- Enable the POSIX shell option `errexit` for the entire script
- Manually check for non-zero exit code and "cascade the error"

The latter was chosen because it is a localized change that does not
affect the behavior of the entire script.
Copy link
Collaborator

@evelikov evelikov left a comment

Choose a reason for hiding this comment

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

There's nothing to apologise for - stuff slips through the cracks.

Updated PR looks great. Thanks again o/

@evelikov evelikov merged commit 98fd2eb into dell:master Dec 4, 2023
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fallback to /usr/lib/os-release if /etc one is missing
2 participants