A Little About Linux: Partitioning and Designing A Disk Layout

If you're dealing with disks in Linux, you may come across the older Parallel PATA/IDE disks, the newer Serial SATA disks and of course the ever present SCSI.

The device naming convention for the Parallel disks is usually shown in the form of 'hd' and then a letter, so we're looking at a naming convention of /dev/hda, /dev/hdb, /dev/hdc etc. with the partition number then shown after the device name e.g. /dev/hda1 and /dev/hda2 meaning the first and second partition of hda.

SATA and SCSI drives are treated as the same by Linux, so the device naming convention for these drives are the same i.e. /dev/sda, /dev/sdb etc.  USB and Firewire (IEEE-1394) devices are also assigned drive names using the same convention as they use the SCSI subsystem.

Partitioning of Disks
So, there are a number of reasons why you would want to carve up a disk into partitions, but I'm not going through them all here, needless to say you can do a search if you to want to investigate these further.  Linux also has a number of different ways of performing this partitioning and we'll go through each option below.  In this section we are dealing with PC based systems as there are different partitioning mechanisms for non-x86 based machines. 

Basic Partitioning
First, we'll start with the Basic partitioning method which quite frankly goes back to the ark and you will definitely have encountered on Windows.  This method means that you can have up to 4 primary partitions, or you can create 1 extended partition to replace one of the primary partitions, and this can then be further divided into logical partitions.  I've explained this into the diagram below;

Basic Linux Disk Partitioning

The first layout in the diagram is the most basic, You can split the disk up into 4 Primary Partitions and leave it at that.  The disk device will be named /dev/sdb (if it is a SCSI disk, or is being treated as one by Linux) with the partitions named as /dev/sdb1, /dev/sdb2, /dev/sdb3 and  /dev/sdb4.  It's very simple.

But I want more than 4 partitions, it's the 21st Century for crying out loud!  Yes, as disk sizes grew they had to come up with a way of maintaining backwards compatibility whilst allowing more than 4 partitions, so they came up with this little gem... The Extended Partition. 

Instead of having 4 Primary Partitions, we make one of them an Extended Partition and then create a load of Logical Partitions within the Extended (as shown within the second layout in the diagram.)  In the case of Linux, /dev/sdb1/dev/sdb2 and /dev/sdb3 are the Primary Partitions as before, /dev/sdb4 (not shown on the diagram) would become the Extended Partition and /dev/sdb5, /dev/sdb6, /dev/sdb7 and /dev/sdb8 would all be Logical Partitions within the Extended.  The only other thing you will need to know about Logical Partitions is that they will always start at /dev/sdb5, even though you may only have 1 Primary Partition at /dev/sdb1 and the Extended Partition at /dev/sdb2, the first Logical will always be /dev/sdb5.

In Linux, as well as specifying the primary and extended partitions, you will also need to specify the partition type code which shouldn't be confused with the type of file system.

The partition type is a hexadecimal value which represents a specific function. The most popular ones are; 83 (Linux), 82 (Linux Swap), 85 (Linux Extended), b (FAT32) but they even have options for things like fb (VMware VMFS) and bf (Solaris).  These codes can be used by certain Operating Systems (such as Windows) so they know what partitions they can attempt to access and which they should ignore. Linux will normally ignore these type codes except during the boot process when it helps in deciding how the system is configured especially those identified as extended.

Partitioning Tools
The basic partitioning tool used to create the partition is fdisk and the common commands for this tool are shown below;

To show all devices...
fdisk -l

To show the partition table of a specific device...
fdisk -l /dev/sda

To create partitions, and then use the options available...
fdisk /dev/sda

p - Print the Partition Table
d - Delete a partition
n - Add a new partition
l -  Change partition type
w - Write table to disk and exit

Parted is an alternative to fdisk and does similar things but completely differently, they also have different options.  Normally you can choose which of the two you use unless the disk you are partitioning is greater than 2Tb in which case you need to use parted as fdisk won't work for disks above 2Tb.  Generally though, it's handy if you know both and the common parted commands are shown below;

Start the application in a basic way...
parted /dev/sdb

GNU Parted 2.1
Using /dev/sdb

Welcome to GNU Parted! Type 'help' to view a list of commands.

Now, if we have a 4TB drive and we want to make one big partition, this is how we go about it. First create a new EFI GUID Partition Table/disklabel...

(parted) mklabel gpt

Then you're prompted when you're about to trash everything...

Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes

(parted)

Now set the unit of size which can be MB, GB, TB or even % for percentage of device size...

(parted) unit TB

Create the size of the partition (the figures are the start and end)...

(parted) mkpart primary 0.00TB 4.00TB

Then, print out what has been created...

(parted) print

Model: Samsung D3 Station (scsi)
Disk /dev/sdb: 4001GB
Sector size (logical/physical): 4096B/4096B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags

 1      1049kB  4001GB  4001GB               primary

Finally, save and exit...

(parted) quit

Logical Volume Manager (LVM)
LVM is a tool for managing devices (such as Hard Drives) which is an alternative to creating standard disk partitions.  It allows for striping, allocation, mirroring and resizing of disks by creating logical volumes that span multiple physical devices.  Apart from the /boot partition which must be located on a standard partition, all other partitions can be created as logical volumes and the disks can be added and removed without downtime.

The way it works is that each physical disk (normally a hard drive) or groups of physical disks are assigned to one or more Physical Volumes (it is easier to understand if one physical hard disk is equal to one physical volume.)  These are then split into Logical Volumes.  These are then all collected together in one administrative unit called a Volume Group.  The commands for LVM are as follows;

Assign the Physical Disks to Physical Volumes...
# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

Assign the Physical Volumes to a Volume Group...
# vgcreate vg_palindrome /dev/sdb1
Volume group "vg_palindrome" successfully created

Carve up the Volume Group into Logical Volumes...

# lvcreate -L 10G vg_palindrome
Logical volume "lvol0" created

Display all of the Physical Volumes...
# pvdisplay

  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg_palindrome
  PV Size               19.99 GiB / not usable 1.43 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              5118
  Free PE               2558
  Allocated PE          2560

  PV UUID               1npMIZ-2kYB-WAbl-q1Gr-yiHj-aNSM-Q3b74g

Display all of the Volume Groups...
# vgdisplay

  --- Volume group ---
  VG Name               vg_palindrome
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       2560 / 10.00 GiB
  Free  PE / Size       2558 / 9.99 GiB
  VG UUID               teyWpv-jzi7-7OFc-Toa6-WXIv-1AJv-Cj8j4p


Display all of the Logical Volumes...
# lvdisplay

  --- Logical volume ---
  LV Path                /dev/vg_palindrome/lvol0
  LV Name                lvol0
  VG Name                vg_palindrome
  LV UUID                fr8PsV-6ksj-kWSo-Atqg-aMAa-j7Pa-dfqanr
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2014-10-06 21:16:50 +0100
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

Increase the Logical Volume to the full size of the Physical Volume...
# lvresize -l 100%VG /dev/vg_palindrome/lvol0

  Extending logical volume lvol0 to 19.99 GiB
  Logical volume lvol0 successfully resized

Reduce Logical Volume by 15GB...
# lvresize -L -15G /dev/vg_palindrome/lvol0

  WARNING: Reducing active logical volume to 4.99 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
  Reducing logical volume lvol0 to 4.99 GiB
  Logical volume lvol0 successfully resized

Preparing Partitions (Formatting and Creating Filesystems)
After partitions have been created, they need to be prepared before anything can be stored on them.  I like to think of it as analogous to taking a kitchen drawer and putting a cutlery divider in it, it creates a low-level structure which organises the data.

There are a number of file systems that Linux can both read and create (and therefore a number of different designs of cutlery dividers!), some of them are Linux specific, but others are from Unix, Windows and even file systems that are long forgotten (almost!)  Some of them are listed below;

ext2, ext3, ext4 - The standard Linux journalling file systems starting in late 1990s up to the present day, they are usually the default option in most Linux distributions.

ReiserFS - A journalling file system created for Linux that particularly excels a storing huge amounts of small files.

JFS - IBM created the 'Journalling File System' for AIX and latterly OS/2 and it's the OS/2 version that ends up in Linux.

XFS - Originally developed by Silicon Graphics for their IRIX OS and added as one of the default file systems in Linux.

Windows Filesystems - FAT16, FAT32 and NTFS are all available in Linux with different levels of compatibility.  Linux can normally read and write to and from FAT file systems well, but generally can only read from NTFS successfully. 

Finally, once you've chosen your file system, it's time to create it.  Generally, you would use the command mkfs to make the file system.  For instance, the command below will create an ext3 file system on /dev/sda1;

mkfs -t ext3 /dev/sda1

For the common file systems you can also use;

mkfs.ext4 /dev/sda1
mkfs.xfs /dev/sda1
mkfs.reiserfs /dev/sda1

If you use mke2fs /dev/sda1, it's the equivalent of using mkfs -t ext2 /dev/sda1 or mkfs.ext2 /dev/sda1 and if you use mke2fs -j /dev/sda1 

Swap Partitions - A Special Case
Unlike other partitions that you may wish to create, the swap partition is a special case as it doesn't hold files but is treated as part of memory by Linux.  Apart from that, swap partitions appear to be mounted in the normal way, but they do have separate commands;

To make the swap partition (effectively turn a standard partition into swap space)
mkswap /dev/sdb1

To activate a previously created swap partition
swapon /dev/sdb1

Once created and activated, to survive a reboot you must add an entry in /etc/fstab.
/dev/sdb1         swap      swap    defaults        0 0

You can verify that this is set by using either cat /proc/swaps or free.

No comments: