0

I need to find out on what disk a specific filesystems on my server is located. For this case, let's use my /home directory as an example:

[root@master playbooks]# df -h /home
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/rl-home  4.0G   62M  4.0G   2% /home

[root@master playbooks]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0 16.7G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0 15.7G  0 part
  ├─rl-root 253:0    0   10G  0 lvm  /
  ├─rl-swap 253:1    0  1.7G  0 lvm  [SWAP]
  └─rl-home 253:2    0    4G  0 lvm  /home
sr0          11:0    1 1024M  0 rom

So for the /home filesystem, I would like to get sda or sda2 at least.

I would like to restrain from some complex awk commands.

Artur Meinild
  • 21,605
  • 21
  • 56
  • 89
John Ronald
  • 1,476
  • 2
  • 14
  • 29
  • 1
    You have only one disk, everything is therefore in that disk. And you're using LVM so LVM tools is what you need to manage it. They're volumes so why are expecting "to get sda or sda2"? – ChanganAuto Jul 19 '22 at 08:27
  • So you need to write a script, that takes the mounted directory as an input `/home` and gives the disk the partition resides on as an output `sda2`? If this is the case, please ask your question like this - be precise. – Artur Meinild Jul 19 '22 at 08:27
  • 3
    LVM volumes can span multiple disks, so in general it might not be possible for you to get `sda2` or even `sda` for some random LVM volume. – muru Jul 19 '22 at 08:33
  • Edited my answer to reflect the situation with LVM volumes. – Artur Meinild Jul 19 '22 at 08:34
  • @ChanganAuto It was a customer's request.. But now as I think of it I also find it a bit nonsense. Anyway this is just a test server, other servers will have multiple disks – John Ronald Jul 19 '22 at 08:35
  • @muru You are right, I did not realize that straight away. – John Ronald Jul 19 '22 at 08:36

2 Answers2

4

Use the df command combined with a very simple grep and awk:

df | grep "search string" | awk '{ print $1 }'

So for instance:

df | grep "/home" | awk '{ print $1 }'

Result in:

/dev/sda2

In the case of an LVM volume, you'll the volume as a result instead of the physical disk (since this is the way LVM works).

Artur Meinild
  • 21,605
  • 21
  • 56
  • 89
  • 1
    Seems like the correct answer. I knew there is this way but I did not realize that it is not possible to trace particular disk in case of logical volumes. Anyway thanks for effort. – John Ronald Jul 19 '22 at 08:58
  • 1
    You can make it shorter with like `df | awk '/\/home/ {print $1}'` and make it more accurate with like `df /home --output=source | tail -1` as `grep` might match anything with `/home` in it e.g. `/backup/home` – Raffa Jul 19 '22 at 10:17
4

You can use findmnt to get the device (well, whatever's listed in df) using:

findmnt /home -n -o source

(See also, this other answer by me for looking up the UUID of the source.)

muru
  • 193,181
  • 53
  • 473
  • 722