The Btrfs file system for Linux learning

Source: Internet
Author: User
Tags uuid

Btrfs is a very powerful file system on the Linux system, full name: B-tree, was developed by Oracle in 2007 based on the GPL agreement. So how do we create and use it for Btrfs file system?

Below we will study it in the experiment with the CENTOS7 system.

First, understand the characteristics of Btrfs

Btrfs file system has a lot of features, and here's a few of its core features:

1, multi-Physical volume support: Btrfs can be composed of multiple underlying physical volumes, RAID support to Online "add", "Remove", "modify".

2, copy update mechanism: Copy, update and replace pointers, rather than "in-place" update;

3, data and meta-data check code: Checksum

4, Support sub-volume: Can create sub-volume under Btrfs file system

5. Support Snapshot

6, transparent compression: refers to the storage of the file is compressed form, and for the user we do not know

Ii. How to create a Btrfs file system

Create a Btrfs file system first we need multiple disks, in the virtual machine we want to add a few disks

[[email protected] ~]# fdisk -l

 

磁盘 /dev/sda:128.8 GB, 128849018880 字节,251658240 个扇区

Units = 扇区 of 1 * 512 = 512 bytes

扇区大小(逻辑/物理):512 字节 / 512 字节

I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘标签类型:dos

磁盘标识符:0x000abcfd

 

设备 Boot Start End Blocks Id System

/dev/sda1 * 2048 1026047 512000 83 Linux

/dev/sda2 1026048 251658239 125316096 8e Linux LVM

 

磁盘 /dev/sdc:21.5 GB, 21474836480 字节,41943040 个扇区

Units = 扇区 of 1 * 512 = 512 bytes

扇区大小(逻辑/物理):512 字节 / 512 字节

I/O 大小(最小/最佳):512 字节 / 512 字节

 

 

磁盘 /dev/sdd:21.5 GB, 21474836480 字节,41943040 个扇区

Units = 扇区 of 1 * 512 = 512 bytes

扇区大小(逻辑/物理):512 字节 / 512 字节

I/O 大小(最小/最佳):512 字节 / 512 字节

 

 

磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区

Units = 扇区 of 1 * 512 = 512 bytes

扇区大小(逻辑/物理):512 字节 / 512 字节

I/O 大小(最小/最佳):512 字节 / 512 字节

As I added 3 disks, the SDB, SDC, and SDD respectively gave them a space of 20G.

[[email protected] ~]# btrfs <==============点击2次TAB键可以看出以下好多命令和btrfs文件系统有关的命令

btrfs btrfs-debug-tree btrfs-map-logical btrfs-zero-log

btrfsck btrfs-find-root btrfs-show-super

btrfs-convert btrfs-image btrfstune

[[email protected] ~]# btrfs

We'll just talk about the common commands of Btrfs file system:

1. Mkfs.btrfs: Create Btrfs file system

-L "LABEL": Specify Volume label

-D <type>: Specifies the type of data store, such as: Raid0,raid1,raid5,raid10,single

-M: Indicates that the metadata is a specified storage

-O <feature>: Indicates some options

-O List-all: Lists all supported feature

[[email protected] ~]# mkfs.btrfs -L mydata /dev/sdc /dev/sdd <===把dev下的sdc和sdd两块磁盘创建成一个

Btrfs v3.16.2 名为mydata的btrfs文件系统

See http://btrfs.wiki.kernel.org for more information.

 

Turning ON incompat feature ‘extref‘: increased hardlink limit per file to 65536

adding device /dev/sdd id 2

fs created label mydata on /dev/sdc

nodesize 16384 leafsize 16384 sectorsize 4096 size 40.00GiB


2. See if Btrfs created a successful use command: Btrfs filesystem show as follows

Label: ‘mydata‘ uuid: 179e892a-aa8b-4f25-825d-c0a87d99096d

Total devices 2 FS bytes used 612.00KiB

devid 1 size 15.00GiB used 1.28GiB path /dev/sdc

devid 2 size 20.00GiB used 1.28GiB path /dev/sdd

 

Btrfs v3.16.2

Note: When we created the filesystem, we specified two disks, and the path on our side uses/DEV/SDC and the/DEV/SDD effect is the same.

3, we above the Btrfs file system created by the volume labeled MyData, we use the command to see if the volume label is the MyData we created

[[email protected] ~]# btrfs filesystem label /dev/sdc

mydata

4, if we have created a Btrfs file system and view the edge of the command, then how to mount and use this file system?

[[email protected] ~]# mkdir /mydata <====创建目录mydata

[[email protected] ~]# mount -t btrfs /dev/sdc /mydata/ <====将之前创建的btrfs文件系统挂载到mydata下

[root[email protected] ~]# df -h <==== using the DF command to see if the mount was successful

文件系统 容量 已用 可用 已用% 挂载点

/dev/mapper/centos-root 50G 6.9G 44G 14% /

devtmpfs 481M 0 481M 0% /dev

tmpfs 490M 80K 490M 1% /dev/shm

tmpfs 490M 7.1M 483M 2% /run

tmpfs 490M 0 490M 0% /sys/fs/cgroup

/dev/mapper/centos-home 68G 44M 68G 1% /home

/dev/sda1 497M 126M 371M 26% /boot

/dev/sdc 35G 1.2M 30G 1% /mydata <==== so you can see the btrfs we created

File system is mounted.

[[email protected] ~]# cd /mydata/ <======切换到mydata目录下

[[email protected] mydata]# touch a.txt <=====创建一个文件

[[email protected] mydata]# ll <=====查看文件,这样我们的btrfs文件系统就可以使用了

总用量 0

-rw-r--r--. 1 root root 0 8月 29 21:08 a.txt

drwxr-xr-x. 1 root root 96 8月 28 10:56 logs

If you uninstall, use the Umount command directly with the partition path.

5, Btrfs file system can also achieve transparent compression, then what is transparent compression it. is when we create the Btrfs file system, the system will automatically compress the background and we do not see this side, then how to achieve compression? Let's show the following

Usage: Mount-o compress={lzo|zlib} DEVICE mount_point

[[email protected] ~]# umount /mydata/ <====== uninstalling the file system first

[[email protected] ~]# mount -o compress=lzo /dev/sdc /mydata <====== in transparent compression mount

6, the implementation of the specified Btrfs file system size adjustment

[email protected] ~]# btrfs filesystem resize -10G /mydata <====== reduce 10G to MyData first

Resize ‘/mydata‘ of ‘-10G‘

[[email protected] ~]# df -lh <===== viewing the file system capacity

文件系统 容量 已用 可用 已用% 挂载点

/dev/mapper/centos-root 50G 6.9G 44G 14% /

devtmpfs 481M 0 481M 0% /dev

tmpfs 490M 80K 490M 1% /dev/shm

tmpfs 490M 7.1M 483M 2% /run

tmpfs 490M 0 490M 0% /sys/fs/cgroup

/dev/mapper/centos-home 68G 44M 68G 1% /home

/dev/sda1 497M 126M 371M 26% /boot

/dev/sdc 25G 1.3M 9.5G 1% /mydata<===, the capacity we're mounting here is reduced.

[[email protected] ~]# btrfs filesystem resize +5G /mydata<==== then we add 5G to MyData.

Resize ‘/mydata‘ of ‘+5G‘

[[email protected] ~]# df -lh

文件系统 容量 已用 可用 已用% 挂载点

/dev/mapper/centos-root 50G 6.9G 44G 14% /

devtmpfs 481M 0 481M 0% /dev

tmpfs 490M 80K 490M 1% /dev/shm

tmpfs 490M 7.1M 483M 2% /run

tmpfs 490M 0 490M 0% /sys/fs/cgroup

/dev/mapper/centos-home 68G 44M 68G 1% /home

/dev/sda1 497M 126M 371M 26% /boot

/dev/sdc 30G 1.3M 20G 1% /mydata<===== here to see the capacity is increased

Of course, if we want to increase and decrease the capacity not only to use the method of increasing capacity, but also to reduce the physical volume by adding goods, such as:

[[email protected] ~]# btrfs device add /dev/sdb /mydata/<=====将物理磁盘sdb加入到mydata文件系统

[[email protected] ~]# btrfs balance start /mydata<===== for balanced operation (data is split evenly for each piece of equipment)

Done, had to relocate 3 out of 3 chunks

[[email protected] ~]# df -lh<===== look at the size of the MyData, you can see that the size increases

文件系统 容量 已用 可用 已用% 挂载点

/dev/mapper/centos-root 50G 6.9G 44G 14% /

devtmpfs 481M 0 481M 0% /dev

tmpfs 490M 80K 490M 1% /dev/shm

tmpfs 490M 7.1M 483M 2% /run

tmpfs 490M 0 490M 0% /sys/fs/cgroup

/dev/mapper/centos-home 68G 44M 68G 1% /home

/dev/sda1 497M 126M 371M 26% /boot

/dev/sdc 50G 1.3M 40G 1% /mydata

[[email protected] ~]# btrfs device delete /dev/sdb /mydata/<===== deleting SDB

[[email protected] ~]# df -lh<===== See MyData capacity again, here you can see the reduction

文件系统 容量 已用 可用 已用% 挂载点

/dev/mapper/centos-root 50G 6.9G 44G 14% /

devtmpfs 481M 0 481M 0% /dev

tmpfs 490M 80K 490M 1% /dev/shm

tmpfs 490M 7.1M 483M 2% /run

tmpfs 490M 0 490M 0% /sys/fs/cgroup

/dev/mapper/centos-home 68G 44M 68G 1% /home

/dev/sda1 497M 126M 371M 26% /boot

/dev/sdc 30G 1.8M 20G 1% /mydata

7. Change the file system data and the level of the meta-data raid

[[email protected] ~]# btrfs balance start -mconvert=raid1 /mydata 指明元数据的为RAID1模式

Done, had to relocate 2 out of 3 chunks

[[email protected] ~]# btrfs balance start -dconvert=raid1 /mydata 指明数据位RAID1模式

Done, had to relocate 1 out of 3 chunks


Note: We only need 2 physical partitions when we specify RAID1 and 0, if you need to build RAID5, you need at least 3 physical disks

8. Management of sub-volumes in Btrfs file system

(1), Sub-volume creation

[[email protected] ~]# btrfs subvolume create /mydata/logss <===== creating LOGSS under the MyData file system

Create subvolume ‘/mydata/logss‘

[[email protected] ~]# btrfs subvolume list /mydata/<===== View MyData All the sub-volumes, log is the one I created earlier

ID 262 gen 108 top level 5 path logs

ID 267 gen 114 top level 5 path logss

Note: If the parent volume is mounted, the child volume is automatically mounted on the file system. Of course, the sub-volume is also mounted separately, if you want to uninstall the parent volume first

(2), sub-volume separate mount

For example, if we want to mount the LOGSS Sub-volume separately, do the following:

Unmount the parent volume first

[[email protected] ~]# mount -o subvol=logss /dev/sdc /mnt <===== to mount the sub-volume to the MNT directory

[[email protected] ~]# cd /mnt/

[[email protected] mnt]# ll

总用量 0

[[email protected] mnt]# cp /etc/fstab /mnt/ <=====拷贝一个文件到mnt下

[[email protected] mnt]# ll /mnt/

总用量 4

-rw-r--r--. 1 root root 541 8月 29 22:33 fstab

[[email protected] mnt]# cd

[[email protected] ~]# umount /mnt/ <===== unloading a sub-volume

[[email protected] ~]# mount /dev/sdc /mydata/ <===== to mount the parent volume again

[[email protected] ~]# cd /mydata/logss/ <===== into the sub-volume

[[email protected] logss]# ll <===== found out that the files we copied were still there.

总用量 4

-rw-r--r--. 1 root root 541 8月 29 22:33 fstab

(3), delete sub-volume

How to delete a sub-volume using the command: Btrfs subvolume Delete

[email protected] ~]# btrfs subvolume delete /mydata/logss<===== Deleting a sub-volume LOGSS

Transaction commit: none (default)

Delete subvolume ‘/mydata/logss‘

[[email protected] ~]# ll /mydata/ <===== viewing the contents of the parent volume is no longer logss.

总用量 16

-rw-r--r--. 1 root root 0 8月 29 21:08 a.txt

-rw-r--r--. 1 root root 13430 8月 29 21:43 functions

drwxr-xr-x. 1 root root 96 8月 28 10:56 logs

(4), create a snapshot of the sub-volume

<===== Create a sub-volume first LOGSS

Create subvolume ‘/mydata/logss‘

[[email protected] ~]# btrfs subvolume snapshot /mydata/logss /mydata/logss_snapshot<===== Create a snapshot of a sub-volume named Logss--snapshot Create a snapshot of '/mydata/logss ' in '/mydata/logss_snapshot '

[[email protected] ~]# btrfs subvolume list /mydata/ <===== View MyData Another sub-volume, this is the snapshot we created

ID 262 gen 108 top level 5 path logs

ID 267 gen 124 top level 5 path logss

ID 268 gen 124 top level 5 path logss_snapshot

9. How to convert Btrfs file system to ext file system

First, you need to create a new partition, create a EXT4 filesystem on the new partition, and I won't do the demo, just create the new partition as the Ext4 file system.

[[email protected] ~]# mke2fs -t ext4 /dev/sdb1 将sdb1创建成ext4的文件系统

mke2fs 1.42.9 (28-Dec-2013)

文件系统标签=

OS type: Linux

块大小=4096 (log=2)

分块大小=4096 (log=2)

Stride=0 blocks, Stripe blocks

655360 inodes, 2621440 blocks

131072 blocks (5.00%) reserved for the super user

第一个数据块=0

Maximum filesystem blocks=2151677952

80 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

 

Allocating group tables: 完成

正在写入inode表: 完成

Creating journal (32768 blocks): 完成

Writing superblocks and filesystem accounting information: 完成


[[email protected] ~]# mount /dev/sdb1 /mnt/ 将ext4的文件系统挂载到mnt目录下

[[[email protected] ~]# cp/etc/fstab/mnt/copy of the fstab to the MNT directory under the ETC directory

[[email protected] ~]# ll /mnt/ 查看mnt下的文件

总用量 20

-rw-r--r--. 1 root root 541 8月 29 23:35 fstab

drwx------. 2 root root 16384 8月 29 23:33 lost+found

[[email protected] ~]# umount /mnt/ 卸载ext4的文件系统

[[email protected] ~]# fsck -f /dev/sdb1 对sdb1分区进行检测

fsck,来自 util-linux 2.23.2

e2fsck 1.42.9 (28-Dec-2013)

第一步: 检查inode,块,和大小

第二步: 检查目录结构

第3步: 检查目录连接性

Pass 4: Checking reference counts

第5步: 检查簇概要信息

/dev/sdb1: 12/655360 files (0.0% non-contiguous), 83138/2621440 blocks

[[email protected] ~]# btrfs-convert /dev/sdb1 将sdb1的文件系统转化成btrfs文件系统

creating btrfs metadata.

creating ext2fs image file.

cleaning up system chunk.

conversion complete. 转化完成

[[email protected] ~]# btrfs filesystem show /dev/sdb1 查看sdb1的属性

Label: none uuid: c8db9d69-3e6a-4c99-835e-6e109b3a0169

Total devices 1 FS bytes used 324.80MiB

devid 1 size 10.00GiB used 10.00GiB path /dev/sdb1

 

Btrfs v3.16.2

[[email protected] ~]# mount /dev/sdb1 /mnt 挂载btrfs文件系统

[[email protected] ~]# ll /mnt 查看fstab文件和之前一致

总用量 8

drwxr-xr-x. 1 root root 10 8月 29 23:37 ext2_saved

-rw-r--r--. 1 root root 541 8月 29 23:35 fstab

drwx------. 1 root root 0 8月 29 23:33 lost+found

[[email protected] ~]# umount /mnt/ 卸载mnt目录上的文件系统

[[email protected] ~]# btrfs-convert -r /dev/sdb1 将btrfs降级为ext4文件系统

rollback complete. 提示降级成功

[[email protected] ~]# blkid /dev/sdb1 查看sdb1的信息,为ext4文件系统降级完成

/dev/sdb1: UUID="84965289-5f82-4fe3-b0e7-6160726d7a25" TYPE="ext4"

[[email protected] ~]# btrfs-convert /dev/sdb1 再次升级为btrfs文件系统

creating btrfs metadata.

creating ext2fs image file.

cleaning up system chunk.

conversion complete.

[[email protected] ~]# blkid /dev/sdb1

/dev/sdb1: UUID="e582e4e2-a343-4d20-ba0e-dda43a3a90e5" UUID_SUB="95c8bc9b-ae3e-4989-904f-753781a83fab" TYPE="btrfs"

[[email protected] ~]#

Third, summary


All the content described above, just my summary of learning, which may be some of the shortcomings and the wrong place, I hope friends more guidance.

The Btrfs file system for Linux learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.