Note: This is an RHCSA 7 exam objective.
Prerequisites
This tutorial requires 200MB of available disk space.
Before extending any unencrypted logical volume, let’s create one of 100MB called lv_vol in the vg volume group:
# lvcreate --size 100M --name lv_vol vg
Only unencrypted Ext4 and XFS file systems can be extended, not vfat.
Note1: In this tutorial, the lvextend and lvreduce commands are used to respectively increase and decrease the size of a logical volume. For brevity’s sake, the lvresize command is not discussed here, but the same operations can be done in a slightly different way with this command (see comments).
Note2: With RHEL 7.3, the NetworkManager service got two new directives in its unit file: ProtectSystem=true and ProtectHome=read-only. These directives forbid some changes made to system (/usr, /boot, /root, /run/user) and /home directories, mainly the creation of symbolic links to them (see details here). For most operations, this shouldn’t change anything, but it’s better to know that.
Ext4 logical volume extension
To create an ext4 file system on the previously created logical volume, type:
# mkfs.ext4 /dev/vg/lv_vol mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 25688 inodes, 102400 blocks 5120 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=33685504 13 block groups 8192 blocks per group, 8192 fragments per group 1976 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done
Optionally, mount the new file system on /mnt:
# mount /dev/vg/lv_vol /mnt
To allocate all the space available in the volume group to an unencrypted Ext4-formatted logical volume (here /dev/vg/lv_vol), type:
# lvextend -l +100%FREE -r /dev/vg/lv_vol
Note: The file system is automatically extended (-r) without any need to unmount it.
To extend the size of an unencrypted Ext4-formatted logical volume (here /dev/vg/lv_vol) by 50MB, type:
# lvextend --size +50M -r /dev/vg/lv_vol Rounding size to boundary between physical extents: 52.00 MiB Extending logical volume lv_vol to 152.00 MiB Logical volume lv_vol successfully resized resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/mapper/vg-lv_vol is mounted on /mnt; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/mapper/vg-lv_vol is now 155648 blocks long.
Although there is no reason to do so, you can still do it in two steps:
# lvextend --size +50M /dev/vg/lv_vol Rounding size to boundary between physical extents: 52.00 MiB Extending logical volume lv_vol to 152.00 MiB Logical volume lv_vol successfully resized # resize2fs /dev/vg/lv_vol resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/vg/lv_vol is mounted on /mnt; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/vg/lv_vol is now 155648 blocks long.
Ext4 logical volume reduction
Conversely, to reduce the size of a logical volume (here by 50MB), you have to follow these steps:
Unmount the file system (here /dev/vg/lv_vol):
# umount /dev/vg/lv_vol
Reduce the size of the logical volume (here /dev/vg/lv_vol) and the associated file system at the same time (-r):
# lvreduce --size -50M -r /dev/vg/lv_vol Rounding size to boundary between physical extents: 48.00 MiB fsck from util-linux 2.23.2 /dev/mapper/vg-lv_vol: 11/37544 files (9.1% non-contiguous), 10390/155648 blocks resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/mapper/vg-lv_vol to 106496 (1k) blocks. The filesystem on /dev/mapper/vg-lv_vol is now 106496 blocks long. Reducing logical volume lv_vol to 104.00 MiB Logical volume lv_vol successfully resized
Mount the file system (here /dev/vg/lv_vol):
# mount /dev/vg/lv_vol /mnt
XFS logical volume extension
To create a XFS file system on the previously created logical volume called lv_vol (see prerequisites), type:
# mkfs.xfs /dev/vg/lv_vol meta-data=/dev/vg/lv_vol isize=256 agcount=4, agsize=6400 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=25600, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
Mount the new file system on /mnt:
# mount /dev/vg/lv_vol /mnt
To extend the size of an unencrypted XFS-formatted logical volume (here /dev/vg/lv_vol) by 50MB, type:
# lvextend --size +50M -r /dev/vg/lv_vol Rounding size to boundary between physical extents: 52.00 MiB Extending logical volume lv_vol to 152.00 MiB Logical volume lv_vol successfully resized meta-data=/dev/mapper/vg-lv_vol isize=256 agcount=4, agsize=6400 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=25600, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 25600 to 38912
Note1: The file system is automatically extended (-r) without any need to unmount it.
Note2: You can’t reduce a XFS file system even though you unmount it. You have to back it up (with tar or another tool), drop it and recreate it.
Although there is no reason to do so, you can still do it in two steps:
# lvextend --size +50M /dev/vg/lv_vol Rounding size to boundary between physical extents: 52.00 MiB Extending logical volume lv_vol to 152.00 MiB Logical volume lv_vol successfully resized # xfs_growfs /mnt meta-data=/dev/mapper/vg-lv_vol isize=256 agcount=4, agsize=6400 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=25600, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=853, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 25600 to 38912
Note: The xfs_growfs command accepts only a mounted XFS filesystem as parameter.
Additional Resources
Although outside of the RHCSA objectives, TheGeekDiary website offers an interesting tutorial about shrinking the LVM root file system.
You can alternatively use “lvresize” command.
Ex:
“lvresize -L 10G -r /dev/vg/lv_vol”
Explanation:
-L option – sets volume to fixed 10 GB (using smaller “g” will assume GiB)
-r = resizing file system automatically ( replaces “e2fsck” and “resize2fs”/”xfs_growfs”) which can speed things up
NB: before mounting a resized logical volume without “-r” option, ALWAYS resize the file system.
Example :
shrinking the log. volume – first reduce the file system to the needed size by :
“e2fsck /”
“resize2fs / ”
Then resize the volume via “lvreduce”/”lvresize”.
When extending the log. volume – first “lvextend”/”lvresize” and then “e2fsck” + “resize2fs”
Some time ago i failed the exam because the time was not enough to fix the mistake with resizing logical volumes.The safest way to resize is via “lvresize” with the -r option.
Thanks for all these very useful explanations.
Hi
In extending xfs, I think you shoud use the below command:
# lvextend –size +50M /dev/vg/lv_vol
# xfs_growfs /dev/vg/lv_vol
and not:
# lvextend –size +50M /dev/vg/lv_vol
# xfs_growfs /mnt
Please correct me if I’m wrong?
I have another question: when I tried to extend the xfs, I receive the below error:
# lvextend -L 16M /dev/datacontainer10/datacopy10
New size given (1 extents) not larger than existing size (50 extents)
Run `lvextend –help’ for more information.
Can you please advise what is wrong?
P.S: extent is 50, and PE size is 16
Concerning the xfs_growfs command, both options work. You can try.
Furthermore, I advise you to use the following command:
# lvextend -r –size +50M /mnt
This way you do everything in only one step, which is quicker and less prone to error (both are important aspects during the exam).
Concerning the second point, you are specifying an absolute size smaller than the current one.
Either you specify a relative size (+50M for example) or a bigger absolute size (300M for example).
lvextend is only used for increasing size, not for reducing it (->lvreduce).
Also, you can’t reduce the size of a xfs file system (however, this works with ext4).
I have seen some of the sample questions where a floating range was mentioned.
For instance, increase lv to 390M and the size of the floating range should set between 380M and 400M.
What exactly the floating range mean here? is it something to do with volume group?
Hi CertDepot
First of all, thank you very much for your efforts.
I have my RHCSA exam on this Friday (first time, no NDA so far). I need to know three things.
1. My understanding is that in the exam objectives, reducing logical volumes (lvreduce) is not included and only “Extend existing logical volumes” is specifically written. Same was mentioned by Sander in one of his videos. Can you please confirm this?
2. In case it is not part of the exam objectives (possibility due to the fact that size of xfs can’t be reduced) or in the exam, existing file systems are only xfs. It will not be possible to reduce any of them. In that case, if available disk space is not enough to complete a given task, then the only solution will be to delete swap partition and then vgreduce, do the task and make the remaining space as swap.
Please confirm if this approach is correct.
3. In case if it is in the exam objectives and the root partition is ext4 with free space. Can you please give a quick way to reduce the size of the root partition. Although, I have done it but still need to know if a quicker and better way is possible.
Thanks in advance.
1) Even though reducing an ext4 file system is not in the exam objectives, you are supposed to know how to do it.
2) I don’t know if it’s the only solution but it is a solution.
3) Reducing the root partition is a complex operation. This is not part of the exam objectives (the root partition is a special case). I wouldn’t try to reduce the root partition during the exam: it’s too risky.
Thank you very much for your reply and confirmation. I have successfully reduced Ext4 root logical volume without any rescue disk and following is my procedure.
1. Reboot system with “rd.break” option at grub2 menu
2. Copy “resize2fs” file to tmp folder of ramfs as I couldn’t find it in ramfs .
# cp /sysroot/usr/sbin/resize2fs /tmp
3. unmount /sysroot
4. Run fsck on root file system
# e2fsck -f /dev/rhel/root
5. Run resize2fs on root file system (reduce to 3GB in my case).
# /tmp/resize2fs /dev/rhel/root 3G
6. exit and boot normally and login to the system.
7. Check locking_type in the lvm config and change it to 1
# grep locking_type /etc/lvm/lvm.conf
# locking_type = 4
change it to locking_type = 1
8. Then run lvreduce
# lvreduce -L 3G /dev/rhel/root
Done.
I was stuck at point-7 but got this info from a blog.
Interesting. Thanks.
Is it really necessary to convert the partition into lvm before join them to a volume? There is an example in this video of Ralph Nyberg https://youtu.be/iLQJIDC6MAM?t=3m19s
Yes. You have to execute the pvcreate command on a partition before using it.
partition converted to LVM —>pvcreate—>vgcreate
Is that correct?
Partition creation with fdisk/parted -> pvcreate -> vgcreate/vgextend
Sorry to get back to the subject. I just try to find out if converting the partition into lvm is necessary before applying the command pvcreate. So I would like to ask you if the following is correct:
create partition with fdisk—> convert the partition to lvm with fdisk—>pvcreate -> vgcreate/vgextend
You don’t need to convert the partition to lvm with fdisk.
You only need to create the partition and define the type of partition with fdisk.
After, you run pvcreate and vgcreate/vgextend.
Thanks Certdepot
Thank you for the great site. Any reason why you tend to use –size ( instead of the easier -L ) and
–name ( instead of easier -n ) for your lvcreate and lvextend commands ? Just wondering.
It’s only because I find it easier to remember.
I agree with CertDepot, I find –size making more sense when it comes to remembering than for example capital -L.