How to Resize LVM Root in Oracle Linux 7

 

Author: Dude!

 

 

 

1. About

This document explains how to resize the LVM root and home volumes in order to reclaim space to create additional files systems. The tasks includes a backup and restore in rescue mode to USB media using the xfsdump and xfsrestore utilities, and adding additional LVM volumes. The instructions are written for Red Hat Enterprise Linux 7 and apply to all derivatives. The system used in the example is a minimal Oracle Linux 7.3 installation on a 64 GB hard drive.

 

The information is provided under the terms and conditions of http://www.oracle.com/us/legal/terms/index.html.

Please do not plagiarize. Use on your own risk.

 

Copyright 2017, Dude@OTN

 

 

2. Analysis

 

Below example shows the space utilization of a minimal Oracle Linux 7.3 installation on a 64 GB hard drive. The installation was done using automatic partitioning and the installer created a 40 GB LVM root and a 20 GB home volume.

 

For the current project, the size of the root and home volumes are not necessary, and instead, 47 GB of disk space is needed to create additional LVM volumes named u01, u02 and u03. Volumes u02 and u03 should to be using an older EXT3 file system for reasons of application compatibility.

 

Analyze the disk space usage of the root and home file systems.

[root@ol1 ~]# df -PhT

Filesystem          Type      Size  Used Avail Use% Mounted on

devtmpfs            devtmpfs  2.0G     0  2.0G   0% /dev

tmpfs               tmpfs     2.0G     0  2.0G   0% /dev/shm@

tmpfs               tmpfs     2.0G  8.4M  2.0G   1% /run

tmpfs               tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup

/dev/mapper/ol-root xfs       40G  1.2G   39G   3% /

/dev/sda1           xfs      1014M  155M  859M  16% /boot

/dev/mapper/ol-home xfs        20G   33M   20G   1% /home

tmpfs               tmpfs     396M     0  396M   0% /run/user/0

 

Analyze current LVM volumes.

[root@ol1 ~]# lvs --segments -o +devices

  LV   VG Attr       #Str Type   SSize  Devices       

  home ol -wi-ao----    1 linear 19.39g /dev/sda2(992)

  root ol -wi-ao----    1 linear 39.72g /dev/sda2(5957)

  swap ol -wi-ao----    1 linear  3.88g /dev/sda2(0)  

 

 

The LVM volumes required are as following:

 

LVM VolumeCurrentUsedNewFS#
/dev/ol/root40 GB1.2 GB9 GBXFS- 31 GB
/dev/ol/home20 GB33 MB3 GBXFS- 17 GB
/dev/ol/u012 GBXFS+  2 GB
/dev/ol/u0215 GBEXT3+ 15 GB
/dev/ol/u0330 GBEXT3+ 30 GB

 

 

By reducing the LVM root volume to 9 GB and home to 3 GB, 48 GB of volume space can be reclaimed, which is enough to create volumes u01, u02 and u03. To avoid free space fragmentation however, both, root and home volumes will need to be relocated. A complete backup and restore of the root and home volume to external USB should be an appropriate safeguard and assist in the process.

 

 

Note:

Starting with RHEL 7 and consequently Oracle Linux 7,  XFS has become the default file system. According to XFS FAQ, it is not possible to reduce a XFS volume and the only way to shrink it is to do a complete dump, mkfs and restore.

 

 

3. Requirements

 

The following will be required:

 

  • Oracle Linux 7 standard installation DVD.
  • Direct or remote access to the physical server console.
  • External hard drive - 8 GB USB flash drive will be sufficient.

 

4. Rescue Mode

 

The LVM root volume cannot be removed while in use. It is therefore necessary to start the computer from external media and to access the system in rescue mode, also formerly known as single user mode. This will also quiescence the volumes and ensure no applications are running and no regular users are accessing the system when performing the backup and restore operations.

 

In order to start the computer from DVD or USB you may have to enter the PC boot menu, or change the boot order in the PC BIOS configuration. Please see the instruction manual of your computer for the details.

 

To start the computer into rescue mode, insert the standard Oracle Linux installation media and restart the computer.

If you are using VirtualBox, you can mount the ISO disk image and restart the guest OS.

 

Use the up and down arrow key to select Troubleshooting.

Screen Shot 2017-01-06 at 2.55.35 AM.jpg

 

Select Rescue a Oracle Linux system.

Select option 3 - Skip to shell.

Screen Shot 2017-01-06 at 3.00.41 AM.jpg

 

 

Tip:

If you are not fancy using the console, the following will let you connect using your ssh client from your desktop, assuming DHCP is available.

 

 

[anaconda root@localhost ~]# ip link show

[anaconda root@localhost ~]# ip link set enp0s3 down

[anaconda root@localhost ~]# dhclient enp0s3

[anaconda root@localhost ~]# ip address | grep global

[anaconda root@localhost ~]# ssh-keygen -t rsa -N "" -f /etc/ssh/ssh_host_rsa_key

[anaconda root@localhost ~]# /sbin/sshd -f /etc/ssh/sshd_config.anaconda

 

Screen Shot 2017-01-08 at 3.49.56 AM.jpg


After that, you should be able to connect with a ssh client. No prompt for password !!!

Screen Shot 2017-01-08 at 3.27.23 PM.jpg

 

 

 

5. Backup to USB

 

You will need a USB flash disk or hard drive in order to backup the LVM root and home volume.

Make it has enough capacity. As mentioned earlier, 8 GB should be sufficient.

 

Tip:To use USB in Oracle VirtualBox, attach the USB drive to your host system and select Settings from the VirtualBox Machine menu. Select Ports, then USB and add the USB device using the flash drive + symbol, where you can choose the device from the list of detected items. Press OK and unplug and re-insert the USB media so that VirtualBox can capture the device.

 

Plug in the USB device.

 

Determine the USB media device name (/dev/sdb).
[anaconda root@localhost ~]# dmesg | tac | grep -m 1 "removable"

[  658.877169] sd 3:0:0:0: [sdb] Attached SCSI removable disk ]

Partition and initialize the USB disk.

Warning: All existing data on the USB media will be erased!

[anaconda root@localhost ~]# parted -s /dev/sdb mklabel msdos mkpart primary 1 -- -1

[anaconda root@localhost ~]# parted -s /dev/sdb print

Model: SanDisk U3 Contour (scsi)

Disk /dev/sdb: 8213MB

Sector size (logical/physical): 512B/512B

Partition Table: mdse.

Disk Flags:
Number  Start   End     Size    Type     File system  Flags

1      1049kB  8212MB  8211MB  primary

Initialize partition no. 1, e.g.: /dev/sdb1

[anaconda root@localhost ~]# mkdosfs -n Linuxback -v -F 32 -I /dev/sdb1

mkfs.fat 3.0.20 (12 Jun 2013)

/dev/sdb1 has 253 heads and 62 sectors per track,

logical sector size is 512,

using 0xf8 media descriptor, with 16037888 sectors;

filesystem has 2 32-bit FATs and 8 sectors per cluster.

FAT size is 15632 sectors, and provides 2000824 clusters.

There are 32 reserved sectors.

Volume ID is 89cea7e0, volume label LINUXBACK  .

 

 

Mount the USB file system.

[anaconda root@localhost ~]# mkdir /mnt/usb

[anaconda root@localhost ~]# mount /dev/sdb1 /mnt/usb

 

[anaconda root@localhost ~]# df -hT /mnt/usb

Filesystem     Type  Size  Used Avail Use% Mounted on

/dev/sdb1      vfat  7.7G  4.0K  7.7G   1% /mnt/usb

Activate the LVM volumes.

[anaconda root@localhost ~]# vgchange -a y

3 logical volume(s) in volume group "ol" now active

[anaconda root@localhost ~]# lvscan

  ACTIVE            '/dev/ol/swap' [3.88 GiB] inherit

  ACTIVE            '/dev/ol/home' [19.39 GiB] inherit

  ACTIVE            '/dev/ol/root' [39.72 GiB] inherit

Mount LVM home and root and verify the correct file systems are mounted.

[anaconda root@localhost ~]# mkdir /mnt/root

[anaconda root@localhost ~]# mkdir /mnt/home

[anaconda root@localhost ~]# mount /dev/ol/root /mnt/root

[anaconda root@localhost ~]# mount /dev/ol/home /mnt/home

 

[anaconda root@localhost ~]# df -hT /mnt/home /mnt/root

Filesystem          Type  Size  Used Avail Use% Mounted on

/dev/mapper/ol-home xfs    20G   33M   20G   1% /mnt/home

/dev/mapper/ol-root xfs    40G  1.2G   39G   3% /mnt/root

 

 

Create a backup of the LVM root and home volume using the xfsdump utility.

 

When prompted for session and media label, simply enter "root" and "home".

(Below shows only partial output.)

[anaconda root@localhost ~]# xfsdump -f /mnt/usb/root.dmp /mnt/root

please enter label for this dump session (timeout in 300 sec)

-> root

please enter label for media in drive 0 (timeout in 300 sec)

-> root

xfsdump: media file size 1113760064 bytes

xfsdump: dump complete: 74 seconds elapsed

xfsdump: Dump Summary:

xfsdump:   stream 0 /mnt/usb/root.dmp OK (success)

xfsdump: Dump Status: SUCCESS

 

[anaconda root@localhost ~]# xfsdump -f /mnt/usb/home.dmp /mnt/home

please enter label for this dump session (timeout in 300 sec)

-> home

please enter label for media in drive 0 (timeout in 300 sec)

-> home

xfsdump: media file size 25600 bytes

 

xfsdump: dump complete: 2 seconds elapsed

xfsdump: Dump Summary:

xfsdump:   stream 0 /mnt/usb/home.dmp OK (success)

xfsdump: Dump Status: SUCCESS

 

Verify the results.

[anaconda root@localhost ~]# ls -l /mnt/usb

total 1088456

-rwxr-xr-x. 1 root root      25600 Jan  8 14:38 home.dmp

-rwxr-xr-x. 1 root root 1113760064 Jan  8 14:35 root.dmp

 

Unmount the USB backup, LVM root and home volumes.

[anaconda root@localhost ~]# umount /mnt/usb /mnt/root /mnt/home

 

 

 

6. Resizing LVM Root and Home

 

As mentioned earlier, XFS file systems cannot be resized and will essentially have to be erased and re-initialized.

 

Note:

Be sure to have completed the instructions so far. Do not continue if you do not have a backup of the LVM root and home volume, since the data is going to be deleted.

 

 

Deactivate and remove the LVM root and home volumes.

Warning: All data on these volumes will be lost!

[anaconda root@localhost ~]# vgchange -a n

  0 logical volume(s) in volume group "ol" now active

[anaconda root@localhost ~]# lvremove /dev/ol/root /dev/ol/home

  Logical volume "home" successfully removed

  Logical volume "root" successfully removed

 

Verify the physical LVM free volume space has increased.

[anaconda root@localhost ~]# pvs

  PV         VG Fmt  Attr PSize  PFree

  /dev/sda2  ol lvm2 a--  63.00g 59.12g

 

Recreate the LVM root and home volume.

Respond with "y" when prompted to wipe the previous XFS signature.

[anaconda root@localhost ~]# lvcreate -L 9G -n root ol

WARNING: xfs signature detected on /dev/ol/root at offset 0. Wipe it? [y/n]: y

  Wiping xfs signature on /dev/ol/root.

  Logical volume "root" created.

[anaconda root@localhost ~]# lvcreate -L 3G -n home ol

  Logical volume "home" created.


[anaconda root@localhost ~]# pvs

  PV         VG Fmt  Attr PSize  PFree

  /dev/sda2  ol lvm2 a--  63.00g 47.12g

 

 

Active LVM and show available LVM volumes.

[anaconda root@localhost ~]# vgchange -a y

  3 logical volume(s) in volume group "ol" now active

[anaconda root@localhost ~]# lvscan

  ACTIVE            '/dev/ol/swap' [3.88 GiB] inherit

  ACTIVE            '/dev/ol/root' [9.00 GiB] inherit

  ACTIVE            '/dev/ol/home' [3.00 GiB] inherit

 

Initialize and mount the LVM root and home volumes.
(Below shows only partial output.)

[anaconda root@localhost ~]# mkfs.xfs -L root /dev/ol/root

[anaconda root@localhost ~]# mkfs.xfs -L home /dev/ol/home

 

 

 

7. Restoring LVM Root and Home

 

Mount the LVM root and home volumes.

[anaconda root@localhost ~]# mount /dev/ol/root /mnt/root

[anaconda root@localhost ~]# mount /dev/ol/home /mnt/home

 

Verify that the correct volumes are mounted.

[anaconda root@localhost ~]# df -PhT /mnt/root /mnt/home

Filesystem          Type  Size  Used Avail Use% Mounted on

/dev/mapper/ol-root xfs   9.0G   33M  9.0G   1% /mnt/root

/dev/mapper/ol-home xfs   3.0G   33M  3.0G   2% /mnt/home

 

Mount the USB backup media.

[anaconda root@localhost ~]# mount /dev/sdb1 /mnt/usb

 

[anaconda root@localhost ~]# ls -l /mnt/usb

total 1088456

-rwxr-xr-x. 1 root root      25600 Jan  8 14:38 home.dmp

-rwxr-xr-x. 1 root root 1113760064 Jan  8 14:35 root.dmp

 

Restore the LVM root and home volumes.

(Below shows only partial output.)

[anaconda root@localhost ~]# xfsrestore -f /mnt/usb/root.dmp /mnt/root

xfsrestore: restore complete: 206 seconds elapsed

xfsrestore: Restore Summary:

xfsrestore:   stream 0 /mnt/usb/root.dmp OK (success)

xfsrestore: Restore Status: SUCCESS

 

[anaconda root@localhost ~]# xfsrestore -f /mnt/usb/home.dmp /mnt/home

xfsrestore: restore complete: 0 seconds elapsed

xfsrestore: Restore Summary:

xfsrestore:   stream 0 /mnt/usb/home.dmp OK (success)

xfsrestore: Restore Status: SUCCESS

 

Verify the result.

[anaconda root@localhost ~]# df -PhT

Filesystem          Type      Size  Used Avail Use% Mounted on

/dev/mapper/live-rw ext4      2.0G  1.1G  941M  53% /

devtmpfs            devtmpfs  2.0G     0  2.0G   0% /dev

tmpfs               tmpfs     2.0G  4.0K  2.0G   1% /dev/shm

tmpfs               tmpfs     2.0G  8.4M  2.0G   1% /run

tmpfs               tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup

/dev/sr0            iso9660   415M  415M     0 100% /run/install/repo

tmpfs               tmpfs     2.0G  380K  2.0G   1% /tmp

/dev/mapper/ol-root xfs       9.0G  1.2G  7.9G  13% /mnt/root

/dev/mapper/ol-home xfs       3.0G   33M  3.0G   2% /mnt/home

/dev/sdb1           vfat      7.7G  1.1G  6.6G  14% /mnt/usb

 

 

Make sure that there is no mismatch between the UUID of the actual device and the UUID used in the file system table (fstab) of the restored LVM root volume. If necessary, edit the file system table accordingly. Normally this is should not be necessary, because LVM root and home volumes are usually not mounted using their UUID value.

[anaconda root@localhost ~]# lsblk -o name,size,mountpoint,uuid /dev/sda

NAME        SIZE MOUNTPOINT UUID

sda          64G           

|-sda1        1G            433b636e-9b4a-4ea1-8960-6c2288433ac5

`-sda2       63G            hCDgtc-lOph-wvHY-Za5Q-VtY5-r0fo-JcKMsa

  |-ol-root   9G /mnt/root  16e712b1-c194-4591-83d1-41dc2a36d6b8

  `-ol-home   3G /mnt/home  45a6818a-b13e-4a21-806b-454394eba056

 

[anaconda root@localhost ~]# cat /mnt/root/etc/fstab

#

# /etc/fstab

# Created by anaconda on Sun Jan  5 17:30:10 2017

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

/dev/mapper/ol-root                       /      xfs     defaults        0 0

UUID=433b636e-9b4a-4ea1-8960-6c2288433ac5 /boot  xfs     defaults        0 0

/dev/mapper/ol-home                       /home  xfs     defaults        0 0

/dev/mapper/ol-swap                       swap   swap    defaults        0 0

 

 

Remove the Oracle Linux installation media and USB storage device.

 

Dismount all mounted volumes and restart the system.

[anaconda root@localhost ~]# umount /mnt/*

umount: /mnt/install: not mounted

umount: /mnt/sysimage: not mounted

 

[anaconda root@localhost ~]# reboot

 

 

Verify the result.

[root@ol1 /]# df -PhT

Filesystem          Type      Size  Used Avail Use% Mounted on

devtmpfs            devtmpfs  2.0G     0  2.0G   0% /dev

tmpfs               tmpfs     2.0G     0  2.0G   0% /dev/shm

tmpfs               tmpfs     2.0G  8.4M  2.0G   1% /run

tmpfs               tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup

/dev/mapper/ol-root xfs       9.0G  1.2G  7.9G  13% /

/dev/mapper/ol-home xfs       3.0G   33M  3.0G   2% /home

/dev/sda1           xfs      1014M  156M  859M  16% /boot

tmpfs               tmpfs     396M     0  396M   0% /run/user/0

 

 

8. Creating Additional LVM Volumes

 

It should now be possible to create the additional LVM volumes.

 

Analyze the LVM space.

[root@ol1 /]# lvs --segments -o +devices

  LV   VG Attr       #Str Type   SSize Devices       

  home ol -wi-ao----    1 linear 3.00g /dev/sda2(3296)

  root ol -wi-ao----    1 linear 9.00g /dev/sda2(992)

  swap ol -wi-ao----    1 linear 3.88g /dev/sda2(0)  

 

[root@ol1 /]# pvs

  PV         VG Fmt  Attr PSize  PFree

  /dev/sda2  ol lvm2 a--  63.00g 47.12g

 

Create new LVM volumes u01, u02 and u03.

[root@ol1 /]# lvcreate -L 2G -n u01 ol

  Logical volume "u01" created.

[root@ol1 /]# lvcreate -L 15G -n u02 ol

  Logical volume "u02" created.

[root@ol1 /]# lvcreate -L 30G -n u03 ol

  Logical volume "u03" created.

 

The LVM volume space should now be used, with only a small amount remaining.

[root@ol1 /]# pvs

  PV         VG Fmt  Attr PSize  PFree

  /dev/sda2  ol lvm2 a--  63.00g 124.00m

 

Initialize the new LVM volumes.

(Below shows only partial output.)

[root@ol1 /]# mkfs.xfs -L u01 /dev/ol/u01

 

[root@ol1 /]# mkfs.ext3 -L u02 /dev/ol/u02

 

[root@ol1 /]# mkfs.ext3 -L u03 /dev/ol/u03

 

Create mount points (/u01 /u02 /u03) and add the appropriate LVM volumes to the /etc/fstab file system table to mount during startup.

[root@ol1 /]# mkdir /u01 /u02 /u03

 

[root@ol1 /]# lvscan

  ACTIVE            '/dev/ol/swap' [3.88 GiB] inherit

  ACTIVE            '/dev/ol/root' [9.00 GiB] inherit

  ACTIVE            '/dev/ol/home' [3.00 GiB] inherit

  ACTIVE            '/dev/ol/u01' [2.00 GiB] inherit

  ACTIVE            '/dev/ol/u02' [15.00 GiB] inherit

  ACTIVE            '/dev/ol/u03' [30.00 GiB] inherit

 

Modify /etc/fstab accordingly.

[root@ol1 /]# cat /etc/fstab

#

# /etc/fstab

# Created by anaconda on Sun Jan  8 17:30:10 2017

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

/dev/mapper/ol-root                        /       xfs   defaults        0 0

UUID=433b636e-9b4a-4ea1-8960-6c2288433ac5  /boot   xfs   defaults        0 0

/dev/mapper/ol-home                        /home   xfs   defaults        0 0

/dev/mapper/ol-swap                        swap    swap  defaults        0 0

/dev/mapper/ol-u01                         /u01    xfs   defaults        0 0

/dev/mapper/ol-u02                         /u02    ext3  defaults        0 2

/dev/mapper/ol-u03                         /u03    ext3  defaults        0 2

 

Either restart the system or manually mount all devices listed in /etc/fstab.

[root@ol1 /]# mount -a

 

Verify the result.

[root@ol1 /]# df -PhT

Filesystem          Type      Size  Used Avail Use% Mounted on

devtmpfs            devtmpfs  2.0G     0  2.0G   0% /dev

tmpfs               tmpfs     2.0G     0  2.0G   0% /dev/shm

tmpfs               tmpfs     2.0G  8.4M  2.0G   1% /run

tmpfs               tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup

/dev/mapper/ol-root xfs       9.0G  1.2G  7.9G  13% /

/dev/mapper/ol-home xfs       3.0G   33M  3.0G   2% /home

/dev/sda1           xfs      1014M  156M  859M  16% /boot

tmpfs               tmpfs     396M     0  396M   0% /run/user/0

/dev/mapper/ol-u01  xfs       2.0G   33M  2.0G   2% /u01

/dev/mapper/ol-u02  ext3       15G   38M   14G   1% /u02

/dev/mapper/ol-u03  ext3       30G   44M   28G   1% /u03

 

 

That's it. Best of luck!