{"id":3195,"date":"2014-06-18T22:49:49","date_gmt":"2014-06-18T14:49:49","guid":{"rendered":"http:\/\/rmohan.com\/?p=3195"},"modified":"2014-06-18T22:50:25","modified_gmt":"2014-06-18T14:50:25","slug":"lvm-linux","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3195","title":{"rendered":"LVM Linux"},"content":{"rendered":"<p>Logical Volume Manager<\/p>\n<p>Page Contents<\/p>\n<p>\u2022Gathering Information<br \/>\n\u2022Creating and Moving Volume Groups<br \/>\n\u2022?Partition The Disk<br \/>\n?Initialise The Physical Volumes<br \/>\n?Add the PVs to a Volume Group<br \/>\n?Add Logical Volumes to the Volume Group<br \/>\n?Add Filesystems to the Logical Volumes<\/p>\n<p>\u2022<br \/>\n\u2022Migrating a Volume Group to Another System<br \/>\n\u2022Reducing the Size of a Logical Volume<br \/>\n\u2022Extending the Size of a Logical Volume<br \/>\n\u2022Creating a Snapshot Logical Volume<\/p>\n<p>LVM2 allows you to create a Volume Group from one or more physical volumes or disks. The volume group can then be divided into logical volumes by assigning space from the physical volumes. Logical volumes equate to partitions and can be used to contain filesystems<\/p>\n<p>The &#8216;lvm&#8217; command can be run to begin an interactive lvm session where commands can be issued to manage a logical volume<\/p>\n<p>Gathering Information<\/p>\n<p>Top Bottom<\/p>\n<p>Three commands can be used to display detailed information about Volume Groups, Physical Volumes and Logical Volumes:<br \/>\n\u2022vgdisplay<br \/>\n\u2022pvdisplay<br \/>\n\u2022lvdisplay<\/p>\n<p>Summary information can be displayed via:<br \/>\n\u2022vgs<br \/>\n\u2022pvs<br \/>\n\u2022lvs<\/p>\n<p>The vgscan, pvscan and lvscan display the same information in a different format.<\/p>\n<p>Creating and Moving Volume Groups<\/p>\n<p>Top Bottom<\/p>\n<p>This section deals with adding a brand new harddisk to your system, managed by LVM. We are going to create a new volume group to hold media files on disk \/dev\/sdb.<\/p>\n<p>Partition The Disk<\/p>\n<p>Top Bottom<\/p>\n<p>use cfdisk to create three partitions of type 8E &#8211; Linux LVM<br \/>\ncfdisk (util-linux-ng 2.16)<\/p>\n<p>Disk Drive: \/dev\/sdb<br \/>\nSize: 500107862016 bytes, 500.1 GB<br \/>\nHeads: 255\u00a0\u00a0 Sectors per Track: 63\u00a0\u00a0 Cylinders: 60801<\/p>\n<p>Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Flags\u00a0\u00a0\u00a0\u00a0\u00a0 Part Type\u00a0 FS Type\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [Label]\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Size (MB)<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nsdb1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Primary\u00a0\u00a0 Linux LVM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 149996.21<br \/>\nsdb2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Primary\u00a0\u00a0 Linux LVM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 149996.21<br \/>\nsdb3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Primary\u00a0\u00a0 Linux LVM\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 200112.84<br \/>\nInitialise The Physical Volumes<\/p>\n<p>Top Bottom<\/p>\n<p>Use pvcreate to initialise the physical volumes for use with LVM:<br \/>\nlvm&gt; pvcreate \/dev\/sdb1 \/dev\/sdb2 \/dev\/sdb3<br \/>\nPhysical volume &#8220;\/dev\/sdb1&#8221; successfully created<br \/>\nPhysical volume &#8220;\/dev\/sdb2&#8221; successfully created<br \/>\nPhysical volume &#8220;\/dev\/sdb3&#8221; successfully created<br \/>\nAdd the PVs to a Volume Group<\/p>\n<p>Top Bottom<\/p>\n<p>Use vgcreate to add the physical volumes to a new volume group called &#8216;media&#8217;:<br \/>\nlvm&gt; vgcreate media \/dev\/sdb1 \/dev\/sdb2 \/dev\/sdb3<br \/>\nVolume group &#8220;media&#8221; successfully created<br \/>\nAdd Logical Volumes to the Volume Group<\/p>\n<p>Top Bottom<\/p>\n<p>Use lvcreate to create the Logical Volumes that will contain the filesystems of the volume group:<br \/>\nlvm&gt; lvcreate -L10G -ndocuments media<br \/>\nLogical volume &#8220;documents&#8221; created<br \/>\nlvm&gt; lvcreate -L100G -nmusic media<br \/>\nLogical volume &#8220;music&#8221; created<br \/>\nlvm&gt; lvcreate -L100G -nvideo media<br \/>\nLogical volume &#8220;video&#8221; created<br \/>\nlvm&gt; lvcreate -L15G -npictures media<br \/>\nLogical volume &#8220;pictures&#8221; created<br \/>\nAdd Filesystems to the Logical Volumes<\/p>\n<p>Top Bottom<\/p>\n<p>Use mkfs to create the filesystems:<br \/>\nmkfs -t ext4 \/dev\/media\/documents<br \/>\nmkfs -t ext4 \/dev\/media\/music<br \/>\nmkfs -t ext4 \/dev\/media\/video<br \/>\nmkfs -t ext4 \/dev\/media\/pictures<br \/>\nWith the filesystems prepared, they can now be mounted as \/dev\/media\/documents, \/dev\/media\/music, \/dev\/media\/video and \/dev\/media\/pictures for the device name.<\/p>\n<p>Migrating a Volume Group to Another System<\/p>\n<p>Top Bottom<\/p>\n<p>To migrate a Volume Group to another system the Volume Group must be exported from the old system and then imported to the new system. Prior to exporting the VG, umount the filesystems, then de-activate the Volume Group:<br \/>\nlvm&gt; vgchange -an media<br \/>\n4 logical volume(s) in volume group &#8220;media&#8221; now inactive<br \/>\nlvm&gt; vgexport media<br \/>\nVolume group &#8220;media&#8221; successfully exported<br \/>\nWith the disks attached to the new system, use vgscan to see if the Volume group is visible and lvscan to see which LVs are active:<br \/>\nlvm&gt; vgscan<br \/>\nReading all physical volumes.\u00a0 This may take a while&#8230;<br \/>\nFound volume group &#8220;media&#8221; using metadata type lvm2<br \/>\nFound volume group &#8220;vg_zadoc&#8221; using metadata type lvm2<br \/>\nlvm&gt; pvscan<br \/>\nPV \/dev\/sdb1\u00a0\u00a0 VG media\u00a0\u00a0\u00a0\u00a0\u00a0 lvm2 [139.69 GiB \/ 39.69 GiB free]<br \/>\nPV \/dev\/sdb2\u00a0\u00a0 VG media\u00a0\u00a0\u00a0\u00a0\u00a0 lvm2 [139.69 GiB \/ 114.69 GiB free]<br \/>\nPV \/dev\/sdb3\u00a0\u00a0 VG media\u00a0\u00a0\u00a0\u00a0\u00a0 lvm2 [186.37 GiB \/ 86.37 GiB free]<br \/>\nPV \/dev\/sda2\u00a0\u00a0 VG vg_zadoc\u00a0\u00a0 lvm2 [465.25 GiB \/ 0\u00a0\u00a0\u00a0 free]<br \/>\nTotal: 4 [931.00 GiB] \/ in use: 4 [931.00 GiB] \/ in no VG: 0 [0\u00a0\u00a0 ]<br \/>\nlvm&gt; lvscan<br \/>\ninactive\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;\/dev\/media\/music&#8217; [100.00 GiB] inherit<br \/>\ninactive\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;\/dev\/media\/video&#8217; [100.00 GiB] inherit<br \/>\ninactive\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;\/dev\/media\/documents&#8217; [10.00 GiB] inherit<br \/>\ninactive\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;\/dev\/media\/pictures&#8217; [15.00 GiB] inherit<br \/>\nACTIVE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;\/dev\/vg_zadoc\/lv_root&#8217; [50.00 GiB] inherit<br \/>\nACTIVE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;\/dev\/vg_zadoc\/lv_home&#8217; [409.41 GiB] inherit<br \/>\nACTIVE\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;\/dev\/vg_zadoc\/lv_swap&#8217; [5.84 GiB] inherit<br \/>\nTo import and activate the Volume group use vgimport and vgchange:<br \/>\nlvm&gt; vgimport media<br \/>\nVolume group &#8220;media&#8221; successfully imported<br \/>\nlvm&gt; vgchange -ay media<br \/>\n4 logical volume(s) in volume group &#8220;media&#8221; now active<br \/>\nReducing the Size of a Logical Volume<\/p>\n<p>Top Bottom<\/p>\n<p>To reduce the size of a Logical Volume, the filesystem must first be reduced using resize2fs, before attempting to issue the lvreduce command. A disk check is also required beforehand:<br \/>\nunmount \/dev\/system\/test<br \/>\ne2fsck -f \/dev\/system\/test<br \/>\nresize2fs \/dev\/system\/test 8G<br \/>\nlvreduce -L 8G \/dev\/system\/test<br \/>\nmount \/dev\/system\/test \/media\/test<\/p>\n<p>Extending the Size of a Logical Volume<\/p>\n<p>Top Bottom<\/p>\n<p>When increasing the size of a Logical Volume, the filesystem is resized after the LV is extended<\/p>\n<p>Increase LV to 12G:<br \/>\nlvm lvextend -L 12G \/dev\/system\/test<br \/>\ne2fsck -f \/dev\/system\/test<br \/>\nresize2fs \/dev\/system\/test<\/p>\n<p>Increase LV by 2G:<br \/>\nlvm lvextend -L +2G \/dev\/system\/test<br \/>\ne2fsck -f \/dev\/system\/test<br \/>\nresize2fs \/dev\/system\/test<\/p>\n<p>Increase a volume by 20 physical extents:<br \/>\nlvm lvextend -l +20 \/dev\/system\/test<br \/>\ne2fsck -f \/dev\/system\/test<br \/>\nresize2fs \/dev\/system\/test<\/p>\n<p>Creating a Snapshot Logical Volume<\/p>\n<p>Top Bottom<\/p>\n<p>A snapshot logical volume allows you to create an exact copy of a volumes data at the time it was created:<br \/>\nlvcreate -L 5G -s -n homebackup \/dev\/system\/home<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logical Volume Manager<\/p>\n<p>Page Contents<\/p>\n<p>\u2022Gathering Information \u2022Creating and Moving Volume Groups \u2022?Partition The Disk ?Initialise The Physical Volumes ?Add the PVs to a Volume Group ?Add Logical Volumes to the Volume Group ?Add Filesystems to the Logical Volumes<\/p>\n<p>\u2022 \u2022Migrating a Volume Group to Another System \u2022Reducing the Size of a Logical Volume \u2022Extending [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,17],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3195"}],"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=3195"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3195\/revisions"}],"predecessor-version":[{"id":3197,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3195\/revisions\/3197"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}