{"id":6608,"date":"2017-04-06T23:20:38","date_gmt":"2017-04-06T15:20:38","guid":{"rendered":"http:\/\/rmohan.com\/?p=6608"},"modified":"2017-04-06T23:20:38","modified_gmt":"2017-04-06T15:20:38","slug":"btrfs-in-centos-7","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6608","title":{"rendered":"BTRFS in CentOS 7"},"content":{"rendered":"<p>In these days we can find lot of file systems and they are also used in many Operating systems and block devices . Comparing with this file systems BTRFS is a newbie . Btrfs is a new copy on write (CoW) filesystem for Linux aimed at implementing advanced features while focusing on fault tolerance, repair and easy administration.<\/p>\n<p>Recently CentOS\/RHEL is also supports BTRFS .<\/p>\n<p>So lets start the tutorial ,<\/p>\n<p>Consider we have one disk \/dev\/sdb .<\/p>\n<blockquote><p><em># fdisk \/dev\/sdb<\/em><\/p><\/blockquote>\n<p>And create disk called <em>\/dev\/sdb1 .<\/em><\/p>\n<p>Format it using BTRFS .<\/p>\n<blockquote><p><em># <span class=\"skimlinks-unlinked\">mkfs.btrfs<\/span> \/dev\/sdb1<\/em><\/p><\/blockquote>\n<p>For testing purpose we are mounting this to <strong>\/mnt .<\/strong><\/p>\n<blockquote><p><em># mount \/dev\/sdb1 \/mnt<\/em><\/p><\/blockquote>\n<p>Now , BTRFS is mounted under \/mnt . Just check with <strong><em>df<\/em> <\/strong>command .<\/p>\n<p>Basically BTRFS is working with subvolume\u2019s . First we need to create a subvolume . Fo that we don\u2019t need to unmount file system .<\/p>\n<blockquote><p><em># btrfs sub create \/mnt\/volume1<\/em><\/p>\n<p><em># tree \/mnt<\/em><\/p>\n<p><em># btrfs sub list \/mnt<\/em><\/p><\/blockquote>\n<p>Now we need to remount with \u201cvolume1\u201d as our default mount mount. For that ,<\/p>\n<blockquote><p><em># umount \/mnt<\/em><\/p>\n<p><em># mount -o subvol=volume1 \/dev\/sdb1 \/mnt<\/em><\/p><\/blockquote>\n<p>With these commands we can able to mount particular subvoumes to our mount points .<\/p>\n<blockquote><p><em># btrfs sub get-default \/mnt<\/em><\/p><\/blockquote>\n<p>Above command will show default sub volume .<\/p>\n<p>To create new sub volume under <em>volume1<\/em> ,<\/p>\n<blockquote><p><em># btrfs sub create \/mnt\/newsub1<\/em><\/p>\n<p><em># btrfs sub create \/mnt\/newsub2<\/em><\/p><\/blockquote>\n<p>Above command will create new subvolume <em>newsub1<\/em> under <em>volume1<\/em> .<\/p>\n<p>Now the subvolume tree is look like,<\/p>\n<p><strong><em>VOLUME1 -&gt; NEWSUB1<\/em><\/strong><\/p>\n<p><strong><em>-&gt; NEWSUB2<\/em><\/strong><\/p>\n<p>Ok. Now we need to create another main subvolume like <em>volume1<\/em> . For that ,<\/p>\n<blockquote><p><em># umount \/mnt<\/em><\/p>\n<p><em># mount -o subvolid=0 \/dev\/sdb1 \/mnt<\/em><\/p>\n<p><em># btrfs sub create \/mnt\/volume2<\/em><\/p>\n<p><em># cd \/mnt<\/em><\/p>\n<p><em># ls<\/em><\/p>\n<p><em>volume1\u00a0\u00a0\u00a0 volume2<\/em><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><strong>Snapshot using BTRFS<\/strong><\/p>\n<p>Now we know how to create btrfs filesystem and subvolumes . This is only\u00a0 just beginning , using BTRFS we can configure RAID levels , remote transfer..etc . Later we can discuss about these mechanism .<\/p>\n<p>How can we take a snapshot ?\u00a0 Its simple .<\/p>\n<p>Consider we need to take a snapshot of <em>volume2 .<\/em><\/p>\n<blockquote><p># cd \/mnt<\/p>\n<p># btrfs sub snap volume2 volume2.snap<\/p><\/blockquote>\n<p>Format is <em>btrfs sub snap &lt;source&gt; &lt;destination&gt;<\/em><\/p>\n<blockquote><p># btrfs sub list \/mnt<\/p><\/blockquote>\n<p>This command will list all sub volumes including snapshots .<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How to take CentOS 7 root (\/) snapshot ?<\/strong><\/p>\n<p>By default CentOS\/RHEL is installing using XFS file system . Try to install centos using BTRFS file system . We can convert our existing file system to BTRFS , that will cover on next section .<\/p>\n<p>After installing CentOS\/rhel using BTRFS filesystem ,<\/p>\n<blockquote><p># btrfs sub list \/<\/p>\n<p>ID 257 gen 871 top level 5 path root<br \/>\nID 260 gen 41 top level 257 path var\/lib\/machines<\/p><\/blockquote>\n<p>From this we know that root and var\/lib\/machines are the sub volumes . But from below command we can see that default sub volume mounted under \/ is root (ID 5). For more clarification you can check <em>\/etc\/fstab<\/em> .<\/p>\n<blockquote><p># btrfs sub get-default \/<\/p>\n<p>ID 5 (FS_TREE)<\/p><\/blockquote>\n<p>To take snapshot we need to mount btrfs partition to other folder , for that first we need to know which partition .<\/p>\n<blockquote><p># df -h<\/p>\n<p>Filesystem\u00a0\u00a0\u00a0\u00a0\u00a0 Size\u00a0 Used Avail Use% Mounted on<br \/>\n\/dev\/sda3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 18G\u00a0 1.1G\u00a0\u00a0 15G\u00a0\u00a0 7% \/<\/p><\/blockquote>\n<p>From this we can see that <em>\/dev\/sda3<\/em> is the partition which we need to mount .<\/p>\n<blockquote><p># mkdir \/btrfs<\/p>\n<p># mount -o subvolid=0 \/dev\/sda3 \/btrfs<\/p>\n<p># cd \/btrfs<\/p>\n<p># ls<\/p>\n<p>root<\/p><\/blockquote>\n<p>To make snapshot,<\/p>\n<blockquote><p># btrfs sub snap root root-$(date +%Y%m%d)<\/p>\n<p># ls<\/p>\n<p>root\u00a0 <span class=\"skimlinks-unlinked\">root.snap<\/span><\/p>\n<p># btrfs sub list \/<\/p>\n<p>ID 257 gen 946 top level 5 path root<br \/>\nID 260 gen 41 top level 257 path root\/var\/lib\/machines<br \/>\nID <strong>262<\/strong> gen 946 top level 5 path root-20160607<\/p><\/blockquote>\n<p>Now we have one snapshot of our root file system .<\/p>\n<p>To boot with our new snapshot need to set some configurations ,<\/p>\n<p>Remove all the \u201crootflags=subvol=root\u201d arguments from\u00a0<em>\/boot\/grub2\/<span class=\"skimlinks-unlinked\">grub.cfg<\/span><\/em>. If you don\u2019t do this, it will disregard the default subvolume id we just set and always boot into the root subvolume.<\/p>\n<blockquote><p># sed -i \u2018s\/rootflags=subvol=root \/\/\u2019\u00a0\/boot\/grub2\/<span class=\"skimlinks-unlinked\">grub.cfg<\/span><\/p>\n<p># btrfs sub set-default 262 \/.<\/p>\n<p># reboot<\/p><\/blockquote>\n<p>After booting up , we need to check root subvolume .<\/p>\n<blockquote><p># btrfs subvolume show \/.<\/p>\n<p>Name:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 root-20160607<br \/>\nuuid:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1a65c55f-4e07-134c-8e34-f17574d2f4ac<br \/>\nParent uuid:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 dfd7589a-0fa4-5c4b-8b95-312d6c619f69<br \/>\nCreation time:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2016-06-07 02:05:05<br \/>\nObject ID:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 262<br \/>\nGeneration (Gen):\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 958<br \/>\nGen at creation:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 946<br \/>\nParent:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5<br \/>\nTop Level:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 5<br \/>\nFlags:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2013<br \/>\nSnapshot(s):<\/p><\/blockquote>\n<p>Success . Now we know how to create snapshot and point that to default right ?<\/p>\n<p>So , how can we <strong>delete those unwanted subvolumes ?<\/strong> Its easy .<\/p>\n<blockquote><p># mount -o subvolid=0 \/dev\/sda3 \/btrfs<\/p>\n<p># cd \/btrfs<\/p>\n<p># ls<\/p>\n<p>root root-20160607<\/p>\n<p># btrfs sub delete root<\/p><\/blockquote>\n<p>Default <em>rm<\/em> command will not work here .<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Snapper Utility<\/strong><\/p>\n<p>Snapper is an advanced tool for snapshoting BTRFS systems\u00a0 .<\/p>\n<p>To install ,<\/p>\n<blockquote><p># yum install snapper -y<\/p><\/blockquote>\n<p>Snapper utility is working with configuration\u00a0 file and <em>.snapshot<\/em> hidden directory . Configurations are stored under <em>\/etc\/snapper\/configs\/<\/em> location.<\/p>\n<p>When we are run a snapper configuration it will automatically create conf file under <em>\/etc<\/em> and create .snapshot directory under that sub volume .<\/p>\n<p>For example ,<\/p>\n<p>We can use <em>\/dev\/sdb1<\/em> and <em>volume1<\/em> as default sub volume .<\/p>\n<blockquote><p># mount -o subvol=volume1 \/dev\/sdb1 \/mnt<\/p>\n<p># cd \/mnt<\/p>\n<p># snapper -c volume1 create-config \/mnt\/<\/p><\/blockquote>\n<p>This command will create <em>volume1<\/em> config file under <em>\/etc\/snapper<\/em> and <em>.snapshot<\/em> folder on this location .<\/p>\n<p>To take a snapshot using snapper ,<\/p>\n<blockquote><p># snapper -c volume1 create -d \u201cFirst snapshot\u201d<\/p>\n<p># snapper -c volume1 list<\/p><\/blockquote>\n<p>To see the difference between snapshots ,<\/p>\n<blockquote><p># snapper -c volume1 status 1..0<\/p><\/blockquote>\n<p>This means snapper is comparing snapshot ID 1 with 0 and list differences .<\/p>\n<p>If you need to recover previous snapshot files ,<\/p>\n<blockquote><p># snapper -c volume1 undochange 1..0<\/p><\/blockquote>\n<p><em><strong>Task to do your self<\/strong> <\/em>: Create some files under sub volume and take snapshot . Revert back using snapper.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In these days we can find lot of file systems and they are also used in many Operating systems and block devices . Comparing with this file systems BTRFS is a newbie . Btrfs is a new copy on write (CoW) filesystem for Linux aimed at implementing advanced features while focusing on fault tolerance, repair [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6608"}],"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=6608"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6608\/revisions"}],"predecessor-version":[{"id":6609,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6608\/revisions\/6609"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}