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

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

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

Postby kiyop » 2016-10-06 11:56

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/
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: [SOLVED] How to extract HDD block device from partition

Postby Hypex » 2016-10-08 10:13

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.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

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

Postby kiyop » 2016-10-09 00:34

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/
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: [SOLVED] How to extract HDD block device from partition

Postby Hypex » 2017-01-07 10:56

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

Postby Hypex » 2017-01-17 07:14

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. :-?
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

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

Postby cpoakes » 2017-01-18 01:55

Since mapping between device names and device major:minor numbers is a convention not inherent to the kernel (e.g. mknod /dev/anythingIwant b 8 2), a general solution would simply ignore the string value.

  • Map the given partition device name (/dev/sda3, /dev/hda7, /dev/mtdblock0p1, /dev/anythingIwant) to the major:minor device number
  • Use those to create a temporary root device for major:0
  • Perform actions on the temporary root device, and
  • Delete the temporary root device.
The task can be completed in C or in the shell without understanding the underlying conventions, save for one applicable to all *nix: the minor number 0 is used for the whole device on partitionable devices. Saves the programmer from needing to understand all the naming conventions employed by device drivers (Curse you mtdblock driver with your stupid names!).
User avatar
cpoakes
 
Posts: 95
Joined: 2015-03-29 04:54

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

Postby Hypex » 2017-01-27 09:29

Thanks cpoakes. That sheds some more light on the process. And what steps are needed to be performed. Which work for both conventional device nodes and mtb block naming schemes.
Hypex
 
Posts: 25
Joined: 2016-09-23 04:39
Location: Southern Aussieland

Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable