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 ?
Linux – What Does resize2fs Command Do
lvmresize2fs
lvmresize2fs
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 ?
Best Answer
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.Mounted, Extending
Here,
resize2fs
attempts 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.
Unmounted, Extending
This time,
resize2fs
does the work instead of the kernel. Mostly this consists of writing additional filesystem metadata to the newly available storage.Mounted, Shrinking
This isn't supported. It should just print out an error. This is the trivial behavior.
Unmounted, Shrinking
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),
resize2fs
makes 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.lvextend
andlvreduce
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 tolvreduce
is an unfortunately easy way to lose data;-r
prevents this (by ensuring thatresize2fs
is told the same size).