Now Reading
How To Generate Btrfs File System in Linux
1

How To Generate Btrfs File System in Linux

Btrfs File System- Creation and Management in Linux

This article guide us to create and manage the Btrfs File System in linux with video tutuorial. Btrfs file system is a GPL-licensed copy-on-write (COW) developed by multiple companies. In this files can be created in any characters except “/” and NULL, which has self-healing features and have the capability of spanning multiple volumes.

Features

  • Added convert option to show progress.
  • Ability to link lost files to lost+found. This is a fix for a recent kernel Bug.
  • By default mkfs skinny-metadata feature is available from kernel 3.10.
  • To repair the severely corrupted file-systems with care.
  • Sub volumes for file-system.
  • To see the overview of file-system usage rather than df.
  • Check and defragment online files system.
  • Extend base file storage.

To Install and Create Btrfs Filesystem

Use the following command to install btrfs package.

On Debian based Distro’s

# sudo apt-get install btrfs-tools -y

On RedHat based Distro’s

[root@linuxhelp ~]# yum install btrfs-progs -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package btrfs-progs.x86_64 0:3.12-4.el7 will be updated
.
.
.
Running transaction
  Updating   : btrfs-progs-3.19.1-1.el7.x86_64                                                                                          1/2 
  Cleanup    : btrfs-progs-3.12-4.el7.x86_64                                                                                            2/2 
  Verifying  : btrfs-progs-3.19.1-1.el7.x86_64                                                                                          1/2 
  Verifying  : btrfs-progs-3.12-4.el7.x86_64                                                                                            2/2 

Updated:
  btrfs-progs.x86_64 0:3.19.1-1.el7                                                                                                         

Complete!

After installing the btrfs package, enable the Kernel module using the following command.

[root@linuxhelp ~]# modprobe btrfs

Setup logical volumes and create the btrfs file-system. Verify the disk attached to the system before creating it.

[root@linuxhelp ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b5211

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 5122047 2048000 82 Linux swap / Solaris
/dev/sda3 5122048 41943039 18410496 83 Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Use ‘fdisk‘ interface to create partitions on the /dev/sdb disk as follows.

[root@linuxhelp ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disk label with disk identifier 0x8d064c98.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x8d064c98

Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 8e Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Run the partprobe command to add the disk information to kernel. After that, list the partition as shown below.

[root@linuxhelp ~]# partprobe /dev/sdb
[root@linuxhelp ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b5211

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 5122047 2048000 82 Linux swap / Solaris
/dev/sda3 5122048 41943039 18410496 83 Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x8d064c98

Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 8e Linux LVM

To Setup Logical Volume

Use the pvcreate and vgcreate command to create Physical volume and volume group on /dev/sdb1 disk.

[root@linuxhelp ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@linuxhelp ~]# vgcreate linuxhelp /dev/sdb1
Volume group "linuxhelp" successfully created

Create the Logical volume in the volume group.

[root@linuxhelp ~]# lvcreate -L 500M -n lv1 /dev/mapper/linuxhelp
Logical volume "lv1" created
[root@linuxhelp ~]# lvcreate -L 500M -n lv2 /dev/mapper/linuxhelp
Logical volume "lv2" created

Now list the created Physical volume, Volume group and logical volumes.

[root@linuxhelp ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 linuxhelp lvm2 a-- 2.00g 1.02g
[root@linuxhelp ~]# vgs
VG #PV #LV #SN Attr VSize VFree
linuxhelp 1 2 0 wz--n- 2.00g 1.02g
[root@linuxhelp ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv1 linuxhelp -wi-a----- 500.00m
lv2 linuxhelp -wi-a----- 500.00m

To Create Btrfs Filesystem

Create the file-system for our logical volumes by using below command.

[root@linuxhelp ~]# mkfs.btrfs /dev/mapper/linuxhelp-lv1
SMALL VOLUME: forcing mixed metadata/data groups
btrfs-progs v3.19.1
See http://btrfs.wiki.kernel.org for more information.

Turning ON incompat feature 'mixed-bg': mixed data and metadata block groups
Turning ON incompat feature 'extref': increased hardlink limit per file to 65536
Turning ON incompat feature 'skinny-metadata': reduced-size metadata extent refs
Created a data/metadata chunk of size 8388608
fs created label (null) on /dev/mapper/linuxhelp-lv1
nodesize 4096 leafsize 4096 sectorsize 4096 size 500.00MiB

Next mount the file-system by issuing the following command.

[root@linuxhelp ~]# mount /dev/mapper/linuxhelp-lv1 /mnt

With the help of df command, verify the mount point.

[root@linuxhelp ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 3.8G 14G 22% /
devtmpfs 486M 0 486M 0% /dev
tmpfs 494M 140K 494M 1% /dev/shm
tmpfs 494M 7.1M 487M 2% /run
tmpfs 494M 0 494M 0% /sys/fs/cgroup
/dev/sda1 497M 116M 382M 24% /boot
/dev/sr0 3.9G 3.9G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/linuxhelp-lv1 500M 32K 496M 1% /mnt

To Add Devices 

Extend the mount point size /mnt/ or we can add the device to mount point, it will extend the size of file-system.

[root@linuxhelp ~]# btrfs device add /dev/mapper/linuxhelp-lv2 /mnt

Verify the mount point size using ‘df -h‘ command.

[root@linuxhelp ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 3.9G 14G 22% /
devtmpfs 486M 0 486M 0% /dev
tmpfs 494M 140K 494M 1% /dev/shm
tmpfs 494M 7.1M 487M 2% /run
tmpfs 494M 0 494M 0% /sys/fs/cgroup
/dev/sda1 497M 116M 382M 24% /boot
/dev/sr0 3.9G 3.9G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/linuxhelp-lv1 1000M 32K 995M 1% /mnt

Check the status of the newly added device(/mnt).

[root@linuxhelp ~]# btrfs device stats /mnt
[/dev/mapper/linuxhelp-lv1].write_io_errs 0
[/dev/mapper/linuxhelp-lv1].read_io_errs 0
[/dev/mapper/linuxhelp-lv1].flush_io_errs 0
[/dev/mapper/linuxhelp-lv1].corruption_errs 0
[/dev/mapper/linuxhelp-lv1].generation_errs 0
[/dev/mapper/linuxhelp-lv2].write_io_errs 0
[/dev/mapper/linuxhelp-lv2].read_io_errs 0
[/dev/mapper/linuxhelp-lv2].flush_io_errs 0
[/dev/mapper/linuxhelp-lv2].corruption_errs 0
[/dev/mapper/linuxhelp-lv2].generation_errs 0

In case to remove the logical volume “linuxhelp-lv2″ from mount point, use the following command.

[root@linuxhelp ~]# btrfs device delete /dev/mapper/linuxhelp-lv2 /mnt
[root@linuxhelp ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 3.9G 14G 22% /
devtmpfs 486M 0 486M 0% /dev
tmpfs 494M 140K 494M 1% /dev/shm
tmpfs 494M 7.1M 487M 2% /run
tmpfs 494M 0 494M 0% /sys/fs/cgroup
/dev/sda1 497M 116M 382M 24% /boot
/dev/sr0 3.9G 3.9G 0 100% /run/media/root/CentOS 7 x86_64
/dev/mapper/linuxhelp-lv1 500M 32K 496M 1% /mnt

Additional options in Btrfs

 To Check the disk usage of mount point

[root@linuxhelp ~]# btrfs filesystem df /mnt
System, single: total=4.00MiB, used=4.00KiB
Data+Metadata, single: total=8.00MiB, used=28.00KiB

 To List the btrfs filesystem

[root@linuxhelp ~]# btrfs filesystem show --mounted
Label: none uuid: 9055cf28-7e21-4fcd-9095-8199c2684f35
Total devices 1 FS bytes used 32.00KiB
devid 1 size 500.00MiB used 12.00MiB path /dev/mapper/linuxhelp-lv1
btrfs-progs v3.19.1

 To  Force a sync for the file-system.

[root@linuxhelp ~]# btrfs filesystem sync /mnt
FSSync '/mnt'

 To Defragment the files and directory metadata.

[root@linuxhelp ~]# btrfs filesystem defragment -v -r /mnt
btrfs-progs v3.19.1

-r - recursively defragment through directories.
-v - verbose.
-c - to compress the file contents while defragmenting.

To Check btrfs file-system using btrfsck command

[root@linuxhelp ~]# umount /mnt
[root@linuxhelp ~]# btrfsck /dev/mapper/linuxhelp-lv1
Checking filesystem on /dev/mapper/linuxhelp-lv1
UUID: 9055cf28-7e21-4fcd-9095-8199c2684f35
checking extents
checking free space cache
checking fs roots
checking csums
checking root refs
found 32768 bytes used err is 0
total csum bytes: 0
total tree bytes: 32768
total fs tree bytes: 8192
total extent tree bytes: 4096
btree space waste bytes: 27300
file data blocks allocated: 0
referenced 0
btrfs-progs v3.19.1

To Create the Btrfs Subvolumes

Create or delete the subvolume by using the following simple commands. Before creating subvolume list, check if any subvolume is existing.

[root@linuxhelp ~]# btrfs subvolume list /mnt/linuxhelp_btrfs1/

Use the below command to create subvolume.

[root@linuxhelp ~]# btrfs subvolume create /mnt/vol1
Create subvolume '/mnt/vol1'
[root@linuxhelp ~]# btrfs subvolume create /mnt/vol2
Create subvolume '/mnt/vol2'

List and check the newly created subvolumes.

[root@linuxhelp ~]# btrfs subvolume list /mnt
ID 256 gen 10 top level 5 path vol1
ID 257 gen 11 top level 5 path vol2

Navigate to mount point /mnt/linuxhelp_btrfs1/ and then list newly created subvolumes which have created in the above step.

[root@linuxhelp ~]# cd /mnt/vol1

Now copy some of the random files from /etc/ to subvol1.

[root@linuxhelp vol1]# cp /etc/*.conf .
[root@linuxhelp vol1]# ls
asound.conf e2fsck.conf ipsec.conf libuser.conf nfsmount.conf radvd.conf sos.conf vconsole.conf
brltty.conf fprintd.conf kdump.conf locale.conf nsswitch.conf request-key.conf sudo.conf wvdial.conf
chrony.conf fuse.conf krb5.conf logrotate.conf numad.conf resolv.conf sudo-ldap.conf yum.conf
colord.conf hba.conf ksmtuned.conf man_db.conf oddjobd.conf rsyncd.conf sysctl.conf
dnsmasq.conf host.conf ld.so.conf mke2fs.conf pbm2ppa.conf rsyslog.conf updatedb.conf
dracut.conf idmapd.conf libaudit.conf mtools.conf pnm2ppa.conf sestatus.conf usb_modeswitch.conf

To Mount the Subvolume

Use the following command to know the ID of subvolumes.

[root@linuxhelp ~]# btrfs subvolume list /mnt
ID 256 gen 13 top level 5 path vol1
ID 257 gen 11 top level 5 path vol2

Before mounting the subvolumes one by one to the parent volume, unmount the parent volume (/mnt/)

[root@linuxhelp ~]# umount /mnt

Now mount the subvolume using it’s ID to the parent volume.

[root@linuxhelp ~]# mount -o subvolid=256 /dev/mapper/linuxhelp-lv1 /mnt/

This will mount the subvol1 using it’s ID to /mnt/ Just list and check the mount point.

Use the following command to make the subvolume as default.

[root@linuxhelp ~]# btrfs subvolume set-default 256 /mnt

List the subvolume by using the following command.

[root@linuxhelp ~]# btrfs subvolume get-default /mnt
ID 256 gen 13 top level 5 path vol1

To Create Snapshots

To create a snapshot of source, use the below command. Here, we are taking snapshot of /mnt/ to the same location.

[root@linuxhelp ~]# btrfs subvolume snapshot /mnt /mnt
Create a snapshot of '/mnt' in '/mnt/mnt'

To create a snapshot with snap-test use the following command.

[root@linuxhelp ~]# btrfs subvolume snapshot /mnt /mnt/snap-test
Create a snapshot of '/mnt' in '/mnt/snap-test'
[root@linuxhelp ~]# ls /mnt/
asound.conf e2fsck.conf ipsec.conf libuser.conf mtools.conf pnm2ppa.conf sestatus.conf updatedb.conf
brltty.conf fprintd.conf kdump.conf locale.conf nfsmount.conf radvd.conf snap-test usb_modeswitch.conf
chrony.conf fuse.conf krb5.conf logrotate.conf nsswitch.conf request-key.conf sos.conf vconsole.conf
colord.conf hba.conf ksmtuned.conf man_db.conf numad.conf resolv.conf sudo.conf wvdial.conf
dnsmasq.conf host.conf ld.so.conf mke2fs.conf oddjobd.conf rsyncd.conf sudo-ldap.conf yum.conf
dracut.conf idmapd.conf libaudit.conf mnt pbm2ppa.conf rsyslog.conf sysctl.conf

We can read and write any contents in the above snapshots. To create a read-only snapshot use -r option as shown below.

[root@linuxhelp ~]# btrfs subvolume snapshot -r /mnt /mnt/read-only
Create a readonly snapshot of '/mnt' in '/mnt/read-only'

For Permanent Mounting

Use fstab entry for permanent mounting.

[root@linuxhelp ~]# vim /etc/fstab

Add the following line to fstab.

/dev/mapper/linuxhelp-lv1 /mnt btrfs defaults 0 0

 

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.

For more help topics browse our website www.linuxhelp.com

Be the first to comment, we value your suggestions. For further queries please comment below.

1 Comments

Leave a Response