1

Contrary to this question, I would like to know which (physical) disks currently do not have any partitions or LVs mounted. This is for a script, which shall return disks where none of their partitions or LVs are currently mounted in the system.

Is there a (fairly) simple way to find that out using command-line tools (ideally using a mix of bash built-ins, grep, lsblk and sed, but avoiding "awkward" programming or highly specialized 3rd party programs if possible).

EDIT (for clarification):

I am looking for a way to get "/dev/sdc" and/or "/dev/sdd" if not a single partition (e.g. none of /dev/sdc{1,2,3,4}) or LV (e.g. /dev/mapper/some_VG_name-LV_name) is currently mounted on the system. If even a single partition or LV is mounted, the disk shall not be returned.

4
  • lsblk command? Commented Dec 31, 2019 at 14:22
  • Yes. lsblk is possibly one good source (as mentioned in the question itself). However, finding the right switches to return precisely what I am looking for is currently not so successful. Using lsblk --list --paths will detach any LVs from their drives as they are listed as /dev/mapper/... instead of /dev/sd?/... Keep in mind that a script shall parse it. Not a human eye. ;-)
    – Phoenix
    Commented Dec 31, 2019 at 14:26
  • 1
    You are looking for attached devices that are NOT mounted. Can you change the headline of your question?
    – UncaAlby
    Commented Dec 31, 2019 at 16:53
  • Ok. Done. Thanks for making me aware. Though it would be fine either way. If I go through disks to find partitions or logical volumes attached, I could just revert the results to get what I need. In essence I would need to get the disks, however, and not all unmounted partitions.
    – Phoenix
    Commented Dec 31, 2019 at 16:57

1 Answer 1

2

Found the solution, which works for me:

aAllDisks=($(/usr/bin/lsblk --nodeps --noheadings --output NAME --paths))
aFreeDisks=()
for sDisk in "${aAllDisks[@]}"; do
    sMounts="$(/usr/bin/lsblk --noheadings --output MOUNTPOINT "${sDisk}" | grep -vE "^$")"
    if [ "${sMounts}" == "" ]; then
        aFreeDisks+=("${sDisk}")
    fi
done
echo "${aFreeDisks[@]}"

This will first get all installed physical disks and then loop through them. If a disk does not have any mounts, it will be gathered for later use. In the above example, I simply echo it out.

3
  • It should be noted that despite its name and despite it not being mentioned anywhere in its manual page, lsblk will also list lvm2 volumes, luks partitions and so on.
    – josch
    Commented Jun 19, 2022 at 10:49
  • this doesn't work. It shows /dev/loop* in my case for example, because snap uses loopback devices a lot. Having a mount point doesn't mean that the device is a disk or partition
    – phuclv
    Commented Jun 6 at 13:54
  • For all intent and purpose it does work. I tested it back in 2019 as well as now once again (for you). Even loopback devices (depending on their contents) can include partitions/LVs. If you do not like the idea of having loopback (or any other) devices included, have a closer look at the default output of lsblk as well as its help (lsblk --help). You can exclude major block device IDs as simple as adding --exclude ID[,ID...] (even only including the ones you are after is possible using --include ID[,ID...]). Besides the given snippet, you can also filter out the result. Up to you.
    – Phoenix
    Commented Jun 6 at 14:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .