[SOLVED] How to extract HDD block device from partition dev

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

[SOLVED] How to extract HDD block device from partition dev

Postby Hypex » 2016-09-23 05:14

Hi guys.

I had a search on the net for this but nothing came up exactly as I am looking for. I'm working on a custom Debian Jessie installer for an "obscure" PowerPC platform, AmigaOne XE, and in validating the boot partition I need to scan the start of the HDD and locate a boot block. So I need to convert the boot partition back to the root block device that is pointing to the whole HDD.

To put this into perspective and in a way easy to understand here is an example. Say the boot partition is at:
Code: Select all
/dev/sda2
How do I convert that to?
Code: Select all
/dev/sda


I know I can use some Bash commands to knock off the number, and a friend told me how, but I'm wondering if there is a proper way to get the root block device?

My code is in C so right now I'm opening it as a file and reading in blocks. So if there is also a function in the Linux API that can help that would be fine also. I know hardly anything about the Linux device API except the convenience of being able to transparently open devices as file.

I could keep on searching online and test programs I find to see if that helps but I thought I would just ask. Sorry if root block device is an abuse of terms. But I think you know what I mean there. :-)

I'm happy to use a shell command or a Linux function call for this purpose. Either will be fine. It will be run from a partman hook in d-i.
Last edited by Hypex on 2017-01-17 11:54, edited 3 times in total.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to extract HDD block device from partition device?

Postby Head_on_a_Stick » 2016-09-23 07:05

You can use dd(1) to copy the sectors with a defined offset, if that's what you mean.
"Are you quite sure that all those bells and whistles, all those wonderful facilities of your so called powerful programming languages, belong to the solution set rather than the problem set?" — Edsger W. Dijkstra
User avatar
Head_on_a_Stick
 
Posts: 6577
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: How to extract HDD block device from partition device?

Postby edbarx » 2016-09-23 09:30

Hard disk device names do not have a numerical suffix. This means, what you were told is correct as you only need to drop the number at the end. Another way, is to examine the contents of /dev, but why do it the hard way if a simpler way exists?
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.
User avatar
edbarx
 
Posts: 5384
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E

Re: How to extract HDD block device from partition device?

Postby Hypex » 2016-09-23 09:33

Head_on_a_Stick wrote:You can use dd(1) to copy the sectors with a defined offset, if that's what you mean.


I considered using dd inside a script but how would I find the offset? You can give an offset to dd but see no way for it to read from a file and determine the offset from the disk itself.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to extract HDD block device from partition device?

Postby Hypex » 2016-09-23 09:37

edbarx wrote:Another way, is to examine the contents of /dev, but why do it the hard way if a simpler way exists?


That's why I was asking. To see if there is a dedicated command that can return it to me. Something along the lines of blkid or tune2fs. Or lsblk I also read about.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to extract HDD block device from partition device?

Postby pylkko » 2016-09-23 09:59

Hypex wrote:
edbarx wrote:Another way, is to examine the contents of /dev, but why do it the hard way if a simpler way exists?


That's why I was asking. To see if there is a dedicated command that can return it to me. Something along the lines of blkid or tune2fs. Or lsblk I also read about.


Yes there is. Probably many, you need to google it.

I am not sure what you want. Your post is too vague, but if it is the case that you want a command that returns the offset numbers then GNU package Parted comes to mind. Or fdisk -l
User avatar
pylkko
 
Posts: 1106
Joined: 2014-11-06 19:02

Re: How to extract HDD block device from partition device?

Postby ralph.ronnquist » 2016-09-23 10:17

The "containment" relation between /dev/sda and /dev/sda2 is not immediately available in C. You can use stat on /dev/sda2 to obtain the major/minor device id components, but I don't know of a reverse mapping to a path name for "major/0".

The relationship between sda2 and sda is present in the sysfs, i.e., the /sys tree, where sda2 is a sub-directory of the sda point. You might for instance follow up the link /sys/class/block/sda2 to the resolved parent directory, which would be named .../sda, and infer that (most likely) /dev/sda is a device node for the device as a whole.
User avatar
ralph.ronnquist
 
Posts: 303
Joined: 2015-12-19 01:07
Location: Melbourne, Australia

Re: How to extract HDD block device from partition device?

Postby Hypex » 2016-09-23 15:34

pylkko wrote:I am not sure what you want. Your post is too vague


Well I had hoped to explain that in my example and by my title but it looks like that isn't enough. I'm running a script as a partman hook to make sure the boot partition fits certain criteria. Now this boot partition contains kernels and a menu setup like GRUB and can be any partition on disk. So my script knows the device node of the partition.

What I want is the disk node containing that partition. That is the block device for that whole disk. I did Google for things like "How to get disk device from partition device" and assorted variations but all I seem to get are the reverse of what I want. :-)

Another way to explain it is how would you read the raw GPT data using only the partition device as a reference.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to extract HDD block device from partition device?

Postby dasein » 2016-09-23 16:02

I too am finding your post difficult to understand, particularly because none of the things you say you've investigated (tune2fs, blkid, etc.) comes anywhere close to approaching a solution to the problem as I understand it. I find myself wondering if this is an instance of the XY problem.

That said, my best guess about what you're looking to do (rightly or wrongly) is to identify the starting sector of a particular disk partition. That information is contained in the partition table (PT). IF my inference is correct, then you have a couple of options: (1) have your code read and parse the partition table directly or (2) use something like fdisk or parted, as suggested above.

Again, this is a total stab in the dark based on an unreliable guess about what you're trying to accomplish. No warranty, express or implied.

Reading the PT directly is the more portable and self-contained solution, but it's also the more dangerous approach, and it's a lot more work. Personally, I would urge you in the strongest possible terms to avoid mucking around with the PT, where tiny errors can have huge consequences. In the alternative, make and verify a backup of the PT (or better still, the entire device) before proceeding.
User avatar
dasein
 
Posts: 7775
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: How to extract HDD block device from partition device?

Postby fsmithred » 2016-09-23 19:18

This is what I use:
Code: Select all
find /dev -mindepth 1 -maxdepth 1  -name "*[sh]d[a-z]"

If you want them in order, pipe the output to sort.
fsmithred
 
Posts: 1862
Joined: 2008-01-02 14:52

Re: How to extract HDD block device from partition device?

Postby Hypex » 2016-09-24 02:19

ralph.ronnquist wrote:You can use stat on /dev/sda2 to obtain the major/minor device id components, but I don't know of a reverse mapping to a path name for "major/0".


Yes I read that as well.

ralph.ronnquist wrote:The relationship between sda2 and sda is present in the sysfs, i.e., the /sys tree


Okay so that brings it closer. It looks like I could scan that as well as I could the disk block devices in /dev for a match.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to extract HDD block device from partition device?

Postby ralph.ronnquist » 2016-09-24 02:25

No scan needed. From
Code: Select all
/dev/$NAME
you find the drive as
Code: Select all
/dev/$(basename $(dirname $(readlink -f /sys/class/block/$NAME)))
without any scanning/searching.

(Unless /dev/$NAME is the drive already)
User avatar
ralph.ronnquist
 
Posts: 303
Joined: 2015-12-19 01:07
Location: Melbourne, Australia

Re: How to extract HDD block device from partition device?

Postby Hypex » 2016-09-24 03:09

dasein wrote:I too am finding your post difficult to understand, particularly because none of the things you say you've investigated (tune2fs, blkid, etc.) comes anywhere close to approaching a solution to the problem as I understand it.


I do have a talent of making jokes with obvious references that no one gets so perhaps I've somehow written my post that way. :-)

I also pondered what extra details to put in and leave out. I decided to put some in before people wonder what I am doing. But at the same time extra details can confuse the question also.

But that is why I came here. I did some research. On and off. Then thought I'll just ask the damn question. Now my dilemma is I didn't know how to ask it! :-P

I find myself wondering if this is an instance of the XY problem.


LOL. That's a good one. In my case I hadn't attempted to solve it yet. I was going to try matching the block devices to the partition with a search. But wondered if there was a more direct approach.

That said, my best guess about what you're looking to do (rightly or wrongly) is to identify the starting sector of a particular disk partition.


Funny as it sounds that would be too much information. I mean, for what I want, at least as far as my script is concerned, a block device is perfectly fine.

Reading the PT directly is the more portable and self-contained solution, but it's also the more dangerous approach, and it's a lot more work.


At the end of the day, or just before the base system is installed rather, I need to read it. Usually reading is a safe option. So I wouldn't have considered backing up the PT if I just want to read parts of it; and the obvious fact that to back it up I would need to read it, so that looks like a chicken and egg there. ;-) In any case I don't need to modify the PT. I just need to briefly scan it using my C program which takes a device as an argument, assuming it is a disk block device.

Now I could actually skip this step all together. But there is the fine line between instructions and following. So I don't want to get into the situation of a user installing Debian on their system, unintentionally missed a specific set up detail, and find they cannot boot the new Debian install because there is no boot menu for it and they complain my latest iteration doesn't work again. :-D
Last edited by Hypex on 2016-09-24 10:38, edited 1 time in total.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to extract HDD block device from partition device?

Postby Hypex » 2016-09-24 03:14

Just want to say thanks for everyone's input so far. So I'll go off and do some testing. And see what I find. :-)
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: How to extract HDD block device from partition device?

Postby Hypex » 2016-09-29 08:05

Hello again. Well I've gone through all the options and without a specific command or such to do the job the easiest solution is to simply manipulate the text. This is how my friend suggested it.

Assuming boot_part="/dev/sda2". This will extract the boot device:
echo "$boot_part" | tr -d "[:digit:]"

A small one liner I can stick into my boot_device. Oh and I can't believe they even trimmed down the trim command to tr. :-)
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable