Partitioning is dividing a single hard drive into many logical drives known as partitions which can be worked on separately. It can be used for dual-booting where you can have multiple operating systems installed. With partitioning, you can upgrade your hard drive and allows for efficient disk management. It also increases backup and security where if one operation is affected, the other is not affected.

Filesystems are a collection of files on a hard drive or partitions. They give you a method of organizing files, handling data on a disk, and managing storage. There are different file systems that Linux offers like Ext, Ext2, Ext3, Ext4, JFS, ReiserFS, XFS, btrfs, and swap.

Tools used to Partition Disks on Linux

There are several tools used to partition disks in Linux, They include; fdisk, gdisk, parted, mkfs and mkswap.

1. fdisk

It is also known as format disk. It is a command-line utility used to create partitions and manipulate data on a disk. To install it use the following command.

### Ubuntu / Debian ###
sudo apt update
sudo apt install fdisk -y

### CentOS / RHEL / Fedora ###
sudo yum -y install util-linux

To list the devices, you can use the following command.

$ sudo fdisk -l
Disk /dev/vda: 60 GiB, 64424509440 bytes, 125829120 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
Disklabel type: dos
Disk identifier: 0x38b72722

Device     Boot   Start       End   Sectors  Size Id Type
/dev/vda1  *       2048   1050623   1048576  512M  b W95 FAT32
/dev/vda2       1052670 125827071 124774402 59.5G  5 Extended
/dev/vda5       1052672 125827071 124774400 59.5G 83 Linux

Disk /dev/vdb: 30 GiB, 32212254720 bytes, 62914560 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

This will list down all the disks available and their partitions if they have any. As shown above, I have one disk with 2 partitions with the second one having any partitions.

Create a partition on the empty disk using the following command. It will enter into the partition editor and you will type ‘n’ for creating a new partition. Then you can press enter to choose the default size allocation or input the size you want. Afterward, you have to write the changes with ‘w‘ to save the changes.

$ sudo fdisk /dev/vdb
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xd898b73d.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (1-4, default 1): 2
First sector (2048-62914559, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-62914559, default 62914559): 

Created a new partition 2 of type 'Extended' and of size 30 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

You can list the partitions of a specific disk by specifying the name of the device. Thus we can list the partition we have created for the disk /dev/vdb by using the following command.

$ sudo fdisk -l /dev/vdb
Disk /dev/vdb: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: dos
Disk identifier: 0xd898b73d

Device     Boot Start      End  Sectors Size Id Type
/dev/vdb2        2048 62914559 62912512  30G  5 Extended

To delete the partition, use the following command. Then you will use the ‘d’ option to delete the partition and write the changes.

$ sudo fdisk /dev/vdb
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 2
Partition 2 has been deleted.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

You can check to confirm if the partition was deleted using the following command

sudo fdisk -l /dev/vdb

And that is how you create an MBR partition with fdisk.

2. gdisk

It is also referred to as GPT fdisk. It is a set of text-mode partitioning tools that creates and manipulates partition tables. It allows up to 128 partitions. To install it, use the following command if you do not have it.

### Ubuntu / Debian ###
sudo apt install gdisk -y

### CentOS / RHEL / Fedora ###
sudo yum -y install gdisk

To create a GPT partition layout on the /dev/vdb disk, use the following command. You will still use option ‘n’ to create a new partition then fill in prompts questions or press Enter for the default size. Then write the changes with the option ‘w’ to complete the process.

$ sudo gdisk /dev/vdb
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************


Command (? for help): n
Partition number (1-128, default 1): 2
First sector (34-62914526, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-62914526, default = 62914526) or {+-}size{KMGTP}: 
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/vdb.
The operation has completed successfully.

Run the following command to use the new table without rebooting.

sudo partprobe

If you get an error, continue to manually reboot your system.

To list the disk and specify the one we have manipulated, use the following command.

$ sudo gdisk -l /dev/vdb
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/vdb: 62914560 sectors, 30.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5E78E36F-63ED-4F3D-95CE-D93A4B15B71B
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 62914526
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   2            2048        62914526   30.0 GiB    8300  Linux filesystem

To delete the GPT disk partition, use the following command. Then use the ‘d’ option to delete the partition, select the partition number and write the changes.

$ sudo gdisk /dev/vdb
GPT fdisk (gdisk) version 1.0.5

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): d
Using 2

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/vdb.
The operation has completed successfully.

Run partprobe or reboot your system.

sudo partprobe

3. Parted

It is a command-line tool utility that allows you to manage disk partitions. It easily does the low-level partitioning tasks of disks like adding, shrinking, and deleting disk partitions. It comes pre-installed in many Linux distributions but if you do not have it, use the following command to install it.

### Ubuntu / Debian ###
sudo apt install parted -y

### CentOS / RHEL / Fedora ###
sudo yum -y install parted

You can start parted with the following command.

sudo parted

You can see that parted is started with the default disk drive. To list all the devices use the following command

(parted) print all

Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags


Model: Virtio Block Device (virtblk)
Disk /dev/vda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  538MB   537MB   primary   fat32        boot
 2      539MB   64.4GB  63.9GB  extended
 5      539MB   64.4GB  63.9GB  logical   ext4

To change the disk drive (If you have more than one), use the following command. I will not change the disk drive as I am already using the empty disk as the default one.

(parted) select [new disk drive]

To create a partition on the disk drive, first, give it a name. It might show you a warning that data in that disk will be destroyed type ‘yes‘ to continue.

(parted) mklabel msdos

Then use the following command to create a partition. The size is in MB, so if we want to create a 5GB partition size we will start at 1 and end at 5000.

(parted) mkpart 

Partition type?  primary/extended? e                                      
Start? 1                                                                  
End? 5000

Then once you print the disk drive, the partition will appear as shown below.

(parted) print

Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  5000MB  4999MB  extended               lba

To exit parted, use the quit command.

$ quit

Using mkfs command in Linux

It stands for ‘make file system’. It is a command used to format disks with a specific file system. It initializes the volume and file system label and the startup block.

Install all tools used to create filesystems.

### Ubuntu / Debian ###
sudo apt install e2fsprogs xfsprogs   -y

### CentOS / RHEL / Fedora ###
sudo yum -y install parted

To format a partition with a file system let’s say ext4, use the following command. To partition to other extended file systems alternatives like ext2 and ext3, just replace the name of the file system in the command. If I was changing the file system for the /dev/vdb partition just created above, It will be as below.

$ sudo mkfs.ext4 /dev/vdb
mke2fs 1.45.5 (07-Jan-2020)
Found a dos partition table in /dev/vdb
Proceed anyway? (y,N) y
Discarding device blocks: done                            
Creating filesystem with 7864320 4k blocks and 1966080 inodes
Filesystem UUID: c7ccbc28-9827-48f2-817f-89bcc69d4a77
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

To verify the newly created file system, use the following command.

$ sudo file -sL /dev/vdb
/dev/vdb: Linux rev 1.0 ext4 filesystem data, UUID=c7ccbc28-9827-48f2-817f-89bcc69d4a77 (extents) (64bit) (large files) (huge files)

To create other file systems like XFS, VFAT, exFAT, use the following commands.

### XFS ###
sudo mkfs.xfs {disk-drive-partition}

### VFAT ###
sudo mkfs.vfat {disk-drive-partition}

### exFAT ###
sudo mkfs.exfat {disk-drive-partition}

Using mkswap command in Linux

It stands for ‘make swap’. It is used to set up a Linux swap area on a storage partition. So you typically move a swap space to a different partition other than the one that was created during system installation. It is mainly used to save space say, on a low-capacity boot drive.

Create a Swap from Disk Partition

To set up a disk to be used as a swap partition, use the following command.

$ sudo mkswap /dev/vdb
mkswap: /dev/vdb: warning: wiping old ext4 signature.
Setting up swapspace version 1, size = 30 GiB (32212250624 bytes)
no label, UUID=ca35b3a1-eed7-4b6b-87c0-03a0b8a303c9

You can use the swapon command to specify the devices on which swapping is to take place. Generally activate a swap area.

sudo swapon /dev/vdb

To deactivate the swap area use the following command

sudo swapoff /dev/vdb

Create a Swap from a File

A swap file is a memory size backed up in a file that a system uses to offload RAM. First, create a swapfile of the size you want say 2GB, using the following command.

sudo fallocate -l 1G /swapfile

If you get an error like ‘fallocate failed‘ use the other command.

$ sudo dd if=/dev/zero of=/swap-file count=2 bs=1GiB
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 3.14619 s, 683 MB/s

Then set the correct permissions to allow only the root to read and write the file

sudo chmod 600 /swapfile
sudo chown -v root:root /swapfile

Then use the following command to set up the file as a Linux swap area

sudo mkswap /swapfile

Then enable swap with the following command

sudo swapon /swapfile

To deactivate use the following command.

sudo swapoff -v /swapfile

You can also remove the swap file using the following command

sudo rm -v -i /swapfile

btrfs File System

Btrfs is a type of file system in Linux used in high-performance and large storage servers that focuses on fault tolerance and easy administration of advanced file system features. It is a Copy on Write (CoW) file system whereby when the data to be modified is copied and written to a different free location on the filesystem which therefore reduces data corruption.
Its features include:

Extended File system
This allows efficient storage for small files that can be stored directly into their metadata improving the performance.

Cloning and Snapshots
In btrfs, a snapshot is a copy of an entire subvolume taken at any given time. It simplifies data migration as it can be taken at any point in time. Cloning on the other hand is a lightweight copy of a file or an entire subvolume.

Multiple device support
This system can be used to enable the system administrator to allocate storage on demand as it supports managing multiple devices at the file system level. It has built-in RAID support including RAID0, RAID1, and RAID10 levels. Portions of disks called chunks are used to create RAID logical volumes that can be mirrored across multiple devices. Each device has an allocation tree that shows which portions of a device are currently allocated to a chunk. The logical addressing allows for efficient chunk relocation.

Subvolumes
A subvolume is a part of a file system that is independent of its own file and hierarchy. They can be mounted and renamed as normal directories. An initial subvolume is created when a btrfs file system is created. They use the same available space as the whole file system and can share file extents between each other.

Compression
Btrfs supports automatic and transparent file compression that significantly reduces the size of a file and increases the lifespan of flash data. There are three compression methods available, ZLIB, LZO, and ZSTD each with various levels.

If I was using my empty disk, /dev/vdb, To create a btrfs file system, the command would be as follows.

sudo mkfs.btrfs /dev/vdb

Conclusion

From this guide, we have learned about managing Linux Partitions from the command line using different tools like fdisk, gdisk, parted, mkfs, and mkswap. We have also gone through file systems and the different types available like btrfs, exFAT, VFAT, and extf. We have also created a swap file and seen how to activate and deactivate a swap area using swapon/swapoff commands.

LEAVE A REPLY

Please enter your comment!
Please enter your name here