I am used to being able to use the system-config-lvm
utility in RedHat Linux (I was using RHEL 5) to resize the LVM while the system was running. To be clear, I did this while running on the disk I was using for the system I was using and there were no errors or issues. When doing this, I was told that the volumes would have to be unmounted then mounted back when done. This worked when I did it.
I tried doing this in CentOS 6.5 with system-config-lvm
. I first tried to resize the /home
partition to be smaller so I could grow the /root
partition. I sized it down, clicked apply, I was informed that it would have to be unmounted first, I clicked OK, and I was told it couldn't be unmounted because the device was busy. This is already different behavior than RHEL, and I understand that LV's are intended to have the ability to be resized while in use. Well some degree of searching online said that this was not actually the case, and it is not possible (to unmount the volume while in use) and recommended to use a live CD and perform the operation with the file system not yet mounted. This is what I did. I took a CentOS 6.5 Live CD, ran system-config-lvm
to resize the LVs (basically swap the size of the /
(root) and /home
volumes), it was successful, then I rebooted.
When I rebooted, the boot was interrupted by file system errors that were detected. I decided to undo my logical volume resize back to their original sizes (which required booting back into the Live CD and repeating the LVM process), rebooted and still errors. The errors resided only with the /home
volume, because the /
(root) volume was totally intact. To try and rescue, I tried doing fsck -y
and that didn't quite work, and it's okay because this whole file system was a clone of a VM to begin with. I don't mind losing the data.
But what I'm after is, why did this happen? Or better yet, what is the correct way to do this? Ideally of course without creating file system errors. Could this ever be the result of sizing a volume to be too small than contents of the volume (I believe I checked and this was not the case, but I cannot be totally sure)? I see this server fault post that recommends using resize2fs
. Is that the correct way to do it, and not system-config-lvm
?
Lvm manages block devices, not the filesystem on top of them. To grow your filesystem you first grow the block device (the LV).
Then you grow your filesystem using resize2fs for ext2/3/4.
It will grows the filesystem to the new size of your volume.
This works online (mounted) or offline (unmounted).
For xfs you would use xfs_growfs, which ONLY works online (mounted). You cannot shrink xfs filesystems and you only can shrink ext2/3/4 filesystems offline (unmounted).
It is vitally important that you first shrink your filesystem (resize2fs New size filesystem) and after that you shrink your logical volume.
Lvm has 3 layers. The lowest layer is called the physical layer, all commands start with pv. pvcreate takes the disk or partition (the physical volume) and slices it into a lot of numbered blocks of a a size (default 4 MB). The blocks are called physical extends, PE for short. You can see it with pvdisplay.
The middle layer is the volume group, all commands start wuth vg. Vgcreate then takes the PEs of one or more physical volumes (disks) and groups them together into a volume group.
The upper layer is the logical volume layer, all commands start with lv. (I trust you see the pattern ).
lvcreate takes a number of blocks according to the desired size of the logical volume (not necessarily all on the same disk) and presents them as a bblock device to the linux system.
On top of that new block device ( the logical volume ) you then create your filesystem. (ext4, xfs, vfat, ntfs ...) You mount this filesystem and use it to store your files.
Now comes the fun part: If you run out of space in your filesystem you just grow your logical volume and then the filesystem on top of it.
If there are no more free PE in your volumegroup you add a new dusk to your machine, do pvcreate new-disk, add it to your volume group with vgextend and you can grow your logical volume bigger than the original disk.
Important: Your filesystem ( where you store your date,your folders and files ) lives on top of the logical volume, so if you shrink your lv without shrinking the filesystem first it is lke you chop of part of your disk where data and filesystem structures still point to, so your filesystem is damaged.