Showing posts with label resize. Show all posts
Showing posts with label resize. Show all posts

Wednesday, November 7, 2018

LVM Resize – How to Decrease an LVM Partition

Here we show you how to shrink an LVM volume or partition in Linux by first resizing the file system followed by resizing the logical volume.

See here if you’re instead trying to do the opposite and expand an LVM volume.

Note: In this example we are working in CentOS 7, some commands may differ in different Linux distributions. As of CentOS 7 the default file system is XFS which is not currently possible to shrink, this example is working with the ext4 file system.

In this example we will work through shrinking logical volume /var/centos/var from 10GB to 5GB.

Overview of Logical Volume Manager (LVM)
Before working through the resizing process it’s important you first understand some basic concepts around physical volumes, volume groups, logical volumes, and the file system.

  • Physical Volume (PV): This can be created on a whole physical disk (think /dev/sda) or a Linux partition.
  • Volume Group (VG): This is made up of at least one or more physical volumes.
  • Logical Volume (LV): This is sometimes referred to as the partition, it sits within a volume group and has a file system written to it.
  • File System: A file system such as ext4 will be on the logical volume.
LVM Resize – How to decrease or shrink the logical volume
To decrease the size of an LVM partition you must first decrease the file system within in order to avoid possible data corruption. As there is the potential for this to happen if you enter the command incorrectly, it is strongly recommended that you have a full backup of your data before proceeding. Shrinking a logical volume will give you more space in the volume group, meaning that you could instead extend another logical volume with this new found space.

The first step will depend on if you’re looking to shrink a LVM root volume, or non-root volume.

Shrinking a root volume
The root volume would typically be the logical volume that is mounted to /. You cannot unmount this to shrink it as it’s in use by the running operating system meaning that you will have to first boot from a Live CD to complete this. Once booted into the Live CD, you may first need to run the below command to pick up LVM volumes, however this usually happens during boot so may not be required, if in doubt just run it.
vgchange -a y
Shrinking a non-root volume
Alternatively if the volume you are shrinking is a non-root volume, that is any other volume not mounted to the root of the file system, you can unmount the volume as shown below to proceed. Please note that when you unmount the volume the data will not be available, so you may need to schedule down time and stop running applications that use data from it prior to unmounting. Unmount by specifying either the logical volume or the location it’s currently mounted to, in the below example we specify the logical volume which can be found in /dev/(vg-name)/(lv-name).
umount /dev/centos/var
All following steps now apply to both a root or non-root volume.
Before being able to attempt to shrink the size of an LVM volume, you must first run a file system check on it. If you don’t do this, you will get an error message and will not be able to proceed. This is a required step as resizing a file system in a bad state could cause data corruption. The -f flag makes the check run even if the file system appears clean, while -y assumes yes to all questions and will respond if asked to fix a problem.
e2fsck -fy /dev/centos/var
Next you need to shrink the file system, to be safe we’re going to shrink the file system lower than what the logical volume will shrink to. This is because we don’t want to accidentally shrink the logical volume to a size lower than the file system in the next step, as this can result in corruption and data loss. Don’t worry, we’ll reclaim the space at the end.

The command below will shrink the file system so that it is only 4G in size total, note that what ever size you specify to shrink to you must have in free space within the file system otherwise you must first delete data.
resize2fs /dev/centos/var 4G
Once the file system has been reduced, we can shrink the size of the logical volume with the lvreduce command. Reduce this to the size that you want the volume to be, as specified by the -L flag. Instead if you want to reduce by a specified size, simply put a – in front of the size. Both are shown below for completeness, however you only need to run one.

To reduce to 5G
lvreduce -L 5G /dev/vg/disk-name
To reduce by 5G
lvreduce -L -5G /dev/vg/disk-name
Once you execute the lvreduce command you will get a warning advising the size you have chosen to reduce to so use this as a chance to confirm you’re shrinking the logical volume to a size that is NOT smaller than the size you previously shrunk the file system to. Once you have confirmed it’s fine to proceed enter ‘y’ and press enter.

After the logical volume has been lowered to the required size, run resize2fs on the volume as this will extend the file system to use all available space within the logical volume. This makes use of all remaining free space so that none is wasted from when we previously shrunk the file system to a lower size than the logical volume.
resize2fs /dev/centos/var
At this point all that’s left to do is mount the volume. If this was a root volume and you’re working within a Live CD, simply boot back into your primary Linux operating system.

If this was a non-root volume and you unmounted it to complete the reduction, simply mount it back. You can do this with ‘mount -a’ assuming you have the configuration already set in /etc/fstab, otherwise specify the logical volume and where it should mount to. Here we’re manually mounting to /mnt just for testing.
mount /dev/centos/var /mnt
After you’ve either booted back to primary operating system or completed the mount, check the space shown with the ‘df’ command to confirm it has been decreased as expected.
[root@CentOS7 /]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  9.8G  1.4G  8.5G  14% /
devtmpfs                 908M     0  908M   0% /dev
tmpfs                    914M     0  914M   0% /dev/shm
tmpfs                    914M  8.6M  905M   1% /run
tmpfs                    914M     0  914M   0% /sys/fs/cgroup
/dev/sda1                497M   96M  402M  20% /boot
/dev/mapper/centos-var  4.8G   20M  4.6G   1% /mnt
In this example /dev/centos/var is correctly showing as shrunk down from the original 10G.

Summary
We have now successfully shrunk a file system and corresponding LVM logical volume. Root volumes can only be shrunk by unmounting the file system which requires booting into a Live CD to complete the work. For non-root volumes the file system must first be unmounted so that you can shrink the volume. First the file system was checked, then reduced. The logical volume itself was then reduced. It is strongly recommended to have a backup before attempting this to avoid data loss through mistakes.
Share:

LVM Resize – How to Increase an LVM Partition

Here we show you how to expand an LVM volume or partition in Linux by first resizing logical volume followed by resizing the file system to take advantage of the additional space.

See here if you’re instead trying to do the opposite and shrink an LVM volume.

Note: In this example we are working in CentOS 7, some commands may differ in different Linux distributions.

In this example we will work through expanding logical volume /var/centos/var from 5GB to 10GB. We currently have this logical volume mounted to /mnt.

Overview of Logical Volume Manager (LVM)

Before working through the resizing process it’s important you first understand some basic concepts around physical volumes, volume groups, logical volumes, and the file system.
  • Physical Volume (PV): This can be created on a whole physical disk (think /dev/sda) or a Linux partition.
  • Volume Group (VG): This is made up of at least one or more physical volumes.
  • Logical Volume (LV): This is sometimes referred to as the partition, it sits within a volume group and has a file system written to it.
  • File System: A file system such as ext4 will be on the logical volume.

LVM Resize – How to increase or expand the logical volume

This process is extremely easy to do with LVM as it can be done on the fly with no downtime needed, you can perform it on a mounted volume without interruption. In order to increase the size of a logical volume, the volume group that it is in must have free space available.

To view the free space of your volume group, run vgdisplay command as shown below and look at the “Free PE / Size” field.
[root@CentOS7 ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               20.74 GiB
  PE Size               4.00 MiB
  Total PE              5309
  Alloc PE / Size       4030 / 15.74 GiB
  Free  PE / Size       1280 / 5.00 GiB
  VG UUID               VvG6Sp-wIgb-LTh0-szdU-s9R1-a6K9-qHassI
In this example we have 5GB of free space in the volume group, as shown by “Free PE / Size 1279 / 5.00 GiB”.

Note: If you do not have any or enough free space in the volume group, you will first need to expand the volume group to complete the resize. Alternatively if you have multiple LVM partitions, you could shrink a different logical volume first to create space within the volume group.

Now that we have confirmed there is space free within the volume group, confirm the name of the logical volume you want to increase as well as how much space you plan on adding. The below lvdisplay command will show all logical volumes and their current size. It will also show the volume group that the logical volume is a member of, so ensure that the correct volume group has been checked for enough space with vgdisplay as previously mentioned to prevent trying to increase a logical volume that is inside some other volume group.

As shown in the example below, we are going to be working with the logical volume “var” which is in volume group “centos”, the volume group we saw in vgdisplay. In this example we only have just the one volume group, but you may have more so you need to check.
[root@CentOS7 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/var
  LV Name                var
  VG Name                centos
  LV UUID                7PNgg2-ZmnG-a26g-zRoT-PRVM-RDc1-oq6J4M
  LV Write Access        read/write
  LV Creation host, time CentOS7, 2015-04-16 07:50:25 +1000
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
Now it’s time to expand the logical volume. In the below example we are using the -L flag to increase by a size specified (M for Megabytes, G for Gigabytes, T for Terabytes). You can alternatively remove the + to increase to the amount specified rather than by the amount specified.
lvextend -L+5G /dev/centos/var
  Rounding size to boundary between physical extents: 4.90 GiB
  Size of logical volume centos/var changed from 5.00 GiB (1280 extents) to 10.00 GiB (2560 extents).
  Logical volume var successfully resized
The above command will increase the logical volume /dev/centos/var by 5GB, currently it is already 5GB so this will increase it to a total of 10GB. You could achieve the same with “lvextend -L 10G /dev/centos/var” which will increase the logical volume to 10GB as well, as this is what was specified with no +. Alternatively if you instead want to just use all free space in the volume group rather than specifying a size to increase to, run “lvextend -l +100%FREE /dev/centos/var”.

We can run the below lvdisplay command shown below to check that the extend completed as expected.
[root@CentOS7 ~]# vgdisplay
  --- Logical volume ---
  LV Path                /dev/centos/var
  LV Name                var
  VG Name                centos
  LV UUID                7PNgg2-ZmnG-a26g-zRoT-PRVM-RDc1-oq6J4M
  LV Write Access        read/write
  LV Creation host, time CentOS7, 2015-04-16 07:50:25 +1000
  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:2
Now that the logical volume has been extended, we can resize the file system. This will extend the file system so that it takes up the newly created space inside the logical volume. The command may differ depending on the type of file system you are using.

Use this for ext3/4 based file systems
resize2fs /dev/centos/var
Alternatively, use this for xfs based file systems
xfs_growfs /dev/centos/var
After the file system has been resized the space should be ready to use. If you run a ‘df’ command to view the disk space you should see that it’s been increased successfully.
[root@CentOS7 mnt]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  9.8G  1.4G  8.5G  14% /
devtmpfs                 908M     0  908M   0% /dev
tmpfs                    914M     0  914M   0% /dev/shm
tmpfs                    914M  8.6M  905M   1% /run
tmpfs                    914M     0  914M   0% /sys/fs/cgroup
/dev/sda1                497M   96M  402M  20% /boot
/dev/mapper/centos-var    10G   33M   10G   1% /mnt
In this example I have run a ‘mount /dev/centos/var /mnt’ to mount the logical volume to /mnt, as shown above /mnt is correctly reporting a size of 10G.

Summary

We have now successfully expanded a file system and corresponding LVM logical volume without any down time. This was done by first expanding the logical volume, and then performing an on-line resize of the file system.
Share: