1 Author: James Powell <james (at) hotmail (dot) com>
7 Synopsis: Using ZFS with Linux From Scratch
9 ===============================================================================
13 Copyright (c) 2014 James Powell
15 Permission is hereby granted, free of charge, to any person obtaining a copy of
16 this software and associated documentation files (the "Software"), to deal in
17 the Software without restriction, including without limitation the rights to
18 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
19 of the Software, and to permit persons to whom the Software is furnished to do
20 so, subject to the following conditions:
22 The above copyright notice and this permission notice shall be included in all
23 copies or substantial portions of the Software.
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 ===============================================================================
37 This document is provided to demonstrate how to effectively use the ZFS File
38 system using ZFS-On-Linux as the primary /(root) partition. ZFS is a highly
39 advanced file system with many features not normally found on GNU/Linux,
40 including the BtrFS file system which is still considered experimental and
41 unstable, even though it's been supported in the Linux kernel since 2.6.xx.
42 ZFS is licensed under the CDDL, which unfortunately in NOT compatible with the
43 GNU's GPL license the Linux kernel is under. This means that you can not get
44 a premade GNU/Linux distribution with built-in ZFS support.
46 Linux From Scratch, however, is an odd exemption from this. Because LFS is not
47 a prebuilt distribution, LFS falls outside the legal status of GPL and CDDL
48 problematics into a sort of grey area.
50 ZFS-On-Linux was developed by Lawrence Livermoore National Laboratories for
51 their own usage under the umbrella of the OpenZFS Project sponsored by the
52 Illumos Foundation for the Illumos Kernel and the OpenIndiana operating system.
53 The OpenZFS project is under a full disclosure license agreement and OpenZFS
54 is a fully open source project. The only implementation not sponsored or used
55 by OpenZFS is the Oracle-ZFS implementation which is closed source, and has
56 been developed separately from OpenZFS.
58 ZFS-On-Linux currently uses zpool version 5000 and fs version 5.
60 This software hint is considered experimental, so please use at your own risk
61 on a rightful system you are willing to sacrifice if needed.
63 ===============================================================================
67 You must first run and assign partitioning using fdisk, cfdisk, gdisk
68 or any other partitioning tool.
70 Make sure you assign the following such as this example:
72 /dev/sda1: Primary Partition - 83 Partition type (Linux) - At least 512KB in
75 This will serve at the primary /boot partition to where you will be installing
76 the Linux kernel and boot loader.
78 /dev/sda2: Primary Partition - BF Partition Type (Solaris) - Remaining disk
81 This will serve as the ZFS partition space as well as the swap space.
83 You must now have obtained the Solaris Porting Layer (SPL) and ZFS-On-Linux
84 packages for your distribution, and have installed them.
86 NOTE: At this point, it does not matter whether ZFS is built-into or installed
87 as a module, you just need to have it installed.
89 ===============================================================================
93 For the Remainder of the hint any partitions used will be examples only. Please
94 adjust partition values as needed.
96 First you must create a zpool to setup where the zfs-root will go. To perform
97 this action run the following command:
99 zpool create -m none zfs-root /dev/sda2
101 This will create a blank zpool in the Solaris assigned disk space for ZFS to
102 work with. At this point, the partition is very useless for LFS, so we'll need
103 to make some adjustments to let LFS self manage things.
105 Let's now set the mountpoint.
107 First run this command:
109 zfs get mountpoint zfs-root
111 The result should be at first it shows ZFS has no mountpoint. Because we want
112 the native mount tools to work with ZFS rather than the proprietary mount tools
113 we'll change the mountpoint:
115 zfs set mountpoint=legacy zfs-root
117 Legacy allows ZFS to be mounted and unmounted using the traditional mount and
120 If you would like to see all the options of your zfs partition run:
124 Now let's mount our brand new ZFS partition.
126 mount -v zfs-root -t zfs $LFS
128 Just like in Chapter 2: Mounting the New Partition, this will mount the ZFS
129 partition normally. To unmount it, simply run:
133 And the partition is unmounted just like a normal EXT*, JFS, or ReiserFS file
136 Now as normal, create a boot partition:
140 ZFS isn't always boot friendly with various operating systems. Some like
141 FreeBSD can boot to ZFS, but not all can. Because various Linux bootloaders
142 like Grub don't always support every single file system, unless rebuilt for it,
143 which you can do with Grub, you'll need a boot partition.
145 ZFS doesn't allow swapfiles, but you can use a ZFS volume as swap. Let's create
146 a swap space now using a zvol
148 zfs create -V 8G -b $(getconf PAGESIZE) \
149 -o primarycache=metadata \
151 -o com.sun:auto-snapshot=false zfs-root/swap
153 In truth, you should not require more than 8GB swap space on any computer, for
154 any reason of usage. However, unlike a traditional file system, you can always
155 use the zfs tools to add or reduce swap space as needed.
157 Now we'll set the swap up like normal but using the zvol as the target:
159 mkswap -f /dev/zvol/zfs-root/swap
161 Now let's turn on the swap partition:
163 swapon /dev/zvol/zfs-root/swap
165 Now you should be able to build your system normally. When unmounting
166 everything should remount as normal, if not, that means trouble happened and
167 you should check your zpool status such as:
169 zpool status zfs-root
171 When you set your fstab, you should now take care about how you setup the ZFS
172 parition and swap. ZFS uses these types of entries:
174 zfs-root / zfs defaults,atime,dev,exec,suid,xattr,nomand,zfsutil 0 0
175 /dev/zvol/zfs-root/swap none swap discard 0 0
176 /dev/sda1 /boot ext4 defaults 0 2
178 This will setup the swap with discard feature. This will reduce fragmentation
179 levels in the swap area if it's not full. ZFS supports a vast deal of options
180 for the file system, as you can see, these are the recommended defaults.
182 Now when you first boot a system with ZFS, you'll notice that the mount tools
183 may complain about fsck.zfs missing. To be honest, there is no fsck.zfs utility
184 period. ZFS has it's own utility, but it's best used offline. To make sure you
185 don't get complaints on boot, let's make a stubfile:
187 cat > /sbin/fsck.zfs << "EOF"
192 That should keep the error read outs to a minimum when booting.
194 Now let's talk about the kernel. Becasue I, personally, recommend you use a
195 kernel with built-in modules, this next step will focus on just that.
197 Let's first get the SPL (Solaris Porting Layer) sources:
199 http://archive.zfsonlinux.org/downloads/zfsonlinux/spl/spl-0.6.3.tar.gz
203 http://archive.zfsonlinux.org/downloads/zfsonlinux/zfs/zfs-0.6.3.tar.gz
205 Save these into /sources where you've stored everything else.
207 Now unpack the SPL and ZFS sources, and we'll start with getting SPL ready:
209 First go to the kernel sources and run the following against an existing kernel
210 source that's been configured:
214 This should prepare the kernel for a source merge. Now change to the spl
215 directory and get it prepped.
220 --includedir=/usr/include \
221 --datarootdir=/usr/share \
222 --enable-linux-builtin=yes \
223 --with-linux=/usr/src/linux-3.15.6 \
224 --with-linux-obj=/usr/src/linux-3.15.6
225 ./copy-builtin /usr/src/linux-3.15.6
229 This will directly merge SPL into the kernel source, and build a few libraries.
230 Now let's merge in ZFS:
235 --includedir=/usr/include \
236 --datarootdir=/usr/share \
237 --enable-linux-builtin=yes \
238 --with-linux=/usr/src/linux-3.15.6 \
239 --with-linux-obj=/usr/src/linux-3.15.6 \
240 --with-spl=/root/src/spl-0.6.3
241 --with-spl-obj=/root/src/spl-0.6.3
242 ./copy-builtin /usr/src/linux-3.15.6
246 This will merge ZFS into the kernel and built what amounts to the zfsprogs
247 package... if it did exist.
249 Now run make menuconfig. Under the root directory you'll see SPL support as a
250 new option in the menu. Now enable it as [*] for Built-in. Now open the
251 File-Systems menu and you'll see ZFS in there too. Again, enable it [*] for
254 Now build and install your kernel normally.
256 Before you reboot, you'll need to create a hostid. To do this, you'll need to
257 generate it but there is not utility to do so, so use this instead:
263 cat > writehostid.c << "EOF"
270 res = sethostid(gethostid());
274 fprintf(stderr, "Error! No permission to write the"
275 " file used to store the host ID.\n"
279 fprintf(stderr, "Error! The calling process's effective"
280 " user or group ID is not the same as"
281 " its corresponding real ID.\n");
284 fprintf(stderr, "Unknown error.\n");
294 gcc -v writehostid.c -o writehostid
300 This will generate a proper hostid file.
302 If all goes well, on the first system boot, you'll boot into ZFS just like any
303 other file system, but to get subsequent reboots to work without issue we'll
304 need to create a cache file for the zpool:
306 zpool set cachefile=/etc/zfs/zpool.cache zfs-root
308 Now everything should work as intended. Congradulations on your new file
311 If you'd like to learn more information on ZFS and it's capabilities which are
312 beyond the scope of this hint, visit: http://en.wikipedia.org/wiki/ZFS
314 ===============================================================================
318 I'd like to thank my partners in crime Keith Hedger and Arthur Radley for being
319 awesome teammates in our work in GNU/Linux.
321 I'd also like to thank SlackWiki and ArchWiki for their awesome coverage of ZFS
324 ===============================================================================
330 Version - 0.1 - Initial Draft and Public Release
332 ===============================================================================