0

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.

2
  • 1
    IIRC SD card in Pi slot is always /dev/mmcblk* and USB connected drive is /dev/sd* (not necessarily sda). SD card in USB SD card reader will also show up as /dev/sd*. Look at drive mounted on /boot with e.g. lsblk
    – Dirk
    Oct 13, 2021 at 9:37
  • 1
    Why does it matter? What can you possibly do that's useful with that info?
    – Dougie
    Oct 13, 2021 at 10:00

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.