Resizing a Linux partition without reboot

From Phormix Wiki
Jump to: navigation, search

Resizing a Linux partition

Summary

Reallocating space on a linux system has various steps, starting at the underlying infrastructure and then down to the level of the filesystem The steps in general include, in order:

  • Resizing the underlying infrastructure
  • Re-reading the disk and partition changes
  • Changing the partition boundaries
  • For LVM: Resizing your PV, VG, and LV (Logical Volume)
  • Resizing the filesystem

This has been broken down in sections below.

 

Reading disk changes


Rescanning a partition table can be down via a couple ways. 

Check the underlying infrastructure

Using Partprobe

The "partprobe" command (part of the parted package) can be used to re-scan the partitions without a reboot
 


By rescanning SCSI bus

for X in /sys/class/scsi_host/host* ; do echo "- - -" > ${X}/scan; done


Rescan the disk itself

Rescan the disks

for X in /sys/block/[vs]d?; do echo ${X}; echo >${X}/device/rescan; done

Physical partition resizing

The following is an example of resizing a log partition (sdb). Be sure to test in a non-prod environment before making changes in production.

Resizing a partition is actually a matter of removing the old partition entry and then re-adding it with a new size but the same start-point Be sure to also retain the partition type (LVM=8e, etc). It is not so much changing the size as redefining the "boundaries" of the partition

In this case, the partition takes the entire disk. In other cases, the "fdisk" step may need adjustment if the partition does not take a whole disk
 

Check current disk, note size etc

fdisk -l /dev/sdb

Rescan disk (see above for the various ways to do this)

Check current disk, size should have increased to match changes in your underlying hardware or virtual hardware (VM's)

fdisk -l /dev/sdb

Resize parent partition. In this example we're using a partition that spans the full disk /dev/sdb, has a type of LVM. We're also using fdisk but you could use gparted, parted, etc

This assumes it's an MSDOS type partition table. The steps will vary for other partition table types (such as GPT)

The fdisk steps below include

  • delete existing partition
  • New partition
    • Primary (vs extended. This applies to MSDOS type partitions only)
    • Partition #1
    • Lower Bound
    • Upper Bound
  • Change Type
    • Type 8e (Linux LVM)
fdisk /dev/sdb
 d
 n
 p
 1
 [default]
 [default]
 t
 8e

You are now ready to resize your volume and/or filesystem


LVM resizing

 

Changing the size of an LVM Physical Volume, Volume Group, and Logical Volume

In this example, it's assumed that the size the physical (or virtual) disk device/partition has change and you need to make changes to LVM to take advantage of this

we're using the sdb1 physical partition with an LVM filesystem. It has a volume group "vg1" and a logical voume called "log" which we want to increase size for

 

Resize LVM physical volume

pvresize /dev/sdb1

Resize LVM logical volume 

  • If you wanted to resize by a fixed amount, e.g. 2G
lvresize -L +2G /dev/vg1/log
  • OR, if you wanted to use max available size
lvresize -l 100%FREE /dev/vg1/log

Afterwards, you will likely still need to resize your filesystem

Adding to a Volume Group and resizing Logical Volume

In this example, you've added a new partition/disk and wish to make it available as part of your LVM Volume Group,then apply the newly available space to the volume

We've added partition /dev/sdc1 which we now was to add to the volume group "vg1", then add 2GB of space to the logical volume "log"


Create the physical volume in /dev/sdc1

pvcreate /dev/sdc1

Extend the Volume Group using the Physical Volume

vgextend vg1 /dev/sdc1

Using the newly available space, add some to the log volume

lvresize -L +2G /dev/mapper/vg1-log

 

Afterwards, you will likely still need to resize your filesystem

 

FileSystem Resizing

The following steps detail how to resize various filesystems.

For the case of the example the filesystem is on /dev/sdb1 but it could be anywhere (physical partition, lvm volume, etc) so please ensure you have the correct device name(s) for your system!

It is assumed you have already resized the underlying infrastructure and partitions per the above

 

EXT2

Unmount (you will have to ensure nothing else is accessing the partition/mountpoint)

umount /dev/sdb1

FSCK (check) filesystem

e2fsck -f /dev/sdb1

Resize filesystem

resize2fs /dev/sdb1

Remount the FS

mount /dev/sdb1

EXT3

Unmount (you will have to ensure nothing else is accessing the partition/mountpoint)

umount /dev/sdb1

FSCK (check) filesystem

e2fsck -f /dev/sdb1

Remove Journal

tune2fs -O ^has_journal /dev/sdb1

FSCK (check) filesystem

e2fsck -f /dev/sdb1

Resize filesystem

resize2fs /dev/sdb1

Restore journal

tune2fs -j /dev/sdb1

Remount the FS

mount /dev/sdb1

EXT4

Ext4 is nice in that you do a full hot-resize without any unmounting. Thus you have just one step

Resize filesystem

resize2fs /dev/sdb1

XFS

XFS has been able to hot-resize for some time. You can simply group the filesystem using

xfs_growfs /dev/sdb1

 


Reboot as needed

In many cases, you'll want to ensure that your filesystem comes up as expected after a reboot. As with any significant change, I still recommend doing so in a controlled manner at the most opportune time. Failing to do so may later lead to discovering your system is unbootable or has errors.

reboot