1、linux安装zfs

1
2
3
$ sudo apt update
$ sudo apt install zfsutils-linux
$ zfs --version

We have a few hard drives in our test system that we plan to use with ZFS. We’ll show you various things you can do with them in this section.

When you plug new hard disks into your system, ZFS addresses them by their device name - normally something along the lines of /dev/sda or similar. You can use the fdisk command to see what hard drives you have available.

1
$ sudo fdisk -l

2、创建zfs storage pool

ZFS works by “pooling” disks together. These pools (commonly called “zpools”) can be configured for various RAID levels.

The first zpool we’ll look at is a RAID 0. This works by striping your data across multiple disks. When a file is read from or written to the storage pool, all the disks will work together to present a portion of the data. This offers you a speed boost for your read and write speeds, but it doesn’t do anything for redundancy. As a matter of fact, any disk failure in the pool will result in a complete loss of data.

1
$ sudo zpool create mypool /dev/sdb /dev/sdc

This command has created a ZFS storage pool named “mypool” with two hard drives, /dev/sdb and /dev/sdc. You can see details about your storage pools any time by running this command:

1
2
3
$ zpool status
#And you can see a more concise report of your ZFS storage pools by executing:
$ zpool list

Your newly created pool will be mounted automatically for you, and you can begin to use it right away. A nice feature of ZFS is that you don’t need to go through a lengthy partitioning (when using whole disks) or formatting process. The storage is just accessible right away.

1
$ df -hT | grep zfs

If you want to add another hard disk to the pool, take a look at this command where we add hard disk /dev/sdd to our previously created mypool storage pool:

1
$ sudo zpool add mypool /dev/sdd

You can see that the drive has been added to the zpool with the zpool status command.

We can destroy our zpool at any time with the following command:

1
$ sudo zpool destroy mypool 

In the case of RAID 0 zpools, you can’t remove any disk from the pool without destroying the pool entirely and losing all data. ZFS has many different options, some of which allow for the removal or failure of disks while still maintaining the integrity of the pool.

Other types of ZFS storage pools are created in the same manner as we’ve shown you above, but you need to supply an extra argument in the zpool command when creating the pool. Let’s look at some examples.

A mirrored storage pool is ZFS’ equivalent to RAID 1. This gives you redundancy because all your data is mirrored from one hard disk to one or more others. To make a mirrored storage pool, use the following command syntax:

1
$ sudo zpool create mypool mirror /dev/sdb /dev/sdc

Of course, more disks can be added to the pool to create additional redundancy.

Now, let’s take a look at RAID-Z pools. RAID-Z is very similar to RAID 5, but improves upon it with better speed and avoiding some of the common errors associated with RAID 5.

RAID-Z will give you speed plus redundancy by using block level striping and distributed parity. There are three types of RAID-Z available, depending on how much parity you want.

  • raidz1 (or just raidz) - single parity 单个奇偶校验位,至少需要三个磁盘 类似 RAID-5
  • raidz2 - double parity 两个奇偶校验,至少需要四个磁盘 类似 RAID-6
  • raidz3 - triple parity 三重奇偶校验,至少需要五个磁盘

Here’s how you can create a RAID-Z pool. Use raidz2 or raidz3 in place of of raidz in this command if you want more parity (keep in mind you’ll also need additional disks in that case):

1
$ sudo zpool create mypool raidz /dev/sdb /dev/sdc /dev/sdd

3、Encryption on ZFS

After creating your ZFS storage pool, you can configure encryption on it with the following commands. For this example, we are still using our three disk RAID-Z pool named mypool.

1
$ sudo zfs create -o encryption=on -o keylocation=prompt -o keyformat=passphrase mypool/encrypted

You’ll be asked to enter a passphrase twice for the encryption.

A new directory is created under /mypool/encrypted, and anything in that directory is encrypted. Whenever you reboot, you’ll need to manually mount the encrypted dataset. Be sure to use the -l flag when mounting encrypted datasets. You’ll be prompted to enter the passphrase you chose earlier.

1
$ sudo zfs mount -l mypool/encrypted

ZFS is a file system focused on high availability and data integrity. It’s perfect for storage/NAS servers and any environment where read and write speeds are crucial along with hard drive redundancy.

ref:

  1. https://linuxconfig.org/configuring-zfs-on-ubuntu-20-04
  2. https://www.latentexistence.me.uk/zfs-and-ubuntu-home-server-howto/
  3. http://www.ha97.com/4963.html (hdparm 常用方式)
  4. http://blog.gqylpy.com/gqy/16786/ (linux下Nas使硬盘自动进入休眠省电状态)
  5. https://zhuanlan.zhihu.com/p/89575699 (Linux系统家庭服务器下机械硬盘优化方法)
  6. https://github.com/bcwu/ZFS_on_Linux_LUKS_startup_automount
  7. https://wiki.archlinux.org/index.php/Hdparm