Becoming Linux System Administrator: Disk management (part I)

Audrey's weBlog & reViews
7 min readApr 26, 2024

A system administrator can decide the partitioning scheme. A partition refers to each part when a disk is divided into several parts. The reason for partitioning a disk is in general to allocate each section for a different purpose. Furthermore, separate partitions can prevent them from affecting each other. In Linux, usually there exists a partition for user home directories (/home) such that a user cannot affect the entire disk interfering with the operating system. The normal operation of the system will be crucial especially if the system is a website server, preventing the service outage.

Mother’s Day Special Offer up to 50% Discount for Amazon Devices (shop)

Hey guys! I really appreciate each and every one of you for visiting my posts. I will share several Promo Codes to thank you guys in a separate post, click below. Benefit from them (saving up to 50%) if you are an Amazon shopper and happen to need one of those items.

Photo by benjamin lehman on Unsplash

MBR partitioning

The master boot record (MBR) is a boot sector containing information on how the logical partitions are organized. It is gradually replaced by the GUID Partition Table, see below, due to the limitation of storage space up to 2 TB with MBR scheme. This partitioning scheme by default allows for only up to four partitions. One can use an extended partition, i.e., a container for other partitions, to create more than 4 logical partitions.

GUID partition table

The global unique identifier (GUID) partition table, i.e., GPT, is a part of the Unified Extensible Firmware Interface (UEFI) standard. By default, 128 partitions are possible with GPT allowing for the storage up to 9.4 ZB. However, the GPT does not support older operating systems.

Mount point

A directory to access the data on a partition is called a mount point. There is a partition mounted on / under which any additional partitions will be mounted, e.g., /home for user home directories.

fdisk

The fdisk utility can be used to create and modify the partitions. First, you can check the available disks on the system by running “fdisk -l” command (the output below is not from my system, but copied from the book Linux Administration: The Linux Operating System and Command Line Guide for Linux Administrators (kindle, paperback) for educational purposes):

user@debian:~$ sudo fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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 label type: dos
Disk identifier: 0x000ad6ec

Device Boot Start End Blocks Id System
/dev/sda1 2048 4098047 2048000 82 Linux swap / Solaris
/dev/sda2 * 4098048 417794047 206848000 83 Linux

Disk /dev/sdb: 549.8 GB, 549755813888 bytes, 1073741824 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


user@debian:~$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.

Command (m for help):

If you look at the output of “fdisk -l” in the above, i.e., the first example, this system has two disks, i.e., /dev/sda, which is already partitioned and /dev/sdb which is not partitioned. If you run the command “fdisk /dev/sda”, i.e., on the partitioned disk, you get the warning as shown in the second example above. Now let’s run the fdisk command on the unpartitioned disk /dev/sdb:

user@debian:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.38.1).
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
Building a new DOS disklabel with disk identifier 0xde069e1a.

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
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
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)

Command (m for help):

If you type m (for help), it shows several options for command actions as shown above. Now if we enter n (for adding a new partition), you can choose the partition type (p, e), see below where we first generate a 1 GB swap partition, then the second one 20 GB partition, and the final partition will be remaining space on the disk. Making partitions as below (seemingly complicated at first glance) can be done with the self-explanatory prompts.

user@debian:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.38.1).
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
Building a new DOS disklabel with disk identifier 0xde069e1a.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-1073741823, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1073741823, default 1073741823): +1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (2099200-1073741823, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-1073741823, default 1073741823): +20G
Partition 2 of type Linux and of size 20 GiB is set

Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (44042240-1073741823, default 44042240):
Using default value 44042240
Last sector, +sectors or +size{K,M,G} (44042240-1073741823, default 1073741823):
Using default value 1073741823
Partition 3 of type Linux and of size 491 GiB is set

Command (m for help): p
Disk /dev/sdb: 549.8 GB, 549755813888 bytes, 1073741824 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 label type: dos
Disk identifier: 0xf22b19b6

Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 82 Linux swap / Solaris
/dev/sdb2 2099200 44042239 20971520 83 Linux
/dev/sdb3 44042240 1073741823 514849792 83 Linux

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

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

In the above example, one can finally view (print) the partition table by typing p, and then write the changes made to the disk with w. Note that we have generated a new MBR partition using the above input. For a GPT partition, the only different step is that you should first type g as shown below before making the next steps.

Command (m for help): g

Do you love the topics about hacking and cybersecurity? Watch for free the best TV show on hacking, i.e., Mr. Robot (all 45 episodes), by signing up for free 30-Day trial on Prime (Watch Mr. Robot Now). You can cancel anytime.

If you are a book lover, I have a free gift for you. Make sure to sign up for 30 Day free trial on Audible. Cancel it after enjoying 30 days for free, no strings attached.

This post is based on the book Linux Administration: The Linux Operating System and Command Line Guide for Linux Administrators (kindle, paperback). This book is a very nice guide for a beginning IT administrator.

This post includes affiliate links. If you click on any of them and purchase it, I earn a commission at no extra cost to you.

--

--