在CentOS7上安装ZFS

ZFS是Linux上非常流行的文件系统。它是一个128位的文件系统。这意味着ZFS可以变得非常大。它支持逻辑卷,raid,快照和许多其他高级文件系统功能。但让ZFS在CentOS 7上运行并不是那么简单。在本文中,我将向您展示如何在CentOS 7.4上安装和执行ZFS的基本配置。

安装ZFS文件系统

在CentOS 7上默认不启用ZFS文件系统支持,而且ZFS在CentOS 7的官方软件包存储库中是不可用,必须从ZFS的官方软件包存储库中安装它。可以查看这里了解更多详情。

首先检查一下CentOS 7的版本:

1
2
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core)

然后使用一下命令添加ZFS的官方存储库:

1
yum install http://download.zfsonlinux.org/epel/zfs-release.el7_4.noarch.rpm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Loaded plugins: fastestmirror
zfs-release.el7_4.noarch.rpm | 5.2 kB 00:00:00
Examining /var/tmp/yum-root-YssJaZ/zfs-release.el7_4.noarch.rpm: zfs-release-1-5.el7_4.noarch
Marking /var/tmp/yum-root-YssJaZ/zfs-release.el7_4.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package zfs-release.noarch 0:1-5.el7_4 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================================================================================
Installing:
zfs-release noarch 1-5.el7_4 /zfs-release.el7_4.noarch 2.9 k

Transaction Summary
=====================================================================================================================================================================================================
Install 1 Package

Total size: 2.9 k
Installed size: 2.9 k
Is this ok [y/d/N]:
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : zfs-release-1-5.el7_4.noarch 1/1
Verifying : zfs-release-1-5.el7_4.noarch 1/1

Installed:
zfs-release.noarch 0:1-5.el7_4

Complete!

ZFS模块可以通过两种方式加载到内核,DKMS和kABI。它们之间的区别在于,如果您安装基于DKMS的ZFS模块,然后由于某种原因更新了操作系统的内核,则必须再次重新编译ZFS内核模块。否则它将无法工作。但是基于kABI的ZFS模块的优势在于,如果更新操作系统的内核,则不需要重新编译。

在本文中,我将安装基于DKMS的ZFS内核模块(由于安装基于kABI时报错,google未果,因此放弃此安装方式)。

在CentOS 7上安装ZFS存储库时,默认情况下会启用基于DKMS的存储库。

DKMS安装方式需要先安装dkms

1
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Loaded plugins: fastestmirror
epel-release-latest-7.noarch.rpm | 15 kB 00:00:00
Examining /var/tmp/yum-root-YssJaZ/epel-release-latest-7.noarch.rpm: epel-release-7-11.noarch
Marking /var/tmp/yum-root-YssJaZ/epel-release-latest-7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================
Installing:
epel-release noarch 7-11 /epel-release-latest-7.noarch 24 k

Transaction Summary
=============================================================================================================================================================
Install 1 Package

Total size: 24 k
Installed size: 24 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : epel-release-7-11.noarch 1/1
Verifying : epel-release-7-11.noarch 1/1

Installed:
epel-release.noarch 0:7-11

Complete!

安装dkms:

1
yum install dkms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 8.9 kB 00:00:00
epel | 3.2 kB 00:00:00
extras | 3.4 kB 00:00:00
mysql-connectors-community | 2.5 kB 00:00:00
mysql-tools-community | 2.5 kB 00:00:00
mysql56-community | 2.5 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/3): epel/x86_64/group_gz | 88 kB 00:00:00
(2/3): epel/x86_64/updateinfo | 948 kB 00:00:00
(3/3): epel/x86_64/primary | 3.6 MB 00:00:01
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* epel: mirror01.idc.hinet.net
* extras: mirrors.163.com
* updates: mirrors.163.com
epel 12723/12723
Resolving Dependencies
--> Running transaction check
---> Package dkms.noarch 0:2.6.1-1.el7 will be installed
--> Processing Dependency: elfutils-libelf-devel for package: dkms-2.6.1-1.el7.noarch
--> Running transaction check
---> Package elfutils-libelf-devel.x86_64 0:0.170-4.el7 will be installed
--> Processing Dependency: elfutils-libelf(x86-64) = 0.170-4.el7 for package: elfutils-libelf-devel-0.170-4.el7.x86_64
--> Running transaction check
---> Package elfutils-libelf.x86_64 0:0.168-8.el7 will be updated
--> Processing Dependency: elfutils-libelf(x86-64) = 0.168-8.el7 for package: elfutils-libs-0.168-8.el7.x86_64
---> Package elfutils-libelf.x86_64 0:0.170-4.el7 will be an update
--> Running transaction check
---> Package elfutils-libs.x86_64 0:0.168-8.el7 will be updated
---> Package elfutils-libs.x86_64 0:0.170-4.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================
Installing:
dkms noarch 2.6.1-1.el7 epel 75 k
Installing for dependencies:
elfutils-libelf-devel x86_64 0.170-4.el7 base 38 k
Updating for dependencies:
elfutils-libelf x86_64 0.170-4.el7 base 195 k
elfutils-libs x86_64 0.170-4.el7 base 267 k

Transaction Summary
=============================================================================================================================================================
Install 1 Package (+1 Dependent package)
Upgrade ( 2 Dependent packages)

Total download size: 575 k
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/4): elfutils-libelf-devel-0.170-4.el7.x86_64.rpm | 38 kB 00:00:00
(2/4): elfutils-libelf-0.170-4.el7.x86_64.rpm | 195 kB 00:00:00
(3/4): elfutils-libs-0.170-4.el7.x86_64.rpm | 267 kB 00:00:00
warning: /var/cache/yum/x86_64/7/epel/packages/dkms-2.6.1-1.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEYB/s | 500 kB --:--:-- ETA
Public key for dkms-2.6.1-1.el7.noarch.rpm is not installed
(4/4): dkms-2.6.1-1.el7.noarch.rpm | 75 kB 00:00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 814 kB/s | 575 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid : "Fedora EPEL (7) <epel@fedoraproject.org>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package : epel-release-7-11.noarch (installed)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : elfutils-libelf-0.170-4.el7.x86_64 1/6
Installing : elfutils-libelf-devel-0.170-4.el7.x86_64 2/6
Installing : dkms-2.6.1-1.el7.noarch 3/6
Updating : elfutils-libs-0.170-4.el7.x86_64 4/6
Cleanup : elfutils-libs-0.168-8.el7.x86_64 5/6
Cleanup : elfutils-libelf-0.168-8.el7.x86_64 6/6
Verifying : dkms-2.6.1-1.el7.noarch 1/6
Verifying : elfutils-libs-0.170-4.el7.x86_64 2/6
Verifying : elfutils-libelf-devel-0.170-4.el7.x86_64 3/6
Verifying : elfutils-libelf-0.170-4.el7.x86_64 4/6
Verifying : elfutils-libs-0.168-8.el7.x86_64 5/6
Verifying : elfutils-libelf-0.168-8.el7.x86_64 6/6

Installed:
dkms.noarch 0:2.6.1-1.el7

Dependency Installed:
elfutils-libelf-devel.x86_64 0:0.170-4.el7

Dependency Updated:
elfutils-libelf.x86_64 0:0.170-4.el7 elfutils-libs.x86_64 0:0.170-4.el7

Complete!

安装ZFS:

1
yum install zfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* epel: mirror01.idc.hinet.net
* extras: mirrors.163.com
* updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package zfs.x86_64 0:0.7.11-1.el7_4 will be installed
--> Processing Dependency: zfs-kmod = 0.7.11 for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: spl = 0.7.11 for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libzpool2 = 0.7.11 for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libzfs2 = 0.7.11 for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libuutil1 = 0.7.11 for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libnvpair1 = 0.7.11 for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libzpool.so.2()(64bit) for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libzfs_core.so.1()(64bit) for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libzfs.so.2()(64bit) for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libuutil.so.1()(64bit) for package: zfs-0.7.11-1.el7_4.x86_64
--> Processing Dependency: libnvpair.so.1()(64bit) for package: zfs-0.7.11-1.el7_4.x86_64
--> Running transaction check
---> Package libnvpair1.x86_64 0:0.7.11-1.el7_4 will be installed
---> Package libuutil1.x86_64 0:0.7.11-1.el7_4 will be installed
---> Package libzfs2.x86_64 0:0.7.11-1.el7_4 will be installed
---> Package libzpool2.x86_64 0:0.7.11-1.el7_4 will be installed
---> Package spl.x86_64 0:0.7.11-1.el7_4 will be installed
--> Processing Dependency: spl-kmod = 0.7.11 for package: spl-0.7.11-1.el7_4.x86_64
---> Package zfs-dkms.noarch 0:0.7.11-1.el7_4 will be installed
--> Running transaction check
---> Package spl-dkms.noarch 0:0.7.11-1.el7_4 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
Package Arch Version Repository Size
=============================================================================================================================================================
Installing:
zfs x86_64 0.7.11-1.el7_4 zfs 414 k
Installing for dependencies:
libnvpair1 x86_64 0.7.11-1.el7_4 zfs 31 k
libuutil1 x86_64 0.7.11-1.el7_4 zfs 36 k
libzfs2 x86_64 0.7.11-1.el7_4 zfs 131 k
libzpool2 x86_64 0.7.11-1.el7_4 zfs 594 k
spl x86_64 0.7.11-1.el7_4 zfs 29 k
spl-dkms noarch 0.7.11-1.el7_4 zfs 457 k
zfs-dkms noarch 0.7.11-1.el7_4 zfs 4.9 M

Transaction Summary
=============================================================================================================================================================
Install 1 Package (+7 Dependent packages)

Total download size: 6.5 M
Installed size: 30 M
Is this ok [y/d/N]: y
Downloading packages:
(1/8): libuutil1-0.7.11-1.el7_4.x86_64.rpm | 36 kB 00:00:02
(2/8): libnvpair1-0.7.11-1.el7_4.x86_64.rpm | 31 kB 00:00:02
(3/8): libzfs2-0.7.11-1.el7_4.x86_64.rpm | 131 kB 00:00:00
(4/8): spl-0.7.11-1.el7_4.x86_64.rpm | 29 kB 00:00:00
(5/8): spl-dkms-0.7.11-1.el7_4.noarch.rpm | 457 kB 00:00:00
(6/8): zfs-0.7.11-1.el7_4.x86_64.rpm | 414 kB 00:00:00
(7/8): zfs-dkms-0.7.11-1.el7_4.noarch.rpm | 4.9 MB 00:00:02
(8/8): libzpool2-0.7.11-1.el7_4.x86_64.rpm | 594 kB 00:00:25
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 238 kB/s | 6.5 MB 00:00:28
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libuutil1-0.7.11-1.el7_4.x86_64 1/8
Installing : libnvpair1-0.7.11-1.el7_4.x86_64 2/8
Installing : libzpool2-0.7.11-1.el7_4.x86_64 3/8
Installing : spl-dkms-0.7.11-1.el7_4.noarch 4/8
Loading new spl-0.7.11 DKMS files...
Building for 3.10.0-693.el7.x86_64
Building initial module for 3.10.0-693.el7.x86_64
Done.

spl.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/

splat.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/
Adding any weak-modules

depmod....

DKMS: install completed.
Installing : zfs-dkms-0.7.11-1.el7_4.noarch 5/8
Loading new zfs-0.7.11 DKMS files...
Building for 3.10.0-693.el7.x86_64
Building initial module for 3.10.0-693.el7.x86_64
Done.

zavl.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/

znvpair.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/

zunicode.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/

zcommon.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/

zfs.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/

zpios.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/

icp.ko.xz:
Running module version sanity check.
- Original module
- No original module exists within this kernel
- Installation
- Installing to /lib/modules/3.10.0-693.el7.x86_64/extra/
Adding any weak-modules

depmod....

DKMS: install completed.
Installing : spl-0.7.11-1.el7_4.x86_64 6/8
Installing : libzfs2-0.7.11-1.el7_4.x86_64 7/8
Installing : zfs-0.7.11-1.el7_4.x86_64 8/8
Verifying : zfs-dkms-0.7.11-1.el7_4.noarch 1/8
Verifying : spl-0.7.11-1.el7_4.x86_64 2/8
Verifying : zfs-0.7.11-1.el7_4.x86_64 3/8
Verifying : libzfs2-0.7.11-1.el7_4.x86_64 4/8
Verifying : spl-dkms-0.7.11-1.el7_4.noarch 5/8
Verifying : libnvpair1-0.7.11-1.el7_4.x86_64 6/8
Verifying : libzpool2-0.7.11-1.el7_4.x86_64 7/8
Verifying : libuutil1-0.7.11-1.el7_4.x86_64 8/8

Installed:
zfs.x86_64 0:0.7.11-1.el7_4

Dependency Installed:
libnvpair1.x86_64 0:0.7.11-1.el7_4 libuutil1.x86_64 0:0.7.11-1.el7_4 libzfs2.x86_64 0:0.7.11-1.el7_4 libzpool2.x86_64 0:0.7.11-1.el7_4
spl.x86_64 0:0.7.11-1.el7_4 spl-dkms.noarch 0:0.7.11-1.el7_4 zfs-dkms.noarch 0:0.7.11-1.el7_4

Complete!

重启系统:

1
reboot

重启完毕,检查是否已加载ZFS内核模块:

1
lsmod | grep zfs

如果没有任何输出,则ZFS内核没有加载,需要手工加载:

1
modprobe zfs

此时再次检查是否已加载ZFS:

1
2
3
4
5
6
7
8
[root@localhost ~]# lsmod | grep zfs
zfs 3560372 0
zunicode 331170 1 zfs
zavl 15236 1 zfs
icp 270148 1 zfs
zcommon 73440 1 zfs
znvpair 89131 2 zfs,zcommon
spl 102369 4 icp,zfs,zcommon,znvpair

ZFS基本配置

现在需要一个可用磁盘驱动器或分区来配置ZFS,检查已有的磁盘:

1
2
3
4
5
6
7
8
9
10
[root@localhost data]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 29G 0 part
├─centos-root 253:0 0 27G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 50G 0 disk
sdc 8:32 0 50G 0 disk
sr0 11:0 1 1024M 0 rom

现在我又sdb和sdc磁盘可用,每个都是50GB大小,我将使用sdb来创建ZFS。

现在必须创建一个ZFSchi。可以将ZFS池命名为任何名称。将会在根目录下创建同名的目录。

1
zpool create zfsdata /dev/sdb

创建完毕后,查看系统可用的ZFS池:

1
2
3
[root@localhost data]# zpool list
NAME SIZE ALLOC FREE EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT
zfsdata 49.8G 106K 49.7G - 0% 0% 1.00x ONLINE -

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost data]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 27G 21G 6.5G 77% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 52K 1.9G 1% /dev/shm
tmpfs 1.9G 8.7M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 872M 15% /boot
.host:/ 300G 97G 204G 33% /mnt/hgfs
tmpfs 378M 0 378M 0% /run/user/1001
tmpfs 378M 0 378M 0% /run/user/0
zfsdata 49G 0 49G 0% /zfsdata

至此,ZFS基本安装完成,后续可以快乐的使用ZFS这个文件系统了。