Storage with LVM in linux
A Logical Volume Manager(LVM) provides and abstraction layer between filesystems and physical devices. This means that physical devices can be added or removed from a system without disrupting service. LVM also allows for snapshots and where the filesystem permits, dynamic resizing.
There are three fundamental terms:
- Physical Volume
- is a block device, a driver interface such as a to hard disk, a software RAID or an iSCSI initiator.
- Logical Volume
- is presented by the LVM to the OS as a physical device, and so can contain a filesystem. A Logical Volume is analogous to a disk partition.
- Volume Group
- is a collection of logical volumes and physical volumes.
Getting Started
Some fundamental steps to start using LVM. Read the man pages if your situation is interesting.
- Prepare disks for usage by LVM
- Create a Volume Group
- Create one or more logical volumes
- Create a filesystem in each logical volume
- Mount filesystems
You can refer to the block devices as /dev/sda or /dev/hdb or whatever, but nowadays I think it's a good idea to refer by id, because that way you don't fall afoul of the BIOS unexpectedly renaming your devices for you. blkid(8), dmesg(1) and parted(8) are useful tools to find out about your disks and to set/list names.
If you have 2 physical disks available:
pvcreate /dev/disk/by-id/<some long string> pvcreate /dev/disk/by-id/<another long string>
Create a volume group to use the newly setup physical volume:
vgcreate vg0 /dev/disk/by-id/<some long string> /dev/disk/by-id/<another long string>
Create one or more logical volumes:
lvcreate -L<size> -n lv0 vg0 lvcreate -L<size> -n lv1 vg0
The size parameter (-L) is required, the naming (-n) is optional, but probably a good idea.
Create filesystems:
mkfs.xfs /dev/vg0/lv0 mkfs.ext4 /dev/vg0/lv1
Mount filesystems wherever you need them:
mount /dev/vg0/lv0 /ux0 mount /dev/vg0/lv1 /ux1
Investigation Commands
- pvscan
- vgscan
- lvscan
- pvdisplay
- vgdisplay
- lvdisplay
The display commands show attributes of a single instance, the scan commands look for all instances on the system
Modification Commands
- {vg,lv}{change,extend,remove,reduce}
- pv{change,remove,resize}
How to add a physical disk and grow a filesystem
Initialise the disk to be used by LVM. The disk could be specified as /dev/disk/by-id/<something>. Prepare the disk:
pvcreate /dev/disk/by-id/<blah>
Add it to the volume group. You might need to vgdisplay to get the name:
vgextend vg0 /dev/disk/by-id/<blah>
Expand the logical volume:
lvextend lv0 /dev/disk/by-id/<blah>
Grow the filesystem:
fsadm resize /dev/disk/by-id/<blah>
For xfs, you need to specify the argument by mount point:
xfs_growfs /ux0
Here's the same example, but on a CentOS6 box that is being presented a (second) LUN by Xen:
pvcreate /dev/xvdb vgextend vg_root /dev/xvdb lvextend -L+100G /dev/mapper/vg_root-lv_var fsadm resize /dev/mapper/vg_root-lv_var
The +100G is the size of the new disk that we're adding. If you're not certain, you can get it from the Free PE/Size entry in the output of vgdisplay. I have verified that this is a viable approach to growing a live, mounted filesystem. Yes, on the fly, as in while it's being used.