Partitioning and Filesystems

Howto create filesystems on a Linux System

What is a Linux Filesystem?


A filesystem is an organisation of data and metadata on a storage device. Basically filesystems contain files, directories and sub directories. Linux has a tree like structure with the "/" as its root directory. Each partition that you create on a block device or disk will have a filesystem added to it. The tree view structure is created when you mount a filesystem on a device at a point known as a "mount point". This will become clearer in the examples that follow.
The general layout of directories within this tree like structure is covered in our section File Hierarchy Standard.


Partitioning


Partitioning is the name given to the slicing up of a hard disk into slices or partitions. Each partition is created to a given size, no partition may overlap. Any space that is not used on a disk is classed as free space. Linux supports many different partition formats, however the most common type on a Linux PC is the MS-DOS format. This format supports up to four primary partitions. One of these primary partitions can be replaced with an extended partition which can contain up to twelve logical partitions. This gives us fifteen usable partitions as we do not count the extended partition as it is not usable for normal data. In the examples that follow we will be using the above format, however, a format does exist for larger drives that will allow for more partitions to be used. This format is known as the "GUID Partition Table" of "GPT". This format can support up to 128 partitions by default.



Disk and Linux


The Linux operating system can support many different types of disk. Linux can handle IDE, SCSI, CDs, DVDs and removable media such as USB memory sticks and external drives. The most common type of disks are IDE and the newer SATA disks. The original IDE standard could support two disks on one connection. These connections were known as "Master" and "Slave". Many PCs also had a second IDE connection giving access to two more "Secondary" devices. Another not so popular type of disk used by PCs was "SCSI". Although these were often much faster, they also had a much higher price. In the last few years a newer standard has emerged called "SATA". SATA is a modern version of IDE. However, under Linux they will have the same naming standard used by SCSI disks (SCSI emulation layer).


Disk naming conventions


IDE Devices

/dev/hda
Primary Master IDE

/dev/hdb
Primary Slave IDE

/dev/hdc
Secondary Master IDE

/dev/hdd
Secondary Slave IDE


SCSI Devices

/dev/sda
First SCSI Drive

/dev/sdb
Second SCSI Drive


/dev/sdc
Third SCSI Drive

It is important to note that SCSI devices do not have the four device limitation like that of IDE devices. Also some Linux distributions use the SCSI emulation layer for working with your disks even though they maybe IDE!


Primary Partitions


As mentioned earlier there can be up to four primary partitions on a disk, these are normally numbered as:

/dev/hda1
/dev/hda2
/dev/hda3
/dev/hda4


Extended Partitions


An extended partition takes one of the allocations that a primary partition could take. However, the extended partition can not contain usable data. Instead it contains references logical partitions. You can have a maximum of 12 logical partitions. Only one extended partition is allowed on a physical disk.



Logical Partitions


A logical partition exists within an extended partition. Logical partitions are numbered from five through to sixteen (12 in total).



Simple Example of a Partition Layout


To display your current partition layout you can issue the command "fdisk -l" as "root". The example below shows a very simple layout utilising one physical disk:



root@john-desktop:~# fdisk -l /dev/sda

Disk /dev/sda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders, total 312581808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000132a7

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   307357695   153677824   83  Linux
/dev/sda2       307359742   312580095     2610177    5  Extended
/dev/sda5       307359744   312580095     2610176   82  Linux swap / Solaris


Partitioning a new disk with fdisk


fdisk is one of the most popular partitioning tools that is bundled with most Linux distributions. To work with fdisk you will need to be running with escalated privileges or as root. "fdisk" is started by typing fdisk followed by the device you are going to be working with.

!! WARNING -- fdisk is a very powerful command and can completely destroy a valid working partition. This command should only be used if you are 100% sure of what you are doing.

Example of fdisk's main menu screen



Command (m for help): m
Command action
   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)

In the following examples I will be using a test server running under virtual box to demonstrate fdisk in use. For testing I have created a 2Gb disk which we will divide into smaller chunks.

First let us take a look at our current configuration. To do this we issue the "fdisk -l" command as "root":



[root@centos ~]# fdisk -l

Disk /dev/sda: 9663 MB, 9663676416 bytes
255 heads, 63 sectors/track, 1174 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00095943

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1175     8924160   8e  Linux LVM

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_centos-lv_root: 7021 MB, 7021264896 bytes
255 heads, 63 sectors/track, 853 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_centos-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

From the above we can see that there are two devices on our CentOS system. "/dev/sda" and "/dev/sdb". The first device "/dev/sda" is a 9GB disk which contains two partitions "sda1 and sda2". The second device "/dev/sdb" is 2GB in size and has no partition table. This is the disk we are going to partition.

In the following example, we are going to create seven partitions on our 2GB disk. 2147MB divided by seven partitions roughly equates to around 300MB per partition.

Based on above I have 261 Cylinders so 261 cylinders divided by 7 partitions is roughly 37 cylinders per partition.

I have 4 primary partitions 3 of these will be 37 cylinders in size. The extended partition will be 4 * 37 = 148 cylinders.

Example:



[root@centos ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xb85002be.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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)
p
Partition number (1-4): 1
First cylinder (1-261, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): 37

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (38-261, default 38): 38
Last cylinder, +cylinders or +size{K,M,G} (38-261, default 261): 75

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (76-261, default 76): 76
Last cylinder, +cylinders or +size{K,M,G} (76-261, default 261): 113

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Selected partition 4
First cylinder (114-261, default 114): 114
Last cylinder, +cylinders or +size{K,M,G} (114-261, default 261):
Using default value 261

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb85002be

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          37      297171   83  Linux
/dev/sdb2              38          75      305235   83  Linux
/dev/sdb3              76         113      305235   83  Linux
/dev/sdb4             114         261     1188810    5  Extended


As you can now see we have created four partitions. Three primary and one extended partition.

Next we are going to create the logical partitions



Command (m for help): n
First cylinder (114-261, default 114): 114
Last cylinder, +cylinders or +size{K,M,G} (114-261, default 261): 151

Command (m for help): n
First cylinder (152-261, default 152): 152
Last cylinder, +cylinders or +size{K,M,G} (152-261, default 261): 189

Command (m for help): n
First cylinder (190-261, default 190): 190
Last cylinder, +cylinders or +size{K,M,G} (190-261, default 261): 227

Command (m for help): n
First cylinder (228-261, default 228): 228
Last cylinder, +cylinders or +size{K,M,G} (228-261, default 261): 261

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb85002be

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          37      297171   83  Linux
/dev/sdb2              38          75      305235   83  Linux
/dev/sdb3              76         113      305235   83  Linux
/dev/sdb4             114         261     1188810    5  Extended
/dev/sdb5             114         151      305203+  83  Linux
/dev/sdb6             152         189      305203+  83  Linux
/dev/sdb7             190         227      305203+  83  Linux
/dev/sdb8             228         261      273073+  83  Linux


Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Once we have finished creating our logical partitions, we can check our layout by selecting the "P" option from the menu. This will display our current partition table. It is important to remember at this point we have not written these changes, they are only in memory. TO make our changes permanent, we need to issue the "w" command to write out our new partition table.


Creating filesystems on partitions


The next step to making our new partitions usable is to format these with a filesystem of our choice. The command for creating a filesystem is "mkfs". The basic syntax is: mkfs [options] [-t type fs-options] device [size]

Now we must create our filesystems on our new partitions. Only the primary and logical partitions require a filesystem. We do not need to use the "mkfs" command on the Extended partition.



[root@centos ~]# mkfs.ext3 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
74296 inodes, 297168 blocks
14858 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
37 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729, 204801, 221185

Writing inode tables: done                           
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

We have used the command "mkfs.ext3 /dev/sdb1" to create a filesystem type of Ext3 on partition sdb1. We will repeat this process with each of our partitions (sdb2,sdb3,sdb5,sdb6,sdb7,sdb8). Once we have created our filesystems we will need to create a mount point.


Mount Points


A mount point is basically an empty directory that is attached to the current filesystem. Once created we can attach our new filesystems to this point.

When we mount a filesystem we are basically indicating to the system which partition/device we want to mount at a given mount point. The basic syntax for mounting is: mount /dev/sdb1 /test1. Here "/dev/sdb1" is our device and "/test1" is an empty directory we shall use for our mount point.

Before we can issue our mount command, we need to create some directories that we can use for our mount point:



[root@centos /]# mkdir test{1..7}

[root@centos /]# ls -l

drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test1
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test2
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test3
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test4
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test5
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test6
drwxr-xr-x.   2 root root  4096 Mar  4 23:13 test7


Now we can issue our mount commands:



[root@centos /]# mount /dev/sdb1 /test1
[root@centos /]# mount /dev/sdb2 /test2
[root@centos /]# mount /dev/sdb3 /test3
[root@centos /]# mount /dev/sdb5 /test4
[root@centos /]# mount /dev/sdb6 /test5
[root@centos /]# mount /dev/sdb7 /test6
[root@centos /]# mount /dev/sdb8 /test7

Now to verify that we have created and mounted our partitions, we can issue the "df" command to display our filesystem usage:



[root@centos /]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/vg_centos-lv_root
                       6748936   3076548   3329556  49% /
tmpfs                   515456       284    515172   1% /dev/shm
/dev/sda1               495844     51987    418257  12% /boot
/dev/sr0               3626176   3626176         0 100% /media/CentOS_6.3_Final
/dev/sdb1               287782     10287    262637   4% /test1
/dev/sdb2               295555     10287    270007   4% /test2
/dev/sdb3               295555     10287    270007   4% /test3
/dev/sdb5               295561     10287    270014   4% /test4
/dev/sdb6               295561     10287    270014   4% /test5
/dev/sdb7               295561     10287    270014   4% /test6
/dev/sdb8               264445     10287    240505   5% /test7

And finally we need to add our handy work into a location "/etc/fstab". This will then automatically mount our filesystems when the system gets rebooted.

For full information on how to add our devices into "/etc/fstab", click Mount Table