Click to See Complete Forum and Search --> : To understand a partition table in Linux


saikee
05-22-2009, 07:18 PM
Introduction

In migrating some non-Linux operating systems we have to be precise with their partition boundaries. This requires us to have a good understanding of how a hard disk is partitioned.

I wrote this thread to make it easier for a user to read a partition table in Linux. A partition table does not get changed when the same hard disk is used in Microsoft Windows, BSD or Solaris operating systems. Therefore even a user has no interest in Linux the partition table as displayed in Linux can be regarded at least a very good “second opinion” of the status of the hard disk.

Linux has a superb collection of partitioning tools. Since Linux has been designed to co-exist with all other PC systems the partition table information in Linux is therefore comprehensive and have information otherwise omitted by the other operating systems interested in displaying information only relevant to their own systems.

A simple partition table

For demonstration I first deleted all the entries in the partition table of disk sdb by using a simple script as follow:-
for ((i=1; i<=4; i++)); do echo d;echo $i;done > k4; echo w>>k4 | fdisk /dev/sdb < k4

Next I use another script to generate 3 primaries and one logical partition each 10 cylinders large in the same hard disk sdb

for ((i=1;i<=5;i++));do echo n;if [ $i = 4 ];then echo e;echo;echo +9;elif [ $i = 5 ];then echo;echo +9;echo w; else echo p; echo $i; echo; echo +9;fi;done >c4 | fdisk /dev/sdb < c4

When I list the new partition table I got
root@saikee-desktop-1:/home/saikee# fdisk -l /dev/sdb

Disk /dev/sdb: 61.4 GB, 61491756544 bytes
255 heads, 63 sectors/track, 7475 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xc7d8763d

Device Boot Start End Blocks Id System
/dev/sdb1 1 10 80293+ 83 Linux
/dev/sdb2 11 20 80325 83 Linux
/dev/sdb3 21 30 80325 83 Linux
/dev/sdb4 31 40 80325 5 Extended
/dev/sdb5 31 40 80293+ 83 Linux


The first column is the partition names, the second column is the starting cylinder number, the third column is the ending cylinder number, the fourth column shows the number of blocks in each partition, the fifth column shows the partition types and the last column describes the system the partition associated with.

Over 100 partition types are supported by Linux as shown below
0 Empty 1e Hidden W95 FAT1 80 Old Minix bf Solaris
1 FAT12 24 NEC DOS 81 Minix / old Lin c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 82 Linux swap / So c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c7 Syrinx
5 Extended 41 PPC PReP Boot 85 Linux extended da Non-FS data
6 FAT16 42 SFS 86 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set de Dell Utility
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext df BootIt
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a5 FreeBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a6 OpenBSD f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux RAID auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT

The partition type or ID is very important for a Linux user doing multi-booting as that tells what the operating system likely to be inside each partition.

A modern hard disk still counts the number of bytes by cylinders, heads and sectors. It is universal for a modern hard disk to have 512 bytes per sector because apparently that is what the read/write head does in each procedure call. If a user wants only one byte from the hard disk the read/write arm still reads a whole sector and gives it to the operating system. It is the operating system that separates the byte out to meet the request from the user.

The baker's way of seeing a cylinder of a hard disk

The best way to visualise the hard disk storage is to imagine a cylinder is a loaf of bread. The length of the bread is measure by number of head. The height of the loaf is measured by the number of sectors and the thickness of the loaf is measured by the number of bytes. Therefore if a user cut vertically a slice from the end of the loaf he gets the first track of 63 sectors high by 512 bytes wide. To get the MBR he has to put the slice flat and cut the edge of its first sector off. The thin piece of bread is now one sector thick and 512 bytes long. If a partition is bootable then the first slice from the first loaf is always cut away and reserved as the “boot sector” for the purpose of keeping the boot loader inside.

When a hard disk claims to have 7475 cylinders one can imagine it as 7475 loaves of bread of identical size.

Notice the small difference in sizes?

The command “fdisk-l /dev/sdb” displays the disk sdb partition table which is the modern Logical Block Addressing (LBA) convention. The storage in a hard disk is now measured by the number of blocks each 1024 bytes long. Therefore when I have 10 cylinders, 255 heads per cylinder, 63 sectors per head and 512 bytes per sector I should have 10x255x63x512 = 82,252,800 bytes which divided by 1024 should give me exactly 80,325 blocks. This is indeed the figure I have for sdb2 and sdb3.

It can be seen that even I have created 4 partitions of identical size of 10 cylinders each but the partition table has shown two different sizes. Primary partition sdb1 and logical partition sdb5 are smaller than the two primary sdb2 and sdb3 by about 32kb.

The 32kb is only a fraction of a cylinder which has 8,225,280 bytes. Thus this information is not easy to to see when the nearest number of cylinders is reported in the partition table.

This information can be confirmed if I ask a different partitioning tool “parted” for a second opinion.

Model: ATA IC35L060AVVA07-0 (scsi)

Disk /dev/sdb: 61.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 32.3kB 82.3MB 82.2MB primary ntfs
2 82.3MB 165MB 82.3MB primary
3 165MB 247MB 82.3MB primary
4 247MB 329MB 82.3MB extended
5 247MB 329MB 82.2MB logical

The above information shows the partition sda1 starts at 32.3k position.

The partition table tells us the following

(1) Location of the MBR - Primary sdb1 does not commence from the beginning of the first cylinder. It commences after 32kb. This shows the MBR of the hard disk is a separate entity and has a complete track of 63 sectors of 63x512 = 32256 bytes permanently reserved for it. Thus the MBR is always inside the first cylinder of first primary partition for the purpose of providing a record of the partition table and the starting code of the booting operation. In a NT version Microsoft Windows it is called partition(0). In Grub when we define (hd0) as the root we boot the MBR but if we choose the root as (hd0,0) we boot the system inside sdb1.

(2) Primary partition 2 to 4 have no partition table - The second and third primaries are larger because potentially they can be data-only partitions and have no necessity to set aside one track to store the MBR or the partition table. I have used up the primary sdb4 as an extended partition. Had I not done that the sdb4, as a primary partition, will be the exactly the same size as sdb2 and sdb3. In the event sda4 is indicated as an extended partition and since an extended partition has no partition table it has the full size of 80325 blocks. The partition tables for the 1-4 primaries are inside the MBR so there is no need for each primary partition to keep its own partition table.

(3) Each logical partition needs its own partition table - The logical partition sdb5 has exactly the same size as the first primary sdb1. This is because the partition table in the MBR only records the first 4 primaries and every logical partition must carry its own partition table which, apart from defining its own beginning and end points in the hard disk, it has to carry the hard disk address for the next logical partition down the chain. Thus only a logical partition can match exactly the same size as the first primary partition because both have one track less for housing the partition table. This also explain why logical partition must work as a continuous chain.

(4) A Partition always starts and ends at exactly the cylinder boundary - It is common in many operating systems, Microsoft Windows and Linux, to have a partition created in units of the nearest cylinder. This is to say the boundary of a partition must end at a cylinder and the next partition will have to start from the next cylinder.