Table of Contents
LVM
Volumes
For this article, we assume that the volume group we are working it called datavg and we are talking about a logical volume called database.
Create
- Create a 4GB logical volume called
database. and format it with ext4.lvcreate -L 4G datavg -n database
- We then format it with the ext4 filesystem.
mkfs -t ext4 /dev/mapper/datavg_database
Increase
Extend the volume database by an extra 1GB
lvextend -L +1G /dev/mapper/datavg_database -r
If you skip the -r you could use resize2fs directly.
lvextend -L +1G /dev/mapper/datavg_database resize2fs /dev/mapper/datavg_database
Extend database to 10GB (assuming it is smaller than 10GB).
lvextend -L 10G /dev/mapper/datavg_database resize2fs /dev/mapper/datavg_database
Decrease
A more thorough explanation can be found [http://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/ here]. It is important to understand the difference between using the standard computer gigabytes (1024^3 bytes) or drive manufacturer gigabytes (1000^3 bytes). To be safe, when reducing the size of a partition, we shrink the file system to about 10% smaller than the size we actually want it to be, shrink the logical volume and then increase the file system size to match the logical volume.
In this example, we shrink
database
from 10G to 5GB. We assume that the volume does not have more than 5GB of data.
- Firstly, unmount the partition.
umount /dev/mapper/data_database
- Check the file system.
e2fsck -f /dev/mapper/data_database
- Shrink the file system. Notice that we are deliberately shrinking it below our target size of 5GB.
resize2fs /dev/mapper/data_database4G
- Shrink the volume
database.lvreduce -L 5G /dev/mapper/data_database
- Expand the file system from 4G to 5G.
resize2fs /dev/mapper/data_database
Delete
- Ensure that the volume is not mounted.
umount /dev/mapper/data_database
- Then delete to volume.
lvremove /dev/mapper/data_database-f
Display Information
Summary of free space on each logical volume.
lvs
Scan available logical volumes and report.
lvscan
Detailed information on each logical volume.
lvdisplay
Volume Groups
Create
Create a volume group called datavg on the physical volume on /dev/sdb.
vgcreate datavg /dev/sdb
Display Information
Summary of free space on each volume group.
vgs
Scan available volume groups and report.
vgscan
Detailed information on each volume group.
vgdisplay
Physical Volumes
Create
To dedicate an entire partition to a physical volume, use this the following command (in which we allocate the entire disk space of device /dev/sdb).
pvcreate /dev/sdb
To allocate just a partition, use the following command (in which we allocate the entire disk space of the second partition /dev/sdb2).
pvcreate /dev/sdb2
Display Information
Summary of free space on each physical volume.
pvs
Scan available physical volumes and report.
pvscan
Detailed information on each physical volume.
pvdisplay
Extend
If a virtualised environment, it is easy to increase the size of a virtual hard drive. How to actually do it is covered elsewhere but, once done, you may want to alter your physical volume to make use of the new space. This is easy if the disk is used for nothing but the physical volume. However, if the physical volume resides on a partition, the process is more complicated and risky.
On Dedicated Disk
After the machine has been powered on (after the virtual disk has been expanded), run the following command as the root user. In this example, we assume that the disk being expanded is /dev/sdb and that the entire disk is dedicated to the physical volume.
pvresize /dev/sdb
On Dedicated Partition
This would be used when you have (for example) extended a virtual disk to be larger and need the LVM to realise that it now has more room to play with.
In this example, we will be expanding /dev/sda2. Use 'pvs' before and afterwards to list the size of the partition.
pvs /dev/sda2
The following procedure only works if you are partition that stores the physical volume exists at the end of the disk next to the new blank space.
In case you hadn't noticed, this is dangerous - For pity's sake, practice this on a test system before doing it in production
After the machine has been powered on (after the virtual disk has been expanded), run the following command as the root user. In this example, we assume that the disk being expanded is /dev/sda and that the partition /dev/sda2 is the one being expanded.
fdisk /dev/sda
The following is a screen dump of how this went for me when I tried it.
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): c
DOS Compatibility flag is not set
Command (m for help): u
Changing display/entry units to sectors
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders, total 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00067256
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 423935 210944 83 Linux
/dev/sda2 423936 41943039 20759552 8e Linux LVM
Command (m for help): d
Partition number (1-4): 2
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
Partition 1 is already defined. Delete it before re-adding it.
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First sector (423936-125829119, default 423936): 423936
Last sector, +sectors or +size{K,M,G} (423936-125829119, default 125829119):
Using default value 125829119
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
After a reboot, you will have to run
pvresize /dev/sdb2
