Ubuntu 16.04: Extend and reduce LVM root filesystem


Table of Contents

1 System environment

Install Ubuntu 16.04 with LVM. Ubuntu 16.04 with LVM have boot delay problem, you can fix this problem with this solution.

0001_LVM.png

Partitions are as below.

 

/dev/vda1 mount point /boot /dev/vda5 physical volume for ubuntu-vg /dev/ubuntu-vg/root mount point / /dev/ubuntu-vg/swap_1 swap /dev/vdb appended disk

 

/proc/partitions is as below.

$ cat /proc/partitionsmajor minor #blocks name<snip> 253 0 83886080 vda 253 1 498688 vda1 253 2 1 vda2 253 5 83384320 vda5 11 0 1048575 sr0 252 0 82333696 dm-0 252 1 1048576 dm-1 253 16 83886080 vdb

2 Append disk to LVM and extend ext4 filesystem

Do below process with running Ubuntu 16.04 installed at storage.

2.1 Create partition of appended disk

Run fdisk to /dev/vdb.

$ sudo fdisk /dev/vdbWelcome to fdisk (util-linux 2.27.1).Changes will remain in memory only, until you decide to write them.Be careful before using the write command.Command (m for help):Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions)Select (default p):Using default response p.Partition number (1-4, default 1):First sector (2048-167772159, default 2048):Last sector, +sectors or +size{K,M,G,T,P} (2048-167772159, default167772159):Created a new partition 1 of type ‘Linux’ and of size 80 GiB.Command (m for help):The partition table has been altered.Calling ioctl() to re-read partition table.Syncing disks.

2.2 Create physical volume

Create physical volume with pvcreate.

$ sudo pvcreate /dev/vdb1 Physical volume “/dev/vdb1” successfully created

2.3 Append physical volume to valume group

Append /dev/vdb1 to ubuntu-vg with vgextend.

$ sudo vgextend ubuntu-vg /dev/vdb1Volume group “ubuntu-vg” successfully extended

pvdisplay display list of physical volume. /dev/vdb1 is used by ubuntu-vg.

$ sudo pvdisplay — Physical volume — PV Name /dev/vda5 VG Name ubuntu-vg PV Size 79.52 GiB / not usable 2.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 20357 Free PE 0 Allocated PE 20357 PV UUID 9YrynY-3kGZ-PGiP-d7W6-8k21-JQW1-Echp5a — Physical volume — PV Name /dev/vdb1 VG Name ubuntu-vg PV Size 80.00 GiB / not usable 3.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 20479 Free PE 20479 Allocated PE 0 PV UUID oJ75n9-BQbp-zFQd-t0Ct-ChNL-zpzT-MTzWbd

2.4 Extend logical volume

Extend /dev/ubuntu-vg/root with lvextend.

$ sudo lvextend -l +100%FREE /dev/ubuntu-vg/root

dm-0 is extended by the amount of /dev/vdb1.

$ cat /proc/partitionsmajor minor #blocks name<snip> 253 0 83886080 vda 253 1 498688 vda1 253 2 1 vda2 253 5 83384320 vda5 11 0 1048575 sr0 252 0 166215680 dm-0 252 1 1048576 dm-1 253 16 83886080 vdb 253 17 83885056 vdb1

2.5 Extend ext4 filesystem

Filesystem size is as below before extending.

$ df -h /Filesystem Size Used Avail Use% Mounted on/dev/mapper/ubuntu–vg-root 78G 5.0G 69G 7% /

Extend filesystem with resize2fs without umount.

$ sudo resize2fs /dev/ubuntu-vg/root

Filesystem size is as below after extending.

$ df -h /Filesystem Size Used Avail Use% Mounted on

3 Reduce ext4 filesystem and remove physical volume

Reducing ext4 filesystem needs to umount. In this case, run LiveDVD Ubuntu 16.04. And remain size must be larger than reducing size.

3.1 Run LiveDVD

Boot LiveDVD and select “Try Ubuntu”.

0002_LiveDVD.png

3.2 Check physical volume size

Reduce size must be larger than physical volume size. Check physical volume size with pvdisplay.

$ sudo pvdisplay /dev/vdb1 — Physical volume — PV Name /dev/vdb1 VG Name ubuntu-vg PV Size 80.00 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 20479 Free PE 0 Allocated PE 20479 PV UUID oJ75n9-BQbp-zFQd-t0Ct-ChNL-zpzT-MTzWbd

Reduce size should be 4MiB x 20479 = 81916MiB.

3.3 Reduce ext4 filesystem and logical volume

Reduce ext4 filesystem and logical volume at once with lvreduce -r option.

$ sudo lvreduce -r -L -81916M /dev/ubuntu-vg/rootfsck from util-linux 2.27.1/dev/mapper/ubuntu–vg-root: clean, 208635/10395648 files,1987952/41553920 blocksresize2fs 1.42.13 (17-May-2015)Resizing the filesystem on /dev/mapper/ubuntu–vg-root to 20583424(4k) blocks.The filesystem on /dev/mapper/ubuntu–vg-root is now 20583424 (4k)blocks long. Size of logical volume ubuntu-vg/root changed from 158.52 GiB (40580 extents) to 78.52 GiB (20101 extents). Logical volume root successfully resized.

3.4 Remove physical volume from volume group

Move data in /dev/vdb1 to /dev/vda5 with pvmove. In this case, there is no data in /dev/vdb1.

$ sudo pvmove /dev/vdb1 /dev/vda5 No free extents on physical volume “/dev/vda5”. No specified PVs have space available.

Remove /dev/vdb1 from ubuntu-vg with vgreduce.

$ sudo vgreduce ubuntu-vg /dev/vdb1 Removed “/dev/vdb1” from volume group “ubuntu-vg”

3.5 Erase physical volume

Erase physical volume with pvremove

$ sudo pvremove /dev/vdb1 Labels on physical volume “/dev/vdb1” successfully wiped

pvdisplay does not display /dev/vdb1.

$ sudo pvdisplay — Physical volume — PV Name /dev/vda5 VG Name ubuntu-vg PV Size 79.52 GiB / not usable 2.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 20357 Free PE 0 Allocated PE 20357 PV UUID 9YrynY-3kGZ-PGiP-d7W6-8k21-JQW1-Echp5a

Shutdown LiveDVD and boot Ubuntu 16.04 installed at storage. Filesystem size is reduced.

$ df -h /Filesystem Size Used Avail Use% Mounted on/dev/mapper/ubuntu–vg-root 78G 5.0G 69G 7% /

Android | Linux | SDL - Narrow Escape