home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / install / libfdisk / TechNotes.txt < prev    next >
Encoding:
Text File  |  1997-09-17  |  12.7 KB  |  316 lines

  1. Notes on the libfdisk library (as of June 18, 1997)
  2. ---------------------------------------------------
  3.  
  4. Contents
  5. --------
  6.  
  7.   (1)     Background Information on Partitioning
  8.   (2)     Libfdisk Programming Notes
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. Section 1  - Background Information on Partitioning
  18. ---------------------------------------------------
  19.  
  20.  
  21.    Hard drives are normally partitioned which each serve a different
  22. function (root, /usr, /tmp, /var, etc).  There are some basic facts
  23. one needs to be aware of before writing software to manipulate these
  24. partitions.
  25.  
  26.  Some quick terminology:
  27.  
  28.  
  29.        Geometry  - describes the logical layout of data on a hard drive,
  30.                    in terms of cylinders, heads, and sectors.
  31.  
  32.        Cylinder  - fundamental unit to control size of a partition
  33.  
  34.        Head      - there are several heads per cylinder
  35.  
  36.        Sector    - basic component of a hard drive, made of 512 bytes
  37.  
  38.        Partition - subcomponent of a hard drive, composed of a
  39.                    whole number of cylinders.
  40.  
  41.        Partition Table - A partition table can hold 4 partition definitions
  42.  
  43.        Master Boot Record (MBR) - The first partition table on a drive
  44.                    occurs are sector 1, head 0, cylinder 0, the first
  45.                    sector on the disk. It contains a boot loader as well.
  46.  
  47.        Primary Partition - the 4 partitions in the MBR are the primary
  48.                    partitions.
  49.  
  50.        Extended Partition - To get past the limitation of 4 primary
  51.                    partitions, and extended partition is created. 
  52.                    It can contains an unlimited number of logical
  53.                    partitions.
  54.  
  55.        Logical Partition - Just like a normal partition, except it is
  56.                    located in an extended partition.
  57.  
  58.  
  59.  Some basic rules/facts:
  60.  
  61.   Partitions are defined by a partition table, which consists of the
  62.     last 66 bytes of a sector. The first part of the sector can possibly
  63.     contain a boot loader. The MBR always contains a boot loader, normally
  64.     created by LILO. The partition table in the MBR defines the 4
  65.     primary partitions. These are numbered '1' through '4'.
  66.  
  67.   A partition table entry looks like (in libfdisk the type is RawPartition)
  68.  
  69.      struct RawPartition {
  70.           unsigned char active;
  71.           unsigned char start_head;
  72.           unsigned char start_sec;
  73.           unsigned char start_cyl;
  74.           unsigned char type;
  75.           unsigned char end_head;
  76.           unsigned char end_sec;
  77.           unsigned char end_cyl;
  78.           unsigned int  start;
  79.           unsigned int  size;
  80.         };
  81.  
  82.   The fields are:
  83.  
  84.     active       - if 0x80, DOS will boot from this partition. 0 otherwise.
  85.     start_head   -\ 
  86.     start_sec    -|---- Define start of partition
  87.     start_cyl    -/
  88.     end_head     -\ 
  89.     end_sec      -|---- Define end of partition
  90.     end_cyl      -/
  91.     start        - 'linear' sector of start of partition
  92.     size         - partition size in sectors
  93.  
  94.  
  95.   Of note - The start cylinder is actually 10 bits, two of which are
  96.             stored in the up 2 bits of the sector. So the range of
  97.             values for each component is
  98.  
  99.              1 <= sector   <= 63
  100.              0 <= heads    <= 255
  101.              0 <= cylinder <= 1023
  102.  
  103.   Note that sectors start with 1, not zero. This is historical.
  104.  
  105.   Note also that IDE drives can only accept a head up to 63, so this
  106.     limits the range of heads you can send to the drive.
  107.  
  108.   One of the primary partitions can of type 5, which means it is an
  109.     extended partition.  This is like a normal primary partition,
  110.     except at the start of it is a partition table. This table defines
  111.     how the space WITHIN the expended partition is partitioned. It
  112.     can contain another extended partition, etc etc. These extended
  113.     partitions form a linked list. The non-extended partitions within
  114.     extended partitions are called 'logical' partition. They are numbered
  115.     suequentially in the linked list starting from '5'.
  116.  
  117.   To understand this, here is the example output from the command 'fdisk -l':
  118.  
  119.      Disk /dev/hdb: 64 heads, 63 sectors, 620 cylinders
  120.      Units = cylinders of 4032 * 512 bytes
  121.  
  122.         Device Boot   Begin    Start      End   Blocks   Id  System
  123.      /dev/hdb1            1        1       51   102784+  83  Linux native
  124.      /dev/hdb2           52       52       84    66528   82  Linux swap
  125.      /dev/hdb3           85       85      620  1080576    5  Extended
  126.      /dev/hdb5           85       85      288   411232+  83  Linux native
  127.      /dev/hdb6          289      289      542   512032+  83  Linux native
  128.      /dev/hdb7          543      543      620   157216+  83  Linux native
  129.  
  130.   This disk has a geometry of 64 heads, 63 sectors, 620 cylinders. Partitions
  131.     are defined in terms of cylinders, which are 64 heads/cyl * 63 sect/head,
  132.     or 4032 sectors each. There are 3 primary partitions, one of which is
  133.     an extended partition. There are 3 logical partitions as well.
  134.  
  135.   Graphically, the disks looks like:
  136.  
  137.       [PT = Partition Table, MBR = Master Boot Record]
  138.  
  139.    Sector #        
  140.             ----------------------------------
  141.          0  |MBR                           PT|  
  142.             |--------------------------------|  
  143.             |                                |  
  144.             ~                                ~  
  145.             |                                |  
  146.         63  |--------------------------------|  /dev/hdb1
  147.             |                                |  
  148.             | Primary partition, type 83     |  
  149.             |                                |  
  150.             ~                                ~
  151.             ~                                ~  
  152.             |                                |  
  153.     205632  |--------------------------------|  /dev/hdb2
  154.             |                                |  
  155.             | Primary partition, type 82     |  
  156.             |                                |  
  157.             ~                                ~  
  158.             ~                                ~  
  159.             |                                |  
  160.     338688  |--------------------------------|  /dev/hdb3 -------------
  161.             |                              PT|                       ^
  162.             |--------------------------------|                       |
  163.             ~                                ~                       |
  164.             |                                |                       |
  165.     338751  |--------------------------------|  /dev/hdb5            |
  166.             |                                |                       |
  167.             | Logical partition, type 83     |                       |
  168.             |                                |                       |
  169.             ~                                ~                       |
  170.             ~                                ~                       |
  171.             |                                |                       |
  172.    1161216  |--------------------------------|           ---------   |
  173.             |                             PT |                  ^    E
  174.             |--------------------------------|                  |    x
  175.             ~                                ~                  |    t
  176.             |                                |                  |    e
  177.    1161279  |--------------------------------|  /dev/hdb6       |    n
  178.             |                                |                  E    d
  179.             | Logical partition, type 83     |                  x    e
  180.             |                                |                  t    d
  181.             ~                                ~                  e    |
  182.             ~                                ~                  n    |
  183.             |                                |                  d    |
  184.             |                                |                  e    |
  185.    2185344  |--------------------------------|            ---   d    |
  186.             |                             PT |             ^    |    |
  187.             |--------------------------------|             |    |    |
  188.             ~                                ~             E    |    |
  189.             |                                |             x    |    |
  190.    2185407  |--------------------------------|  /dev/hdb7  t    |    |
  191.             |                                |             e    |    |
  192.             | Logical partition, type 83     |             n    |    |
  193.             ~                                ~             d    |    |
  194.             ~                                ~             e    |    |
  195.             |                                |             d    |    |
  196.             |                                |             |    |    |
  197.             |                                |             V    V    V
  198. end of disk |--------------------------------|            -------------
  199.  
  200.  
  201.   Note that the three extended partitions are nested within each other.
  202.  
  203.  
  204. ---------- End of Section 1------------------------
  205.  
  206.  
  207. Section 2 - Libfdisk Programming Notes
  208. ---------------------------------------------------
  209.   
  210.   Data Types (libfdisk.h):
  211.   -------------------------
  212.  
  213.    These are mostly internal use only:
  214.       RawPartition       -  exact representation of entry in partition table
  215.       RawPartitionTable  -  4 RawPartition's
  216.  
  217.    These apps usually use:
  218.       Partition              - abstracted representation of a partition. Apps 
  219.                                should normally use this, not RawPartition
  220.       PartitionTable         - Holds 4 Partition's plus other data
  221.       ExtendedPartitionTable - Holds contents of an extended partiton
  222.       HardDrive              - completely describes hard drive
  223.  
  224.   Functions:
  225.   ----------
  226.    In general, these routines return 0 on success. A value < 0 means
  227.      a serious error occurred and errno has the error. Values > 0
  228.      mean an libfdisk error occurred, like you passed a logical
  229.      partition number < 5. There should be a standard list of these
  230.      errors, but this does not exist yet. Read the function to see
  231.      how it is done.
  232.  
  233.   Raw low level i/o (rawio.[ch])
  234.   -----------------------------------
  235.  
  236.   These routines should not generally be used by an application, with
  237.     these exceptions:
  238.  
  239.    fdiskOpenDevice(char *name, HardDrive **hd)
  240.    fdiskCloseDevice(HardDrive *hd)
  241.  
  242.       These open a device and create a HardDrive structure describing it.
  243.       This HardDrive structure will be used by the programmer for practically
  244.       all other function calls in libfdisk. fdiskOpenDevice MUST be called
  245.       before you can do anything else!
  246.  
  247.   Other functions are:
  248.    
  249.    fdiskSectorToCHS() - convert absolute sector num to cyl/head/sector
  250.  
  251.    fdiskReadPartitionTable  () -|
  252.    fdiskWritePartitionTable () -+-- read/write a RawPartitionTable to disk
  253.  
  254.    fdiskReadMBR () -|
  255.    fdiskWriteBR () -+-- read/write the MBR RawPartitionTable to disk
  256.  
  257.    fdiskSetAttrPrimary() - set various attributes of a primary partition
  258.  
  259.    fdiskRemovePrimary()  - remove primary partition from disk
  260.  
  261.   Extended Partition Ops (extended.[ch])
  262.   -------------------------------------------
  263.  
  264.   These routines manipulate extended partitions. The primary partitions
  265.     are defined in a statically allocated structure in the HardDrive 
  266.     data type, whereas extended partitions form a linked list rooted at
  267.     the HardDrive data type. These routines should not generally be
  268.     called by an application.
  269.  
  270.    fdiskIsExtended() - returns 1 if partition type is extended, 0 otherwise
  271.  
  272.    fdiskInsertExtended() - insert extended partition into linked link
  273.  
  274.    fdiskRemoveExtended() - remove    ""       ""     from   ""    ""
  275.  
  276.    fdiskRenumberLogical() - go thru linked list, number partitions sequentially
  277.  
  278.    fdiskInsertLogical() - add logical partition to given extended partition
  279.  
  280.    fdiskRemoveLogical() - remove ""      ""    from ""      ""       ""
  281.  
  282.    fdiskSetAttrLogical() - set attributes of selected logical partition
  283.  
  284.    fdiskFindLogical() - find a given logical partition (>=5) in linked list.
  285.  
  286.    
  287.   User Operations
  288.   ----------------------
  289.  
  290.   These operations are what and app should generally be using.
  291.   They use partition #'s, which are 1-4 for primary partitions, and >=5
  292.   for logical partitions.
  293.  
  294.   These operations attempt to hide the difference between extended and
  295.   primary partitions. These application just manipulates the number,
  296.   location, and sizes of a list of partitions. When the manipulation is
  297.   finished, the application can then dump the new configuration to disk.
  298.  
  299.     fdiskReadPartitions()   -|
  300.     fdiskWritePartitions()  -+-- Read/Write all partitions from a disk
  301.  
  302.     fdiskInsertPartition()  -|
  303.     fdiskRemovePartition()  -+-- Insert/Remove a partition from disk
  304.  
  305.     fdiskSetAttrPartition() - change attributes of a partition
  306.  
  307.    Following need to be written:
  308.  
  309.     fdiskVerifyPartitions() - do a check that current table makes sense
  310.  
  311.     fdiskPrintPartitions()  - output string buffer of current partitions
  312.  
  313.     fdiskPartitionType()    - convert partition type to a human read. string
  314.  
  315.  
  316.