devsetup1-4 Exercise 3: A gpucheck Function ============================================= THE FUNCTION (add to ~/.bashrc) ------------------------------- gpucheck() { local ok=0 echo "Running kernel: $(uname -r)" echo "DKMS (nvidia):" if dkms status 2>/dev/null | grep -i nvidia; then : else echo " (no nvidia entry)" ok=1 fi if lsmod | grep -q '^nvidia'; then echo "Module loaded: yes" else echo "Module loaded: NO" ok=1 fi if nvidia-smi >/dev/null 2>&1; then echo "nvidia-smi: works" else echo "nvidia-smi: FAILED" ok=1 fi return "$ok" } USING IT -------- source ~/.bashrc gpucheck echo "status: $?" A healthy machine prints the kernel, an nvidia line from dkms status that includes the word "installed", "Module loaded: yes", "nvidia-smi: works", and "status: 0". Any failure sets the return status to 1, so it also works in scripts: gpucheck || echo "fix the GPU before continuing". HOW IT WORKS ------------ - dkms status prints one line per module and kernel. grep -i nvidia both shows the lines and tells the if whether there were any. - lsmod lists loaded kernel modules; the nvidia module's name starts with "nvidia". - nvidia-smi exits non-zero if it cannot talk to the driver, so redirecting its output away and testing the status is enough. - The function keeps going after a failure and reports everything, rather than stopping at the first problem, so one run shows the whole picture. WHEN TO RUN IT -------------- 1. Right after installing the driver and rebooting (proves it worked). 2. After every apt full-upgrade that installed a new kernel, BEFORE rebooting. At that point dkms status should show an nvidia entry marked installed for the NEW kernel. The "Module loaded" and nvidia-smi lines will describe the kernel you are still running, which is fine; the dkms line is the one that matters at this stage. 3. Again after the reboot, to confirm the new kernel really works. If step 2 shows the new kernel has no installed nvidia entry, do not reboot into it. Your old kernel is still installed and still works. NOTE ---- This is a health check, not a repair tool. It tells you that something is wrong and roughly where; the troubleshooting table in the chapter tells you what to do about it. WHY THIS WORKS AS AN ANSWER --------------------------- It turns the chapter's advice ("check dkms status before rebooting") into a single command you will actually remember to run, with a return status so other scripts (such as the Chapter 10 provisioning script) can use it.