The logical volume manager. Inconspicuous, unassuming but could quite honestly be one of the most integral parts of the linux OS. For those who have not really cared or taken much notice about the finer points of LVM, then this blog post may change your mind.
LVM can be used in a multitude of ways some of them being -
- Provide data protection by way or mirroring volumes
- Provide increased I/O performance by way of striping across volumes
- Ability to resize and reshape volumes on the fly
- Provide backup utilities using snapshots
These are a just a few of the benefits of understanding more about LVM. My experience in particular was to create GPT partitions on 22 SAN attached LUNS without losing my data. My LVM metadata was written directly on the LUN rather than a partition on the LUN so I had the problem of now trying to create the partition which required the first 34 sectors which would overwrite the LVM metadata which was situated at the first 4 sectors. Dilemma.
Each LUN was in it's own volume group and the logical volume that the file system sits on, was within that volume group. So the solution here was to add another physical volume to the volume group with the vgextend command and remove the existing one so I could then do what I want with it. It can immediately be seen how this may be benificial to other problems. For example if the problem was to replace the existing disks with bigger or faster disks, then the same methodology would also be relevant.
I now have 2 physical volumes in my volume group and using the pvmove command I could move the extents off the volume I so wish. One piece of advice here though is that this can be extremely time consuming, so it is a good idea to reduce the file system to not much bigger than what is absolutely necessary and in turn reduce the size of the logical volume.
Once the pvmove command is complete, the vgreduce -a command can be executed to remove the physical volume from the volume group. If the physical volume is still in use, vgreduce will inform you that ir is so and therefore will not trash your VG. It is always a good idea anyway to use the -t option (test).
Now that all the extents have been moved to the replacement disk, the old disk can be removed with the pvremove command and then what ever is required to be done with the disk/s can be done. To put back all that needs to be done is the reverse of the process - pvcreate, vgextend to add the disk again and then pvmove the subsitute disk.
Don't forget to lvresize and resize2fs back to the way it was after your done!
Another handy little tip is to keep copies of your /etc/lvm/archive and /etc/lvm/backup directories. Should for any reason you lose the lvm metadata on your physical volumes, then your going to need the above to piece everything back together.