What is LVM?
LVM is a tool for logical volume management which includes allocating disks, striping, mirroring and resizing logical volumes. With LVM, a hard drive or set of hard drives is allocated to one or more physical Volume Groupical volumes.LVM physical volumes can be placed on other block devices which might span two or more disks.
The physical volumes are combined into logical volumes, with the exception of the /boot/ partition. The /boot/ partition cannot be on a logical volume group because the boot loader cannot read it. If the root (/) partition is on a logical volume, create a separate /boot/ partition which is not a part of a volume group.
LVM Terms
Step 1 : Create Physical Volumes
pvcreate /dev/hda# /dev/hda#
Step 2 : Create Volume Group
vgcreate VG1 /dev/hda# /dev/hda#
Step 3 : Change Volume Group to ACTIVE(optional)
vgchange -a y VG1
Step 4 : Create Logical Volume
lvcreate -L +600M -n LV1 VG1
Step 5 : Format the Logical Volume
mkfs -t ext3 /dev/VG1/LV1
Step 6: Create a directory
mkdir -p /mnt/data
Step 7: Mount in /etc/fstab
/dev/VG1/LV1 /mnt/data ext3 defaults 0 0
Step 8: Activate the new volume
mount –a
Check the newly mounted Logical Volume
For Short details
#pvscan
#lvscan
#vgscan
For Long Full Details
#pvdisplay
#lvdisplay
#vgdisplay
RESIZING THE LVM : Note Only on RHEL 5.3 and above you can directly increase filesystem without unmount
Step 1: Resize the LVM
lvextend -L +200M /dev/VG1/LV1
Step 2: Configuring the HDD for new extended space
resize2fs /dev/VG1/LV1
You have one or more physical volumes (/dev/sdb1 – /dev/sde1 in our example), and on these physical volumes you create one or more volume groups (e.g. fileserver), and in each volume group you can create one or more logical volumes. If you use multiple physical volumes, each logical volume can be bigger than one of the underlying physical volumes.
It is a good practice to not allocate the full space to logical volumes, but leave some space unused. That way you can enlarge one or more logical volumes later on if you feel the need for it.
Check the avaible disk on the server with following two commands
fdisk -l
Let’s find out about our hard disks:
fdisk -lThe output looks like this:webmanual01:~# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes Device Boot Start End Blocks Id System Disk /dev/sdb: 85.8 GB, 85899345920 bytes Disk /dev/sdb doesn’t contain a valid partition table Disk /dev/sdc: 85.8 GB, 85899345920 bytes Disk /dev/sdc doesn’t contain a valid partition table Disk /dev/sdd: 85.8 GB, 85899345920 bytes Disk /dev/sdd doesn’t contain a valid partition table Disk /dev/sde: 85.8 GB, 85899345920 bytes Disk /dev/sde doesn’t contain a valid partition table Disk /dev/sdf: 85.8 GB, 85899345920 bytes Disk /dev/sdf doesn’t contain a valid partition table There are no partitions yet on /dev/sdb – /dev/sdf |
Create Partion
We will create the partitions /dev/sdb1, /dev/sdc1, /dev/sdd1, and /dev/sde1 with 25 GB and leave /dev/sdf untouched for now. It will act our hard disks had only 25GB of space instead of 80GB for now, therefore we assign 25GB to /dev/sdb1, /dev/sdc1, /dev/sdd1, and /dev/sde1:
fdisk /dev/sdb
webmanual01:~# fdisk /dev/sdbThe number of cylinders for this disk is set to 10443. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)Command (m for help): <– m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition’s system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)Command (m for help): <– n Command action e extended p primary partition (1-4) <– p Partition number (1-4): <– 1 First cylinder (1-10443, default 1): <– < Press ENTER> Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-10443, default 10443): <– +25000M Command (m for help): <– t 0 Empty 1e Hidden W95 FAT1 80 Old Minix be Solaris boot Command (m for help): <– w |
Now we do the same for the hard disks /dev/sdc – /dev/sde:
fdisk /dev/sdc fdisk /dev/sdd fdisk /dev/sde |
Verify
Run fdisk -l again. The output should look like this:
webmanual01:~# fdisk -lDisk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System /dev/sda1 * 1 18 144553+ 83 Linux /dev/sda2 19 2450 19535040 83 Linux /dev/sda4 2451 2610 1285200 82 Linux swap / SolarisDisk /dev/sdb: 85.8 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System Disk /dev/sdc: 85.8 GB, 85899345920 bytes Device Boot Start End Blocks Id System Disk /dev/sdd: 85.8 GB, 85899345920 bytes Device Boot Start End Blocks Id System Disk /dev/sde: 85.8 GB, 85899345920 bytes Device Boot Start End Blocks Id System Disk /dev/sdf: 85.8 GB, 85899345920 bytes Disk /dev/sdf doesn’t contain a valid partition table |
Create PV
Create PV on available disks on the servers
webmanual01:~# pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 Physical volume ”/dev/sdb1″ successfully created Physical volume ”/dev/sdc1″ successfully created Physical volume ”/dev/sdd1″ successfully created Physical volume ”/dev/sde1″ successfully |
Create VG
vgcreate vgtest /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 |
Creating LV
Create a lvtesta of space 1 G on VG vgtesta
root@webmanual:/root> lvcreate -L 1024M /dev/vgtesta -n lvtesta Incorrect metadata area header checksum Logical volume “lvtesta” created |
Checking LV properties
root@webmanual:/root> lvdisplay /dev/vgtesta/lvtestaIncorrect metadata area header checksum — Logical volume — LV Name /dev/vgtesta/lvtesta VG Name vgtesta LV UUID sXOjN8-cWnW-7DKf-Pm32-kuKU-z7ww-hkGGgw LV Write Access read/write LV Status available # open 0 LV Size 1.00 GB Current LE 1 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:58 |
Creating a directory to mount the LV created on previous step
root@webmanual:/root> mkdir /tmp/testinga root@webmanual:/root> ls -l /tmp/testinga total 0 |
Formationg in ext3 the LV lvtesta
root@webmanual:/root> mkfs -t ext3 /dev/vgtesta/lvtesta mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 131072 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 16384 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 36 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. |
Mount the LV and Make a entry in /etc/fstab
root@webmanual:/root> mount /dev/vgtesta/lvtesta /tmp/testingaroot@webmanual:/root> vi /etc/fstabPress ESC+G then ESC + O/dev/vgtesta/lvtesta /tmp/testinga ext3 defaults 0 0 —> Make enrty like this |
Verify the mounting
root@webmanual:/root> df -h /tmp/testinga /dev/mapper/vgtesta-lvtesta 1008M 34M 924M 4% /tmp/testinga |
Extending LV
root@webmanual:/root> lvextend -L+1G /dev/vgtesta/lvtesta Incorrect metadata area header checksum Extending logical volume lvtesta to 2.00 GB Logical volume lvtesta successfully resized root@webmanual:/root> |
Resize the LV
root@webmanual:/root> resize2fs -p /dev/vgtesta/lvtesta 1G resize2fs 1.39 (29-May-2006) Please run ‘e2fsck -f /dev/vgtesta/lvtesta’ first.Resize failed and asked to run e2fsck . Run e2fsck as below root@webmanual:/root> e2fsck -f /dev/vgtesta/lvtesta e2fsck 1.39 (29-May-2006)
Resize will work nowroot@webmanual:/root> resize2fs -p /dev/vgtesta/lvtesta 1G |
Reducing LV
root@webmanual:/root> lvreduce -L -1G /dev/vgtesta/lvtesta Incorrect metadata area header checksum WARNING: Reducing active logical volume to 1.00 GB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lvtesta? [y/n]: y Reducing logical volume lvtesta to 1.00 GB Logical volume lvtesta successfullyroot@webmanual:/root> df -h /tmp/testinga Filesystem Size Used Avail Use% Mounted on /dev/mapper/vgtesta-lvtesta 1008M 34M 934M 4% /tmp/testinga root@webmanual:/root> |