{"id":2489,"date":"2016-04-19T09:01:24","date_gmt":"2016-04-19T09:01:24","guid":{"rendered":"http:\/\/blog.designed79.co.uk\/?p=2489"},"modified":"2016-04-19T09:27:47","modified_gmt":"2016-04-19T09:27:47","slug":"how-to-create-lvm-using-vgcreate-lvcreate-and-lvextend-lvm2-commands","status":"publish","type":"post","link":"https:\/\/blog.designed79.co.uk\/?p=2489","title":{"rendered":"How To Create LVM Using vgcreate, lvcreate, and lvextend lvm2 Commands"},"content":{"rendered":"<p>LVM stands for Logical Volume Manager.<\/p>\n<p>With LVM, we can create logical partitions that can span across one or more physical hard drives. First, the hard drives are divided into physical volumes, then those physical volumes are combined together to create the volume group and finally the logical volumes are created from volume group.<\/p>\n<p>The LVM commands listed in this article are used under Ubuntu Distribution. But, it is the same for other Linux distributions.<br \/>\n<span id=\"more-5125\"><\/span><br \/>\nBefore we start, install the lvm2 package as shown below.<\/p>\n<pre>$ sudo apt-get intall lvm2\r\n<\/pre>\n<p>To create a LVM, we need to run through the following steps.<\/p>\n<ul>\n<li>Select the physical storage devices for LVM<\/li>\n<li>Create the Volume Group from Physical Volumes<\/li>\n<li>Create Logical Volumes from Volume Group<\/li>\n<\/ul>\n<h3>Select the Physical Storage Devices for LVM \u2013 Use pvcreate, pvscan, pvdisplay Commands<\/h3>\n<p>In this step, we need to choose the physical volumes that will be used to create the LVM. We can create the physical volumes using pvcreate command as shown below.<\/p>\n<pre>$ sudo pvcreate \/dev\/sda6 \/dev\/sda7 \r\nPhysical volume \"\/dev\/sda6\" successfully created                                                 \r\nPhysical volume \"\/dev\/sda7\" successfully created \r\n<\/pre>\n<p>As shown above two physical volumes are created \u2013 \/dev\/sda6 and \/dev\/sda7.<\/p>\n<p>If the physical volumes are already created, you can view them using the pvscan command as shown below.<\/p>\n<p>&nbsp;<\/p>\n<div><\/div>\n<pre>$ sudo pvscan                                                                   \r\n  PV \/dev\/sda6                      lvm2 [1.86 GB]                                                 \r\n  PV \/dev\/sda7                      lvm2 [1.86 GB]                                                 \r\n  Total: 2 [3.72 GB] \/ in use: 0 [0   ] \/ in no VG: 2 [3.72 GB]      \r\n<\/pre>\n<p>You can view the list of physical volumes with attributes like size, physical extent size, total physical extent size, the free space, etc., using pvdisplay command as shown below.<\/p>\n<pre>$ sudo pvdisplay \r\n--- Physical volume --- \r\n  PV Name             \/dev\/sda6 \r\n  VG Name             \r\n  PV Size               1.86 GB \/ not usable 2.12 MB \r\n  Allocatable           yes \r\n  PE Size (KByte)    4096 \r\n  Total PE              476 \r\n  Free PE               456 \r\n  Allocated PE          20 \r\n  PV UUID               m67TXf-EY6w-6LuX-NNB6-kU4L-wnk8-NjjZfv \r\n \r\n  --- Physical volume --- \r\n  PV Name             \/dev\/sda7 \r\n  VG Name             \r\n  PV Size               1.86 GB \/ not usable 2.12 MB \r\n  Allocatable           yes \r\n  PE Size (KByte)    4096 \r\n  Total PE              476 \r\n  Free PE               476 \r\n  Allocated PE          0 \r\n  PV UUID               b031x0-6rej-BcBu-bE2C-eCXG-jObu-0Boo0x \r\n<\/pre>\n<p>Note : PE \u2013 Physical Extents are nothing but equal-sized chunks. The default size of extent is 4MB.<\/p>\n<h3>Create the Volume Group \u2013 Use vgcreate, vgdisplay Commands<\/h3>\n<p>Volume groups are nothing but a pool of storage that consists of one or more physical volumes. Once you create the physical volume, you can create the volume group (VG) from these physical volumes (PV).<\/p>\n<p>In this example, the volume group vol_grp1 is created from the two physical volumes as shown below.<\/p>\n<pre>$ sudo vgcreate vol_grp1 \/dev\/sda6 \/dev\/sda7                                  \r\n  Volume  group \"vol_grp1\" successfully created          \r\n<\/pre>\n<p>LVM processes the storage in terms of extents. We can also change the extent size (from the default size 4MB) using -s flag.<\/p>\n<p>&nbsp;<\/p>\n<p>vgdisplay command lists the created volume groups.<\/p>\n<pre>$ sudo vgdisplay \r\n  --- Volume group ---              \r\n  VG Name                     vol_grp1  \r\n  System ID                         \r\n  Format                        lvm2        \r\n  Metadata Areas            2           \r\n  Metadata Sequence No  1           \r\n  VG Access                   read\/write  \r\n  VG Status                    resizable   \r\n  MAX LV                       0           \r\n  Cur LV                        0           \r\n  Open LV                      0           \r\n  Max PV                       0           \r\n  Cur PV                        2           \r\n  Act PV                       2           \r\n  VG Size                      3.72 GB     \r\n  PE Size                      4.00 MB     \r\n  Total PE                     952         \r\n  Alloc PE \/ Size             0 \/ 0       \r\n  Free  PE \/ Size            952 \/ 3.72 GB \r\n  VG UUID                     Kk1ufB-rT15-bSWe-5270-KDfZ-shUX-FUYBvR \r\n<\/pre>\n<h3>LVM Create: Create Logical Volumes \u2013 Use lvcreate, lvdisplay command<\/h3>\n<p>Now, everything is ready to create the logical volumes from the volume groups. lvcreate command creates the logical volume with the size of 80MB.<\/p>\n<pre>$ sudo lvcreate -l 20 -n logical_vol1 vol_grp1 \r\n  Logical volume \"logical_vol1\" created      \r\n<\/pre>\n<p>To make a LV the MAX size use this<\/p>\n<p>lvcreate -n logical_vol1 -l 100%FREE vol_grp1<\/p>\n<p>Use lvdisplay command as shown below, to view the available logical volumes with its attributes.<\/p>\n<pre> \r\n$ sudo lvdisplay                                  \r\n  --- Logical volume ---                                             \r\n  LV Name                \/dev\/vol_grp1\/logical_vol1              \r\n  VG Name                vol_grp1                                  \r\n  LV UUID                 ap8sZ2-WqE1-6401-Kupm-DbnO-2P7g-x1HwtQ      \r\n  LV Write Access      read\/write                                  \r\n  LV Status              available                                   \r\n  # open                  0                                           \r\n  LV Size                  80.00 MB                                    \r\n  Current LE              20                                          \r\n  Segments               1                                           \r\n  Allocation               inherit                                     \r\n  Read ahead sectors  auto                                        \r\n  - currently set to     256                                         \r\n  Block device            252:0              \r\n<\/pre>\n<p>After creating the appropriate filesystem on the logical volumes, it becomes ready to use for the storage purpose.<\/p>\n<pre>$ sudo  mkfs.ext3 \/dev\/vol_grp1\/logical_vol1 \r\n<\/pre>\n<h3>LVM resize: Change the size of the logical volumes \u2013 Use lvextend Command<\/h3>\n<p>We can extend the size of the logical volumes after creating it by using lvextend utility as shown below. The changes the size of the logical volume from 80MB to 100MB.<\/p>\n<pre>$ sudo lvextend -L100 \/dev\/vol_grp1\/logical_vol1 \r\n  Extending logical volume logical_vol1 to 100.00 MB \r\n  Logical volume logical_vol1 successfully resized \r\n<\/pre>\n<p>We can also add additional size to a specific logical volume as shown below.<\/p>\n<pre>$ sudo lvextend -L+100 \/dev\/vol_grp1\/logical_vol1 \r\n  Extending logical volume logical_vol1 to 200.00 MB \r\n  Logical volume logical_vol1 successfully resized \r\n<\/pre>\n<div>Then add the new VG to your \/etc\/fstab and then mount using<\/div>\n<div><\/div>\n<div>mount -a<\/div>\n","protected":false},"excerpt":{"rendered":"<p>LVM stands for Logical Volume Manager. With LVM, we can create logical partitions that can span across one or more physical hard [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2489","post","type-post","status-publish","format-standard","hentry","category-info-on-tech"],"_links":{"self":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2489"}],"version-history":[{"count":0,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2489\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.designed79.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}