I am writing an installation automation BASH script. I'd like to install the nvme-cli package only if an NVMe SSD is actually present.
I couldn't find a simple way to query the hardware (say, using lshw) to get a kind-of-yes-or-no answer.
Any of you can give a way to extract the specific answer from within a BASH script?
I'd like to use the case conditioning to either install it or not.
6 Answers
A directory test is sufficient:
(the path /sys/class/nvme is created by the module nvme. However, this does not say anything about the operational state of the drive(s) or even if a drive exists, though).
#!/bin/bash
if [[ -d /sys/class/nvme ]]; then
apt-get -y install nvme-cli
fi
Or, if you prefer using globbing to check for the existence of a file(s), a for-loop will have the advantage of avoiding a subshell:
shopt -s nullglob
for _ in /dev/nvme*; do
apt-get -y install nvme-cli; break
done
Also, if you can, you should avoid globbing as an argument, where there are alternatives, e.g., in this case, the use of echo or printf is preferable (which is, in reality, a loop construct).
shopt -s nullglob
if [[ -n $(echo /dev/nvme*) ]]; then
apt-get -y install nvme-cli
fi
dmesg | grep -q nvme
if [ $? = 0 ] ; then
apt install nvme-cli -y
fi
All of these solutions should work about the same; they're all testing to see if the kernel has found a NVME device in any way.
If the installer runs at boot, this is probably adequate. If the installer might run long after boot, then the first line would be better as:
grep -q nvme /var/log/dmesg
- 719
Check to see if any nvme device files exist:
if ls /dev/nvme*
then
echo "Install NVME-CLI"
fi
- 5,533
Try using lsscsi:
NVMECOUNT=`lsscsi | grep nvme | wc -l`
if [ $NVMECOUNT -gt 1 ]
then
echo "Install NVME-CLI"
fi
- 182
Check if the nvme driver is loaded
if grep -q nvme /proc/devices
then
echo "Install NVME-CLI"
fi
- 5,533
In some cases, free space is an issue, besides, i am a firm believer in not installing redundant packaged as a rule, so, eventually I went with:
NVMECOUNT=`lsblk | grep nvme | wc -l`
if [ $NVMECOUNT -gt 1 ]
then
apt -y install nvme-cli
fi
Thanks!
- 1,498