ZFS: Difference between revisions

imported>Aidalgol
imported>2r
added back older mail notification method -- newer method involves recompiling the entire package: took half an hour on my computer
Line 506: Line 506:
ZFS Event Daemon (zed) monitors events generated by the ZFS kernel module and runs configured tasks. It can be configured to send an email when a pool scrub is finished or a disk has failed. [https://search.nixos.org/options?query=services.zfs.zed zed options]
ZFS Event Daemon (zed) monitors events generated by the ZFS kernel module and runs configured tasks. It can be configured to send an email when a pool scrub is finished or a disk has failed. [https://search.nixos.org/options?query=services.zfs.zed zed options]


The <code>zfs</code> package must be built with mail features. The following override is needed as <code>zfs</code> is implicitly used in partition mounting:
=== Alternative 1: Rebuild ZFS with Mail Support ===
The <code>zfs</code> package can be rebuilt with mail features. However, please note that this will cause Nix to recompile the entire ZFS package on the computer, and on every kernel update, which could be very time-consuming on lower-end NAS systems.
 
An alternative solution that does not involve recompliation can be found below.
 
The following override is needed as <code>zfs</code> is implicitly used in partition mounting:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 532: Line 537:
You can customize the mail command with <code>ZED_EMAIL_OPTS</code>. For example, if your upstream mail server requires a certain FROM address: <code>ZED_EMAIL_OPTS = "-r 'noreply@example.com' -s '@SUBJECT@' @ADDRESS@";</code>
You can customize the mail command with <code>ZED_EMAIL_OPTS</code>. For example, if your upstream mail server requires a certain FROM address: <code>ZED_EMAIL_OPTS = "-r 'noreply@example.com' -s '@SUBJECT@' @ADDRESS@";</code>


After a <code>sudo nixos-rebuild switch</code>, you can test ZED notifications by performing a scrub on an existing pool, or on a small test pool:
=== Alternative 2: Enable Mail Notification without Re-compliation ===
 
First, we need to configure a mail transfer agent, the program that sends email:
<syntaxhighlight lang="nix">
{
  programs.msmtp = {
    enable = true;
    setSendmail = true;
    defaults = {
      aliases = "/etc/aliases";
      port = 465;
      tls_trust_file = "/etc/ssl/certs/ca-certificates.crt";
      tls = "on";
      auth = "login";
      tls_starttls = "off";
    };
    accounts = {
      default = {
        host = "mail.example.com";
        passwordeval = "cat /etc/emailpass.txt";
        user = "user@example.com";
        from = "user@example.com";
      };
    };
  };
}
</syntaxhighlight>
 
Then, configure an alias for root account. With this alias configured, all mails sent to root, such as cron job results and failed sudo login events, will be redirected to the configured email account.
 
<syntaxhighlight lang="bash">
tee -a /etc/aliases <<EOF
root: user@example.com
EOF
</syntaxhighlight>
 
Finally, override default zed settings with a custom one:
<syntaxhighlight lang="nix">
{
  services.zfs.zed.settings = {
    ZED_DEBUG_LOG = "/tmp/zed.debug.log";
    ZED_EMAIL_ADDR = [ "root" ];
    ZED_EMAIL_PROG = "${pkgs.msmtp}/bin/msmtp";
    ZED_EMAIL_OPTS = "@ADDRESS@";
 
    ZED_NOTIFY_INTERVAL_SECS = 3600;
    ZED_NOTIFY_VERBOSE = true;
 
    ZED_USE_ENCLOSURE_LEDS = true;
    ZED_SCRUB_AFTER_RESILVER = true;
  };
  # this option does not work; will return error
  services.zfs.zed.enableMail = false;
}
</syntaxhighlight>
 
You can now test this by performing a scrub
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ cd /tmp
# zpool scrub $pool
$ dd if=/dev/zero of=sparse_file bs=1 count=0 seek=512M
$ sudo zpool create test /tmp/sparse_file
$ sudo zpool scrub test
// Check for email when scrub is complete
// Cleanup
$ sudo zpool export test
$ rm sparse_file
</syntaxhighlight>
</syntaxhighlight>