I have a standard ubuntu encrypted lvm setup. /dev/sda3
is encrypted w/ LUKS. Inside that LUKS container is an LVM ( at /dev/mapper/dm_crypt-0
)and inside that LVM is a filesystem mounted from /dev/ubuntu-vg/ubuntu-lv
to /
How can I convert the known mount point of /
to /dev/sda3
in a one-liner in bash, preferably without root? I would be happy to install an separate utility if it could accomplish this.
I have figured out df|tail -n +2|cut -f 1 -d
to get me /dev/mapper/ubuntu--vg-ubuntu--lv
but how do I get /dev/sda3
from that?
There might be a simpler and more efficient way to do this, but I was able to achieve what you want using
lsblk
,grep
, andawk
without needing root access.tl;dr
Let's break it down and use a test VM as an example, Ubuntu 22.04.3 Server. Start with the output of
lsblk
. As you can see, all the info you want is there. It shows the mountpoint and path up the tree to/dev/sda3
. The challenge is how to parse this output to get the path/dev/sda3
from the mountpoint,/
.Fortunately,
lsblk
has an-o
option that allows you to supply a list of columns to print. This displays additional info that can be parsed withgrep
andawk
. Look atlsblk --help
for a complete list. To start, I choseMOUNTPOINT
andPKNAME
, but I'll also be usingPATH
andKNAME
later.1. Find the
PKNAME
for mountpoint:/
The following line prints the
MOUNTPOINT
andPKNAME
associated with/
mountpoint:Use
awk
to print and assign the 2nd column,PKNAME
, to a variable:2. Find the
PKNAME
for the previousPKNAME
Use
lsblk
again, this time withPATH
,PKNAME
, andKNAME
as columns. Usegrep
to find lines containing the value of$pkname
:This results in two matches, but we only want a match where
$pkname
matches the 3rd column, which isKNAME
. The 2nd column,PKNAME
, has to be included, because that's the next value needed.Use
awk
again to get that value, and re-assign it to your variable:3. Find the
PATH
for the previousPKNAME
Use
lsblk
to displayPATH
andKNAME
columns,grep
to find the matching line, andawk
to print the first column,PATH
:Finally, combine all of the commands together, separated by semi-colons. To be complete, tack on an
unset
at the end to clear the variable,$pkname
. There's your one-liner without needing root access.