一、回顾
1、lvm2
lvm:边界动态扩展或收缩;快照;
pv --> vg --> lv
PE:
LE:
2、dd:复制
3、关闭默认启动的图形界面
[root@node1 ~]# systemctl set-default multi-user.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
二、btrfs文件系统:(技术预览版)
1、Btrfs(B-tree,Butter FS,Better FS),GPL,Oracle从2007年左右开始研发,使用写时复制机制(COW);其设计就是为了用来取代ext3/ext4的
2、核心特性
a、多物理卷支持:btrfs可由多个底层物理卷组成:支持RAID,以联机“添加”,“移除”,“修改”
b、写时复制更新机制(CoW):复制,更新及替换指针,而非“就地”更新
c、数据及元数据校验码:checksum ,自动检测文件是否受损,一旦受损就会尝试自动修复。
d、支持子卷:sub_volume,在一个卷上创建多个子卷,把每一个子卷进行单独挂载。
e、快照:支持快照的快照,也就是增量快照机制
f、透明压缩:
三、相应命令
1、创建btrfs :mkfs.btrfs
-L 'LABEL':指定卷标
-m
-d
-O
[root@node1 ~]# mkfs.btrfs -O list-all #列出所支持的所有特性 Filesystem features available: mixed-bg - mixed data and metadata block groups (0x4, compat=2.6.37, safe=2.6.37) extref - increased hardlink limit per file to 65536 (0x40, compat=3.7, safe=3.12, default=3.12) raid56 - raid56 extended format (0x80, compat=3.9) skinny-metadata - reduced-size metadata extent refs (0x100, compat=3.10, safe=3.18, default=3.18) no-holes - no explicit hole extents for files (0x200, compat=3.14, safe=4.0)
2、演示btrfs的使用
a、创建btrfs文件系统
[root@node1 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─centos-root 253:0 0 17G 0 lvm / └─centos-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 10G 0 disk sdc 8:32 0 10G 0 disk sdd 8:48 0 10G 0 disk sr0 11:0 1 4.2G 0 rom [root@node1 ~]# mkfs.btrfs -L mydata -f /dev/sdb /dev/sdc btrfs-progs v4.9.1 See http://btrfs.wiki.kernel.org for more information. Label: mydata UUID: 668a05e4-fbb7-4815-a61c-2e15b860fa26 Node size: 16384 Sector size: 4096 Filesystem size: 20.00GiB Block group profiles: Data: RAID0 2.00GiB Metadata: RAID1 1.00GiB System: RAID1 8.00MiB SSD detected: no Incompat features: extref, skinny-metadata Number of devices: 2 Devices: ID SIZE PATH 1 10.00GiB /dev/sdb 2 10.00GiB /dev/sdc
b、查看已经创建的btrfs文件系统
[root@node1 ~]# btrfs filesystem show Label: 'mydata' uuid: 668a05e4-fbb7-4815-a61c-2e15b860fa26 Total devices 2 FS bytes used 112.00KiB devid 1 size 10.00GiB used 2.01GiB path /dev/sdb devid 2 size 10.00GiB used 2.01GiB path /dev/sdc
此时我们会发现我们的/dev/sdb和/dev/sdc的uuid和我们创建的btrfs文件系统是一样的,但是他的子卷的UUID是不一样的,UUID_SUB表示的是每一个物理子卷的物理属性的UUID。
[root@node1 ~]# blkid /dev/sdb /dev/sdb: LABEL="mydata" UUID="668a05e4-fbb7-4815-a61c-2e15b860fa26" UUID_SUB="052552b0-1fa3-4e95-a060-9e8ee8ef2599" TYPE="btrfs" [root@node1 ~]# blkid /dev/sdc /dev/sdc: LABEL="mydata" UUID="668a05e4-fbb7-4815-a61c-2e15b860fa26" UUID_SUB="e3ce8b73-ea60-4579-b1a7-ddc85b5a9a91" TYPE="btrfs"
c、文件系统属性查看:btrfs filesystem show命令
[root@node1 ~]# btrfs filesystem show --mounted #显示已经挂载的 [root@node1 ~]# btrfs filesystem show --all-device#显示所有的btrfs文件系统 Label: 'mydata' uuid: 668a05e4-fbb7-4815-a61c-2e15b860fa26 Total devices 2 FS bytes used 112.00KiB devid 1 size 10.00GiB used 2.01GiB path /dev/sdb devid 2 size 10.00GiB used 2.01GiB path /dev/sdc [root@node1 ~]# btrfs filesystem show /dev/sdb #显示/dev/sdb所在的btrfs文件系统的信息 Label: 'mydata' uuid: 668a05e4-fbb7-4815-a61c-2e15b860fa26 Total devices 2 FS bytes used 112.00KiB devid 1 size 10.00GiB used 2.01GiB path /dev/sdb devid 2 size 10.00GiB used 2.01GiB path /dev/sdc
d、btrfs文件系统的挂载f和卸载
[root@node1 ~]# mkdir /mydata [root@node1 ~]# mount -t btrfs /dev/sdb /mydata/ #挂载任意一个磁盘即可 [root@node1 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 17G 4.8G 13G 29% / devtmpfs devtmpfs 982M 0 982M 0% /dev tmpfs tmpfs 993M 0 993M 0% /dev/shm tmpfs tmpfs 993M 8.7M 984M 1% /run tmpfs tmpfs 993M 0 993M 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 127M 888M 13% /boot tmpfs tmpfs 199M 0 199M 0% /run/user/0 /dev/sdb btrfs 20G 17M 18G 1% /mydata
[root@node1 ~]# umount /mydata/ [root@node1 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 17G 4.8G 13G 29% / devtmpfs devtmpfs 982M 0 982M 0% /dev tmpfs tmpfs 993M 0 993M 0% /dev/shm tmpfs tmpfs 993M 8.7M 984M 1% /run tmpfs tmpfs 993M 0 993M 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 127M 888M 13% /boot tmpfs tmpfs 199M 0 199M 0% /run/user/0
e、使用透明压缩机制,相当于数据在存放时会首先进行压缩再存放至挂载的文件系统中,不过这个过程我们是不感知的。
mount -o compress={lzo|zlib} DEVICE MOUNT_POINT
[root@node1 ~]# mount -o compress=lzo /dev/sdb /mydata/ [root@node1 ~]# cp /etc/rc.d/init.d/functions /mydata/ [root@node1 ~]# ll /mydata/ total 20 -rw-r--r-- 1 root root 17500 Feb 1 20:18 functions
f、在线调整btrfs文件系统的大小,要调整到最大可以使用 resize max参数。
[root@node1 ~]# btrfs filesystem resize -5G /mydata/ [root@node1 ~]# df -hT /mydata/ #可以看到减小了5G Filesystem Type Size Used Avail Use% Mounted on /dev/sdb btrfs 15G 18M 8.0G 1% /mydata
[root@node1 ~]# btrfs filesystem resize +5G /mydata/ Resize '/mydata/' of '+5G' [root@node1 ~]# df -hT /mydata/ #可以看到增加了5G Filesystem Type Size Used Avail Use% Mounted on /dev/sdb btrfs 20G 18M 18G 1% /mydata
g、真正的占据的空间要通过其自带的命令查看
[root@node1 ~]# btrfs filesystem df /mydata/ Data, RAID0: total=2.00GiB, used=780.00KiB #数据默认是RAID0 System, RAID1: total=8.00MiB, used=16.00KiB Metadata, RAID1: total=1.00GiB, used=112.00KiB 元数据默认是RAID1 GlobalReserve, single: total=16.00MiB, used=0.00B
3、btrfs device:btrfs操作设备。
a、btrfs device add,添加一块设备到挂载的文件系统上
[root@node1 ~]# btrfs device add /dev/sdd /mydata/ [root@node1 ~]# df -hT /mydata/ #可以看到大小也自动增加了 Filesystem Type Size Used Avail Use% Mounted on /dev/sdb btrfs 30G 18M 26G 1% /mydata
b、将开始的两块硬盘上的数据均衡到/dev/sdd上
[root@node1 ~]# btrfs balance status /mydata #查看均衡的状态,这个只有在均衡的过程中才能查看到 No balance found on '/mydata' [root@node1 ~]# btrfs balance start /mydata #开始均衡 WARNING: Full balance without filters requested. This operation is very intense and takes potentially very long. It is recommended to use the balance filters to narrow down the balanced data. Use 'btrfs balance start --full-balance' option to skip this warning. The operation will start in 10 seconds. Use Ctrl-C to stop it. 10 9 8 7 6 5 4 3 2 1 Starting balance without any filters. Done, had to relocate 3 out of 3 chunks #可以看到只是均衡了3个chunks
如果正在均衡的时候想暂停下来可以使用pause,如果想要继续可以使用resume
[root@node1 ~]# btrfs balance pause /mydata
[root@node1 ~]# btrfs balance resume /mydata
c、移除某一个物理卷
[root@node1 ~]# btrfs device delete /dev/sdb /mydata/ #删除/dev/sdb [root@node1 ~]# df -lhT /mydata/ Filesystem Type Size Used Avail Use% Mounted on /dev/sdc btrfs 20G 18M 20G 1% /mydata [root@node1 ~]# btrfs filesystem show /mydata/ Label: 'mydata' uuid: 668a05e4-fbb7-4815-a61c-2e15b860fa26 Total devices 2 FS bytes used 716.00KiB devid 2 size 10.00GiB used 1.28GiB path /dev/sdc devid 3 size 10.00GiB used 1.28GiB path /dev/sdd
4、修改数据的组织机制,即RAID级别
[root@node1 ~]# btrfs device add /dev/sdb /mydata/ [root@node1 ~]# btrfs balance start -dconvert=raid5 /mydata/ #修改数据存储类型为raid5 Done, had to relocate 1 out of 3 chunks [root@node1 ~]# btrfs balance start -mconvert=raid5 /mydata/ #修改元数据存储类型为raid5 Done, had to relocate 2 out of 3 chunks
[root@node1 ~]# btrfs filesystem df /mydata/ Data, RAID5: total=2.00GiB, used=588.00KiB System, RAID5: total=64.00MiB, used=16.00KiB Metadata, RAID5: total=256.00MiB, used=112.00KiB GlobalReserve, single: total=16.00MiB, used=0.00B
5、子卷操作
a、创建子卷
[root@node1 ~]# btrfs subvolume create /mydata/logs Create subvolume '/mydata/logs'
b、列出子卷
[root@node1 ~]# btrfs subvolume list /mydata/ ID 264 gen 78 top level 5 path logs #可以看到子卷对应的子卷id为264
c、挂载子卷,当父卷挂载后子卷会自动挂载。现在我们只挂载子卷
[root@node1 ~]# df -hT /mydata/ Filesystem Type Size Used Avail Use% Mounted on /dev/sdc btrfs 30G 17M 29G 1% /mydata [root@node1 ~]# umount /mydata/ [root@node1 ~]# mount -o subvol=logs /dev/sdb /mnt/ #需要通过-o参数指定子卷的名称 [root@node1 ~]# df -hT /mnt/ Filesystem Type Size Used Avail Use% Mounted on /dev/sdc btrfs 30G 17M 29G 1% /mnt
d、显示子卷的详细信息
[root@node1 ~]# btrfs subvolume show /mnt /mnt Name: logs UUID: 46b6fe7e-e57f-d441-afd1-afd06067817b Parent UUID: - Received UUID: - Creation time: 2020-02-01 21:35:37 +0800 Subvolume ID: 264 Generation: 78 Gen at creation: 78 Parent ID: 5 Top level ID: 5 Flags: - Snapshot(s):
e、现在我们复制/etc/fstab文件到/mnt路径下,然后再挂载我们的父卷到/mydata下,可以看到依然可以在/mydata/logs路径中看到此文件
[root@node1 ~]# cp /etc/fstab /mnt/ [root@node1 ~]# mount /dev/sdb /mydata/ [root@node1 ~]# ls -l /mydata/logs/ total 4 -rw-r--r-- 1 root root 465 Feb 1 21:46 fstab
f、也可以通过子卷id进行挂载
[root@node1 ~]# btrfs subvolume list /mydata/ ID 264 gen 81 top level 5 path logs ID 265 gen 79 top level 5 path cache [root@node1 ~]# umount /mnt/ [root@node1 ~]# mount -o subvolid=265 /dev/sdb /mnt/ [root@node1 ~]# df -hT /mnt/ Filesystem Type Size Used Avail Use% Mounted on /dev/sdc btrfs 30G 17M 29G 1% /mnt
g、删除子卷
[root@node1 ~]# btrfs subvolume delete /mydata/logs/ Delete subvolume (no-commit): '/mydata/logs'
[root@node1 ~]# btrfs subvolume list /mydata/ ID 265 gen 82 top level 5 path cache
6、创建子卷快照
a、首先我们还是创建子卷/mydata/logs,然后复制文件到其中。(子卷的快照必须与子卷在同一个父卷中)
[root@node1 ~]# btrfs subvolume create /mydata/logs Create subvolume '/mydata/logs' [root@node1 ~]# cp /etc/grub2.cfg /mydata/logs/
b、然后我们创建该子卷的快照
[root@node1 ~]# btrfs subvolume snapshot /mydata/logs /mydata/logs_snapshot Create a snapshot of '/mydata/logs' in '/mydata/logs_snapshot' [root@node1 ~]# btrfs subvolume list /mydata/ ID 265 gen 82 top level 5 path cache ID 266 gen 86 top level 5 path logs ID 267 gen 86 top level 5 path logs_snapshot
[root@node1 ~]# ls -l /mydata/logs_snapshot/ total 8 -rw-r--r-- 1 root root 4253 Feb 1 22:03 grub2.cfg
c、然后我们给原子卷中grub2.cfg文件添加内容后查看快照卷中grub2.cfg文件内容是否也被添加
[root@node1 ~]# echo "How are you,snapshot" >> /mydata/logs/grub2.cfg [root@node1 ~]# tail -1 /mydata/logs_snapshot/grub2.cfg #可以看到并没有添加 ### END /etc/grub.d/41_custom ###
d、删除快照卷
[root@node1 ~]# btrfs subvolume delete /mydata/logs_snapshot/ Delete subvolume (no-commit): '/mydata/logs_snapshot'
e、我们还可以对单个文件创建快照
[root@node1 logs]# cp --reflink grub2.cfg grub2.cfg_snap
7、将ext3或ext4转换为btrfs文件系统
a、首先将数据存储格式调整为single和元数据存储格式调整为raid1
[root@node1 logs]# btrfs balance start -dconvert=single /mydata/ Done, had to relocate 1 out of 3 chunks [root@node1 logs]# btrfs balance start -mconvert=raid1 /mydata/ Done, had to relocate 2 out of 3 chunks
b、接下来我们干掉一块磁盘
[root@node1 logs]# btrfs device delete /dev/sdd /mydata/
c、然后我们将/dev/sdd格式化为ext4文件系统并挂载至/mnt,然后复制/etc/fstab文件到/mnt路径中
d、接下来我们开始转换
首先我们先卸载/dev/sdd1 然后fsck一下 /dev/sdd1
[root@node1 logs]# umount /mnt/ [root@node1 logs]# fsck -f /dev/sdd1 fsck from util-linux 2.23.2 e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/sdd1: 12/655360 files (0.0% non-contiguous), 83138/2621184 blocks
然后我们使用btrfs-convert 命令进行转换
[root@node1 logs]# btrfs-convert /dev/sdd1 create btrfs filesystem: blocksize: 4096 nodesize: 16384 features: extref, skinny-metadata (default) creating ext2 image file creating btrfs metadatacopy inodes [o] [ 0/ 12] conversion complete
此时我们查看相应的btrfs文件系统可以发现增加了/dev/sdd1
[root@node1 logs]# btrfs filesystem show Label: 'mydata' uuid: 668a05e4-fbb7-4815-a61c-2e15b860fa26 Total devices 2 FS bytes used 504.00KiB devid 2 size 10.00GiB used 288.00MiB path /dev/sdc devid 4 size 10.00GiB used 1.28GiB path /dev/sdb Label: none uuid: faed6308-9c33-44cc-947a-6781272e5121 Total devices 1 FS bytes used 325.27MiB devid 1 size 10.00GiB used 583.66MiB path /dev/sdd1
然后我们开始挂载并使用
[root@node1 logs]# mount /dev/sdd1 /mnt/ [root@node1 logs]# df -hTl /mnt/ Filesystem Type Size Used Avail Use% Mounted on /dev/sdd1 btrfs 10G 342M 9.7G 4% /mnt [root@node1 logs]# cat /mnt/fstab # # /etc/fstab # Created by anaconda on Tue Jun 4 22:30:32 2019 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=fd514a18-84be-4460-8130-706b3ec88673 /boot xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
e、我们还可以将其降回ext4文件系统
[root@node1 /]# umount /mnt/ [root@node1 /]# btrfs-convert -r /dev/sdd1 rollback complete [root@node1 /]# blkid /dev/sdd1 /dev/sdd1: UUID="6f62ede6-7c16-4da3-976b-4cc716b61afb" TYPE="ext4"