Lilo And Fdisk Use The Right Geometry example essay topic
Then you have a problem,because the usual INT13 BIOS interface to disk I / O uses a 10-bit field for thecylinder on which the I / O is done, so that cylinders 1024 and past areinaccessible. Fortunately, Linux does not use the BIOS, so there is no problem. Well, except for two things: (1) When you boot your system, Linux isn't running yet and cannot save you fromBIOS problems. This has some consequences for LILO and similar boot loaders. (2) It is necessary for all operating systems that use one disk to agree onwhere the partitions are. In other words, if you use both Linux and, say, DOS onone disk, then both must interpret the partition table in the same way.
This hassome consequences for the Linux kernel and for fdisk. Below a rather detailed description of all relevant details. Note that I usedkernel version 2.0.8 source as a reference. Other versions may differ a bit.2.
BootingWhen the system is booted, the BIOS reads sector 0 (known as the MBR - theMaster Boot Record) from the first disk (or from floppy), and jumps to the codefound there - usually some bootstrap loader. These small bootstrap programs foundthere typically have no own disk drivers and use BIOS services. This means that aLinux kernel can only be booted when it is entirely located within the first1024 cylinders. This problem is very easily solved: make sure that the kernel (and perhaps otherfiles used during bootup, such as LILO map files) are located on a partitionthat is entirely contained in the first 1024 cylinders of a disk that the BIOScan access - probably this means the first or second disk.
Another point is that the boot loader and the BIOS must agree as to the diskgeometry. It may help to give LILO the `linear' option. More details below.3. Disk geometry and partitionsIf you have several operating systems on your disks, then each uses one or moredisk partitions. A disagreement on where these partitions are may havecatastrophic consequences. The MBR contains a partition table describing where the (primary) partitionsare.
There are 4 table entries, for 4 primary partitions, and each looks likestruct partition { char active;/* 0x80: bootable, 0: not bootable */ charbegin [3];/* CHS for first sector */ char type; char end [3];/* CHS for lastsector */ int start;/* 32 bit sector number (counting from 0) */ int length; /*32 bit number of sectors */ }; (where CHS stands for Cylinder / Head/Sector). Thus, this information is redundant: the location of a partition is given bothby the 24-bit begin and end fields, and by the 32-bit start and length fields. Linux only uses the start and length fields, and can therefore handle partitionsof not more than 2^32 sectors, that is, partitions of at most 2 TB. That is twohundred times larger than the disks available today, so maybe it will be enoughfor the next ten years or so. Unfortunately, the BIOS INT13 call uses CHS coded in three bytes, with 10 bitsfor the cylinder number, 8 bits for the head number, and 6 bits for the tracksector number. Possible cylinder numbers are 0-1023, possible head numbers are 0-255, and possible track sector numbers are 1-63 (yes, sectors on a track arecounted from 1, not 0).
With these 24 bits one can address 8455716864 bytes (7.875 GB), two hundred times larger than the disks available in 1983. Even more unfortunately, the standard IDE interface allows 256 sectors / track,65536 cylinders and 16 heads. This in itself allows access to 2^37 = 137438953472bytes (128 GB), but combined with the BIOS restriction to 63 sectors and 1024cylinders only 528482304 bytes (504 MB) remain addressable. This is not enough for present-day disks, and people resort to all kinds oftrickery, both in hardware and in software.4. Translation and Disk ManagersNobody is interested in what the `real' geometry of a disk is. Indeed, thenumber of sectors per track often is variable - there are more sectors per trackclose to the outer rim of the disk - so there is no `real' number of sectors pertrack.
For the user it is best to regard a disk as just a linear array of sectorsnumbered 0, 1, ... , and leave it to the controller to find out where a givensector lives on the disk. This linear numbering is known as LBA. The linear address belonging to (c,h,s) for a disk with geometry (C,H,S) is c*H*S + h*S + (s-1). All SCSI controllersspeak LBA, and some IDE controllers do.
If the BIOS converts the 24-bit (c,h,s) to LBA and feeds that to a controllerthat understands LBA, then again 7.875 GB is addressable. Not enough for alldisks, but still an improvement. Note that here CHS, as used by the BIOS, nolonger has any relation to `reality'. Something similar works when the controller doesn't speak LBA but the BIOS knowsabout translation. (In the setup this is often indicated as `Large'.) Now the BIOSwill present a geometry (C',H',S') to the operating system, and use (C,H,S) while talking to the disk controller. Usually S = S', C' = C / N and H' = H*N,where N is the smallest power of two that will ensure C' <= 1024 (so that leastcapacity is wasted by the rounding down in C' = C / N).
Again, this allows accessof up to 7.875 GB. If a BIOS does not know about `Large' or `LBA', then there are softwaresolutions around. Disk Managers like OnTrack or EZ-Drive replace the BIOS diskhandling routines by their own. Often this is accomplished by having the diskmanager code live in the MBR and subsequent sectors (OnTrack calls this codeDDO: Dynamic Drive Overlay), so that it is booted before any other operatingsystem. That is why one may have problems when booting from a floppy when a DiskManager has been installed. The effect is more or less the same as with a translating BIOS - but especiallywhen running several different operating systems on the same disk, disk managerscan cause a lot of trouble.
Linux does support OnTrack Disk Manager since version 1.3.14, and EZ-Drivesince version 1.3.29. Some more details are given below.5. Kernel disk translation for IDE disksIf the Linux kernel detects the presence of some disk manager on an IDE disk, itwill try to remap the disk in the same way this disk manager would have done, sothat Linux sees the same disk partitioning as for example DOS with OnTrack orEZ-Drive. However, NO remapping is done when a geometry was specified on thecommand line - so a `hd=cyls,heads,secs' command line option might well killcompatibility with a disk manager.
The remapping is done by trying 4, 8, 16, 32, 64,128,255 heads (keeping H*Cconstant) until either C <= 1024 or H = 255. The details are as follows - subsection headers are the strings appearing in thecorresponding boot messages. Here and everywhere else in this text partitiontypes are given in hexadecimal.5.1. EZDEZ-Drive is detected by the fact that the first primary partition has type55. The geometry is remapped as described above, and the partition table fromsector 0 is discarded - instead the partition table is read from sector 1. Diskblock numbers are not changed, but writes to sector 0 are redirected to sector1.
This behaviour can be changed by recompiling the kernel with#define FAKE FDISK FOR EZDRIVE0 in ide. c.5.2. DM6:DDOOnTrack DiskManager (on the first disk) is detected by the fact that the firstprimary partition has type 54. The geometry is remapped as described above andthe entire disk is shifted by 63 sectors (so that the old sector 63 becomessector 0). Afterwards a new MBR (with partition table) is read from the newsector 0.
Of course this shift is to make room for the DDO - that is why there isno shift on other disks.5.3. DM6:AUXOnTrack DiskManager (on other disks) is detected by the fact that the firstprimary partition has type 51 or 53. The geometry is remapped as described above.5.4. DM6:MBRAn older version of OnTrack DiskManager is detected not by partition type, butby signature. (Test whether the offset found in bytes 2 and 3 of the MBR is notmore than 430, and the short found at this offset equals 0x55AA, and is followedby an odd byte.) Again the geometry is remapped as above.5.5. PTBLFinally, there is a test that tries to deduce a translation from the start andend values of the primary partitions: If some partition has start and endcylinder less than 256, and start and end sector number 1 and 63, respectively,and end heads 31, 63 or 127, then, since it is customary to end partitions on acylinder boundary, and since moreover the IDE interface uses at most 16 heads,it is conjectured that a BIOS translation is active, and the geometry isremapped to use 32, 64 or 128 heads, respectively.
(Maybe there is a flaw here,and genhd. c should not have tested the high order two bits of the cylindernumber?) However, no remapping is done when the current idea of the geometryalready has 63 sectors per track and at least as many heads (since this probablymeans that a remapping was done already).6. ConsequencesWhat does all of this mean?For Linux users only one thing: that they must makesure that LILO and fdisk use the right geometry where `right' is defined forfdisk as the geometry used by the other operating systems on the same disk, andfor LILO as the geometry that will enable successful interaction with the BIOSat boot time. (Usually these two coincide.) How does fdisk know about the geometry?It asks the kernel, using the HDIO GETGEOioctl. But the user can override the geometry interactively or on the commandline. How does LILO know about the geometry?It asks the kernel, using the HDIO GETGEOioctl. But the user can override the geometry using the `disk=' option.
One mayalso give the linear option to LILO, and it will store LBA addresses instead ofCHS addresses in its map file, and find out of the geometry to use at boot time (by using INT 13 Function 8 to ask for the drive geometry). How does the kernel know what to answer?Well, first of all, the user may havespecified an explicit geometry with a `hd=cyls,heads,secs' command lineoption. And otherwise the kernel will ask the hardware.6.1. IDE detailsLet me elaborate. The IDE driver has four sources for information about thegeometry.
The first (G user) is the one specified by the user on the commandline. The second (G bios) is the BIOS Fixed Disk Parameter Table (for first andsecond disk only) that is read on system startup, before the switch to 32-bitmode. The third (G phys) and fourth (G log) are returned by the IDE controller asa response to the IDENTIFY command - they are the `physical' and `currentlogical' geometries. On the other hand, the driver needs two values for the geometry: on the one handG fdisk, returned by a HDIO GETGEO ioctl, and on the other hand G used, which isactually used for doing I / O. Both G fdisk and G used are initialized to G user ifgiven, to G bios when this information is present according to CMOS, and to to G phys otherwise. If G log looks reasonable then G used is set to that.
Otherwise,if G used is unreasonable and G phys looks reasonable then G used is set to G phys. Here `reasonable' means that the number of heads is in the range 1-16. To say this in other words: the command line overrides the BIOS, and willdetermine what fdisk sees, but if it specifies a translated geometry (with morethan 16 heads), then for kernel I / O it will be overridden by output of theIDENTIFY command.6.2. SCSI detailsThe situation for SCSI is slightly different, as the SCSI commands already uselogical block numbers, so a `geometry' is entirely irrelevant for actualI / O. However, the format of the partition table is still the same, so fdisk hasto invent some geometry, and also uses HDIO GETGEO here - indeed, fdisk does notdistinguish between IDE and SCSI disks. As one can see from the detaileddescription below, the various drivers each invent a somewhat differentgeometry. Indeed, one big mess.
If you are not using DOS or so, then avoid all extended translation settings,and just use 64 heads, 32 sectors per track (for a nice, convenient 1 MB percylinder), if possible, so that no problems arise when you move the disk fromone controller to another. Some SCSI disk drivers (aha152x, pas16, ppa, qlogicfas,qlogicisp) are so nervous about DOS compatibility that they will not allow aLinux-only system to use more than about 8 GB. This is a bug. What is the real geometry?The easiest answer is that there is no such thing. Andif there were, you wouldn't want to know, and certainly NEVER, EVER tell fdiskor LILO or the kernel about it. It is strictly a business between the SCSIcontroller and the disk.
Let me repeat that: only silly people tellfdisk / LILO/kernel about the true SCSI disk geometry. But if you are curious and insist, you might ask the disk itself. There is theimportant command READ CAPACITY that will give the total size of the disk, andthere is the MODE SENSE command, that in the Rigid Disk Drive Geometry Page (page 04) gives the number of cylinders and heads (this is information thatcannot be changed), and in the Format Page (page 03) gives the number of bytesper sector, and sectors per track. This latter number is typically dependent uponthe notch, and the number of sectors per track varies - the outer tracks havemore sectors than the inner tracks. The Linux program scsiinfo will give thisinformation. There are many details and complications, and it is clear thatnobody (probably not even the operating system) wants to use thisinformation.
Moreover, as long as we are only concerned about fdisk and LILO, onetypically gets answers like C / H/S=4476/27/171 - values that cannot be used byfdisk because the partition table reserves only 10 resp. 8 resp. 6 bits forC / H/S. Then where does the kernel HDIO GETGEO get its information from? Well, eitherfrom the SCSI controller, or by making an educated guess. Some drivers seem tothink that we want to know `reality', but of course we only want to know whatthe DOS or OS/2 FDISK (or Adaptec AFDISK, etc) will use.
Note that Linux fdisk needs the numbers H and S of heads and sectors per trackto convert LBA sector numbers into c / h/s addresses, but the number C ofcylinders does not play a role in this conversion. Some drivers use (C,H,S) = (1023,255,63) to signal that the drive capacity is at least 1023*255*63sectors. This is unfortunate, since it does not reveal the actual size, and wi imit the users of most fdisk versions to about 8 GB of their disks - a reallimitation in these days. In the description below, M denotes the total disk capacity, and C, H, S thenumber of cylinders, heads and sectors per track. It suffices to give H, S if weregard C as defined by M / (H*S). (Thus C is truncated, and H*S*C is notan approximation to the disk capacity M. This will confuse most versions offdisk.) The ppa. c code uses M+1 instead of M and says that due to a bug in sd. c Mis off by 1. advansys: H=64, S=32 unless C > 1024 and moreover the `> 1 GB' option in theBIOS is enabled, in which case H=255, S=63. aha1542: Ask the controller which of two possible translation schemes is in use,and use either H=255, S=63 or H=64, S=32.
In the former case there is a bootmessage 'aha1542. c: Using extended bios translation'. aic7 : H=64, S=32 unless C > 1024, and moreover either the 'extended' bootparameter was given, or the `extended' bit was set in the SEEPROM or BIOS, inwhich case H=255, S=63. buslogic: H=64, S=32 unless C >= 1024, and moreover extended translation wasenabled on the controller, in which case if M < 2^22 then H=128, S=32; otherwiseH=255, S=63. However, after making this choice for (C,H,S), the partition tableis read, and if for one of the three possibilities (H,S) = (64,32), (128,32), (255,63) the value endH=H-1 is seen somewhere then that pair (H,S) is used, anda boot message is printed 'Adopting Geometry from Partition Table'. fdomain: Find the geometry information in the BIOS Drive Parameter Table, orread the partition table and use H=endH+1, S=endS for the first partition,provided it is nonempty, or use H=64, S=32 for M < 2^21 (1 GB), H=128, S=63 forM < 63*2^17 (3.9 GB) and H=255, S=63 otherwise. in2000: Use the first of (H,S) = (64,32), (64,63), (128,63), (255,63) that willmake C <= 1024. In the last case, truncate C at 1023. seagate: Read C,H,S from the disk. (Horrors!) If C or S is too large, then put S=17, H=2 and double H until C <= 1024. This means that H will be set to 0 if M >128*1024*17 (1.1 GB).
This is a bug. ultrastor and u14 34f: One of three mappings ( (H,S) = (16,63), (64,32), (64,63)) is used depending on the controller mapping mode. If the driver does not specify the geometry, we fall back on an edu- cated guessusing the partition table, or using the total disk capac- ity. Look at the partition table. Since by convention partitions end on a cylinderboundary, we can, given end = (endC,endH,endS) for any partition, just put H =endH+1 and S = endS.
(Recall that sectors are counted from 1.) More precisely, thefollowing is done. If there is a nonempty partition, pick the partition with thelargest beginC. For that partition, look at end+1, computed both by adding startand length and by assuming that this partition ends on a cylinder boundary. Ifboth values agree, or if endC = 1023 and start+length is an integral multiple of (endH+1)*endS, then assume that this partition really was aligned on a cylinderboundary, and put H = endH+1 and S = endS. If this fails, either because thereare no partitions, or because they have strange sizes, then look only at thedisk capacity M. Algorithm: put H = M/ (62*1024) (rounded up), S = M/ (1024*H) (rounded up), C = M/ (H*S) (rounded down). This has the effect of producing a (C,H,S) with C at most 1024 and S at most 62