Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org SOLVED: Trying to "reverse engineer" a calculation: any number hackers out there?
 Slackware This Forum is for the discussion of Slackware Linux.

Notices

07-27-2006, 04:14 AM   #1
drkstr
Senior Member

Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep:
SOLVED: Trying to "reverse engineer" a calculation: any number hackers out there?

Here is the solution for calculating the last sector used in the partition table, usefull for when the number of Heads on the disk is not 255. This is handy if you are ever need to write to the partition table manually (such as in a perl script).

Quote:
 Originally Posted by cwwilson721 For all Newbies reading this thread: Don't forget the following: Messing with partition info can corrupt your data! If you do mess it up, odds are it is unrecoverable without a high degree of skill. Sacrifice a chicken to the hdd gods first. KFC will do it for you for a fee... (Last was done tongue in cheek. You don't need to sacrifice a chicken. Pizza works just as well. However, I've had mixed results w/lasagna. Use at your own risk...)
note: I am not an expert so I can't guarantee this is correct, but it did work for me.

================

first use 'sfdisk -l' to get your disk geometry.

let H = the number of Heads on the disk
let C = the number of cylinders on the disk
let T = 63 (I believe this is a constant)
let L = the Last sector written on the partition table

the equation to find the last sector you should use is as follows:

Code:
```let T = 63
let H = 255
let C = the number of cylinders on your disk

h*C*T
let Z =     -----
H*T
then

L = |Z| * H * T

note: |Z| represents "the whole number part of Z" in mathimatical terms.```

================

That's all there is to it.

If you want to see how this is derived, read on. Otherwise, happy disk witting!

first, let's find out which sector sfdisk used by dumping the partition table:

Code:
```root@lpt:~# sfdisk -d /dev/hda
# partition table of /dev/hda
unit: sectors

/dev/hda1 : start=       63, size= 16595082, Id=83
/dev/hda2 : start= 18555075, size=  1076355, Id=82
/dev/hda3 : start= 16595145, size=  1959930, Id=83
/dev/hda4 : start=        0, size=        0, Id= 0```
We can get the last disk used by adding up the total sectors:
63 + 16595082 + 1076355 + 1959930 = Last Sector Used = 19631430

Now let's find the actual number of sectors on the disk:

Code:
```root@lpt:~# sfdisk -l /dev/hda | grep Disk
Disk /dev/hda: 19485 cylinders, 16 heads, 63 sectors/track```
The total sectors is found with:
C*H*T = 19640880

note the variance between the two (remember this number)
19640880 - 19631430 = 9450 sectors

Now let's also note that the two numbers both share a common factor of the number of Tracks (63)
19640880/T=311760

19631430/T=311610

Hmm, this is interesting. Now lets try getting all of the factors to see what we can come up with.

factor(19631430)
2 * 3^3 * 5 * 7 * 13 * 17 * 47

Lets try and get 3 usefull numbers from this (Tracks, Cylinders, and Heads)
Now we know 63 is a factor so let's pull it out. We can also see 255 as a factor, so let's pull that out as well. From what's left, we can see the geometry that was actually written to the disk 1222 cylinders, 255 heads, and 63 Tracks.
(3^2 * 7)(3 * 5 * 17)(2 * 13 * 47)

This is all well and good, but what if we need to find out the last sector we should write to when it is unknown? So we derive the information we just used into an equation.

In order to figure out the last sector that will be written, we need to find out how many cylinders there are based on a 255 head disk geometry.

Consider the Following proof:

Code:
```let S = Total Sectors
let L = Last sector written

S - L
----- = V
(H*T)

or

S - V(H*T) = L

(V represents the variance between S and L in terms of cylinders per H*T)

Since H and T are factors of L, and T is also a factor of S, we could say that

[L/(H*T)] is the whole number part of [S/(H*T)] which we will call |Z|

then we could also say that

S
---  - V = |Z|
H*T

or

S - V(H*T) = |Z|(H*T)

or

L=|Z|(H*T)```

Hopefully this will be of some use to someone. I figured since I spent all the time doing this, I might as well share it with anyone who needs it.

regards,
...drkstr

note: everything form here to post # 09 was written before the above text

** original question **
I have been racking my brain over the past 3 hours trying to "reverse engineer" (for lack of a better term) the way sfdisk calculates the last sector it uses in the partition table. Any of you number hackers out there feel like giving me a hand? Here is an example output from a 'sfdisk -d /dev/hda' on my computer.
Code:
```/dev/hda1 : start=       63, size= 16595082, Id=83
/dev/hda2 : start= 18555075, size=  1076355, Id=82
/dev/hda3 : start= 16595145, size=  1959930, Id=83
/dev/hda4 : start=        0, size=        0, Id= 0```
and here is the disk geometry
Code:
```/dev/hda:
19485 cylinders, 16 heads, 63 sectors/track
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0```
What I need to know is how sfdisk comes up with the last sector sfdisk writes to since it is not the last sector of the disk

Now I don't really know a lot about disk geometry so I've just been plugging numbers in the calculator to see if I can reproduce the numbers with an equation.

I've noticed that both the total sectors on the disk, and the last sector written by sfdisk have a common factor of 63 (Number of tracks) Does this have anything to do with how the last sector written to is calculated, or am I just way off? Is it possible to compute the number I need with an equation?

Any input will be greatly appreciated!
...drkstr
** original question **

Last edited by drkstr; 07-29-2006 at 03:23 PM.

 07-27-2006, 06:30 AM #2 synapse Member   Registered: Jan 2004 Location: On Planet Earth. Distribution: Slackware 12 Posts: 244 Rep: Hi Doesnt the first sector belong to the boot sector and so it is not counted at all? therefore the sectors will be the last -1
 07-27-2006, 07:46 AM #3 Randux Senior Member   Registered: Feb 2006 Location: Siberia Distribution: Slackware & Slamd64. What else is there? Posts: 1,705 Rep: Hi Aaron, I don't understand what you're asking here. There's a lot of info but the question isn't clear (to me). Please spell out what you are trying to understand. P.S. You see that sfdisk counts starting from sector 63, right (in the first example you posted) but from 0 in the second. In the first example this means the the first track (sectors 0 - 62, inclusive) is bypassed. This is the MBR, as synapse alluded to, but what he said about sectors - 1 isn't true- it's sectors - 63. Last edited by Randux; 07-27-2006 at 07:47 AM.
07-27-2006, 10:02 AM   #4
drkstr
Senior Member

Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep:
sorry, this was a bit confusing. I realize I didn't really explain well what I was trying to do.

What I am need to be able to do is edit the output of the partition table dump (the first example) so I overwrite the existing partition table by piping it back through sfdisk. In order to do this I need to be able to calculate how they come up with each number in the dump. All of them are pretty strait forward except for the "total number of sectors included in the partition table".

note: I know over writing the partition table "live" sounds like a bad idea, but it actually works. I've tested it by using cfdisk to change the partition table, then dumped it with sfdisk and piped it back in. When I was done I was able to restore the partition table back from the original (which I dumped to a file before changing anything). This all worked without rebooting or losing any data.

The very last sector it uses is not (total disk size) - (63 sectors) it is actually (total disk size - 9450 sectors) I strongly belive that the number of sectors used by the partition table can be figured out mathmaticly since it is not an arbitrary number (it can be devided evenly by both the number of heads or the number of tracks).

Quote:
 P.S. You see that sfdisk counts starting from sector 63, right (in the first example you posted) but from 0 in the second. In the first example this means the the first track (sectors 0 - 62, inclusive) is bypassed.
The first example is the actual partition table dumped by sfdisk, where the second example is the total disk size (MBR included).
Quote:
 This is the MBR, as synapse alluded to, but what he said about sectors - 1 isn't true- it's sectors - 63.
But then why is the varience from total sectors written to total sectors on the disk 9450 sectors instead of 63?

thanks for everyones interest!
...drkstr

 07-27-2006, 12:35 PM #6 Randux Senior Member   Registered: Feb 2006 Location: Siberia Distribution: Slackware & Slamd64. What else is there? Posts: 1,705 Rep: Good going. I was going to point out that your second set of numbers was in cyls but your first set was in sectors. You can change this in fdisk (don't know about sfdisk/cfdisk, since I don't use those) by going into "expert" mode.
07-27-2006, 01:16 PM   #7
drkstr
Senior Member

Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep:
Quote:
 Good going. I was going to point out that your second set of numbers was in cyls but your first set was in sectors. You can change this in fdisk (don't know about sfdisk/cfdisk, since I don't use those) by going into "expert" mode.
So it's possible I wasn't getting any varience at all, just comparing apples to oarenges?

...drkstr

Last edited by drkstr; 07-27-2006 at 01:23 PM.

 07-27-2006, 01:34 PM #8 Randux Senior Member   Registered: Feb 2006 Location: Siberia Distribution: Slackware & Slamd64. What else is there? Posts: 1,705 Rep: I'm sorry, I don't have time to go over the calculations at the moment, but I did notice from the two examples you pasted that the units were different. Also I think most people find the geometry defined in terms of 255 heads like you mentioned later. I don't know why it's shown as 16 in the initial example you pasted. Last edited by Randux; 07-27-2006 at 01:36 PM.
07-27-2006, 01:55 PM   #9
drkstr
Senior Member

Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep:
Quote:
 I'm sorry, I don't have time to go over the calculations at the moment, but I did notice from the two examples you pasted that the units were different. Also I think most people find the geometry defined in terms of 255 heads like you mentioned later. I don't know why it's shown as 16 in the initial example you pasted.
That's ok, I think I have the calculations pretty much worked out. I'm pretty sure you just confirmed in words what I found in numbers. Which is a big help since I'm always trying to ask why.

I think the problem might have been with the fact that I was working with an old hard drive on a laptop from 2001. The same 'fdisk -l' command run on my server gives the units as 255 Heads which is what led me to think that sfdisk was not using the correct geometry for my disk.

I'm just happy to have it figured out. Now I can get back to writing code again.

...drkstr

 07-28-2006, 02:27 AM #10 drkstr Senior Member   Registered: Feb 2006 Location: Seattle, WA: USA Distribution: Slackware 11.0 Posts: 1,191 Original Poster Rep: Looks like I can mathmaticly determain the last sector used by the partition table. I wrote a brief proof in hopes it can be of use to anyone else. regards, ...drkstr
 07-28-2006, 04:52 AM #11 Randux Senior Member   Registered: Feb 2006 Location: Siberia Distribution: Slackware & Slamd64. What else is there? Posts: 1,705 Rep: Nice job. Maybe put it in lq bookmarks......
 07-28-2006, 12:30 PM #12 XavierP Moderator   Registered: Nov 2002 Location: Kent, England Distribution: Debian Testing Posts: 19,192 Blog Entries: 4 Rep: As requested, thread title changed
 07-29-2006, 07:42 AM #13 gnashley Amigo developer   Registered: Dec 2003 Location: Germany Distribution: Slackware Posts: 4,853 Rep: Several things I see: You have a null number of sectors for the first partition. The partitions are mixed physically and logically -as if some had been created, then some removed or resized or recreated. Logical cylinder boundaries. One of the things that sucks about DOS fdisk is that you can't be very specicfic about partition sizes -some sizes are uncreatable. Linux tools are better of course, they'll warn you sometimes like this: 'number of heads/tracks/sectors doesn't match logical cylinder boundaries' If the point is to 'occupy' all the available space on the disk, then you need to get down to block-size also so you can ask for the proper number of K's for the partition. Most of choose a 'rounded' size like 100MB or 128MB. The logical cylinder boundaries may not match these fiures. When they don't, the last unused sectors are skipped. The next partition starts at a new logical cylinder boundary. If you want to see how sfdisk or whatever does it you could try reading the sources. Last edited by gnashley; 07-29-2006 at 07:43 AM.
07-29-2006, 12:37 PM   #14
drkstr
Senior Member

Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep:
Thanks gnashley.

Logical partitions has presented a problem for me which I have chosen not to deal with at the moment. Currently, I will be able to delete a "group" of logical partitions (IE. the primary extended partition), or one of the true primary partitions. Furthermore, the new partitions will only be created on one of the 4 primary slots, and fail if there are not enough available.

Quote:
 If the point is to 'occupy' all the available space on the disk, then you need to get down to block-size also so you can ask for the proper number of K's for the partition.
This will be the focus down the road, but right now, I am just trying to modify the partition table dump with numbers that will be safe to pipe back into sfdisk.

I'm not quite sure I've achieved this yet, but I will find out shortly

thanks!
...drkstr

 07-29-2006, 12:53 PM #15 cwwilson721 Senior Member   Registered: Dec 2004 Location: In my house. Distribution: Ubuntu 10.10 64bit, Slackware 13.1 64-bit Posts: 2,649 Blog Entries: 1 Rep: For all Newbies reading this thread: Don't forget the following:Messing with partition info can corrupt your data! If you do mess it up, odds are it is unrecoverable without a high degree of skill. Sacrifice a chicken to the hdd gods first. KFC will do it for you for a fee... (Last was done tongue in cheek. You don't need to sacrifice a chicken. Pizza works just as well. However, I've had mixed results w/lasagna. Use at your own risk...)

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post ALInux Programming 1 12-28-2005 05:19 AM blampain Linux - Networking 6 03-30-2004 05:54 AM 8webguy8 Linux - Networking 12 03-17-2004 08:46 AM OrganicX Programming 1 01-30-2004 08:30 PM

All times are GMT -5. The time now is 08:11 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -