What is Disk Partitioning?
In the realm of computer storage, disk partitioning plays a crucial role in organizing and managing data efficiently. By dividing a physical disk into multiple logical sections, known as partitions, users can segregate data, install multiple operating systems, and implement various storage configurations. In this article, we’ll delve into the fundamentals of disk partitioning, its benefits, the differences between MBR and GPT partitioning schemes, and practical examples using the parted program to manipulate disks with the GPT scheme.
Disk partitioning is the process of dividing a physical storage device, such as a hard disk drive (HDD) or solid-state drive (SSD), into multiple logical sections called partitions. Each partition behaves as a separate storage entity, with its own file system and directory structure.
Key Concepts of Disk Partitioning:
- Primary Partition: A basic unit of disk partitioning that can hold an individual file system or operating system.
- Extended Partition: A primary partition used in the Master Boot Record (MBR) partitioning scheme to hold multiple logical partitions.
- Logical Partition: Partitions created within an extended partition to allow for additional storage beyond the four primary partitions limit of MBR.
MBR vs. GPT Partitioning:
- MBR (Master Boot Record): An older partitioning scheme limited to a maximum of four primary partitions or three primary partitions and one extended partition. MBR has limitations on disk size, typically supporting up to 2TB.
- GPT (GUID Partition Table): A modern partitioning scheme that overcomes the limitations of MBR. GPT supports up to 128 partitions and allows for larger disk sizes, making it suitable for modern storage devices.
Benefits of Disk Partitioning
- Data Organization: Partitioning facilitates the organization of data by segregating files, applications, and operating system installations into distinct areas.
- Multi-Boot Configurations: Partitioning enables the installation of multiple operating systems on a single disk, allowing users to dual-boot or multi-boot between different OS environments.
- Data Protection: By separating system files from user data, disk partitioning can enhance data protection and reduce the risk of data loss in the event of system failures or disk errors.
- Performance Optimization: Partitioning can help optimize disk performance by allocating specific partitions for high-demand applications or implementing RAID configurations for improved data redundancy and performance.
Conclusion
Disk partitioning is a fundamental aspect of storage management, offering users the flexibility to organize data, implement multi-boot configurations, and enhance data protection and performance. By understanding the principles of disk partitioning and mastering the tools and techniques for partition management, users can optimize their storage infrastructure to meet their specific needs and requirements, especially when utilizing the modern GPT partitioning scheme for larger disks and increased flexibility.
Examples of Disk Partitioning using parted
Obtaining Disk Information with lsblk
Before performing any disk partitioning operations, it’s essential to obtain information about the available disks and their partitions. The lsblk command provides a convenient way to list block devices and their attributes, including disk size, partition layout, and file system type.
# Use lsblk to list block devices and their attributes
[root@vm1 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 2K 0 rom
vda 252:0 0 20G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 19G 0 part
├─rl-root 253:0 0 17G 0 lvm /
└─rl-swap 253:1 0 2G 0 lvm [SWAP]
vdb 252:16 0 2G 0 disk
vdc 252:32 0 2G 0 disk
vdd 252:48 0 2G 0 disk
vde 252:64 0 2G 0 disk
Creating a Partitions, Format and Mount persistently.
# Identify the disk to partition (e.g., /dev/vdb)
[root@vm1 ~]# parted /dev/vdb print
Error: /dev/vdb: unrecognised disk label
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
# Define the GPT partitioning scheme.
[root@vm1 ~]# parted /dev/vdb mklabel gpt
Information: You may need to update /etc/fstab.
# Create a new partition "data"(e.g., /dev/vdb1) using the 'mkpart' command
[root@vm1 ~]# parted /dev/vdb mkpart data xfs 2048s 2GB
Information: You may need to update /etc/fstab.
# Run the udevadm settle command.
[root@vm1 ~]# udevadm settle
# Format the 2 GB data partition with an XFS file system
[root@vm1 ~]# mkfs.xfs /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=130944 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=523776, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
Discarding blocks...Done.
# Persistently mount it to the /mnt/data directory.
[root@vm1 ~]# mkdir /mnt/data
[root@vm1 ~]# lsblk --fs /dev/vdb1
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
vdb1 xfs 1ca931b9-7922-4449-932e-a62e1682572b
[root@vm1 ~]# echo "UUID=1ca931b9-7922-4449-932e-a62e1682572b /mnt/data xfs defaults 0 0" >> /etc/fstab
# Daemon reload.
[root@vm1 ~]# systemctl daemon-reload
# Mount.
[root@vm1 ~]# mount -a
# Check results.
[root@vm1 ~]# mount | grep data
/dev/vdb1 on /mnt/data type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
Creating Multiple Swap Partitions and Setting Priority
# Identify the disk to partition (e.g., /dev/vdc)
[root@vm1 ~]# parted /dev/vdc print
Error: /dev/vdc: unrecognised disk label
Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
# Define the GPT partitioning scheme.
[root@vm1 ~]# parted /dev/vdc mklabel gpt
Information: You may need to update /etc/fstab.
# Create multiple swap partitions
[root@vm1 ~]# parted /dev/vdc mkpart swap1 linux-swap 2048s 1G
[root@vm1 ~]# parted /dev/vdc mkpart swap2 linux-swap 1000M 2G
# Run the udevadm settle command.
[root@vm1 ~]# udevadm settle
# Initialize partitions as swap spaces.
[root@vm1 ~]# mkswap /dev/vdc1
Setting up swapspace version 1, size = 953 MiB (999288832 bytes)
no label, UUID=0e1e9318-a421-440b-9b8d-81e283e56edd
[root@vm1 ~]# mkswap /dev/vdc2
Setting up swapspace version 1, size = 1.1 GiB (1146089472 bytes)
no label, UUID=a7b96040-6f7f-4608-aec4-aacd89fc2d75
# Persistently mount it using fstab. Set priority 10 to partition 2. Set priority 20 to partition 1.
[root@vm1 ~]# echo "UUID=a7b96040-6f7f-4608-aec4-aacd89fc2d75 swap swap pri=10 0 0" >> /etc/fstab
[root@vm1 ~]# echo "UUID=0e1e9318-a421-440b-9b8d-81e283e56edd swap swap pri=20 0 0" >> /etc/fstab
# Daemon reload.
[root@vm1 ~]# systemctl daemon-reload
# Mount swap.
[root@vm1 ~]# swapon -a
# Check results.
[root@vm1 ~]# swapon --show
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -2
/dev/vdc2 partition 1.1G 0B 10
/dev/vdc1 partition 953M 0B 20
Deleting a Partition:
# Identify the partition to delete (e.g., /dev/vdd)
[root@vm1 ~]# parted /dev/vdd print
Model: Virtio Block Device (virtblk)
Disk /dev/vdd: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 99.6MB 98.6MB d1
2 99.6MB 200MB 101MB d2
3 200MB 300MB 99.6MB d3
4 300MB 400MB 99.6MB d4
[root@vm1 ~]# parted /dev/vdd rm 2
Information: You may need to update /etc/fstab.