Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

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

Programming languages, Coding, Executables, Package Creation, and Scripting.
Message
Author
Hypex
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

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

#1 Post by Hypex »

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.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

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

#2 Post by Head_on_a_Stick »

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

User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

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

#3 Post by edbarx »

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.

Hypex
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

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

#4 Post by Hypex »

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?

#5 Post by Hypex »

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.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

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

#6 Post by pylkko »

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
ralph.ronnquist
Posts: 342
Joined: 2015-12-19 01:07
Location: Melbourne, Australia
Been thanked: 6 times

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

#7 Post by ralph.ronnquist »

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.

Hypex
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

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

#8 Post by Hypex »

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.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

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

#9 Post by dasein »

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.

fsmithred
Posts: 1873
Joined: 2008-01-02 14:52

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

#10 Post by fsmithred »

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.

Hypex
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

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

#11 Post by Hypex »

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.

User avatar
ralph.ronnquist
Posts: 342
Joined: 2015-12-19 01:07
Location: Melbourne, Australia
Been thanked: 6 times

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

#12 Post by ralph.ronnquist »

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)

Hypex
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

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

#13 Post by Hypex »

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?

#14 Post by Hypex »

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?

#15 Post by Hypex »

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. :-)

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: [SOLVED] How to extract HDD block device from partition

#16 Post by kiyop »

The first post:
Hypex wrote: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?
Hypex asked a proper way.
edbarx wrote: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?
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.
The last post:
Hypex wrote: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:]"
What a terrible questioner :lol: I could not imagine his/her meaning of "a proper way".

To Hypex,

How do you get the value of the device file name of the boot partition, such as "/dev/sda2"?
If it is always
/dev/[sh]d[a-z][0-9]*
then

Code: Select all

tr -d "[:digit:]"
or

Code: Select all

tr -d "[0-9]"
or

Code: Select all

sed -e "s/[0-9]//g"
works properly.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

Hypex
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: [SOLVED] How to extract HDD block device from partition

#17 Post by Hypex »

kiyop wrote:What a terrible questioner :lol: I could not imagine his/her meaning of "a proper way".
Haha. :-) Well it was a suggested solution, not the suggested proper way. In my research it looks like there is no exact proper way, as all paths to lead simply manipulating the text.

In my mind a partition device simply points to another drive device upstream with an offset and length. So I was simply wondering if that could be grabbed from the downstream device. So to speak.
How do you get the value of the device file name of the boot partition, such as "/dev/sda2"?
If it is always
...

Cheers. Yes for a normal hda or sda part that works fine.

It would only break for an MTD part or OSX device but since I don't have to worry about that converting the text will be fine. :-)
Last edited by Hypex on 2017-01-07 10:39, edited 1 time in total.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: [SOLVED] How to extract HDD block device from partition

#18 Post by kiyop »

Hypex wrote:Haha. :-) Well it was a suggested solution, not the suggested proper way. In my research it looks like there is no exact proper way, as all paths to lead simply manipulating the text.

In my mind a partition device simply points to another drive device upstream with an offset and length. So I was simpley wondering if that could be grabbed from the downstream device. So to speak.
You may see under /dev/disk. For example, under /dev/disk/by-id, there may be "-part1" or so attached to the device itself. And for encrypted partition in LVM, it is a little tough.
As the other might have mentioned, "lsblk" displays partitions and their device connected by lines.
"fdisk -l" and "parted -l" are also available.
Hypex wrote:
How do you get the value of the device file name of the boot partition, such as "/dev/sda2"?
If it is always
...

Cheers. Yes for a normal hda or sda part that works fine.

It would only break for an MTD part or OSX device but since I don't have to worry about that converting the text will be fine. :-)
OK. You know that there are media which is not detected as "/dev/[sh]d[a-z][0-9]*".

Thanks for replying :)
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

Hypex
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: [SOLVED] How to extract HDD block device from partition

#19 Post by Hypex »

Just for amusement I soon discovered that I needed the partition number itself as well. Not as clear cut as cutting off digits. Isolating digits seems harder since tr doesn't have a keep option I can see.

And then ran into another issue. The boot volume can be mounted under /dev/disk/by-label which totally messes it up. So I need extract the block device from the label device in this case first. :-)

Hypex
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Re: [SOLVED] How to extract HDD block device from partition

#20 Post by Hypex »

Although I had marked this as solved it currently isn't as I am getting corrupted output from tr in the installer. As an example the following is what I input and get as output:

Code: Select all

echo "dev/sda4" | tr -d "[:digit:]"`
/ev/sa4

echo "dev/sda4" | tr -d "/[:alpha:]"`
evda4
Bizarre! Think I need to specify as range. :-?

Post Reply