November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

BTRFS in CentOS 7

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.

Recently CentOS/RHEL is also supports BTRFS .

So lets start the tutorial ,

Consider we have one disk /dev/sdb .

# fdisk /dev/sdb

And create disk called /dev/sdb1 .

Format it using BTRFS .

# mkfs.btrfs /dev/sdb1

For testing purpose we are mounting this to /mnt .

# mount /dev/sdb1 /mnt

Now , BTRFS is mounted under /mnt . Just check with df command .

Basically BTRFS is working with subvolume’s . First we need to create a subvolume . Fo that we don’t need to unmount file system .

# btrfs sub create /mnt/volume1

# tree /mnt

# btrfs sub list /mnt

Now we need to remount with “volume1” as our default mount mount. For that ,

# umount /mnt

# mount -o subvol=volume1 /dev/sdb1 /mnt

With these commands we can able to mount particular subvoumes to our mount points .

# btrfs sub get-default /mnt

Above command will show default sub volume .

To create new sub volume under volume1 ,

# btrfs sub create /mnt/newsub1

# btrfs sub create /mnt/newsub2

Above command will create new subvolume newsub1 under volume1 .

Now the subvolume tree is look like,

VOLUME1 -> NEWSUB1

-> NEWSUB2

Ok. Now we need to create another main subvolume like volume1 . For that ,

# umount /mnt

# mount -o subvolid=0 /dev/sdb1 /mnt

# btrfs sub create /mnt/volume2

# cd /mnt

# ls

volume1    volume2

 

Snapshot using BTRFS

Now we know how to create btrfs filesystem and subvolumes . This is only  just beginning , using BTRFS we can configure RAID levels , remote transfer..etc . Later we can discuss about these mechanism .

How can we take a snapshot ?  Its simple .

Consider we need to take a snapshot of volume2 .

# cd /mnt

# btrfs sub snap volume2 volume2.snap

Format is btrfs sub snap <source> <destination>

# btrfs sub list /mnt

This command will list all sub volumes including snapshots .

 

How to take CentOS 7 root (/) snapshot ?

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 .

After installing CentOS/rhel using BTRFS filesystem ,

# btrfs sub list /

ID 257 gen 871 top level 5 path root
ID 260 gen 41 top level 257 path var/lib/machines

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 /etc/fstab .

# btrfs sub get-default /

ID 5 (FS_TREE)

To take snapshot we need to mount btrfs partition to other folder , for that first we need to know which partition .

# df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G  1.1G   15G   7% /

From this we can see that /dev/sda3 is the partition which we need to mount .

# mkdir /btrfs

# mount -o subvolid=0 /dev/sda3 /btrfs

# cd /btrfs

# ls

root

To make snapshot,

# btrfs sub snap root root-$(date +%Y%m%d)

# ls

root  root.snap

# btrfs sub list /

ID 257 gen 946 top level 5 path root
ID 260 gen 41 top level 257 path root/var/lib/machines
ID 262 gen 946 top level 5 path root-20160607

Now we have one snapshot of our root file system .

To boot with our new snapshot need to set some configurations ,

Remove all the “rootflags=subvol=root” arguments from /boot/grub2/grub.cfg. If you don’t do this, it will disregard the default subvolume id we just set and always boot into the root subvolume.

# sed -i ‘s/rootflags=subvol=root //’ /boot/grub2/grub.cfg

# btrfs sub set-default 262 /.

# reboot

After booting up , we need to check root subvolume .

# btrfs subvolume show /.

Name:                   root-20160607
uuid:                   1a65c55f-4e07-134c-8e34-f17574d2f4ac
Parent uuid:            dfd7589a-0fa4-5c4b-8b95-312d6c619f69
Creation time:          2016-06-07 02:05:05
Object ID:              262
Generation (Gen):       958
Gen at creation:        946
Parent:                 5
Top Level:              5
Flags:                  –
Snapshot(s):

Success . Now we know how to create snapshot and point that to default right ?

So , how can we delete those unwanted subvolumes ? Its easy .

# mount -o subvolid=0 /dev/sda3 /btrfs

# cd /btrfs

# ls

root root-20160607

# btrfs sub delete root

Default rm command will not work here .

 

Snapper Utility

Snapper is an advanced tool for snapshoting BTRFS systems  .

To install ,

# yum install snapper -y

Snapper utility is working with configuration  file and .snapshot hidden directory . Configurations are stored under /etc/snapper/configs/ location.

When we are run a snapper configuration it will automatically create conf file under /etc and create .snapshot directory under that sub volume .

For example ,

We can use /dev/sdb1 and volume1 as default sub volume .

# mount -o subvol=volume1 /dev/sdb1 /mnt

# cd /mnt

# snapper -c volume1 create-config /mnt/

This command will create volume1 config file under /etc/snapper and .snapshot folder on this location .

To take a snapshot using snapper ,

# snapper -c volume1 create -d “First snapshot”

# snapper -c volume1 list

To see the difference between snapshots ,

# snapper -c volume1 status 1..0

This means snapper is comparing snapshot ID 1 with 0 and list differences .

If you need to recover previous snapshot files ,

# snapper -c volume1 undochange 1..0

Task to do your self : Create some files under sub volume and take snapshot . Revert back using snapper.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>