{"id":492,"date":"2009-12-11T16:31:11","date_gmt":"2009-12-11T23:31:11","guid":{"rendered":"http:\/\/www.kernelcrash.com\/blog\/?p=492"},"modified":"2009-12-11T21:54:38","modified_gmt":"2009-12-12T04:54:38","slug":"reverting-lvm-snapshots","status":"publish","type":"post","link":"https:\/\/www.kernelcrash.com\/blog\/reverting-lvm-snapshots\/2009\/12\/11\/","title":{"rendered":"Reverting LVM snapshots"},"content":{"rendered":"<p>One thing that would be nice to have in linux LVM is the ability to take a snapshot of a logical volume, make some changes then &#8216;roll back&#8217; to the state preserved in the snapshot. If you look at the current set of lv commands on most linux distros there is no such option to &#8216;revert to snapshot&#8217;. Now, that I&#8217;m using KVM more, the ability to &#8216;rollback&#8217; a virtual machine installed on a logical volume would be particularly handy.<\/p>\n<p>Well it turns out you can &#8216;revert to snapshot&#8217;, but you just have to use a non-standard tool and some dmsetup commands.  The tool is called dm-merge. Go to the <a href=\"http:\/\/repo.or.cz\/w\/dm-merge.git\">dm-merge git repository<\/a> or <a href=\"http:\/\/repo.or.cz\/w\/dm-merge.git?a=snapshot;h=HEAD;sf=tgz\">download the latest dm-merge snapshot<\/a> . dm-merge just looks at the &#8216;changed blocks&#8217; and pushes the old copies of them back into the original logical volume.  You&#8217;ll need to compile the dm-merge program (just do a &#8216;make&#8217;). dm-merge comes with some doco. It explains two possible methods; reverting with an active logical volume OR reverting with an inactive one. In the former case you&#8217;re attempting to revert to a snapshot when the filesystem you&#8217;re trying to change is already mounted. I imagine that to be a very dangerous activity (but I guess it works well enough if there is little filesystem activity). I chose the latter (safer) method. <\/p>\n<p>Here are my notes on the procedure. If this were a document for work, I would a) tell people they were crazy to even attempt this and b) if you&#8217;re still crazy make sure you make a backup. At least try this procedure out on a test machine first. The possibility of screwing your disk by virtue of a small typing mistake is quite high. <\/p>\n<p>In the example below, I had a logical volume \/dev\/vgz\/lvubuntu910full32bit that I was using as my hda drive in a KVM guest machine, and I had created a snapshot &#8216;snap1&#8217; beforehand;<\/p>\n<blockquote>\n<pre>\r\n   lvcreate --size 1G --snapshot --name snap1 \/dev\/vgz\/lvubuntu910full32bit\r\n<\/pre>\n<\/blockquote>\n<p>One important thing I realised recently is that the amount of space you allocate to a snapshot (eg. 1GB in the example above) can actually be resized without killing the snapshot (eg. lvresize -L 20G \/dev\/vgz\/snap1)<\/p>\n<p>Most of the following is a rehash of what is in the README that comes with dm-merge. I&#8217;m only referring to the method involving an inactive logical volume<\/p>\n<blockquote>\n<pre>\r\n# Firstly, shut down access to your logical volume (ie. \/dev\/vgz\/vgzlvubuntu910full32bit).\r\n   Since this is a KVM guest, all I did was shut down the guest. If you're doing this on a\r\n   mounted logical volume then you should umount it.\r\n \r\n# Duplicate the tables of the logical volumes and snapshot storage (note the use of the \r\n   special -real and -cow suffixes)\r\n\r\ndmsetup table vgz-lvubuntu910full32bit-real |dmsetup create tmplv\r\ndmsetup table vgz-snap1-cow |dmsetup create tmpcow\r\n\r\n# Deactivate the main lv\r\n\r\nlvchange -a n \/dev\/vgz\/lvubuntu910full32bit\r\n\r\n# flush buffers\r\n\r\nblockdev --flushbufs \/dev\/mapper\/{tmplv,tmpcow}\r\n\r\n# Do a dm-merge test run. You see lots of lines that start with 'dd' and a summary at the bottom.\r\n\r\ndm-merge -i \/dev\/mapper\/tmpcow -o \/dev\/mapper\/tmplv -vd\r\n...\r\ndd of=\"\/dev\/mapper\/tmplv\" seek=2135444 if='\/dev\/mapper\/tmpcow' iflag=direct skip=8275 count=1 bs=8b\r\ndd of=\"\/dev\/mapper\/tmplv\" seek=2135445 if='\/dev\/mapper\/tmpcow' iflag=direct skip=8276 count=1 bs=8b\r\ndd of=\"\/dev\/mapper\/tmplv\" seek=2135446 if='\/dev\/mapper\/tmpcow' iflag=direct skip=8277 count=1 bs=8b\r\ndd of=\"\/dev\/mapper\/tmplv\" seek=5056466 if='\/dev\/mapper\/tmpcow' iflag=direct skip=8278 count=1 bs=8b\r\ndd of=\"\/dev\/mapper\/tmplv\" seek=5056467 if='\/dev\/mapper\/tmpcow' iflag=direct skip=8279 count=1 bs=8b\r\nFound 8246 exceptions of chunksize 4096, total size 33775616 bytes (32984 KiB, 32.211 MiB, 0.031 GiB).\r\n\r\n# Do it for real. You get a couple of lines of output plus the same summary line.\r\n\r\n dm-merge -i \/dev\/mapper\/tmpcow -o \/dev\/mapper\/tmplv -f\r\n\r\nArtificial sleep (1 second)\r\nFound a proper MAGIC header: 0x70416e53\r\nvalid = 1\r\nversion = 1\r\nchunk_size = 8 (4096 bytes)\r\nFound 8246 exceptions of chunksize 4096, total size 33775616 bytes (32984 KiB, 32.211 MiB, 0.031 GiB).\r\n\r\n# Flush buffers again\r\n\r\nblockdev --flushbufs \/dev\/mapper\/{tmplv,tmpcow}\r\n\r\n# Remove the tmp lv's. These are just references to data, so it won't delete your real data.\r\n\r\ndmsetup remove tmplv\r\ndmsetup remove tmpcow\r\n\r\n# Activate it\r\n\r\nlvchange -a y \/dev\/vgz\/lvubuntu910full32bit\r\n\r\n<\/pre>\n<\/blockquote>\n<p>Now try to start your KVM guest again &#8230; or remount it to check it out and confirm that it&#8217;s reverted to the original state. If you snapshotted a live KVM guest or a mounted filesystem, then you may find that your KVM guest will do a filesystem check on boot (fair enough), or you may need to run fsck on your filesystem before you can mount it.<\/p>\n<p>Don&#8217;t forget to remove the snapshot you initially made (eg. lvremove \/dev\/vgz\/snap1)<\/p>\n<p>One thing to note is the &#8216;revert to snapshot&#8217; capability has been &#8216;coming&#8217; for a while in mainstream LVM. There&#8217;s a <a href=\"http:\/\/lwn.net\/Articles\/363575\/\">recent mention on LWN<\/a> about it<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing that would be nice to have in linux LVM is the ability to take a snapshot of a logical volume, make some changes then &#8216;roll back&#8217; to the state preserved in the snapshot. If you look at the current set of lv commands on most linux distros there is no such option to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-492","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/posts\/492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":23,"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/posts\/492\/revisions\/519"}],"wp:attachment":[{"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kernelcrash.com\/blog\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}