{"id":99,"date":"2022-03-28T21:27:01","date_gmt":"2022-03-29T01:27:01","guid":{"rendered":"http:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=99"},"modified":"2024-10-11T09:36:00","modified_gmt":"2024-10-11T13:36:00","slug":"lvm-cheat-sheet","status":"publish","type":"post","link":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=99","title":{"rendered":"lvm cheat sheet"},"content":{"rendered":"\n<p>Logical Volume Manager (LVM)<\/p>\n\n\n\n<p>This is a quick and dirty cheat sheet on LVM using Linux, I have highlighted many of the common attributes for each command however this is not an extensive list, make sure you look up the command.<\/p>\n\n\n\n<p>With the pvs, vgs and lvs commands, the number of verboses added the more verbose information for example pvs -vvvvv<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Directory and Files<\/strong><\/td><\/tr><tr><td><strong>Directories and Files<\/strong><\/td><td>## Directories<br>\/etc\/lvm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; default lvm directory location<br>\/etc\/lvm\/backup&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; where the automatic backups go<br>\/etc\/lvm\/cache&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; persistent filter cache<br>\/etc\/lvm\/archive &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; where automatic archives go after a volume group change<br>\/var\/lock\/lvm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; lock files to prevent metadata corruption<br><br># Files<br>\/etc\/lvm\/lvm.conf&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; main lvm configuration file<br>$HOME\/.lvm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8211; lvm history<\/td><\/tr><tr><td><strong>Tools<\/strong><\/td><\/tr><tr><td><strong>diagnostic<\/strong><\/td><td>lvmdump<br>lvmdump -d<br>dmsetup [info|ls|status]<br><br>Note: by default the lvmdump command creates a tar ball<\/td><\/tr><tr><td><strong>Physical Volumes<\/strong><\/td><\/tr><tr><td><strong>display<\/strong><\/td><td>pvdisplay -v<br>pvs -v<br>pvs -a<br>pvs &#8211;segments (see the disk segments used)<br><br><strong>pvs attributes are:<\/strong><br>1. (a)llocatable<br>2. e(x)ported<\/td><\/tr><tr><td><strong>scanning<\/strong><\/td><td>pvscan -v<br><br>Note: scans for disks for non-LVM and LVM disks<\/td><\/tr><tr><td><strong>adding<\/strong><\/td><td>pvcreate \/dev\/sdb1<br><br>## Create physical volume with specific UUID, used to recover volume groups (see miscellaneous section)<br>pvcreate &#8211;uuid \/dev\/sdb1<br><br><strong>Common Attributes that you may want to use:<\/strong><br><br>-M2 create a LVM2 physical volume<\/td><\/tr><tr><td><strong>removing<\/strong><\/td><td>pvremove \/dev\/sdb1<\/td><\/tr><tr><td><strong>checking<\/strong><\/td><td>pvck -v \/dev\/sdb1<br><br>Note: check the consistency of the LVM metadata<\/td><\/tr><tr><td><strong>change physical attributes<\/strong><\/td><td>## do not allow allocation of extents on this drive, however the partition must be in a vg otherwise you get an error<br>pvchange -x n \/dev\/sdb1<br><br><strong>Common Attributes that you may want to use:<\/strong>&#8211;addtag add a tag<br>-x allowed to allocate extents<br>-u change the uuid<\/td><\/tr><tr><td><strong>moving<\/strong><\/td><td>pvmove -v \/dev\/sdb2 \/dev\/sdb3<br><br>Note: moves any used extents from this volume to another volume, in readiness to remove that volume. However you cannot use this on mirrored volumes, you must convert back to non-mirror using &#8220;lvconvert -m 0&#8221;<\/td><\/tr><tr><td><strong>Volume Groups<\/strong><\/td><\/tr><tr><td><strong>display<\/strong><\/td><td>vgdisplay -v<br>vgs -v<br>vgs -a -o +devices<br><br><strong>vgs flags:<\/strong><br>#PV &#8211; number of physical devices<br>#LV &#8211; number of configured volumes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><br><strong>vgs attributes are:<\/strong><br>1. permissions (r)|(w)<br>2. resi(z)eable<br>3. e(x)ported<br>4. (p)artial<br>5. allocation policy &#8211; (c)ontiguous, c(l)ing, (n)ormal, (a)nywhere, (i)nherited<br>6. (c)luster<\/td><\/tr><tr><td><strong>scanning<\/strong><\/td><td>vgscan -v<\/td><\/tr><tr><td><strong>creating<\/strong><\/td><td>vgcreate VolData00 \/dev\/sdb1 \/dev\/sdb2 \/dev\/sdb3<br>vgcreate VolData00 \/dev\/sdb[123]<br><br>## Use 32MB extent size<br>vgcreate VolData00 -s 32 \/dev\/sdb1<br><br><strong>Common Attributes that you may want to use:<\/strong><br><br>-l &nbsp;maximum logical volumes<br>-p maximum physical volumes<br>-s physical extent size (default is 4MB)<br>-A autobackup<\/td><\/tr><tr><td><strong>extending<\/strong><\/td><td>vgextend VolData00 \/dev\/sdb3<\/td><\/tr><tr><td><strong>reducing<\/strong><\/td><td>vgreduce VolData00 \/dev\/sdb3<br><br>vgreduce &#8211;removemissing &#8211;force VolData00<\/td><\/tr><tr><td><strong>removing<\/strong><\/td><td>vgremove VolData00<br><br><strong>Common Attributes that you may want to use:<\/strong><br><br>-f force the removal of any logical volumes<\/td><\/tr><tr><td><strong>checking<\/strong><\/td><td>vgck VolData00<br><br>Note: check the consistency of the LVM metadata<\/td><\/tr><tr><td><strong>change volume attributes<\/strong><\/td><td>vgchange -a n VolData00<br><br><strong>Common Attributes that you may want to use:<\/strong><br><br>-a control availability of volumes within the group<br>-l &nbsp;maximum logical volumes<br>-p maximum physical volumes<br>-s physical extent size (default is 4MB)<br>-x resizable yes or no (see VG status in vxdisplay)<\/td><\/tr><tr><td><strong>renaming<\/strong><\/td><td>vgrename VolData00 Data_Vol_01<br><br>note: the volume group must not have any active logical volumes<\/td><\/tr><tr><td><strong>converting metadata type<\/strong><\/td><td>vgconvert -M2 VolData00<br><br>Note: vgconvert allows you to convert from one type of metadata format to another for example from LVM1 to LVM2, LVM2 offers bigger capacity, clustering and mirroring<\/td><\/tr><tr><td><strong>merging<\/strong><\/td><td># the old volumes group will be merged into the new volume group<br>vgmerge New_Vol_Group Old_Vol_Group<br><br>Note: you must unmount any fielsystems and deactivate the vg that is being merged &#8220;vgchange -a n &#8220;, then you can activiate it again afterwards &#8220;vgchange -a y &#8220;, then perform a vgscan, dont forget to backup the configuration<\/td><\/tr><tr><td><strong>spliting<\/strong><\/td><td>vgsplit Old_Vol_Group New_Vol_Group [physical volumes] [-n logical volume name]<\/td><\/tr><tr><td><strong>importing<\/strong><\/td><td>vgimport VolData00<br><br><strong>Common Attributes that you may want to use:<\/strong><br><br>-a import all exported volume groups<\/td><\/tr><tr><td><strong>exporting<\/strong><\/td><td>## to see if a volume has already been export use &#8220;vgs&#8221; and look at the third attribute should be a x<br>vgexport VolData00<br><br><strong>Common Attributes that you may want to use:<\/strong><br><br>-a export all inactive volume groups<\/td><\/tr><tr><td><strong>backing up<\/strong><\/td><td>## Backup to default location (\/etc\/lvm\/backup)<br>vgcfgbackup VolData00<br><br># Backup to specific location<br>vgcfgbackup -f \/var\/backup\/VolData00_bkup VolData00# Backup to specific location all volume groups (notice the %s)<br>vgcfgbackup -f \/var\/backup\/vg_backups_%s<br><br>Note: the backup is written in plain text and are by default located in \/etc\/lvm\/backup<\/td><\/tr><tr><td><strong>restoring<\/strong><\/td><td>vgcfgrestore -f \/var\/backup\/VolData00_bkup VolData00<br><br><strong>Common Attributes that you may want to use:<\/strong><br><br>-l list backups of file<br>-f backup file<br>-M metadataype 1 or 2<\/td><\/tr><tr><td><strong>cloning<\/strong><\/td><td>vgimportclone \/dev\/sdb1<br><br>Note: used to import and rename duplicated volume group<\/td><\/tr><tr><td><strong>special files<\/strong><\/td><td>vgmknodes VolData00<br><br>Note: recreates volume group directory and logical volume special files in \/dev<\/td><\/tr><tr><td><strong>Logical Volumes<\/strong><\/td><\/tr><tr><td><strong>display<\/strong><\/td><td>lvdisplay -v<br>lvdisplay &#8211;maps&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display mirror volumes<br><br>lvs -v<br>lvs -a -o +devices<br><br>## lvs commands for mirror volumes<br>lvs -a -o +devices<br>lvs -a -o +seg_pe_ranges &#8211;segments## Stripe size<br>lvs -v &#8211;segments<br>lvs -a -o +stripes,stripesize## use complex command<br>lvs -a -o +devices,stripes,stripesize,seg_pe_ranges &#8211;segments<br><br><strong>lvs attributes are:<\/strong><br>1. volume type: (m)irrored, (M)irrored without initail sync, (o)rigin, (p)vmove, (s)napshot, invalid (S)napshot, (v)irtual, mirror (i)mage<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mirror (I)mage out-of-sync, under (c)onversion<br>2. permissions: (w)rite, (r)ead-only<br>3. allocation policy &#8211; (c)ontiguous, c(l)ing, (n)ormal, (a)nywhere, (i)nherited<br>4. fixed (m)inor<br>5. state: (a)ctive, (s)uspended, (I)nvalid snapshot, invalid (S)uspended snapshot, mapped (d)evice present with-out tables,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mapped device present with (i)nactive table<br>6. device (o)pen (mounted in other words)<\/td><\/tr><tr><td><strong>scanning<\/strong><\/td><td>lvscan -v<br>lvmdiskscan<\/td><\/tr><tr><td><strong>creating<\/strong><\/td><td>## plain old volume<br>lvcreate -L 10M VolData00## plain old volume but use extents, use 10 4MB extents (if extent size is 4MB)<br>lvcreate -l 10 VolData00<br><br>## plain old volume but with a specific name web01<br>lvcreate -L 10M -n web01 VolData00<br><br>## plain old volume but on a specific disk<br>lvcreate -L 10M VolData00 \/dev\/sdb1<br><br>## a striped volume called lvol1 (note the captial i for the stripe size), can use -l (extents) instead of -L<br>lvcreate -i 3 -L 24M -n lvol1 vg01## Mirrored volume<br>lvcreate -L 10M -m1 -n data01 vg01<br><br>## Mirrored volume without a mirror log file<br>lvcreate -L 10M -m1 &#8211;mirrorlog core -n data01 vg01<br><br><strong>Common Attributes that you may want to use:<\/strong><br><br>-L size of the volume [kKmMgGtT]<br>-l number of extents<br>-C contiguous [y|n]<br>-i stripes<br>-I stripe size<br>-m mirrors<br>&#8211;mirrorlog<br>-n volume name<\/td><\/tr><tr><td><strong>extending<\/strong><\/td><td>lvextend -L 20M \/dev\/VolData00\/vol01<strong>Common Attributes that you may want to use:<\/strong>-L size of the volume [kKmMgGtT]<br>-l number of extents<br>-C contiguous [y|n]<br>-i stripes<br>-I stripe size<br><br>Note: you can extend a ext2\/ext3 filesystem using the &#8220;resize2fs&#8221; or &#8220;fsadm&#8221; command<br><br>fsadm resize \/dev\/VolData01\/data01<br>resize2fs -p \/dev\/mapper\/VolData01-data01 [size]<br><br>The -p option displays bars of progress while extendingthe filesystem<\/td><\/tr><tr><td><strong>reducing\/resizing<\/strong><\/td><td>lvreduce -L 5M \/dev\/VolData00\/vol01<br>lvresize -L 5M \/dev\/VolData00\/vol01<br><br>Note: rounding will occur when extending and reducing volumes to the next extent (4MB by default), you can use resize2fs or fsadm to shrink the filesystemfsadm resize \/dev\/VolData01\/data01 [size]<br>resize2fs -p \/dev\/mapper\/VolData01-data01 [size]<\/td><\/tr><tr><td><strong>removing<\/strong><\/td><td>lvremove \/dev\/VolData00\/vol01<\/td><\/tr><tr><td><strong>adding a mirror to a non-mirrored volume<\/strong><\/td><td>lvconvert -m1 &#8211;mirrorlog core \/dev\/VolData00\/vol01 \/dev\/sdb2Note: you can also use the above command to remove a unwanted log<\/td><\/tr><tr><td><strong>removing a mirror from a mirrored volume<\/strong><\/td><td>lvconvert -m0 \/dev\/VolData00\/vol01 \/dev\/sdb2Note: the disk in the command is the one you want to remove<\/td><\/tr><tr><td>Mirror a volume that has stripes<\/td><td>lvconvert &#8211;stripes 3 -m1 &#8211;mirrorlog core \/dev\/VolData00\/data01 \/dev\/sdd1 \/dev\/sde1 \/devsdf1<\/td><\/tr><tr><td><strong>change volume attributes<\/strong><\/td><td>lvchange -a n \/dev\/VolData00\/vol01<br><br><strong>Common Attributes that you may want to use:<\/strong>-a availability<br>-C contiguous [y|n]<\/td><\/tr><tr><td><strong>renaming<\/strong><\/td><td>lvrename \/dev\/VolData00\/vol_old \/dev\/VolData00\/vol_new<\/td><\/tr><tr><td><strong>snapshotting<\/strong><\/td><td>lvcreate &#8211;size 100M &#8211;snapshot -name snap \/dev\/vg01\/data01<\/td><\/tr><tr><td><strong>Miscellaneous<\/strong><\/td><\/tr><tr><td><strong>Simulating a disk failure<\/strong><\/td><td>dd if=\/dev\/zero of=\/dev\/sdb2 count=10<\/td><\/tr><tr><td><strong>reparing a failed mirror no LVM corruption<\/strong><\/td><td>## check volume, persume \/dev\/sdb2 has failed<br>lvs -a -o +devices<br><br># remove the failed disk from the volume (if not already done so) , this will convert volume into a non-mirrored volume<br>vgreduce &#8211;removemissing &#8211;force VolData00<br><br>## replace the disk physically, remember to partion it with type 8e<br>fdisk \/dev\/sdb<br>&#8230;&#8230;..<br><br>## add new disk to LVM<br>pvcreate \/dev\/sdb2<br><br>## add the disk back into volume group<br>vgextend VolData00 \/dev\/sdb2<br><br>## mirror up the volume<br>lvconvert -m1 &#8211;mirrorlog core \/dev\/VolData00\/vol02 \/dev\/sdb2<\/td><\/tr><tr><td><strong>corrupt LVM metadata without replacing drive<\/strong><\/td><td># attempt to bring the volume group online<br>vgchange -a y VolData00<br><br># Restore the LVM configation<br>vgcfgrestore VolData00<br><br># attempt to bring the volume grou online<br>vgchange -a y VolData00<br><br># file system check<br>e2fsck \/dev\/VolData00\/data01<\/td><\/tr><tr><td><strong>corrupt LVM metadata but replacing the faulty disk<\/strong><\/td><td># attempt to bring the volume group online but you get UUID conflict errors make note of the UUID number<br>vgchange -a y VolData00<br>vgchange -a n VolData00<br><br>## sometimes it my only be a logical volume problem<br>lvchange -a y \/dev\/VolData00\/web02<br>lvchange -a n \/dev\/Voldata00\/web02<br><br>## replace the disk physically, remember to partion it with type 8e<br>fdisk \/dev\/sdb<br>&#8230;&#8230;..<br><br># after replacing the faulty drive the disk must have the previuos UUID number or you can get it from \/etc\/lvm directory<br>pvcreate &#8211;uuid &lt;<strong>previous UUID number taken from above command<\/strong>&gt; \/dev\/sdb2<br><br># Restore the LVM configation<br>vgcfgrestore VolData00<br><br># attempt to bring the volume group online or logical volume<br>vgchange -a y VolData00<br>lvchange -a y \/dev\/VolData00\/web02<br><br># file system check<br>e2fsck \/dev\/VolData00\/data01Note: if you have backed the volume group configuration you can obtain the UUID number in the backup file by default located in \/etc\/lvm\/backup or running &#8220;pvs -v&#8221;<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Logical Volume Manager (LVM) This is a quick and dirty cheat sheet on LVM using Linux, I have highlighted many of the common attributes for each command however this is not an extensive list, make sure you look up the command. With the pvs, vgs and lvs commands, the number of verboses added the more [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,13],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-file-management","category-os-maintenance"],"_links":{"self":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":1,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}