TEXT   30

zfs-on-lfs-hint.txt

Guest on 5th August 2021 01:43:20 AM

  1.  
  2. License: MIT
  3.  
  4. Synopsis: Using ZFS with Linux From Scratch
  5.  
  6. ===============================================================================
  7.  
  8. The MIT License
  9.  
  10. Copyright (c) James Powell
  11.  
  12. Permission is hereby granted, free of charge, to any person obtaining a copy of
  13. this software and associated documentation files (the "Software"), to deal in
  14. the Software without restriction, including without limitation the rights to
  15. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  16. of the Software, and to permit persons to whom the Software is furnished to do
  17. so, subject to the following conditions:
  18.  
  19. The above copyright notice and this permission notice shall be included in all
  20. copies or substantial portions of the Software.
  21.  
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. SOFTWARE.
  29.  
  30. ===============================================================================
  31.  
  32. Description:
  33.  
  34. This document is provided to demonstrate how to effectively use the ZFS File
  35. system using ZFS-On-Linux as the primary /(root) partition. ZFS is a highly
  36. advanced file system with many features not normally found on GNU/Linux,
  37. including the BtrFS file system which is still considered experimental and
  38. unstable, even though it's been supported in the Linux kernel since 2.6.xx.
  39. ZFS is licensed under the CDDL, which unfortunately in NOT compatible with the
  40. GNU's GPL license the Linux kernel is under. This means that you can not get
  41. a premade GNU/Linux distribution with built-in ZFS support.
  42.  
  43. Linux From Scratch, however, is an odd exemption from this. Because LFS is not
  44. a prebuilt distribution, LFS falls outside the legal status of GPL and CDDL
  45. problematics into a sort of grey area.
  46.  
  47. ZFS-On-Linux was developed by Lawrence Livermoore National Laboratories for
  48. their own usage under the umbrella of the OpenZFS Project sponsored by the
  49. Illumos Foundation for the Illumos Kernel and the OpenIndiana operating system.
  50. The OpenZFS project is under a full disclosure license agreement and OpenZFS
  51. is a fully open source project. The only implementation not sponsored or used
  52. by OpenZFS is the Oracle-ZFS implementation which is closed source, and has
  53. been developed separately from OpenZFS.
  54.  
  55. ZFS-On-Linux currently uses zpool version 5000 and fs version 5.
  56.  
  57. This software hint is considered experimental, so please use at your own risk
  58. on a rightful system you are willing to sacrifice if needed.
  59.  
  60. ===============================================================================
  61.  
  62. Prerequisites:
  63.  
  64. You must first run and assign partitioning using fdisk, cfdisk, gdisk
  65. or any other partitioning tool.
  66.  
  67. Make sure you assign the following such as this example:
  68.  
  69. /dev/sda1: Primary Partition - 83 Partition type (Linux) - At least 512KB in
  70.                                                            size.
  71.  
  72. This will serve at the primary /boot partition to where you will be installing
  73. the Linux kernel and boot loader.
  74.  
  75. /dev/sda2: Primary Partition - BF Partition Type (Solaris) - Remaining disk
  76.                                                              space.
  77.  
  78. This will serve as the ZFS partition space as well as the swap space.
  79.  
  80. You must now have obtained the Solaris Porting Layer (SPL) and ZFS-On-Linux
  81. packages for your distribution, and have installed them.
  82.  
  83. NOTE: At this point, it does not matter whether ZFS is built-into or installed
  84.       as a module, you just need to have it installed.
  85.  
  86. ===============================================================================
  87.  
  88. Hint:
  89.  
  90. For the Remainder of the hint any partitions used will be examples only. Please
  91. adjust partition values as needed.
  92.  
  93. First you must create a zpool to setup where the zfs-root will go. To perform
  94. this action run the following command:
  95.  
  96.   zpool create -m none zfs-root /dev/sda2
  97.  
  98. This will create a blank zpool in the Solaris assigned disk space for ZFS to
  99. work with. At this point, the partition is very useless for LFS, so we'll need
  100. to make some adjustments to let LFS self manage things.
  101.  
  102. Let's now set the mountpoint.
  103.  
  104. First run this command:
  105.  
  106.   zfs get mountpoint zfs-root
  107.  
  108. The result should be at first it shows ZFS has no mountpoint. Because we want
  109. the native mount tools to work with ZFS rather than the proprietary mount tools
  110. we'll change the mountpoint:
  111.  
  112.   zfs set mountpoint=legacy zfs-root
  113.  
  114. Legacy allows ZFS to be mounted and unmounted using the traditional mount and
  115. umount commands.
  116.  
  117. If you would like to see all the options of your zfs partition run:
  118.  
  119.   zfs get all zfs-root
  120.  
  121. Now let's mount our brand new ZFS partition.
  122.  
  123.   mount -v zfs-root -t zfs $LFS
  124.  
  125. Just like in Chapter 2: Mounting the New Partition, this will mount the ZFS
  126. partition normally. To unmount it, simply run:
  127.  
  128.   umount -v $LFS
  129.  
  130. And the partition is unmounted just like a normal EXT*, JFS, or ReiserFS file
  131. system.
  132.  
  133. Now as normal, create a boot partition:
  134.  
  135.   mkfs.ext4 /dev/sda1
  136.  
  137. ZFS isn't always boot friendly with various operating systems. Some like
  138. FreeBSD can boot to ZFS, but not all can. Because various Linux bootloaders
  139. like Grub don't always support every single file system, unless rebuilt for it,
  140. which you can do with Grub, you'll need a boot partition.
  141.  
  142. ZFS doesn't allow swapfiles, but you can use a ZFS volume as swap. Let's create
  143. a swap space now using a zvol
  144.  
  145.   zfs create -V 8G -b $(getconf PAGESIZE) \
  146.             -o primarycache=metadata \
  147.             -o sync=always \
  148.             -o com.sun:auto-snapshot=false zfs-root/swap
  149.            
  150. In truth, you should not require more than 8GB swap space on any computer, for
  151. any reason of usage. However, unlike a traditional file system, you can always
  152. use the zfs tools to add or reduce swap space as needed.
  153.            
  154. Now we'll set the swap up like normal but using the zvol as the target:
  155.  
  156.   mkswap -f /dev/zvol/zfs-root/swap
  157.  
  158. Now let's turn on the swap partition:
  159.  
  160.   swapon /dev/zvol/zfs-root/swap
  161.  
  162. Now you should be able to build your system normally. When unmounting
  163. everything should remount as normal, if not, that means trouble happened and
  164. you should check your zpool status such as:
  165.  
  166.   zpool status zfs-root
  167.  
  168. When you set your fstab, you should now take care about how you setup the ZFS
  169. parition and swap. ZFS uses these types of entries:
  170.  
  171.   zfs-root / zfs defaults,atime,dev,exec,suid,xattr,nomand,zfsutil 0 0
  172.   /dev/zvol/zfs-root/swap none swap discard 0 0
  173.   /dev/sda1 /boot ext4 defaults 0 2
  174.  
  175. This will setup the swap with discard feature. This will reduce fragmentation
  176. levels in the swap area if it's not full. ZFS supports a vast deal of options
  177. for the file system, as you can see, these are the recommended defaults.
  178.  
  179. Now when you first boot a system with ZFS, you'll notice that the mount tools
  180. may complain about fsck.zfs missing. To be honest, there is no fsck.zfs utility
  181. period. ZFS has it's own utility, but it's best used offline. To make sure you
  182. don't get complaints on boot, let's make a stubfile:
  183.  
  184.   cat > /sbin/fsck.zfs << "EOF"
  185.   #!/bin/sh
  186.   exit 0
  187.   "EOF"
  188.  
  189. That should keep the error read outs to a minimum when booting.
  190.  
  191. Now let's talk about the kernel. Becasue I, personally, recommend you use a
  192. kernel with built-in modules, this next step will focus on just that.
  193.  
  194. Let's first get the SPL (Solaris Porting Layer) sources:
  195.  
  196.   http://archive.zfsonlinux.org/downloads/zfsonlinux/spl/spl-0.6.3.tar.gz
  197.  
  198. and the ZFS sources:
  199.  
  200.   http://archive.zfsonlinux.org/downloads/zfsonlinux/zfs/zfs-0.6.3.tar.gz
  201.  
  202. Save these into /sources where you've stored everything else.
  203.  
  204. Now unpack the SPL and ZFS sources, and we'll start with getting SPL ready:
  205.  
  206. First go to the kernel sources and run the following against an existing kernel
  207. source that's been configured:
  208.  
  209.   make prepare scripts
  210.  
  211. This should prepare the kernel for a source merge. Now change to the spl
  212. directory and get it prepped.
  213.  
  214.   ./configure \
  215.       --prefix=/ \
  216.       --libdir=/lib \
  217.       --includedir=/usr/include \
  218.       --datarootdir=/usr/share \
  219.       --enable-linux-builtin=yes \
  220.       --with-linux=/usr/src/linux-3.15.6 \
  221.       --with-linux-obj=/usr/src/linux-3.15.6
  222.   ./copy-builtin /usr/src/linux-3.15.6
  223.   make
  224.   make install
  225.  
  226. This will directly merge SPL into the kernel source, and build a few libraries.
  227. Now let's merge in ZFS:
  228.  
  229.   ./configure \
  230.       --prefix=/ \
  231.       --libdir=/lib64 \
  232.       --includedir=/usr/include \
  233.       --datarootdir=/usr/share \
  234.       --enable-linux-builtin=yes \
  235.       --with-linux=/usr/src/linux-3.15.6 \
  236.       --with-linux-obj=/usr/src/linux-3.15.6 \
  237.       --with-spl=/root/src/spl-0.6.3
  238.       --with-spl-obj=/root/src/spl-0.6.3
  239.   ./copy-builtin /usr/src/linux-3.15.6
  240.   make
  241.   make install
  242.  
  243. This will merge ZFS into the kernel and built what amounts to the zfsprogs
  244. package... if it did exist.
  245.  
  246. Now run make menuconfig. Under the root directory you'll see SPL support as a
  247. new option in the menu. Now enable it as [*] for Built-in. Now open the
  248. File-Systems menu and you'll see ZFS in there too. Again, enable it [*] for
  249. built-in.
  250.  
  251. Now build and install your kernel normally.
  252.  
  253. Before you reboot, you'll need to create a hostid. To do this, you'll need to
  254. generate it but there is not utility to do so, so use this instead:
  255.  
  256.   cd /sources
  257.   mkdir -v hostid
  258.   cd hostid
  259.  
  260.   cat > writehostid.c << "EOF"
  261.   #include <stdio.h>
  262.   #include <errno.h>
  263.   #include <unistd.h>
  264.  
  265.   int main() {
  266.       int res;
  267.       res = sethostid(gethostid());
  268.       if (res != 0) {
  269.           switch (errno) {
  270.               case EACCES:
  271.               fprintf(stderr, "Error! No permission to write the"
  272.                           " file used to store the host ID.\n"
  273.                           "Are you root?\n");
  274.               break;
  275.               case EPERM:
  276.               fprintf(stderr, "Error! The calling process's effective"
  277.                               " user or group ID is not the same as"
  278.                               " its corresponding real ID.\n");
  279.               break;
  280.               default:
  281.               fprintf(stderr, "Unknown error.\n");
  282.           }
  283.           return 1;
  284.       }
  285.       return 0;
  286.   }
  287.   "EOF"
  288.  
  289. Now compile it with:
  290.  
  291.   gcc -v writehostid.c -o writehostid
  292.  
  293. And run the utility:
  294.  
  295.   ./writehostid
  296.  
  297. This will generate a proper hostid file.
  298.  
  299. If all goes well, on the first system boot, you'll boot into ZFS just like any
  300. other file system, but to get subsequent reboots to work without issue we'll
  301. need to create a cache file for the zpool:
  302.  
  303.   zpool set cachefile=/etc/zfs/zpool.cache zfs-root
  304.  
  305. Now everything should work as intended. Congradulations on your new file
  306. system, and enjoy.
  307.  
  308. If you'd like to learn more information on ZFS and it's capabilities which are
  309. beyond the scope of this hint, visit: http://en.wikipedia.org/wiki/ZFS
  310.  
  311. ===============================================================================
  312.  
  313. Acknowledgements:
  314.  
  315. I'd like to thank my partners in crime Keith Hedger and Arthur Radley for being
  316. awesome teammates in our work in GNU/Linux.
  317.  
  318. I'd also like to thank SlackWiki and ArchWiki for their awesome coverage of ZFS
  319. on Linux.
  320.  
  321. ===============================================================================
  322.  
  323. Changelog:
  324.  
  325. 2014-08-14
  326.  
  327. Version - 0.1 - Initial Draft and Public Release
  328.  
  329. ===============================================================================

Raw Paste


Login or Register to edit or fork this paste. It's free.