I am writing a bash script and I really want a way to detect if the user has booted from a microSD card versus an external SSD drive connected via USB. Ideally, this should work with any PI3/4. I am not that familiar with how linux drives are named/labelled. Currently, I am booting my Pi from USB SSD on my Pi4, and lsblk, reports that sda1 contains the firmware, and sda2 the main partition. Can I assume that sda will always be an external USB drive, or will microSD cards use sda as well? If not, what is the best way to tell them apart? Ideally, I would prefer to use a built-in Linux command to establish this (if possible) so my script doesn't need to install any new packages. Thanks in advance.
Asked
Active
Viewed 271 times
1 Answers
1
How generic does your script need to be? Are you going to support non-standard partition configurations, encrypted partitions, overlays, LVM and such? If not, you can trivially run
`mount | grep 'on / '| cut -d' ' -f1`
This will substitute to /dev/mmcblk0p2 (in general, /dev/mmcblk0pX) if you boot from the built-in SD card reader, or /dev/sda2 (in general, /dev/sdXY) if you boot from a USB storage, be it an SD card in a USB card reader, an SSD or an HDD. I think the current Pi bootloader will fail to boot from anything but sda anyway.
You can do the same with lsblk if you prefer:
`lsblk -o pkname,mountpoint | egrep '/$' | cut -d' ' -f1`
This returns either mmcblk0 or sda, which is perhaps easier to parse.
Dmitry Grigoryev
- 28,277
- 6
- 54
- 147