# 102.1 Design hard disk layout

Candidates should be able to design a disk partitioning scheme for a Linux system
Key Knowledge Areas
* Allocate filesystems and swap space to separate partitions or disks.
* Tailor the design to the intended use of the system.
* Ensure the /boot partition conforms to the hardware architecture requirements for booting.
* Knowledge of basic features of LVM.

## Introduction

When installing an operating system you will need to design the layout of your hard-disk partitions and choose a filesystem you will use to format them. The choices you make will depend on the number and size of mass storage devices you have and the purpose for which the computer will be used.
Linux creates filesystems on block devices. A device whose data can only be accessed sequentially, such as keyboards and mice are character devices but devices such as hard-disks, that can have their data accessed randomly are call block devices. Each type of device has different functionality to cater for the differences in data access methods. The Linux kernel's device-mapper framework allows for the creation of virtual block devices on which filesystems can be created.
The advantage of virtual block devices is that Linux can provide functionality such as encrypted drives and raid devices without the need for specialist hardware. Virtual block devices allow for the creation of LVM (Logical Volume Manager) drives and software raid with mdadm for example.
In this manual we will only cover block device drivers that map directly to the underlying hard-disk. i.e. we exclude virtual block device drivers and look at creating filesystems directly on physical disk partitions.
The word partition is used in several different contexts when discussing Linux systems. Sometimes it is used to refer to the partitions on a hard disk and in other context it is used to refer to the location of directories. For example the root partition, which contains the core Linux configuration files, may actually be spread across many disk partitions if it is created on a LVM block device for example.

## Hard Drive Partitions

Originally X86 machines' hard disks only supported a maximum of 4 partitions as, at the time, this was considered more than enough for anyone's needs. Now-a-days it is common to have more than 4 partitions on a single disk. To overcome the 4 partition limitation, in a manner that was backwardly compatible, the standard was extended to enable one of the primary partitions to be created as an “extended partition” which allowed for the creation of many “logical partitions” on the extended partition.
Figure 102.1-1: Hard Disk Partitions
The 4 partitions originally supported by the X86 architecture are called primary partitions. If you need more than 4 partitions, then at least one of the primary partitions needs to be created as an extended partition. The extended partition can then have multiple logical partitions created on it.
Under Linux primary partitions are assigned the number 1-4 depending on the primary partition being created. The extended partition, of which there can be only one, is assigned the number 4 with logical partitions being assigned numbers greater than 4.
So for a master PATA drive on the primary connector the 1st primary partition would be /dev/hda1 and the 3 primary partition would be /dev/hda3, while the extended partition would be /dev/hda4 and the first logical partitions numbered /dev/hda5. Primary partition numbers can be skipped so it is possible to have a disk layout such as /dev/hda1, /dev/hda3 and /dev/hda5 /dev/hda6 /dev/hda7 for the 1st primary partition, extended partition, and then two logical drives on the extended partition (hda3).
If you intend to have more than 4 partitions, it is important to ensure that you create an extended partition for one of the primary partitions, as it is not possible to convert a primary partition to an extended partition. If you need an extended partition, and all primary partitions are used you would have to delete one of the primary partitions to create the extended partition.

## Common Partition Schemes

You may ask why would you need to partition a hard drive and in some cases, especially desktop computers it may not be necessary to provide for more than two partitions. You will always need at least two partitions to cater for how Linux handles virtual memory. Virtual memory is the amount of memory the kernel tells its applications is available for use and this can often exceed the amount of physical memory available. In order to cater for the extra memory which does not really exist, the kernel uses hard disk space as virtual memory. The algorithms used to manage virtual memory and swapping of memory to disk and back are beyond the scope of this manual. What is important to know is that the kernel needs a special disk partition, known as swap space, to use for virtual memory management.
Besides swap space there are other reasons for using extra partitions, especially in machines with more than one disk. These include: preventing log files and temporary files from consuming too much disk space, improving disk performance by splitting read and writes across partitions on different disks, planning for disk expansions and creating a flexible layout for the future. If you have multiple disk, splitting the filesysem across disks will help isolate parts of the system from disk failures in different drives. Even in the case of laptops, which can only take a single drive, there are good reasons for creating a flexible disk layout, for example you may need to potentially accommodate a multi-boot environment later or to make it easier to run virtual machines with virtual hard-disks.
One should be careful though not to create too many partitions, especially when you are not taking advantage of one of the Linux kernels virtual block devices, such as LVM, which enable dynamic allocation of disk partitions to filesystems, as it is a common problem to be running out of disk space on one partition while there is still space available on another, so plan your disk partitioning strategy carefully.
| /boot partition | 50-100MB usually on the 1st partition to ensure all kernels are below the 1024 cylinder limit for older kernel BIOS. |
| Swap | Swap space allocation depends on the amount of physcial ram available. There is much debate about the optimal size of the partition for swap space, especially as the amount of physical memory available in most systems is increasing, but a general rule of thumb is to use twice the amount of ram available but a maximum of around 1 G is recommended. |
| / | The root filesystem partition contains the rest of the Linux operating system and should usually be allocated the remaining disk space. Most distributions today will install the root partition on an logical partition to allow for the possibility of creating more than 4 partitions in future should this be necessary. If all the available space has been allocated to the root partition, it may be necessary to resize the root partition to free up space before this can be done. |
| /boot partition | 100MB-250MB In more modern computers, that do not have the 1024 cylinder limit, you may want to allocate more space to the boot to accommodate additional kernels. Keeping the /boot separate may be necessary especially if the remaining filesystems are loaded on virtual block devices which the boot loader cannot access natively, for example they root filesystem is on a software raid5 block device. |
| swap | As above |
| /home | The /home directory is where all user data and documents are kept. A separate partition means that it will be easier to backup data and protects the kernel and other core system files from running out of disk space due to users storing too much data in their home directories. |
| /usr | The user directory contains is user application. Usually the /usr directory does not grow that much in size and is fairly static. |
| /var | The /var directory contains system log files. Logs files, especially if not maintained properly, can rapidly fill up disk space. By placing the /var directory on a different drive the rest of the system is protected from running out of disk space. |
| /tmp | The /tmp directory is used to house all temporary files. The directory should be cleaned up on reboot or when users log out but often files are left behind. Over time this can cause the system to run out of disk space. Placing /tmp in its own directory protects the rest of the system form this. |
| / | The remainder of the filesystem. |

## How to Partition Using fdisk

During installation you may be presented with a graphical interface to partition your disks but usually when creating or editing your drives partitions you will use a utility such as fdisk.
The fdisk command takes the block device you wish to partition as a parameter. For example to partition the master drive on the primary connector for a PATA drive the command would be
* # fdisk /dev/hda
The fdisk utility provides you with a command prompt to create and delete partitions on block devices.Pressing “m” at the prompt will provide you with a list of possible actions to perform on the block device.
* Command action options
* a toggle a bootable flag
* b edit bsd disklabel
* c toggle the dos compatibility flag
* d delete a partition
* l list known partition types
* m print this menu
* n add a new partition
* o create a new empty DOS partition table
* p print the partition table
* q quit without saving changes
* s create a new empty Sun disklabel
* t change a partition's system id
* u change display/entry units
* v verify the partition table
* w write table to disk and exit
* x extra functionality (experts only)
To Create A Partition
Typing “n” at the command prompt will result in a prompt asking if you want to create an extended or primary partition.
* Command action
* e extended
* p primary partition (1-4)
It is usually good practice to create at least one of your partitions as an extended partition. Once an extended partition has been created subsequent requests to create a new partition will result in a prompt asking if you want to create a logical or primary partition, assuming you have not created the maximum number of primary partitions.
* Command action
* l logical (5 or over)
* p primary partition (1-4)
Once you have your drive partitioned you need to decide what type your partition will be. To get a list of partition types type “l” at the fdisk prompt. This will list a large number of potential partition types but the ones you will be interested in are:
* 82 Linux Swap
* 83 Linux
* 8e Linux LVM
* 85 Linux Extended
For the purposes of this manual we will consider only partition ids 82 and 83. Types 8e and 85 are examples of partition types that are used to create virtual block devices.
Formatting Partitions with Filesystems
A filesystem is a way of storing data in a computer-accessible form on the hard disk. Different filesystems have different algorithms and structures to determine where the data and indexing information, needed by the computer to find data, will be stored. Meta-data relating to the organisation of the filesystem is stored in areas of the disk called superblocks. Superblocks contain critical information for the computer to find files and data on disk. To protect the filesystem from becoming inaccessible when superblocks become corrupted Linux filesystems store multiple copies of the superblock at defined offsets. Some popular Linux filesystems include:
ext2 – the oldest and most well supported filesystem on Linux machines.
ext3 - an extension of the ext2 filesystem which adds journaling support
reiserfs - an enhanced journaling filesystem written by Hans Reiser
For more information on filesystems and how to create them see section 104.1
Managing Swap Space
We have covered swap space earlier in this chapter. Swap space does not contain a file system but is accessed in raw mode by the Linux kernel. Bypassing a filesystem has some speed advantages which is crucial for swap space. After creating the disk partition to be used as swap space , you will need to activate it with
swapon [device] e.g. swapon /dev/sda2
During system installation this will be done automatically for you. Information on the swap partition can be displayed with swapon.
swapon -s # Display the available swap partitions
Mounting filesystem Partitions
When you create a filesystem partition it needs to be mounted to be made available to the system. A mount point makes the filesystem patition available under a well defined location under root (/). Linux follows a well defined filesystem hierarchy and mounting the partition under the correct mount point is crucial if the system is to operate properly.
Mount points are defined under the /etc/fstab configuration file.
Knowledge of basic features of LVM
What is LVM?
LVM is a Logical Volume Manager for the Linux operating system and it is a way or means of allocating hard drive space into logical volumes that can be easily resized instead of partitions.. There are two version of LVM:
* LVM 2 – The latest is version of LVM for Linux. LVM 2 is backward compatible with volumes created with LVM 1. LVM 2 uses the device mapper kernel driver. Device mapper support is in the 2.6 kernel tree and there are patches available for current 2.4 kernels.
* LVM 1 – The version that is in the 2.4 series kernel.
One or more hard drives are allocated to one or more physical volumes but a physical volume can not span over more than one drive The physical volumes are combined into logical volume groups excluding the boot partition. The boot partition can not be included onto a logical volume group because it makes it difficult for the boot loader to read it. Therefore If your root / partition is on a logical volume you should make sure that a separate /boot partition is created and not is not part of any volume group. Since physical volumes can not span over more than one drive, to span over more than one drive, create one or more physical volumes per drive and then link them together through a logical Volume group. .
We start at the bottom, more or less.
The physical media
The word physical simply a hard disk, or a partition. Examples, /dev/hdc, /dev/hdc1, /dev/sda.
Physical Volume (PV)
A PV is nothing more than a physical medium or hard drive with some administrative data added that LVM will recognise.
Physical Extents (PE)
Physical Extents are just big blocks, often with a size of megabytes. Volume Group (VG) :- A VG is made up of a number of Physical Extents (which may have come from multiple Physical Volumes or hard drives). One could generally think of a VG as being made up of several hard drives (/dev/hda and /dev/sda for example) but it’s more accurate to say that it made up of PEs which may be provided by multiple hard drives
Logical Volume (LV) A Logical Volume is the end result of our work, and it's there that we store our information. This is equivalent to the historic idea of partitions.
This shows us two filesystems, spanning two disks. The /home filesystem contains 4 Physical Extents, the /opt filesystem 2. Basic LVM Commands: The following commands can be used to commands to show the physical volume, volume group and logical volume status: pvdisplay, vgdisplay and lvdisplay.
A Scenario explaining how useful LVM could be: Say that you have a 20GB disc and you create the "/home" partition using 15GB and /var 1GB. Imagine that you run out of space and decide that you need 18GB in "/home". Using the old notion of partitions, you'd have to have another drive at least 20GB in size. You could then add the disc, create a new /home, and copy the existing data over. However, with an LVM setup, you could simply extend with free space repartitioned or just add a 5GB (or larger) disc, and add it's storage units to the "/home" partition. Other tools allow you to resize an existing file-system, so you simply resize it to take advantage of the larger partition size and you're back in business. LVM can even make 'snapshots' of itself which enable you to make backups of a non-moving target.
What can it do for me?
* LVM == Logical Volume Manager
* Flexibility
* High availability
* Reliability
* Key Benefits Of LVM:
* Arbitrary "partition" resizing (flexibility).
* Migrate data easily--swap drives on a running system (availability).
Filesystem This filesystem is whatever you want it to be: the standard ext2, ReiserFS, NWFS, XFS, JFX, NTFS, etc... To the linux kernel, there is no difference between a regular partition and a Logical Volume.
Some LVM terminology and Commands:
To show LVM version: lvm version To create the physical disks: pvcreate /dev/sda pvcreate /dev/sdb pvcreate /dev/sdc1 pvcreate /dev/sdc2
To show allocated Physical volumes: pvscan pvdisplay lvm pvs
To setup a volume group: vgcreate vg_name /dev/sda /dev/sdb /dev/sdc1 /dev/sdc2
To display a volume group: vgdisplay lvm vgs
To display a logical volume: lvdisplay lvm lvs
To create Logical Volume: Sets the size to 1GB lvcreate -L1G -n lv_name vg_name
Used files, terms and utilities:* / (root) filesystem
* /var filesystem
* /home filesystem
* swap space
* mount points
* partitions
Previous Chapter | Next Chapter