Disk Partitions

Disk partitioning is the creation of one or more regions on a hard disk, so that an operating system can manage information in each region separately.

Partitioning is typically the first step of preparing a newly manufactured disk, before any files or directories have been created.

The disk stores the information about the partitions locations and sizes in an area known as the partition table that the operating system reads before any other part of the disk.

Each partition then appears in the operating system as a distinct “logical” disk that uses part of the actual disk.

System administrators use a program called a partition editor to create, resize, delete, and manipulate the partitions.

When a hard drive is installed in a computer, it must be partitioned before you can format and use it.

Partitioning a drive is when you divide the total storage of a drive into different pieces. These pieces are called partitions. Once a partition is created, it can then be formatted so that it can be used on a computer.

Why have multiple partitions ?

  • Separation of the operating system and program files from user files.
  • Having a separate area for operating system virtual memory swapping/paging.
  • Keeping frequently used programs and data near each other.
  • Having cache and log files separate from other files. These can change size dynamically and rapidly, potentially making a file system full.
  • Protecting or isolating files, to make it easier to recover a corrupted file system or operating system installation. If one partition is corrupted, other file systems may not be affected.
  • Raising overall computer performance on systems where smaller file systems are more efficient.
  • Partitioning for significantly less than the full size available when disk space is not needed can reduce the time for diagnostic tools such as checkdisk to run or for full image backups to run.

The structure of disk partition

  • On the disk where operating system is installed, will have the first partition as MBR.
  • MBR is a Master Boot Record, which contains two important utilities, IPL ( Initial Program Loader) and PTI (Partition table information).
  • IPL is responsible for booting the operating system, because it contains the boot loader.
  • In earlier versions of Linux i.e upto RHEL 4, the default boot loader was LILO (Linux Loader. But, since RHEL5 on wards it has been changed to GRub(Grand Unified Boot Loader), which is far more superior to LILO.
  • The PTI (Partition table information) is the information about the number of partitions on the disk, sizes of the partition and types of partitions.

The criteria of disk partitioning.

  • Every disk can have only 3 Primary partitions.
  • Primary partition is a partition which usually holds the operating system. Only one amongst the three partitions can be active which will be booted by MBR to load the operating system
  • Extended Partition is a special type of primary partition which can be subdivided into multiple logical partitions. As there can be only 3 primary per disk, and if the user is required to make further partitions then all the space remaining on the disk should be allocated to extended partitions, which can be used to create the logical partitions later. There can be only one extended partition per disk.
  • Logical partitions are the partitions which are created under extended partitions, all the space in the extended partition can be used to create any number of logical partitions.

Disk Identification

  • IDE drive will be shown as /dev/hda
  • SCSI dive will be shown as /dev/sda
  • Virtual drive will be shown as /dev/vda

To view existing partitions :

The following command list all existing disk partition on your system.

# fdisk -l

or

# parted -l

Partition administration  using fdisk.

To enter into disk utility, the syntax is fdisk diskname

# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help):

Use m to list out various options that can be used in fdisk.

Creating a new partition

If you have free space left on your disk /dev/sda and would like to create a new partition under it. Type the following command to enter into command mode of specific hard disk.

# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help):

After entering in command mode, now press n command to create a new partition under /dev/sda with specific size.

# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): n
Command action
  e extended
  p primary partition (1-4)
e

You can enter the size of cylinder by adding +500M in last cylinder. Here, + means addition and 500M means size of new partition.

After creating a new partition, you should run w command to alter and save new changes to partition table and finally reboot your system to verify newly created partition.

Command (m for help): w

The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Deleting a partition

If you would like to delete a specific partition from disk /dev/sda, use d option to delete a partition and specify the device name.

# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): d
Partition number (1-4):

Saving the partition changes

Every time you make a partition or delete a partition, the changes made has to be saved using w, otherwise the creation and deletion will not be considered to be happen.

To quit without saving, use q option.

Command (m for help): w

The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Updating the partition table with out restarting the system.

After creating or deleting a partition the changes will be effected in the partition table only when after restart the system. But there is way to avoid this circumstance. We can use partprobe or partx command to update the partition information without restarting the system.

# partprobe /dev/sda

or

# partx -a /dev/sda

or

# kpartx /dev/sda

Note: In RHEL6 partprobe is not functioning properly, so it is recommended to use partx command only.

Formatting a partition.

After creating a partition we need to assign some file system to it so that we can start storing the data into it.

Syntax : # mkfs.ext4 partition_name

# mkfs.ext4 /dev/sda4

Like wise you can format the different partitions with different file systems like below.

# mkfs.ext3 /dev/sda5

# mkfs.vfat /dev/sda6

Check Size of a Partition.

Use s (displays size in blocks) with fdisk command.

# fdisk -s /dev/sda2
4096000

Fix Partition Table Order.

If you have deleted a logical partition and again recreated it, you might notice partition out of order problem or error message like Partition table entries are not in disk order.

To fix such partition order problems, and assign sda4 to the newly created partition, issue the x to enter an extra functionality section and then enter f expert command to fix the order of partition table as shown below.

# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): x
Expert command (m for help): f
Done.
Expert command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Creating a Swap partition

  • Create a normal partition with fdisk command by using n option.
  • Next, Change hex code to make it swap partition by using t option. The hex code for swap is 82.
  • Update the partition table table using partx -a or kpartx command.

Format the partition with swap file system

# mkswap /dev/sda6

Turn on newly created swap space and verify it.

# swapon /dev/sda6
# swapon -s
# free -m

Making the newly created swap partition to mount after reboot

In order to make the swap partition mount automatic after reboot, we need to make an entry in /etc/fstab file.

entry : /dev/sda6    swap    swap    defaults    0    0

# vi /etc/fstab

Removing the swap partition

  • Deactivate the swap partition using # swapoff /dev/sda6
  • Remove the entry from  /etc/fstab
  • Delete the partition through fdisk.

 

Advertisements