OpenZFS

repository·master·Indexed 11 days ago

https://github.com/openzfs/zfs

An advanced file system and volume manager for Linux and FreeBSD. Documentation includes guides for zpool_influxdb for exporting ZFS pool metrics to InfluxDB and Grafana, as well as boot configuration via zfs-dracut and initramfs-tools, including kernel command line options for root dataset selection and snapshot management.

Tokens
113K
Snippets
333
Records
521
Agent score
91%

What's inside OpenZFS

  1. Explore pyzfs documentation

    master

    The pyzfs documentation provides access to the Python bindings for libzfs_core. The documentation is organized into several key sections:

    • libzfs_core: The primary module containing the core ZFS functionality.
    • libzfs_core.exceptions: Documentation for exception types raised by the library.
    • libzfs_core.ctypes: Documentation for miscellaneous types that correspond to specific width C types used by the underlying library.
  2. Use zpool_influxdb to export ZFS pool metrics

    master

    The zpool_influxdb program generates metrics in InfluxDB line protocol format from ZFS pools. It reads statistics from pools and prints them to stdout. When run without arguments, it samples all imported pools once. You can specify a single pool by providing its name as an argument.

    zpool_influxdb [options] [poolname]
  3. Access OpenZFS official resources and documentation

    master

    For using or developing with OpenZFS, refer to the following official resources:

  4. Use bootfs.snapshot and bootfs.rollback for automated lifecycle management

    master

    You can automate snapshotting and rolling back of the root dataset during the boot process using these kernel command line flags:

    Snapshotting (bootfs.snapshot)

    Enables zfs-snapshot-bootfs.service. This creates a snapshot of the root dataset after the pool is imported but before the rootfs is mounted.

    • Usage: bootfs.snapshot or bootfs.snapshot=snapshot-name.
    • Snapshot Name: If no name is provided, it uses $root_dataset@$(uname -r).
    • Behavior: If snapshot creation fails, the error is noted, but the system continues to boot.

    Rolling back (bootfs.rollback)

    Enables zfs-rollback-bootfs.service. This performs a zfs rollback -Rf on the root dataset after pool import but before the rootfs is mounted.

    • Usage: bootfs.rollback or bootfs.rollback=snapshot-name.
    • Snapshot Name: If no name is provided, it uses $root_dataset@$(uname -r).
    • Behavior: If the rollback fails, the system drops to a rescue shell.
    • Warning: This can cause data loss. Ensure persistent data is not located below the rootfs and that you do not require intermediate snapshots.

    Ordering: If both flags are provided, bootfs.rollback is executed after bootfs.snapshot.

  5. Understand the ZFS init script startup sequence

    master

    The ZFS init scripts are designed to follow a specific dependency chain to ensure system stability:

    1. Import and Mount: zfs-import and zfs-mount run first to ensure all ZFS filesystems (including critical datasets like /var) are available.
    2. Key Loading: zfs-load-key ensures encrypted datasets can be unlocked.
    3. ZED (ZFS Event Daemon): zfs-zed starts after mounting. It depends on /var being available and processes events that occurred before it started. It should start before the sharing service.
    4. Sharing: zfs-share runs last to share filesystems configured with the share* property.
  6. Implement custom key loading for ZFS encrypted roots

    master

    You can provide custom logic to load encryption keys during the initramfs stage by using drop-in scripts.

    Mechanism:

    • The initramfs will copy a shell program located at /etc/zfs/initramfs-tools-load-key and any files matching /etc/zfs/initramfs-tools-load-key.d/* into the initramfs.
    • These scripts are sourced during the boot process.
    • The following variables are available within your script:
      • $ENCRYPTIONROOT: The dataset requiring the key.
      • $KEYLOCATION: The location of the key (e.g., a file path).
    • You can use unquoted $ZPOOL and $ZFS to execute zpool and zfs commands directly.

    Return Codes:

    • A successful return (exit code 0) indicates the key was loaded and stops the search for other key-loading methods.
    • A failure return is non-fatal; the script will proceed to the next available hook.
    # Example: A drop-in script that uses the BLAKE2 checksum of a file as the key
    key="$(b2sum "${KEYLOCATION#file://}")" || return
    printf '%s\n' "${key%% *}" | $ZFS load-key -L prompt "$ENCRYPTIONROOT"
  7. Zstd update policy and compatibility constraints

    master

    Because the compressed byte stream may change between Zstd versions, OpenZFS follows a strict update policy to manage compatibility:

    • Release Timing: Zstd may be updated after a new .0 release is tagged. Critical patches can be applied up until the next release freeze, potentially using a newer upstream version.
    • Version Stability: The Zstd version will not be upgraded within a major release, and multiple Zstd versions are not supported concurrently within a single release.
    • Import Integrity: The library import commit must be a clean, unmodified upstream import. Any OpenZFS-specific integration or local adjustments must be placed in separate, follow-up commits.
    • Documentation: Release notes must highlight Zstd updates and any expected impacts, such as changes in compression ratio/performance or differences in compressed byte streams that might affect deduplication or NOP writes.
    • Backwards Compatibility: If Zstd introduces breaking changes, OpenZFS must maintain backwards compatibility. The Zstd version number is saved within the block header to facilitate future compatibility checks or fixes.
  8. Boot from a ZFS snapshot

    master

    You can specify a snapshot as the <dataset> in your kernel command line arguments.

    Behavior when a snapshot is specified:

    • The script will clone the snapshot and use the resulting clone as the root filesystem.
    • If the clone dataset already exists, it is destroyed before the new one is created.
    • The clone is created with mountpoint=none and canmount=noauto; the initramfs script handles the manual mounting.
    • Fallback: If the specified snapshot does not exist, the script falls back to using the base dataset (the part before the @ symbol) as the boot filesystem.

    Interactive Mode:

    • If you specify a dataset containing an @ (indicating a snapshot) but do not specify a specific snapshot name on the root= command line, the script will prompt the user to choose a snapshot to use during boot.
    # Example: Booting from a specific snapshot via cloning
    root=zfs:rpool/ROOT/default@backup_snapshot
  9. Comparison of zpool_influxdb vs. screen-scraping collectors

    master

    Unlike many community collectors that attempt to screen-scrape the output of the zpool command, zpool_influxdb is optimized to collect metrics directly.

    Advantages of zpool_influxdb:

    • Efficiency: It is significantly more efficient than screen-scraping methods.
    • Reliability: Screen-scraping is prone to failure because zpool output is designed for human readability rather than machine parsing.

    Note: Both methods share the same caveat regarding spa_config reader locks.

  10. How to update the Zstd library in OpenZFS

    master

    To update the Zstd library used by ZFS, you must replace the contents of module/zstd/lib/ with a clean, unmodified upstream import. Follow these steps:

    1. Download the latest release from the official Zstd GitHub releases.
    2. Identify the required files from the Zstd build output using this command:
      grep include [path to zstd]/build/single_file_libs/zstd-in.c | awk '{ print $2 }'
    3. Copy those files to module/zstd/lib/.
    4. Remove debug.c, threading.c, and zstdmt_compress.c from the directory.
    5. Update the Makefiles to reflect the new file list.
    6. If necessary, follow the symbol renaming instructions located in include/zstd_compat_wrapper.h to ensure compatibility.
    grep include [path to zstd]/build/single_file_libs/zstd-in.c | awk '{ print $2 }'
  11. Configure ZFS root pool and filesystem via kernel command line

    master

    The initramfs-tools scripts for ZFS allow you to specify the root pool and dataset using various kernel command line arguments. The script evaluates these arguments in a specific order, and the first match wins.

    Supported argument combinations:

    • rpool=<pool>
    • bootfs=<pool>/<dataset>
    • rpool=<pool> bootfs=<pool>/<dataset>
    • -B zfs-bootfs=<pool>/<fs>
    • root=<pool>/<dataset>
    • root=ZFS=<pool>/<dataset>
    • root=zfs:AUTO
    • root=zfs:<pool>/<dataset>
    • rpool=rpool

    Pool Discovery Behavior:

    • If a pool is explicitly specified, it is used.
    • If root=zfs:AUTO is used, the script searches all available pools. You can exclude specific pools from this search by adding them to the ZFS_POOL_EXCEPTIONS variable in /etc/default/zfs.
    • The import search order is: /dev/disk/by-vdev (if /etc/zfs/vdev_id.conf exists) $\rightarrow$ /dev/disk/by-id and other /dev/disk/by-* directories $\rightarrow$ /dev $\rightarrow$ ZFS cache file.
    • You can modify the import path by setting ZPOOL_IMPORT_PATH in /etc/default/zfs.

    Filesystem Mounting:

    • If a dataset is specified, it is used as the root. If not, the script attempts to find one automatically.
    • Sub-filesystems below the root (e.g., rpool/ROOT/rootfs/var) are automatically mounted if they exist.
    # Example kernel command line arguments:
    rpool=rpool bootfs=rpool/ROOT/default
    root=zfs:rpool/ROOT/default
    root=zfs:AUTO
  12. Install ZFS init script links on RedHat, Fedora, or CentOS

    master

    To set up the ZFS init scripts on RedHat, Fedora, or CentOS systems, use the chkconfig command for each service:

    chkconfig zfs-import
    chkconfig zfs-load-key
    chkconfig zfs-mount
    chkconfig zfs-zed
    chkconfig zfs-share