Linux mpartition Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux mpartition command to create, resize, and delete partitions on storage devices. The mpartition command is a powerful tool for managing disk partitions, allowing you to allocate storage space for different purposes, such as separating your operating system, data, and swap space. The lab will cover the purpose and syntax of the mpartition command, as well as practical examples of creating, resizing, and deleting partitions. By the end of the lab, you will have a solid understanding of how to effectively manage disk partitions using the mpartition command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/rm -.-> lab-422823{{"`Linux mpartition Command with Practical Examples`"}} linux/dd -.-> lab-422823{{"`Linux mpartition Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the mpartition Command

In this step, we will learn about the purpose and syntax of the mpartition command in Linux. The mpartition command is a powerful tool used to create, resize, and delete partitions on a storage device.

First, let's understand the purpose of the mpartition command. The mpartition command is used to manage partitions on a storage device, such as a hard disk or a solid-state drive (SSD). It allows you to create new partitions, resize existing partitions, and delete partitions. This is useful when you need to allocate storage space for different purposes, such as creating separate partitions for your operating system, data, and swap space.

Now, let's look at the syntax of the mpartition command:

mpartition [options] device

Here's a breakdown of the command options:

  • device: The storage device on which you want to perform the partition operation, such as /dev/sda or /dev/nvme0n1.
  • -a, --add: Create a new partition.
  • -d, --delete: Delete a partition.
  • -l, --list: List the current partitions on the device.
  • -n, --new: Create a new partition.
  • -p, --print: Print the partition table.
  • -r, --resize: Resize a partition.
  • -t, --type: Set 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 the example output, we can see that the device /dev/sda has a single partition /dev/sda1 of 20 GB in size.

Create and Manage Partitions Using the mpartition Command

In this step, we will learn how to create and manage partitions using the mpartition command.

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

sudo mpartition -n /dev/sda

This will open the partition editor, where you can create a new partition. Follow the on-screen instructions to create a new partition.

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.

Now, let's list the partitions on the /dev/sda device:

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

In the example output, we can see that a new partition /dev/sda2 has been created with a size of 20 GB.

You can also use the mpartition command to manage existing partitions, such as resizing or deleting them. Refer to the previous step for the available options and their usage.

Resize and Delete Partitions with the mpartition Command

In this step, we will learn how to resize and delete partitions using the mpartition command.

First, let's resize the partition /dev/sda2 that we created in the previous step. Run the following command:

sudo mpartition -r /dev/sda2

This will open the partition editor, where you can resize the partition. Follow the on-screen instructions to resize the partition.

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 delete the partition /dev/sda2:

sudo mpartition -d /dev/sda2

This will delete 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.

Summary

In this lab, we first learned about the purpose and syntax of the mpartition command in Linux. The mpartition command is a powerful tool used to create, resize, and delete partitions on a storage device. We explored the various options available, such as creating new partitions, deleting partitions, and listing the current partitions on a device.

Next, we covered how to create and manage partitions using the mpartition command. This included steps to create new partitions, resize existing partitions, and delete partitions as needed. Understanding these partition management techniques is crucial for efficiently allocating storage space on your system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like