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 h = the number of heads on your disk
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 **