RHEL – Working with LVM
by Rajesh Sivaraman • February 17, 2012 • Linux • 0 Comments
LVM is a logical volume manager for the Linux kernel; it manages disk drives and similar mass-storage devices, in particular large ones. The term “volume” refers to a disk drive or partition thereof. It was originally written in 1998 by Heinz Mauelshagen, who based its design on that of the LVM in HP-UX.
The LVM can:
- Resize Volumes online
- Create Snapshots
- Mirror Volumes
…and more.
First , we need some partitions. We make them using fdisk tool or any other tool of your choice. The only thing you need to remember is to set the partition type to 8e. That will make it LVM Ready.
Once the partitions are ready, we can make them Physical Volumes (PV) using the pvcreate command. Remember , data is stored by LVM in blocks of space called Physical Extents (PE). This can be set by pvcreate command and vary. Default is 4MB. Number of PEs and their size plays an important role according to the type of data you are going to store in there.
When we have our Physical Volumes ready, we will then proceed to create our Volume Group (VG). Think about volume groups as a collection of PVs. You can add PVs to a VG to add more space whenever you need.
Once a Volume Group is available, we can then define Logical Volumes (LV) using the space from VG. Think about LVs as partitions in VG. LVs are available to us for formatting with any file system and mount. LVs can be resized easily according to data requirement and space availability in the containing VG.
How to create a LV in LVM
Here, I am having 2 disks /dev/sdb and /dev/sdc each 1 GB in size. I am going to create one partition in each and mark them as type 8e (LVM). Then I would create a VG (newvg) with both the partitions (/dev/sdb1 & /dev/sdc1 ) as members. So the VG will be 2GB and I will create a LV (logvol01) of 500 MB from the VG. We will format the LV with ext3 filesystem and mount it under /mnt/logvol01 . Terminal output is given below.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
[root@server ~]# fdisk -cu /dev/sdb
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151):
Using default value 2097151
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# fdisk -cu /dev/sdc
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151):
Using default value 2097151
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]#
[root@server ~]# pvcreate /dev/sdb1 /dev/sdc1
Writing physical volume data to disk "/dev/sdb1"
Physical volume "/dev/sdb1" successfully created
Writing physical volume data to disk "/dev/sdc1"
Physical volume "/dev/sdc1" successfully created
[root@server ~]#
[root@server ~]# vgcreate newvg /dev/sdb1 /dev/sdc1
Volume group "newvg" successfully created
[root@server ~]#
[root@server ~]# vgs newvg
VG #PV #LV #SN Attr VSize VFree
newvg 2 0 0 wz--n- 1.99g 1.99g
[root@server ~]#
[root@server ~]# lvcreate -n logvol01 -L 500M newvg
Logical volume "logvol01" created
[root@server ~]#
[root@server ~]# lvdisplay newvg/logvol01
--- Logical volume ---
LV Name /dev/newvg/logvol01
VG Name newvg
LV UUID XN2uEM-Nkbc-JDiT-43as-C5PG-KF2y-1Hg0hn
LV Write Access read/write
LV Status available
# open 0
LV Size 500.00 MiB
Current LE 125
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
[root@server ~]# mkfs.ext3 /dev/newvg/logvol01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@server ~]# mkdir /mnt/logvol01
[root@server ~]# mount /dev/newvg/logvol01 /mnt/logvol01
[root@server ~]# mount | grep logvol01
/dev/mapper/newvg-logvol01 on /mnt/logvol01 type ext3 (rw)
[root@server ~]# df -h /mnt/logvol01
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/newvg-logvol01
485M 11M 449M 3% /mnt/logvol01
[root@server ~]# |
If you are a GUI lover, you could do this using System->Administration->Logical Volume Management. We will look at resizing Logical Volumes, Snapshots and more in future.
Hope you have enjoyed this article. As usual, feel free to try, share and discuss.
Related posts:

