{"id":4693,"date":"2015-04-28T11:41:37","date_gmt":"2015-04-28T03:41:37","guid":{"rendered":"http:\/\/rmohan.com\/?p=4693"},"modified":"2015-04-28T11:43:22","modified_gmt":"2015-04-28T03:43:22","slug":"lvm2-device-filter-and-lvm-metadata-restore","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=4693","title":{"rendered":"LVM2: device filter and LVM metadata restore"},"content":{"rendered":"<h3 class=\"post-title entry-title\">LVM2: device filter and LVM metadata restore<\/h3>\n<div class=\"post-header\"><\/div>\n<div id=\"post-body-73827044589443832\" class=\"post-body entry-content\"><strong>Customize LVM device filter to get rid of the annoying \u201c\/dev\/cdrom: open failed\u201d warning<\/strong><\/p>\n<pre>##\/dev\/cdrom: open failed warning\r\n<\/pre>\n<pre>$pvcreate \/dev\/sdb1\r\n<\/pre>\n<pre>\/dev\/cdrom: open failed: Read-only file system\r\n<\/pre>\n<pre>$ vgcreate vg01 \/dev\/sdb1\r\n<\/pre>\n<pre>\/dev\/cdrom: open failed: Read-only file system\r\n<\/pre>\n<pre>##The error because LVM scan all device files by default, you can exclude some device files by device filters\r\n<\/pre>\n<pre><\/pre>\n<pre>##File \/etc\/lvm\/cache\/.cache contains the device file names scanned by LVM\r\n<\/pre>\n<pre>$ cat \/etc\/lvm\/cache\/.cache\r\n<\/pre>\n<pre>persistent_filter_cache {\r\n<\/pre>\n<pre>valid_devices=[\r\n<\/pre>\n<pre>\"\/dev\/ram11\",\r\n<\/pre>\n<pre>\"\/dev\/cdrom\",\r\n<\/pre>\n<pre><\/pre>\n<pre>##Edit \/etc\/lvm\/lvm.conf, Change default filter  \r\n<\/pre>\n<pre>filter = [ \"a\/.*\/\" ]\r\n<\/pre>\n<pre>#to\r\n<\/pre>\n<pre>filter = [ \"r|\/dev\/cdrom|\",\"r|\/dev\/ram*|\" ]\r\n<\/pre>\n<pre><\/pre>\n<pre>##You need to delete the cache file or ran vgscan to regenerate the file\r\n<\/pre>\n<pre>$rm \/etc\/lvm\/cache\/.cache   OR vgscan<\/pre>\n<p><strong>LVM metadata backup and restore\u00a0<\/strong><br \/>\nLVM record every LVM VG and LV metadata operation and save it to \/etc\/lvm\/backup automatically, old version backup files are archived to \/etc\/lvm\/archive.<br \/>\nThe backup file can be used to rollback LVM metadata changes, for example, if you have removed the VG\/PV or even re-initialize disk with pvcreate, Don&#8217;t panic,as long as file system was not re-created, you can use vgcfgrestore to restore all the data.<br \/>\nThe following is to demonstrate how to recover a LV after it is completed destroyed from PV level (pvremove)<br \/>\n1.Create test LV and write some data<\/p>\n<pre>$pvcreate  \/dev\/sdb1 \/dev\/sdb2\r\n<\/pre>\n<pre>Physical volume \"\/dev\/sdb1\" successfully created\r\n<\/pre>\n<pre>Physical volume \"\/dev\/sdb2\" successfully created\r\n<\/pre>\n<pre>$vgcreate vg01  \/dev\/sdb1 \/dev\/sdb2\r\n<\/pre>\n<pre>Volume group \"vg01\" successfully created\r\n<\/pre>\n<pre>$ lvcreate -L100M -n lv01 vg01\r\n<\/pre>\n<pre>Logical volume \"lv01\" created\r\n<\/pre>\n<pre>$ mkfs.ext3 \/dev\/vg01\/lv01\r\n<\/pre>\n<pre>$ mount \/dev\/vg01\/lv01 \/mnt\/\r\n<\/pre>\n<pre>$cp \/etc\/hosts \/mnt\/\r\n<\/pre>\n<pre>$ ls \/mnt\/\r\n<\/pre>\n<pre>hosts  lost+found<\/pre>\n<p>2.Destroy LV,VG,and PV<\/p>\n<pre>$vgremove vg01\r\n<\/pre>\n<pre>Do you really want to remove volume group \"vg01\" containing 1 logical volumes? [y\/n]: y\r\n<\/pre>\n<pre>Do you really want to remove active logical volume lv01? [y\/n]: y\r\n<\/pre>\n<pre>Logical volume \"lv01\" successfully removed\r\n<\/pre>\n<pre>Volume group \"vg01\" successfully removed\r\n<\/pre>\n<pre><\/pre>\n<pre>#VG is removed and PV was also wiped out\r\n<\/pre>\n<pre>$ pvcreate \/dev\/sdb1 \/dev\/sdb2\r\n<\/pre>\n<pre>Physical volume \"\/dev\/sdb1\" successfully created\r\n<\/pre>\n<pre>Physical volume \"\/dev\/sdb2\" successfully created<\/pre>\n<p>3.Lets recover the LV and the data<\/p>\n<pre>##Find out the backup file to restore from\r\n<\/pre>\n<pre>$vgcfgrestore -l vg01\r\n<\/pre>\n<pre>..\r\n<\/pre>\n<pre>file:         \/etc\/lvm\/archive\/vg01_00002.vg\r\n<\/pre>\n<pre>VG name:      vg01\r\n<\/pre>\n<pre>Description:  Created *before* executing 'vgremove vg01'\r\n<\/pre>\n<pre>Backup time:  Tue May 10 15:41:31 2011\r\n<\/pre>\n<pre><\/pre>\n<pre>##first attempt failed, because PV UUID is changed\r\n<\/pre>\n<pre>$ vgcfgrestore -f \/etc\/lvm\/archive\/vg01_00002.vg vg01\r\n<\/pre>\n<pre>Couldn't find device with uuid 'pVf1J2-rAsd-eWkD-mCJc-S0pc-47zc-ImjXSB'.\r\n<\/pre>\n<pre>Couldn't find device with uuid 'J14aVl-mbuj-k9MM-63Ad-TBAa-S0xF-VElV2W'.\r\n<\/pre>\n<pre>Cannot restore Volume Group vg01 with 2 PVs marked as missing.\r\n<\/pre>\n<pre>Restore failed.\r\n<\/pre>\n<pre><\/pre>\n<pre>##Find old UUID\r\n<\/pre>\n<pre>$ grep -B 2 \/dev\/sdb \/etc\/lvm\/archive\/vg01_00002.vg\r\n<\/pre>\n<pre>pv0 {\r\n<\/pre>\n<pre>id = \"pVf1J2-rAsd-eWkD-mCJc-S0pc-47zc-ImjXSB\"\r\n<\/pre>\n<pre>device = \"\/dev\/sdb1\"    # Hint only\r\n<\/pre>\n<pre>--\r\n<\/pre>\n<pre>pv1 {\r\n<\/pre>\n<pre>id = \"J14aVl-mbuj-k9MM-63Ad-TBAa-S0xF-VElV2W\"\r\n<\/pre>\n<pre>device = \"\/dev\/sdb2\"    # Hint only\r\n<\/pre>\n<pre>$\r\n<\/pre>\n<pre><\/pre>\n<pre>##Recreate PV with the old UUID\r\n<\/pre>\n<pre>$ pvcreate -u pVf1J2-rAsd-eWkD-mCJc-S0pc-47zc-ImjXSB \/dev\/sdb1\r\n<\/pre>\n<pre>Physical volume \"\/dev\/sdb1\" successfully created\r\n<\/pre>\n<pre>$ pvcreate -u J14aVl-mbuj-k9MM-63Ad-TBAa-S0xF-VElV2W  \/dev\/sdb2\r\n<\/pre>\n<pre>Physical volume \"\/dev\/sdb2\" successfully created\r\n<\/pre>\n<pre><\/pre>\n<pre>##run vgcfgrestore again\r\n<\/pre>\n<pre>$ vgcfgrestore -f \/etc\/lvm\/archive\/vg01_00002.vg vg01\r\n<\/pre>\n<pre>Restored volume group vg01\r\n<\/pre>\n<pre><\/pre>\n<pre>##data was also recovered\r\n<\/pre>\n<pre>$ mount \/dev\/vg01\/lv01 \/mnt\/\r\n<\/pre>\n<pre>mount: special device \/dev\/vg01\/lv01 does not exist\r\n<\/pre>\n<pre>$ lvchange -a y vg01\/lv01\r\n<\/pre>\n<pre>$ mount \/dev\/vg01\/lv01 \/mnt\/\r\n<\/pre>\n<pre>$ cat \/mnt\/hosts\r\n<\/pre>\n<pre>127.0.0.1       localhost\r\n<\/pre>\n<pre>..<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>LVM2: device filter and LVM metadata restore Customize LVM device filter to get rid of the annoying \u201c\/dev\/cdrom: open failed\u201d warning<\/p>\n<p> ##\/dev\/cdrom: open failed warning $pvcreate \/dev\/sdb1 \/dev\/cdrom: open failed: Read-only file system $ vgcreate vg01 \/dev\/sdb1 \/dev\/cdrom: open failed: Read-only file system ##The error because LVM scan all device files by default, you can [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4,17],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4693"}],"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=4693"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4693\/revisions"}],"predecessor-version":[{"id":4694,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4693\/revisions\/4694"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}