How to find block device of mount point?

Need help with C, C++, perl, python, etc?

How to find block device of mount point?

Postby Hypex » 2017-01-04 01:56

Hi guys. Okay so this is new twist on an old problem. I just found out. :-)

I was using "df" to find the device of a mount, but found a flaw as a long device name can split the result into two lines. Or it could be mounted as a label so doesn't point directly to a device. In my case both of these together corrupted my script which was grabbing the device from the output.

So I have a device mounted at a point. But it isn't always a straight block device. It could be a UUID or label mount.

Like so:
/dev/disk/by-label/boot on /target/boot

Let's say that "/dev/disk/by-label/boot" actually points to "/dev/sda1". So how to I take "/dev/disk/by-label/boot" and determine if it is an actual block device? And if not how do I find that actual device it points to?

From experimenting I've noticed that the label points are links that go back to the actual device. Not exactly directly but through a "../../". So it seems obvious I need to see if its a link and follow it through.

But before I muck around like that. Is there a better way? Some mash of mount, df or blkid or anything to tell me what I need to know? :-)
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to find block device of mount point?

Postby dasein » 2017-01-04 02:37

This is a problem whose solution can be discovered with just a little tiny bit of effort/exploration on your part...

Spend some time exploring /dev/disk/
User avatar
dasein
 
Posts: 7775
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: How to find block device of mount point?

Postby Hypex » 2017-01-04 05:11

Well I have been exploring "/dev/disk" as that's how I found out about the links. ;-)

From what you imply a simple command won't do it and it has to found from the device point.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to find block device of mount point?

Postby Segfault » 2017-01-04 05:13

Without digging deep into your problem, does findmnt command help?
Segfault
 
Posts: 479
Joined: 2005-09-24 12:24

Re: How to find block device of mount point?

Postby Hypex » 2017-01-06 10:26

Thanks, findmnt looks useful. Now, my problem is the installer doesn't have it included, since I need to use it in the installer. If I could chroot and apt-get a udeb of util-linux where I build my installer that will be useful. :-)
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to find block device of mount point?

Postby kiyop » 2017-01-08 07:22

2 commands: "realpath" and "readlink" may be useful.

If you want to use in initramfs a command which is not in initramfs, you can extract the initramfs file and add the command to the extracted directory and regenerate an initramfs file from the directory.
An example to include /usr/bin/dialog into a directory /usr/bin in the initramfs:
Code: Select all
mkdir initrd
cd initrd
gzip -dc /boot/initrd.img-$(uname -r) | cpio -i
mkdir -p usr/bin
cp -L /usr/bin/dialog usr/bin/
find .|cpio -H newc -o|gzip -9 > ../initrd.img-new
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/
User avatar
kiyop
 
Posts: 3984
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan

Re: How to find block device of mount point?

Postby Hypex » 2017-01-09 12:03

Thanks kiyop. That's almost what I was doing. Yes those commands look useful. I also need tune2fs. But my problem there is that I need to also add depends. And then extract it out. At this point an initrd builder looks good. Be useful to apt-get from a chroot. Closest I got is downloading a package and pointing dpkg to where I wanted it. :-)
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable