How to create and manage Logical Volume Manager (LVM)
Different Steps to create and manage Logical Volume Manager (LVM)
This lesson explains how to create and maintain Logical Volume Manager (LVM) in step by step. The first step in creating Logical Volume Manager is to create partitions using fdisk command.
• Create Partitions using fdisk: Once the partitions are created, remember to change the partition type to “Linux LVM”, which is “8e”. To learn how to use fdisk command and perform this step, click the following link Linux File System Management Tools - fdisk command
• Create physical volumes using “pvcreate” command: If you have a freshly installed hard disk, you can set up a PV on the entire disk (Ex: /dev/sdd) or you can set up a new PV on a properly formatted partition (Ex: /dev/sdd1).
Example:
[root@RHEL06 ~]# pvcreate /dev/sde1 /dev/sde2 /dev/sdf1 /dev/sdf2 /dev/sdg
To display the physical volume details use “pvdisplay” command
[root@RHEL06 ~]# pvdisplay
/dev/hdc: open failed: No medium found
--- NEW Physical volume ---
PV Name /dev/sde1
VG Name
PV Size 4.67 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID trZx3T-gtPc-YqQ9-bMPp-WPVO-3289-moVAmY
--- NEW Physical volume ---
PV Name /dev/sde2
VG Name
PV Size 5.33 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID JXQ3Qs-pG1o-zs2F-HSWH-2r6W-msC0-BFc2u7
--- NEW Physical volume ---
PV Name /dev/sdf1
VG Name
PV Size 4.67 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 7lCT0L-RjE9-VH5Q-nkgP-cipc-dI7S-df7ipD
--- NEW Physical volume ---
PV Name /dev/sdf2
VG Name
PV Size 5.33 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID KLn2Pg-iQt3-38fQ-XB5N-cIG4-i9Ia-Duju1T
--- NEW Physical volume ---
PV Name /dev/sdg
VG Name
PV Size 10.00 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Ha6Bxz-nc5C-KH4C-QlFa-e9mb-VdEU-qyyHPX
Each physical volume has a UUID, which is also displayed when you use “pvdisplay” command.
• Create Volume groups using “vgcreate” command: Once you have two or more PVs, you can create a volume group (VG). You can define the name of the VG.
Example:
[root@RHEL06 ~]# vgcreate myvolgroup /dev/sde1 /dev/sde2 /dev/sdf1 /dev/sdf2 /dev/sdg
To display the volume group details, use command “vgdisplay”.
Example:
[root@RHEL06 ~]# vgdisplay myvolgroup
--- Volume group ---
VG Name myvolgroup
System ID
Format lvm2
Metadata Areas 5
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 5
Act PV 5
VG Size 29.98 GB
PE Size 4.00 MB
Total PE 7675
Alloc PE / Size 0 / 0
Free PE / Size 7675 / 29.98 GB
VG UUID 1Ymzbu-oAcR-UAew-jgSD-lCm2-cS2D-56KszD
• Create Logical Volume/s using “lvcreate” command: We can create multiple Logical Volumes in a single volume group.
Example:
[root@RHEL06 ~]# lvcreate -L 3GB -n lvhome1 myvolgroup
Note:
1) “-L” option specifies the size of the Logical Volume
2) “–n” option specifies the name of the Logical Volume.
To display the Logical Volume details, use command “lvdisplay”.
[root@RHEL06 ~]# lvdisplay
--- Logical volume ---
LV Name /dev/myvolgroup/lvhome1
VG Name myvolgroup
LV UUID PWjY1q-0L2U-Uril-Y8V7-dHxU-6xk2-sAZkj9
LV Write Access read/write
LV Status available
# open 0
LV Size 3.00 GB
Current LE 768
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 253:0
• Create Filesystem on Logical Volume/s using “mkfs” command:
Example:
[root@RHEL06 ~]#mkfs.ext3 /dev/myvolgroup/lvhome1
• Mount the Logical Volume created in File System Hierarchy (FSH).Create the mount point before mounting the Logical Volume, if it does not exist.
Example:
[root@RHEL06 ~]# mkdir /home1
Once you have created the mount point for the new Logical Volume (/home1), mount the Logical Volume there using mount command as shown below.
[root@RHEL06 ~]# mount /dev/myvolgroup/lvhome1 /home1
If you want the Logical Volume to be mounted automatically after the reboot, you should add it to /etc/fstab configuration file.
Commands for managing Logical Volume Manager (LVM)
• lvrename – lvrename command can be used for renaming Logical Volume (LVM)
Example:
[root@RHEL06 mapper]# lvrename myvolgroup lvhome1 lvvar1
Renamed "lvhome1" to "lvvar1" in volume group "myvolgroup"
[root@RHEL06 mapper]# umount /home1
[root@RHEL06 mapper]# mkdir /var1
[root@RHEL06 mapper]# mount /dev/mapper/myvolgroup-lvvar1 /var1
• lvremove – lvremove command can be used for removing a Logical Volume
Example:
[root@RHEL06 mapper]# lvremove /dev/myvolgroup/lvnewhome1
Do you really want to remove active logical volume "lvnewhome1"? [y/n]: y
Logical volume "lvnewhome1" successfully removed
• lvresize – lvresize command can be used to resize the Logical Volumes
Note: lvresize command can be used to increase (expand) or decrease (shrink) the size of the filesystem. Always it is better to unmount the file system before resizeing.
Decreasing (shrinking) the logical volume size without unmounting is not supported.
Example:
[root@RHEL06 mapper]# lvresize -L 7GB /dev/myvolgroup/lvvar1
Extending logical volume lvvar1 to 7.00 GB
Logical volume lvvar1 successfully resized
To resize the file system, with the new storage space added, use resize2fs command. The resizing is supported from kernel version 2.6 onwards. The file system must be ext3 to perform a resize.
Example:
[root@RHEL06 mapper]# resize2fs /dev/myvolgroup/lvvar1
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/myvolgroup/lvvar1 is mounted on /var1; on-line resizing required
Performing an on-line resize of /dev/myvolgroup/lvvar1 to 1835008 (4k) blocks.
The filesystem on /dev/myvolgroup/lvvar1 is now 1835008 blocks long.