- Published on
Disk Management
- Authors
- Name
- Yisak Abraham
- @YisakAbrahamK
Introduction
Disk management is an essential aspect of system administration. It involves managing disks, partitions, and file systems to ensure optimal performance, reliability, and data integrity. In this post, we'll cover some concepts and tools for disk management in Linux.
Definitions and Tools
Disk is a physical storage device used to store data on a computer. It can be a hard disk drive (HDD), solid-state drive (SSD), or other storage media.
Partition is a logical division of a disk that allows you to organize and manage data. It can be formatted with a file system and mounted to a directory in the file system hierarchy. Partitions help separate data, improve performance, and enable multi-boot configurations.
File System is a method used to organize and store data on a disk. It provides a structure for storing, retrieving, and managing files and directories.
Mounting is the process of attaching a file system to a directory in the file system hierarchy. It allows you to access the files and directories stored on the disk.
Disk Management Tools are software applications or utilities used to manage disks, partitions, and file systems. Some disk management tools for Linux include fdisk
, gdisk
mkfs
, and parted
.
Disk structure
Disks are divided into one or more partitions, each of which can be formatted with a file system. The disk structure typically consists of the following components:
Master Boot Record (MBR): The MBR is a small section of the disk that contains the partition table and the boot loader. It is used to boot the operating system. Contains information about the disk's geometry and a table of partitions. MBR supports disk sizes up to 2TB and a maximum of four primary partitions. It is being replaced by GPT in modern systems due to its limitations in disk size and partition count support. But it is still widely used in legacy systems.
GUID Partition Table (GPT): GPT is a modern partitioning scheme that replaces the MBR. It supports larger disk sizes, more partitions, and provides better data integrity. It uses Globally Unique Identifiers (GUIDs) to uniquely identify partitions. GPT is recommended for disks larger than 2TB and systems that require more than four partitions. Supports up to 128 primary partitions. It is also used in UEFI systems.
Partitions: Partitions are logical divisions of a disk that allow you to organize and manage data. They can be primary, extended, or logical partitions.
Primary Partition: A primary partition is a standalone partition that can contain a file system. A disk can have up to four primary partitions or three primary partitions and one extended partition. Primary partitions are used to install operating systems and store data. They are bootable.
Extended Partition: An extended partition is a special type of primary partition that can be subdivided into logical partitions. It is used to overcome the limitation of four primary partitions on a disk. Extended partitions do not contain a file system and are not bootable. They are used to create logical partitions.
Logical Partition: A logical partition is a subdivision of an extended partition. It can contain a file system and store data. Logical partitions are used to organize data and overcome the limitation of four primary partitions on a disk. They are not bootable.
Why partitions?
We can use partitions for several reasons:
Organization: Partitions help organize data by separating different types of data or operating systems. Different partitions can be used for different purposes, such as system files, user files, or backups.
Performance: Partitions can improve performance by isolating data and reducing fragmentation. Placing frequently accessed files on a separate partition can improve read/write speeds.
Security: Partitions can enhance security by isolating sensitive data. Encrypting a partition can protect data from unauthorized access.
Flexibility: Partitions can be resized, deleted, or reformatted as needed, providing flexibility in how you use your disk space.
Partition type can be identified by the partition ID, which is a hexadecimal number that represents the type of partition. Some partition types include:
Partition ID | Type | Description |
---|---|---|
0 | Empty | Empty partition or unformatted partition. |
82 | Linux | Linux swap partition. |
83 | Linux | Linux native file system (ext2, ext3, ext4). |
8E | Linux | Linux LVM partition. |
07 | NTFS | Windows NTFS file system. |
0C | FAT32 | Windows FAT32 file system. |
0E | FAT16 | Windows FAT16 file system. |
42 | LVM | Linux LVM partition. |
83 | Linux | Linux native file system (ext2, ext3, ext4). |
8E | Linux | Linux LVM partition. |
82 | Linux | Linux swap partition. |
07 | NTFS | Windows NTFS file system. |
0C | FAT32 | Windows FAT32 file system. |
0E | FAT16 | Windows FAT16 file system. |
You can view the partition table of a disk using the fdisk
command:
$ sudo fdisk -l /dev/sda
Disk Management Tools
There are some tools available for disk management in Linux. Here are some used tools and their functions:
fdisk
: A disk partitioning tool that allows you to create, delete, and manage disk partitions.gdisk
: A variant offdisk
that supports GPT partitioning.parted
: A disk partitioning tool that supports GPT and provides a more user-friendly interface thanfdisk
.
Comparison between fdisk
, gdisk
, and parted
:
Tool | Con | Pro |
---|---|---|
fdisk | - Limited to MBR partitioning scheme. | - Simple, widely supported. |
gdisk | - More complex to use. | - Supports GPT, handles larger disks. |
parted | - Can be complex for beginners. | - Modern, supports larger disks and partition sizes. |
fdisk
The fdisk
command is a popular disk partitioning tool that allows you to create, delete, and manage disk partitions. It provides an interactive command-line interface for partitioning disks. Here is syntax for using fdisk
:
$ sudo fdisk options device
- device: The device file representing the disk you want to partition (e.g.,
/dev/sda
,/dev/nvme0n1
).
Common options for the fdisk
command include:
Option | Description | example |
---|---|---|
-l | List the partition table of the specified device. | sudo fdisk -l /dev/sda |
b | Specify the sector size of the disk. | sudo fdisk -b 4096 /dev/sda |
u | Change the display units to sectors. | sudo fdisk -u /dev/sda |
p | Print the partition table. | sudo fdisk -p /dev/sda |
s | Change the display units to sectors. | sudo fdisk -s /dev/sda |
v | Verify the partition table. | sudo fdisk -v /dev/sda |
h | Display help information. | sudo fdisk -h |
C | Specify the number of cylinders. | sudo fdisk -C 1024 /dev/sda |
H | Specify the number of heads. | sudo fdisk -H 16 /dev/sda |
S | Specify the number of sectors per track. | sudo fdisk -S 63 /dev/sda |
Some commands
Creating Partition
- Start
fdisk
with the disk you want to create a partition on:
zsh$ sudo fdisk /dev/sda
- Then press
n
to create a new partition. - Choose the partition type (primary, extended, or logical).
- Enter the partition size.
- Press
w
to write the changes to the disk.
- Start
Deleting Partition
- Start
fdisk
with the disk you want to delete the partition from:
zsh$ sudo fdisk /dev/sda
- Then press
d
to delete a partition. - Enter the partition number you want to delete.
- Press
w
to write the changes to the disk.
- Start
Changing Partition Type
- Start
fdisk
with the disk you want to change the partition type:
zsh$ sudo fdisk /dev/sda
- Then press
t
to change the partition type. - Enter the partition number.
- Enter the hexadecimal code for the new partition type.
- Press
w
to write the changes to the disk.
- Start
Mount points
A mount point is a directory in the file system hierarchy where a file system is attached. When you mount a file system, you make its contents accessible at the specified mount point. Mount points are used to access data stored on disks and partitions.
- The root directory
/
is the top-level directory in the file system hierarchy. All other directories are subdirectories of the root directory. - Mount points are created by mounting a file system to a directory in the file system hierarchy.
- The home partition is mounted on
/home
.
Creating and Mounting File Systems
To create a file system on a partition, you can use the mkfs
command. Here is a syntax for creating a file system:
$ sudo mkfs -t filesystem-type device
- Here is an example of creating an ext4 file system on
/dev/sda1
:
$ sudo mkfs -t ext4 /dev/sda1
To mount a file system to a directory, you can use the mount
command. Here is a syntax for mounting a file system:
$ sudo mount device mount-point
- Here is an example of mounting
/dev/sda1
to/mnt
:
$ sudo mount /dev/sda1 /mnt
To unmount a file system, you can use the umount
command. Here is a syntax for unmounting a file system:
$ sudo umount mount-point
- Here is an example of unmounting
/mnt
:
$ sudo umount /mnt
Conclusion
Disk management is a critical aspect of system administration. By understanding disk structures, partitions, file systems, and mount points, you can effectively manage your storage resources. Using disk management tools like fdisk
, gdisk
, and parted
, you can create, delete, and manage partitions on your disks. Mounting file systems to directories allows you to access and manage data stored on your disks. By mastering disk management concepts and tools, you can optimize disk performance, reliability, and data integrity in your Linux system.