Linux resize Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux resize command and learn how to resize partitions and logical volumes. The resize command is a powerful tool for managing storage space on your Linux system. We will start by understanding the basic syntax and usage of the resize command, and then proceed to resize a partition and an LVM (Logical Volume Management) volume group and logical volume using practical examples.

The lab covers the following steps:

  1. Understand the resize command
  2. Resize a partition using the resize command
  3. Resize an LVM volume group and logical volume

Remember, the resize command should be used with caution, as resizing partitions or logical volumes can potentially lead to data loss if not done correctly. Always ensure that you have a backup of your data before performing any resize operations.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") subgraph Lab Skills linux/df -.-> lab-422888{{"`Linux resize Command with Practical Examples`"}} linux/du -.-> lab-422888{{"`Linux resize Command with Practical Examples`"}} end

Understand the resize Command

In this step, we will explore the Linux resize command, which is used to resize partitions and logical volumes. The resize command is a powerful tool for managing storage space on your Linux system.

First, let's understand the basic syntax of the resize command:

resize [options] size device

Here, size is the new size you want to set for the partition or logical volume, and device is the name of the partition or logical volume you want to resize.

Some common options for the resize command include:

  • -f: Force the resize operation, even if the filesystem is not unmounted.
  • -p: Print the new size of the partition or logical volume after the resize operation.
  • -v: Verbose mode, which provides more detailed output.

Now, let's see some examples of using the resize command:

## Resize a partition to 20GB
sudo resize 20G /dev/sda1

Example output:
resize: /dev/sda1 resized

In this example, we resized the /dev/sda1 partition to 20GB using the resize command.

## Resize an LVM logical volume to 50GB
sudo resize 50G /dev/vg0/lv0

Example output:
resize: /dev/vg0/lv0 resized

In this example, we resized the logical volume /dev/vg0/lv0 to 50GB using the resize command.

Remember, the resize command is a powerful tool, but it should be used with caution, as resizing partitions or logical volumes can potentially lead to data loss if not done correctly. Always ensure that you have a backup of your data before performing any resize operations.

Resize a Partition Using the resize Command

In this step, we will learn how to resize a partition using the resize command.

First, let's create a new partition on the virtual disk in our Docker container:

sudo fdisk /dev/sdb
## Create a new partition, e.g., /dev/sdb1

Example output:

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

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

Created a new partition 1 of type 'Linux' and of size 10 GiB.

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

Now, let's resize the newly created partition /dev/sdb1 to 15GB:

sudo resize 15G /dev/sdb1

Example output:

resize: /dev/sdb1 resized

To verify the new size of the partition, we can use the fdisk command:

sudo fdisk -l /dev/sdb

Example output:

Disk /dev/sdb: 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: 0x78b0c0b9

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        2048 31457279 31455232   15G 83 Linux

As you can see, the size of the /dev/sdb1 partition has been successfully resized to 15GB.

Resize an LVM Volume Group and Logical Volume

In this step, we will learn how to resize an LVM (Logical Volume Management) volume group and a logical volume using the resize command.

First, let's create an LVM volume group and a logical volume:

## Create a physical volume
sudo pvcreate /dev/sdc

## Create a volume group
sudo vgcreate vg0 /dev/sdc

## Create a logical volume
sudo lvcreate -L 5G -n lv0 vg0

Example output:

  Physical volume "/dev/sdc" successfully created.
  Volume group "vg0" successfully created
  Logical volume "lv0" created.

Now, let's resize the logical volume lv0 to 10GB:

## Resize the logical volume
sudo lvresize -L 10G /dev/vg0/lv0

Example output:

  Size of logical volume vg0/lv0 changed from 5.00 GiB to 10.00 GiB.
  Logical volume vg0/lv0 successfully resized.

To verify the new size of the logical volume, we can use the lvdisplay command:

sudo lvdisplay /dev/vg0/lv0

Example output:

  --- Logical volume ---
  LV Path                /dev/vg0/lv0
  LV Name                lv0
  VG Name                vg0
  LV UUID                zVBxkH-Tn7d-1234-abcd-1234-1234-1234abcd
  LV Write Access        read/write
  LV Creation host, time labex-ubuntu, 2023-04-18 12:34:56 +0000
  LV Status              available
  ## open                 0
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

As you can see, the size of the logical volume lv0 has been successfully resized to 10GB.

Summary

In this lab, we first explored the Linux resize command, which is used to resize partitions and logical volumes. We learned the basic syntax of the resize command and some common options, such as -f to force the resize operation, -p to print the new size, and -v for verbose output. We then saw examples of using the resize command to resize a partition to 20GB and an LVM logical volume to 50GB.

Next, we created a new partition on the virtual disk in our Docker container using the fdisk command. This set the stage for the next step, where we will learn how to resize the newly created partition using the resize command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like