3

I use LUKS (so my system is encrypted) and LVM.

I don’t want sda displayed because that’s where my root filesystem lives, and it’s my root device. The solution should also work for systems without LUKS or LVM, meaning it must handle all scenarios.

I'm on a Debian 12.x and I want to display all main devices in a one row command, if possible, except my root device, regardless of whether it's an LVM, LUKS or not, etc.

The command should be flexible, and I don’t want to use fixed specifications *--vg-root, etc., if possible.

@bertieb comment: From the added output, it seems the root mountpoint is in a volume group in an encrypted volume on the device is presenting itself as sda -- OP wants that entire device and all associated block 'devices' elided ? i.e. remove root vg AND swap, crypt, containing partition (sda3) plus other partitions, and finally the device (sda)

And that should be my result, so all main devices without sda because that’s my root device where my root filesystem is /, and without me having to specify details, so I can use it on other devices/systems as well, whether LVM, LUKS, etc., without needing to know what root is called, or what the UUID is, etc.

I don't want to see the partitions too, only the main devices without root device, like in this command lsblk -d -o NAME,SIZE --sort SIZE.

This is the output I need:

  • No sda (since it's the root device containing my root filesystem /)

  • Only main devices displayed (regardless of how many exist)

  • No partitions included from the other devices

RESULT I NEED :
NAME     SIZE
nvme0n1  238.5G

My Setup:

$ lsblk -d -o NAME,SIZE --sort SIZE
NAME      SIZE
sda      57,3G
nvme0n1 238,5G
$ lsblk -fs -o NAME
# ROOT DEVICE
sda1
`-sda
sda2
`-sda
desktop--vg-root
`-sdb3_crypt
  `-sda3
    `-sda
...
...
$ lsblk
NAME                 MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
sda                    8:0    1  57,3G  0 disk  
├─sda1                 8:1    1   512M  0 part  /boot/efi
├─sda2                 8:2    1   488M  0 part  /boot
└─sda3                 8:3    1  56,3G  0 part  
  └─sdb3_crypt       254:0    0  56,3G  0 crypt 
    ├─desktop--vg-root   254:1    0  55,3G  0 lvm   /
    └─desktop--vg-swap_1 254:2    0   980M  0 lvm   [SWAP]
nvme0n1              259:0    0 238,5G  0 disk  
└─nvme0n1p1          259:1    0 238,5G  0 part  /user/backup

I don't want that, for example /dev/mapper/desktop--vg-root,sdb3_crypt, sda3 no sda.

But rather this here from the example sda, this is the root device, so I don't need this device in the output.

MY DEVICES, MY SETUP:

NAME           SIZE      
sda            57.3G     
|-sda1         512M      
|-sda2         488M      
`-sda3         56.3G     
`-sdb3_crypt   56.3G     
`-desktop--vg-swap_1980M      
nvme0n1        238.5G    
`-nvme0n1p1    238.5G 

RESULT I NEED:

NAME     SIZE
nvme0n1  238.5G
System setup Debian, lsblk and other information:
Operating System: Debian GNU/Linux 12 (bookworm)  
Kernel: Linux 6.1.0-22-amd64
Architecture: x86-64

lsblk from util-linux 2.38.1

$ apt-get install --only-upgrade util-linux

util-linux is already the newest version (2.38.1-5+deb12u3)
From @terdon RESULT:

mylsblk(){ lsblk -o NAME,SIZE,MOUNTPOINT | awk '$NF!="/"{printf "%-15s%-10s\n", $1,$2}'; }

NAME           SIZE      
sda            57.3G     
|-sda1         512M      
|-sda2         488M      
`-sda3         56.3G     
`-sdb3_crypt   56.3G     
`-desktop--vg-swap_1980M      
nvme0n1        238.5G    
`-nvme0n1p1    238.5G 

$ lsblk -o NAME,SIZE,MOUNTPOINT

NAME                   SIZE MOUNTPOINT
sda                   57.3G 
|-sda1                 512M /boot/efi
|-sda2                 488M /boot
`-sda3                56.3G 
  `-sdb3_crypt        56.3G 
    |-desktop--vg-root    55.3G /
    `-desktop--vg-swap_1  980M [SWAP]
nvme0n1              238.5G 
$ lsblk -o NAME,SIZE,MOUNTPOINT | awk '$NF!="/"{printf "%-15s%-10s\n", $1,$2}';
NAME           SIZE      
sda            57.3G     
|-sda1         512M      
|-sda2         488M      
`-sda3         56.3G     
`-sdb3_crypt   56.3G     
`-desktop--vg-swap_1980M      
nvme0n1        238.5G

$ lsblk -o NAME,SIZE,MOUNTPOINT | awk '$NF=="/"'

|-desktop--vg-root 55.3G /

From Chris Davies RESULT:
Root partition: mapper/desktop--vg-root
Root device: 
NAME      SIZE
sda      57.3G
nvme0n1 238.5G
From @muru:

lsblk -d $(findmnt -no source /)

NAME         MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
desktop--vg-root 254:1    0 55,3G  0 lvm  /
2
  • Let's say you have an LVM or RAID5 or zpool spanning sda1, sda3 and nvme0n1p1, and that is what you're using for your root partition. What output do you expect then? Commented Mar 27 at 13:59
  • 1
    Oh, hang on, if the output is what you show, my function did remove the / filesystem. You might want to add a bit more space to the printf, something like printf "%-25s%-10s\n", but apart from that, it seems to have removed -desktop--vg-root. Isn't that what you want? Commented Mar 27 at 17:39

2 Answers 2

3
+50

I propose the following shell script (let's call it lsblk_without_root_parent.sh):

#!/bin/bash

# Identify the device where the '/' filesystem is mounted from
rootdev=$(findmnt -n -o SOURCE /)

# Get the "inverse" device tree for the root filesystem device, but restrict
# output to device name only. The last line of the output corresponds to the
# "top-level" parent device on which the '/' filesystem resides.
root_topdev=$(lsblk -l -n -s -o NAME "$rootdev" | tail -n 1)

# Print 'lsblk' output, but omit the "root top-level parent device" part of the tree.
lsblk | awk -v excl="$root_topdev" 'BEGIN{p=1} (p==0 && /^[[:alpha:]]/){p=1}  p{if (index($0,excl)==1) p=0} p'

exit 0

Run it as

./lsblk_without_root_parent.sh

Update: The above script prints the full tree (minus the sub-tree starting at the root's top-level parent). Since you want to only print top-level devices anyway, the last statement can be simplified to

# Print 'lsblk' output without dependent devices; omit the device identified as "root top-level parent".
lsblk -d -o NAME,SIZE --sort SIZE | grep -v '^'"$root_topdev"

Explanation

This script uses findmnt to determine which filesystem is mounted as /.

It then uses the -s flag of lsblk to print the "inverse device tree" starting at the root filesystem device to determine its top-level parent, which is the last line of output of lsblk -l -n -s. We restrict the output to the device name only.

In case you only want to print top-level devices, the output of lsblk -d is then filtered by an inverse grep which removes lines starting with the "root top-level parent".

If, on the other hand, you want to have the full tree ouptut (minus the sub-tree starting at the root's top-level parent), an awk program is used to filter the regular lsblk output:

  • By default, it prints everything (p=1).
  • If the output line starts with the "top-level parent", it sets the p flag to 0 to suppress printing.
  • If we are in "suppressed mode", but the line starts with an alphabetic character rather e.g. | or space (which would indicate we are still printing child devices of the root's top-level parent), printing is turned on again.
0
0

Here's a possible approach. Determine the root partition. Then identify its device. Finally exclude it from the list of devices.

partition=$(
    mount |
    awk '$3 == "/" { print substr($1,6) }'
)
echo "Root partition: $partition"

device=$(
    lsblk -i -o NAME |
    tac |
    awk -v partition="$partition" '
        BEGIN { re = "-" partition "$" }
        $0 ~ re { x++ }
        x && /^[a-z]/
    '
)
echo "Root device: $device"

lsblk -d -o NAME,SIZE --sort SIZE |
    awk -v device="$device" '$1 != device'

It may not work where root is on an indirect filesystem such as LVM, LUKS or RAID. I don't have a system to hand where I can test that

3
  • 1
    No, unfortunately not either. I didn't think it would be this difficult. :( I put the output in the question Commented Mar 27 at 23:40
  • Here, you also don’t answer the question that was asked in detail. So the answer is useless to. This whole thing can be shortened with lsblk -d -o NAME,SIZE --sort SIZE. This should actually also get a downvote ;) Commented Oct 8 at 9:39
  • You can’t always measure with a two‑row scale if you want to be that precise ^^ Commented Oct 8 at 9:51

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.