{"id":1327,"date":"2012-09-05T15:51:09","date_gmt":"2012-09-05T07:51:09","guid":{"rendered":"http:\/\/rmohan.com\/?p=1327"},"modified":"2012-09-05T15:51:09","modified_gmt":"2012-09-05T07:51:09","slug":"logical-volume-manager-lvm","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=1327","title":{"rendered":"Logical Volume Manager (LVM)"},"content":{"rendered":"<p><a href=\"http:\/\/rmohan.com\/wp-content\/uploads\/2012\/09\/lvm.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1328\" title=\"lvm\" src=\"http:\/\/rmohan.com\/wp-content\/uploads\/2012\/09\/lvm.jpg\" alt=\"\" width=\"240\" height=\"195\" srcset=\"https:\/\/mohan.sg\/wp-content\/uploads\/2012\/09\/lvm.jpg 240w, https:\/\/mohan.sg\/wp-content\/uploads\/2012\/09\/lvm-150x121.jpg 150w\" sizes=\"(max-width: 240px) 100vw, 240px\" \/><\/a><\/p>\n<p>In the traditional disk partitioning scheme, administrators plan ahead the use of each partition. For instance, a laptop with 120 GB hard drive, 1 GB\u00a0 RAM can have the following partition scheme:<\/p>\n<ul>\n<li>1 GB for swap (\/dev\/sda1),<\/li>\n<li>12 GB for root (\/dev\/sda2),<\/li>\n<li>107 GB for \/home (\/dev\/sda3), meaning, the remaining space.<\/li>\n<\/ul>\n<p>So far so go, but what if the RAM is increased to 2 GB? In order to hibernate the system you must increase the swap, and because the whole hard drive was partitioned leaving no space there isn\u2019t other way than repartitioning the hard drive using a utility, if the file system allows it\u00a0 (for instance,\u00a0 ext[2|3|4] file systems let you resize them).<\/p>\n<p>There isn\u2019t anything more flexible? Yes, it\u2019s called LVM (Logical Volume Manager), and basically it lets you hot resizing of partitions. For example, you can release 1GB from \/home (\/dev\/sda3) and give it to the swap partition (\/dev\/sda1).<\/p>\n<h2>LVM (Logical Volume Manager)<\/h2>\n<p>In order to use LVM, first you must assign physical volumes, then volume groups and finally logical groups. All this might be confusing at first, but let\u2019s try to explain each of these terms and how they are related:<\/p>\n<ul>\n<li><strong>Physical volumes:<\/strong> This is the support media where the data will be stored, and they can be made of several hard drives or partitions of a drive. They don\u2019t have a direct relation with the mount points. <strong>Physical Volume examples:<\/strong> \/dev\/sda , \/dev\/sdb1, etc.<\/li>\n<li><strong>Volume groups:<\/strong> It\u2019s the grouping of logical volumes in a volume to see them as one device. <strong>Physical volume example: <\/strong>\/dev\/vg00<\/li>\n<li><strong>Logical volumes:<\/strong> Here is were the file systems\u00a0(for example, ext2, ext3,xfs,vfat) will reside and so the mount points (\/, \/home, swap, \/usr, \/var, etc). At this level the files systems are formated, and it\u2019s where the data will be distributed logically, as you are costumed to with the traditional partitions. <strong>Logical volumes examples:<\/strong> \/dev\/vg00\/lv_rootfs, \/dev\/vg00\/lv_swap, \/dev\/vg00\/lv_home.<\/li>\n<\/ul>\n<h2>Installing\u00a0 LVM<\/h2>\n<p>On Debian you can install LVM with this package:<\/p>\n<blockquote>\n<pre>aptitude install lvm2<\/pre>\n<\/blockquote>\n<p>On other distributions like Fedora, Red Hat or Ubuntu you must search the name of the package for LVM.<\/p>\n<h2>LVM commands<\/h2>\n<p>In order to create a LVM using physical,\u00a0 groups and logical volumes let\u2019s see some commands available:<\/p>\n<h3>Physical volumes<\/h3>\n<table border=\"0\">\n<tbody>\n<tr>\n<td><strong>Command<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>pvcreate<\/td>\n<td>Initialize physical volume(s) for use by LVM<\/td>\n<\/tr>\n<tr>\n<td>pvs<\/td>\n<td>Display information about physical volumes<\/td>\n<\/tr>\n<tr>\n<td>pvdisplay<\/td>\n<td>Display various attributes of physical volume(s)<\/td>\n<\/tr>\n<tr>\n<td>pvresize<\/td>\n<td>Resize physical volume(s)<\/td>\n<\/tr>\n<tr>\n<td>pvmove<\/td>\n<td>\u00a0Move extents from one physical volume to another<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Volume groups<\/h3>\n<table border=\"0\">\n<tbody>\n<tr>\n<td><strong>Command<\/strong><\/td>\n<td><strong>Description<br \/> <\/strong><\/td>\n<\/tr>\n<tr>\n<td>vgcreate<\/td>\n<td>Create a volume group<\/td>\n<\/tr>\n<tr>\n<td>vgs<\/td>\n<td>Display information about volume groups<\/td>\n<\/tr>\n<tr>\n<td>vgdisplay<\/td>\n<td>Display volume group information such as attributes<\/td>\n<\/tr>\n<tr>\n<td>vgextend<\/td>\n<td>Add physical volumes to a volume group<\/td>\n<\/tr>\n<tr>\n<td>vgreduce<\/td>\n<td>Remove physical volume(s) from a volume group<\/td>\n<\/tr>\n<tr>\n<td>vgremove<\/td>\n<td>\u00a0Remove volume group(s)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Logical volumes<\/h3>\n<table border=\"0\">\n<tbody>\n<tr>\n<td><strong>Command<\/strong><\/td>\n<td><strong>Description<br \/> <\/strong><\/td>\n<\/tr>\n<tr>\n<td>lvcreate<\/td>\n<td>Create a logical volume<\/td>\n<\/tr>\n<tr>\n<td>lvs<\/td>\n<td>Display information about logical volumes<\/td>\n<\/tr>\n<tr>\n<td>lvdisplay<\/td>\n<td>Display information about a logical volume<\/td>\n<\/tr>\n<tr>\n<td>lvresize | lvreduce | lvextend<\/td>\n<td>Resize a logical volume<\/td>\n<\/tr>\n<tr>\n<td>lvmove<\/td>\n<td>Remove logical volume(s) from the system<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Example of how to create a LVM<\/h2>\n<p>Let\u2019 s suppose you have free \u00a08 GB hard drive in \/dev\/sdb , with two partitions of\u00a0 4 GB each (\/dev\/sdb1 and \/dev\/sdb2), and you want to create a LVM on those two partitions to use these mount points:\u00a0 1 GB for the system\u2019s root (\/), 1 GB for swap (swap) and the remaining 6 GB for users directory (\/home). To create the LVM you can type the following commands:<\/p>\n<blockquote>\n<pre><strong>root@buckbeak:~# pvcreate \/dev\/sdb1<\/strong>\r\nPhysical volume \"\/dev\/sdb1\" successfully created\r\n\r\n<strong>root@buckbeak:~#pvcreate \/dev\/sdb2<\/strong>\r\nPhysical volume \"\/dev\/sdb2\" successfully created\r\n\r\n<strong>root@buckbeak:~# pvs<\/strong>\r\n\r\nPV\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 VG\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Fmt\u00a0 Attr PSize\u00a0 PFree\r\n\/dev\/sdb1\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 lvm2 a-\u00a0\u00a0\u00a0 4.00g 4.00g\r\n\/dev\/sdb2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lvm2 a-\u00a0\u00a0\u00a0 3.99g 3.99g\r\n\r\n<strong>root@buckbeak:~# vgcreate vg_group01 \/dev\/sdb1 \/dev\/sdb2<\/strong>\r\n\r\nVolume group \"vg_group01\" successfully created\r\n\r\n<strong>root@buckbeak:~#\u00a0vgs<\/strong>\r\n\r\nVG\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #PV #LV #SN Attr\u00a0\u00a0 VSize\u00a0 VFree\r\nvg_group01 \u00a0 2\u00a0\u00a0 0\u00a0\u00a0 0 wz--n-\u00a0 7.99g 7.99g\r\n\r\n<strong>root@buckbeak:~# lvcreate -n lv_rootfs vg_group01 -L 1G<\/strong>\r\nLogical volume \"lv_rootfs\" created\r\n\r\n<strong>root@buckbeak:~#\u00a0lvcreate -n lv_swap vg_group01 -L 1G<\/strong>\r\nLogical volume \"lv_swap\" created\r\n\r\n<strong>root@buckbeak:~# lvcreate -n lv_home vg_group01 -L 6G<\/strong>\r\nVolume group \"vg_group01\" has insufficient free space (1534 extents): 1536 required\r\n\r\n<strong>root@buckbeak:~# lvcreate -n lv_home vg_group01 -l 1534<\/strong>\r\nLogical volume \"lv_home\" created\r\n\r\n<strong>root@buckbeak:~# lvs<\/strong>\r\nLV\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 VG\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Attr\u00a0\u00a0 LSize\u00a0 Origin Snap%\u00a0 Move Log Copy%\u00a0 Convert\r\nlv_home\u00a0\u00a0 vg_group01 -wi-a-\u00a0 5.99g\r\nlv_rootfs vg_group01 -wi-a-\u00a0 1.00g\r\nlv_swap\u00a0\u00a0 vg_group01 -wi-a-\u00a0 1.00g<\/pre>\n<\/blockquote>\n<p>Now you can make the file system for each logical volume:<\/p>\n<blockquote>\n<pre><strong>root@buckbeak:~# mkfs.ext4 \/dev\/vg_group01\/lv_rootfs<\/strong>\r\nmke2fs 1.41.12 (17-May-2010)\r\nFilesystem label=\r\nOS type: Linux\r\nBlock size=4096 (log=2)\r\nFragment size=4096 (log=2)\r\nStride=0 blocks, Stripe width=0 blocks\r\n65536 inodes, 262144 blocks\r\n13107 blocks (5.00%) reserved for the super user\r\nFirst data block=0\r\nMaximum filesystem blocks=268435456\r\n8 block groups\r\n32768 blocks per group, 32768 fragments per group\r\n8192 inodes per group\r\nSuperblock backups stored on blocks:\r\n32768, 98304, 163840, 229376\r\n\r\nWriting inode tables: done\r\nCreating journal (8192 blocks): done\r\nWriting superblocks and filesystem accounting information: done\r\n\r\nThis filesystem will be automatically checked every 34 mounts or\r\n180 days, whichever comes first.\u00a0 Use tune2fs -c or -i to override.\r\n\r\n<strong>root@buckbeak:~# mkfs.ext4 \/dev\/vg_group01\/lv_home<\/strong>\r\nmke2fs 1.41.12 (17-May-2010)\r\nFilesystem label=\r\nOS type: Linux\r\nBlock size=4096 (log=2)\r\nFragment size=4096 (log=2)\r\nStride=0 blocks, Stripe width=0 blocks\r\n393216 inodes, 1570816 blocks\r\n78540 blocks (5.00%) reserved for the super user\r\nFirst data block=0\r\nMaximum filesystem blocks=1610612736\r\n48 block groups\r\n32768 blocks per group, 32768 fragments per group\r\n8192 inodes per group\r\nSuperblock backups stored on blocks:\r\n32768, 98304, 163840, 229376, 294912, 819200, 884736\r\n\r\nWriting inode tables: done\r\nCreating journal (32768 blocks): done\r\nWriting superblocks and filesystem accounting information: done\r\n\r\nThis filesystem will be automatically checked every 20 mounts or\r\n180 days, whichever comes first.\u00a0 Use tune2fs -c or -i to override.\r\n\r\n<strong>root@buckbeak:~# mkswap \/dev\/vg_group01\/lv_swap<\/strong>\r\nmkswap: \/dev\/vg_group01\/lv_swap: warning: don't erase bootbits sectors\r\non whole disk. Use -f to force.\r\nSetting up swapspace version 1, size = 1048572 KiB\r\nno label, UUID=e743b4f3-1c80-4503-be88-6934d575cd55\r\n\r\n<strong>root@buckbeak:~# swapon \/dev\/vg_group01\/lv_swap<\/strong>\r\n\r\n<strong>root@buckbeak:~# swapon -s<\/strong>\r\nFilename\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Type\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Size\u00a0\u00a0 \u00a0Used\u00a0\u00a0 \u00a0Priority\r\n\/dev\/dm-1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 partition\u00a0\u00a0 \u00a02064376\u00a0\u00a0 \u00a08\u00a0\u00a0 \u00a0-1\r\n\/dev\/dm-5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 partition\u00a0\u00a0 \u00a01048568\u00a0\u00a0 \u00a00\u00a0\u00a0 \u00a0-2<\/pre>\n<\/blockquote>\n<p>If you want to use these logical volumes, you can mount them using the mount command or add them to the \/etc\/fstab file to be mount when the system boots.<\/p>\n","protected":false},"excerpt":{"rendered":"<\/p>\n<p>In the traditional disk partitioning scheme, administrators plan ahead the use of each partition. For instance, a laptop with 120 GB hard drive, 1 GB RAM can have the following partition scheme:<\/p>\n<p> 1 GB for swap (\/dev\/sda1), 12 GB for root (\/dev\/sda2), 107 GB for \/home (\/dev\/sda3), meaning, the remaining space. <\/p>\n<p>So far so [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1327"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1327"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1327\/revisions"}],"predecessor-version":[{"id":1329,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/1327\/revisions\/1329"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}