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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions dkms.in
Original file line number Diff line number Diff line change
Expand Up @@ -329,18 +329,23 @@ do_depmod()
# Grab distro information from os-release.
distro_version()
{
[[ -r /etc/os-release ]] || die 4 $"System is missing /etc/os-release file."
(
. /etc/os-release
if [[ "$ID" = "ubuntu" ]]; then
# ID_LIKE=debian in ubuntu
echo $ID
elif [[ ${#ID_LIKE[@]} != 0 ]]; then
echo ${ID_LIKE[0]}
else
echo $ID
for f in /etc/os-release /usr/lib/os-release; do
if [[ -r $f ]]; then
(
. "$f"
if [[ "$ID" = "ubuntu" ]]; then
# ID_LIKE=debian in ubuntu
echo $ID
elif [[ ${#ID_LIKE[@]} != 0 ]]; then
echo ${ID_LIKE[0]}
else
echo $ID
fi
)
return
fi
)
done
die 4 $"System is missing os-release file."
}

override_dest_module_location()
Expand Down Expand Up @@ -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.

dkms_tree="/var/lib/dkms"
source_tree="/usr/src"
install_tree="/lib/modules"
Expand Down
71 changes: 71 additions & 0 deletions run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,77 @@ EOF
echo 'Removing /usr/src/dkms_build_exclusive_test-1.0'
rm -r /usr/src/dkms_build_exclusive_test-1.0

############################################################################
### Testing os-release detection ###
############################################################################
echo "Backing up /etc/os-release and /usr/bin/os-release"
osrelease_cleanup() {
rm -f _os-release
mv _etc-os-release /etc/os-release &>/dev/null || :
mv _usrlib-os-release /usr/lib/os-release &>/dev/null || :
}
trap osrelease_cleanup EXIT
hyperupcall marked this conversation as resolved.
Show resolved Hide resolved
for f in /etc/os-release /usr/lib/os-release; do
if [ -f "$f" ]; then
cp -f "$f" _os-release
break
fi
done
[ -f _os-release ] || { echo >&2 "Error: file os-release not found"; exit 1; }
mv /etc/os-release _etc-os-release &>/dev/null || :
mv /usr/lib/os-release _usrlib-os-release &>/dev/null || :

echo "Adding the dkms_test-1.0 module with no os-release files (expected error)"
run_with_expected_error 4 dkms add test/dkms_test-1.0 << EOF
Error! System is missing os-release file.
EOF

echo "Creating /etc/os-release"
cp -f _os-release /etc/os-release
echo "Adding the dkms_test-1.0 module with file /etc/os-release"
run_with_expected_output dkms add test/dkms_test-1.0 << EOF
Creating symlink /var/lib/dkms/dkms_test/1.0/source -> /usr/src/dkms_test-1.0
EOF
run_status_with_expected_output 'dkms_test' << EOF
dkms_test/1.0: added
EOF

echo 'Removing dkms_test module'
run_with_expected_output dkms remove -k "${KERNEL_VER}" -m dkms_test -v 1.0 << EOF
Module dkms_test 1.0 is not installed for kernel ${KERNEL_VER} (${KERNEL_ARCH}). Skipping...
Module dkms_test 1.0 is not built for kernel ${KERNEL_VER} (${KERNEL_ARCH}). Skipping...
Deleting module dkms_test-1.0 completely from the DKMS tree.
EOF
echo "Removing /usr/src/dkms_test-1.0"
rm -r /usr/src/dkms_test-1.0
echo "Deleting /etc/os-release"
rm -f /etc/os-release

echo "Creating /usr/lib/os-release"
cp -f _os-release /etc/os-release
echo "Adding the dkms_test-1.0 module with file /usr/lib/os-release"
run_with_expected_output dkms add test/dkms_test-1.0 << EOF
Creating symlink /var/lib/dkms/dkms_test/1.0/source -> /usr/src/dkms_test-1.0
EOF
run_status_with_expected_output 'dkms_test' << EOF
dkms_test/1.0: added
EOF

echo 'Removing dkms_test module'
run_with_expected_output dkms remove -k "${KERNEL_VER}" -m dkms_test -v 1.0 << EOF
Module dkms_test 1.0 is not installed for kernel ${KERNEL_VER} (${KERNEL_ARCH}). Skipping...
Module dkms_test 1.0 is not built for kernel ${KERNEL_VER} (${KERNEL_ARCH}). Skipping...
Deleting module dkms_test-1.0 completely from the DKMS tree.
EOF
echo "Removing /usr/src/dkms_test-1.0"
rm -r /usr/src/dkms_test-1.0
echo "Deleting /usr/lib/os-release"
rm -f /usr/lib/os-release

echo "Restoring /etc/os-release and /usr/bin/os-release"
osrelease_cleanup
trap - EXIT

echo 'Checking that the environment is clean again'
check_no_dkms_test

Expand Down