mpartition Command in Linux

Introduction to Disk Partitioning with mpartition

This lab provides a comprehensive guide on utilizing the Linux mpartition command for effective storage device management. You will master the creation, resizing, and deletion of partitions, essential skills for any systemadmin. The mpartition command is a vital tool for managing disk partitions, enabling you to strategically allocate storage space for diverse needs, such as segregating your operating system, critical data, and swap space. This guide will delve into the purpose, correct syntax, and practical applications of the mpartition command, demonstrated through hands-on examples of partition creation, resizing, and deletion. Upon completion, you will possess a robust understanding of disk partition management using the mpartition command.

Understanding the mpartition Command: Purpose and Syntax

In this section, we will explore the purpose and syntax of the mpartition command within the Linux environment. The mpartition command stands as a potent utility for creating, resizing, and deleting partitions on a given storage device.

Let's begin by grasping the core purpose of the mpartition command. It is designed to facilitate partition management on storage devices like hard disks or solid-state drives (SSDs). It provides the ability to create new partitions, modify the size of existing ones, and remove partitions entirely. This is particularly useful when you require dedicated storage areas for different system functions, such as separate partitions for the operating system itself, user data, and swap space for memory management.

Now, let's examine the syntax of the mpartition command:

mpartition [options] device

Here's a breakdown of the available command options:

  • device: Specifies the target storage device for the partition operation, examples being /dev/sda or /dev/nvme0n1.
  • -a, --add: Used to create a brand-new partition.
  • -d, --delete: Used to delete a pre-existing partition.
  • -l, --list: Displays a list of the current partitions on the specified device.
  • -n, --new: Synonym for -a, creates a new partition.
  • -p, --print: Outputs the current partition table.
  • -r, --resize: Resizes an existing partition.
  • -t, --type: Sets the partition type.

Example output:

$ sudo mpartition -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
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: 0x00000000

Device     Boot Start     End Sectors  Size Id Type
/dev/sda1        2048 41943039 41940992   20G 83 Linux

In this example, the output shows that the device /dev/sda contains a single partition, /dev/sda1, with a size of 20 GB and designated as a Linux partition.

Creating and Managing Partitions with the mpartition Command

This section will provide instructions on how to create and manage partitions using the mpartition command in a Linux systemadmin context.

First, let's create a new partition on the /dev/sda device using the following command:

sudo mpartition -n /dev/sda

Executing this command will launch the partition editor, allowing you to create a new partition. Follow the prompts and on-screen instructions to complete the partition creation process.

Example output:

Welcome to mpartition! Version 2.34.
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
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: 0x00000000

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

Created a new partition 2 of type 'Linux' and of size 20 GiB.

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

Next, let's list the partitions on the /dev/sda device to confirm the creation of the new partition:

sudo mpartition -l /dev/sda

Example output:

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
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: 0x00000000

Device     Boot Start     End Sectors  Size Id Type
/dev/sda1        2048 41943039 41940992   20G 83 Linux
/dev/sda2       2048 41943039 41940992   20G 83 Linux

The output now shows that a new partition, /dev/sda2, has been successfully created with a size of 20 GB.

The mpartition command can also be used to manage existing partitions, including resizing or deleting them. Refer to the previous section for details on available options and their usage.

Resizing and Deleting Partitions Using the mpartition Command

This section details how to resize and delete partitions using the mpartition command in a Linux environment.

First, let's resize the partition /dev/sda2 that was created in the previous section. Execute the following command:

sudo mpartition -r /dev/sda2

This will launch the partition editor, allowing you to resize the specified partition. Follow the on-screen instructions to resize the partition as desired.

Example output:

Welcome to mpartition! Version 2.34.
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
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: 0x00000000

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

Partition 2 has been resized.

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

Now, let's proceed to delete the partition /dev/sda2:

sudo mpartition -d /dev/sda2

This command will remove the partition /dev/sda2 from the device.

Example output:

Welcome to mpartition! Version 2.34.
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
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: 0x00000000

Command (m for help): d
Partition number (1,2, default 2): 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.

After deleting the partition, you can verify the changes by running the mpartition -l /dev/sda command again to confirm that the partition is no longer present.

Conclusion

This lab provided a foundational understanding of the mpartition command in Linux, a critical tool for any systemadmin. We covered its purpose and syntax, highlighting its capabilities for creating, resizing, and deleting partitions on storage devices. We explored essential options for creating new partitions, deleting existing ones, and listing the current partition layout of a device.

Furthermore, we demonstrated how to create and manage partitions effectively using the mpartition command. This included practical steps for creating new partitions, resizing existing ones to optimize storage utilization, and deleting partitions when necessary. Mastering these partition management skills is essential for efficiently allocating and managing storage space within your Linux system.

400+ Linux Commands