resize Command in Linux

Introduction

In this practical lab, we'll delve into the world of Linux storage management using the resize command, focusing on how to resize partitions and logical volumes. The resize command is an indispensable tool for any systemadmin needing to dynamically manage disk space within a Linux environment. This guide will start with a clear explanation of the resize command's syntax and common use cases. We will then move into practical examples, showing you how to resize both a standard disk partition and an LVM (Logical Volume Management) setup, encompassing volume groups and logical volumes.

This lab provides hands-on experience through the following steps:

  1. Gaining a solid understanding of the resize command
  2. Demonstrates how to effectively resize a partition
  3. Explores the process of resizing an LVM volume group and logical volume

A critical note: while powerful, the resize command needs to be handled with care. Incorrect resizing operations can lead to data loss. It's paramount that you create a complete data backup before undertaking any resize tasks on your system.

Understand the resize Command

This section focuses on the essential Linux resize command, a utility designed for dynamically adjusting the size of partitions and logical volumes. It is an essential tool in the arsenal of any systemadmin managing storage on Linux.

Let's start by examining the fundamental syntax of the resize command:

resize [options] size device

In this structure, size represents the intended new size of the partition or logical volume. device is the identifier of the specific partition or logical volume to be resized.

Key options available with the resize command include:

  • -f: This forces the resize operation, even if the filesystem isn't unmounted, potentially bypassing safety checks. Use with caution.
  • -p: Provides immediate feedback by printing the updated size of the partition or logical volume upon successful completion.
  • -v: Enables verbose mode, outputting a more detailed log during the resizing process.

Here are practical examples illustrating how to use the resize command:

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

Example output:
resize: /dev/sda1 resized

In this scenario, the /dev/sda1 partition has been resized to a capacity of 20GB using the resize command, executed with root privileges.

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

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

Here, the logical volume identified as /dev/vg0/lv0 is being adjusted to a size of 50GB through the resize command.

Once again, remember to exercise caution when using the resize command. Mistakes can result in data loss. Always back up your data before any resize operations, particularly on production systems. This tool is most effective when utilized by systemadmin with an understanding of the risks involved.

Resize a Partition Using the resize Command

This section provides a step-by-step guide on resizing a partition using the resize command.

First, we'll create a new partition on the virtual disk within the 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 recently created partition, /dev/sdb1, to a new size of 15GB:

sudo resize 15G /dev/sdb1

Example output:

resize: /dev/sdb1 resized

To confirm the successful resizing of the partition and verify its new size, use the fdisk command with the list option:

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

The output clearly shows that the /dev/sdb1 partition now has a size of 15GB, validating the successful execution of the resize command.

Resize an LVM Volume Group and Logical Volume

This section details the procedure for resizing an LVM (Logical Volume Management) volume group and a logical volume using the resize command, a common task for a systemadmin.

Begin by creating 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 increase the size of 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 confirm the successful resizing and verify the new size of the logical volume, 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

The output clearly indicates that the logical volume lv0 has been successfully resized to a capacity of 10GB.

Summary

This lab introduced the Linux resize command, a powerful utility used to modify the size of partitions and logical volumes. You learned about its syntax and crucial options, including -f for forced resizing, -p to display the new size, and -v for detailed output. Practical examples illustrated how to use resize to change the size of a partition to 20GB and an LVM logical volume to 50GB. These are common tasks in the life of a systemadmin.

You also learned how to create a new partition within a Docker container using the fdisk command. This laid the groundwork for resizing that partition using the resize command, completing the process of dynamic disk management.

400+ Linux Commands