Commit 300cdf0772
Unsigned
Layout: unified · split
content/blog/2024-02-06-zfs.md added +219
| @@ -0,0 +1,219 @@ | |||
| 1 | +++ | ||
| 2 | date = 2024-02-06 | ||
| 3 | title = "How to Create a ZFS Pool on Ubuntu Linux" | ||
| 4 | description = "Learn how to create a simple ZFS pool on Ubuntu Linux." | ||
| 5 | +++ | ||
| 6 | |||
| 7 | This post details the process I used to create ZFS pools, datasets, and | ||
| 8 | snapshots on Ubuntu Server. | ||
| 9 | |||
| 10 | I found the following pages very helpful while going through this process: | ||
| 11 | |||
| 12 | - [Setup a ZFS storage pool](https://ubuntu.com/tutorials/setup-zfs-storage-pool) | ||
| 13 | - [Kernel/Reference/ZFS](https://wiki.ubuntu.com/Kernel/Reference/ZFS) | ||
| 14 | - [ZFS for Dummies](https://blog.victormendonca.com/2020/11/03/zfs-for-dummies/) | ||
| 15 | |||
| 16 | ## Installation | ||
| 17 | |||
| 18 | To start, I installed the ZFS package with the following command: | ||
| 19 | |||
| 20 | ```sh | ||
| 21 | sudo apt install zfsutils-linux | ||
| 22 | ``` | ||
| 23 | |||
| 24 | Once installed, you can check the version to see if it installed correctly. | ||
| 25 | |||
| 26 | ```sh | ||
| 27 | > zsf --version | ||
| 28 | |||
| 29 | zfs-2.1.5-1ubuntu6~22.04.2 | ||
| 30 | zfs-kmod-2.1.5-1ubuntu6~22.04.1 | ||
| 31 | ``` | ||
| 32 | |||
| 33 | ## ZFS Configuration | ||
| 34 | |||
| 35 | Now that ZFS is installed, we can create and configure the pool. | ||
| 36 | |||
| 37 | You have various options for configuring ZFS pools that all come different pros | ||
| 38 | and cons. | ||
| 39 | |||
| 40 | - Striped VDEVs (Raid0) | ||
| 41 | - Mirrored VDEVs (Raid1) | ||
| 42 | - Striped Mirrored VDEVs (Raid10) | ||
| 43 | - RAIDz (Raid5) | ||
| 44 | - RAIDz2 (Raidd6) | ||
| 45 | - RAIDz3 | ||
| 46 | - Nested RAIDz (Raid50, Raid60) | ||
| 47 | |||
| 48 | I will be using Raid10 in this guide. However, the majority of the steps are the | ||
| 49 | same regardless of your chosen pool configuration. | ||
| 50 | |||
| 51 | ### Creating the Pool | ||
| 52 | |||
| 53 | To start, let's list the disks available to use. You can use `fdisk` command to | ||
| 54 | see all available disks. | ||
| 55 | |||
| 56 | ```sh | ||
| 57 | sudo fdisk -l | ||
| 58 | ``` | ||
| 59 | |||
| 60 | Or, if you currently have them mounted, you can use the `df` command to view | ||
| 61 | your disks. | ||
| 62 | |||
| 63 | ```sh | ||
| 64 | > sudo df -h | ||
| 65 | |||
| 66 | Filesystem Size Used Avail Use% Mounted on | ||
| 67 | ... | ||
| 68 | /dev/sda1 7.3T 28K 6.9T 1% /mnt/red-01 | ||
| 69 | /dev/sdb1 7.3T 144G 6.8T 3% /mnt/red-02 | ||
| 70 | /dev/sdc1 7.3T 5.5T 1.9T 75% /mnt/white-02 | ||
| 71 | /dev/sdd1 9.1T 8.7T 435G 96% /mnt/white-01 | ||
| 72 | ``` | ||
| 73 | |||
| 74 | If you're going to use mounted disks, make sure to umount them before creating | ||
| 75 | the pool. | ||
| 76 | |||
| 77 | ```sh | ||
| 78 | sudo umount /dev/sda1 | ||
| 79 | sudo umount /dev/sdb1 | ||
| 80 | ``` | ||
| 81 | |||
| 82 | Now that I've identified the disks I want to use and have them unmounted, let's | ||
| 83 | create the pool. For this example, I will call it `tank`. | ||
| 84 | |||
| 85 | ```sh | ||
| 86 | sudo zpool create -f -m /mnt/pool tank mirror /dev/sda /dev/sdb | ||
| 87 | ``` | ||
| 88 | |||
| 89 | See below for the results of the new ZFS pool named `tank`, with a vdev | ||
| 90 | automatically named `mirror-0`. | ||
| 91 | |||
| 92 | ```sh | ||
| 93 | > zfs list | ||
| 94 | |||
| 95 | NAME USED AVAIL REFER MOUNTPOINT | ||
| 96 | tank 396K 7.14T 96K /tank | ||
| 97 | ``` | ||
| 98 | |||
| 99 | ```sh | ||
| 100 | > zpool status | ||
| 101 | |||
| 102 | pool: tank | ||
| 103 | state: ONLINE | ||
| 104 | config: | ||
| 105 | |||
| 106 | NAME STATE READ WRITE CKSUM | ||
| 107 | tank ONLINE 0 0 0 | ||
| 108 | mirror-0 ONLINE 0 0 0 | ||
| 109 | sda ONLINE 0 0 0 | ||
| 110 | sdb ONLINE 0 0 0 | ||
| 111 | |||
| 112 | errors: No known data errors | ||
| 113 | ``` | ||
| 114 | |||
| 115 | We can also look at the mounted filesystem to see where the pool is mounted and | ||
| 116 | some quick stats. | ||
| 117 | |||
| 118 | ```sh | ||
| 119 | > df -h | ||
| 120 | |||
| 121 | Filesystem Size Used Avail Use% Mounted on | ||
| 122 | ... | ||
| 123 | tank 7.2T 128K 7.2T 1% /tank | ||
| 124 | ``` | ||
| 125 | |||
| 126 | ### Expanding the Pool | ||
| 127 | |||
| 128 | If you want to expand this pool, you will need to add a new VDEV to the pool. | ||
| 129 | Since I am using 2 disks per VDEV, I will need to add a new 2-disk VDEV to the | ||
| 130 | existing `tank` pool. | ||
| 131 | |||
| 132 | ```sh | ||
| 133 | sudo zpool add tank mirror /dev/sdX /dev/sdY | ||
| 134 | ``` | ||
| 135 | |||
| 136 | I added two 8TB hard drives and this process took around 10 seconds to complete. | ||
| 137 | |||
| 138 | When viewing the pool again, you can see that the pool has now doubled in size. | ||
| 139 | We have 14.3 TB useable space and the same space used for mirroring. | ||
| 140 | |||
| 141 | ```sh | ||
| 142 | > zfs list | ||
| 143 | |||
| 144 | NAME USED AVAIL REFER MOUNTPOINT | ||
| 145 | tank 145G 14.3T 104K /tank | ||
| 146 | tank/cloud 145G 14.3T 145G /tank/cloud | ||
| 147 | tank/media 96K 14.3T 96K /tank/media | ||
| 148 | ``` | ||
| 149 | |||
| 150 | ### Creating Datasets | ||
| 151 | |||
| 152 | According to [ZFS | ||
| 153 | Terminology](https://docs.oracle.com/cd/E18752_01/html/819-5461/ftyue.html), a | ||
| 154 | `dataset` can refer to "clones, file systems, snapshots, and volumes. | ||
| 155 | |||
| 156 | For this guide, I will use the `dataset` term to refer to file systems created | ||
| 157 | under a pool. | ||
| 158 | |||
| 159 | Within my `tank` pool, I am going to create some datasets to help organize my | ||
| 160 | files. This will give me location to store data rather than simply dumping | ||
| 161 | everything at the `/tank/` location. | ||
| 162 | |||
| 163 | ```sh | ||
| 164 | sudo zfs create tank/cloud | ||
| 165 | sudo zfs create tank/media | ||
| 166 | ``` | ||
| 167 | |||
| 168 | Once created, you can see these datasets in the output of your pool list: | ||
| 169 | |||
| 170 | ```sh | ||
| 171 | > zfs list | ||
| 172 | NAME USED AVAIL REFER MOUNTPOINT | ||
| 173 | tank 752K 7.14T 104K /tank | ||
| 174 | tank/cloud 96K 7.14T 96K /tank/cloud | ||
| 175 | tank/media 96K 7.14T 96K /tank/media | ||
| 176 | ``` | ||
| 177 | |||
| 178 | ### Creating Snapshots | ||
| 179 | |||
| 180 | Next, let's create our first snapshot. We can do this by calling the `snapshot` | ||
| 181 | command and give it an output name. I will be throwing the current date and time | ||
| 182 | into my example. | ||
| 183 | |||
| 184 | ```sh | ||
| 185 | sudo zfs snapshot tank@$(date '+%Y-%m-%d_%H-%M') | ||
| 186 | ``` | ||
| 187 | |||
| 188 | We can list the snapshots in our pool with the following command: | ||
| 189 | |||
| 190 | ```sh | ||
| 191 | > zfs list -t snapshot | ||
| 192 | NAME USED AVAIL REFER MOUNTPOINT | ||
| 193 | tank@2024-02-06_19-41 0B - 104K - | ||
| 194 | ``` | ||
| 195 | |||
| 196 | ### Destroy Snapshots | ||
| 197 | |||
| 198 | You can always destroy snapshots that are no longer needed: | ||
| 199 | |||
| 200 | ```sh | ||
| 201 | sudo zfs destroy tank@2024-02-06_19-41 | ||
| 202 | ``` | ||
| 203 | |||
| 204 | Once deleted, they will no longer appear in the list: | ||
| 205 | |||
| 206 | ```sh | ||
| 207 | > zfs list -t snapshot | ||
| 208 | no datasets available | ||
| 209 | ``` | ||
| 210 | |||
| 211 | ## My Thoughts on ZFS So Far | ||
| 212 | |||
| 213 | - I sacrificed 25TB to be able to mirror my data, but I feel more comfortable | ||
| 214 | with the potential to save my data by quickly replacing a disk if I need to. | ||
| 215 | - The set-up was surprisingly easy and fast. | ||
| 216 | - Disk I/O is fast as well. I was worried that the data transfer speeds would be | ||
| 217 | slower due to the RAID configuration. | ||
| 218 | - Media streaming and transcoding has seen no noticeable drop in performance. | ||
| 219 | - My only limitation really is the number of HDD bays in my server HDD cage. | ||