What does resize2fs command do when we extend or reduce a Logical volume. Is the function same or different while using lvextend and lvreduce commands ?
There are actually four different behaviors
resize2fs can have (one of them trivial). It depends on if the filesystem is mounted or unmounted and if you're shrinking or extending.
resize2fsattempts an online resize. More or less, this just tells the kernel to do the work. The kernel then begins writing additional filesystem metadata on the newly available storage. You can continue to use the filesystem as this happens.
Note that really old ext3 filesystems may not support online resize. You'll have to unmount the old filesystem to extend.
resize2fsdoes the work instead of the kernel. Mostly this consists of writing additional filesystem metadata to the newly available storage.
This isn't supported. It should just print out an error. This is the trivial behavior.
This is the most time consuming one, and also the most dangerous (though it still should be reasonably safe). If possible (e.g., there is sufficient space),
resize2fsmakes the filesystem use only the first size bytes of the storage. It does this by moving both filesystem metadata and your data around. After it completes, there will be unused storage at the end of the block device (logical volume), unused by the filesystem.
lvreduce change the size of the logical volume. They can additionally change the size of the filesystem if given the
-r option, which is probably the right way to go, especially with reducing. Accidentally giving the wrong size to
lvreduce is an unfortunately easy way to lose data;
-r prevents this (by ensuring that
resize2fs is told the same size).