Skip to content

Instantly share code, notes, and snippets.

@purefan
Created August 7, 2024 23:15
Show Gist options
  • Save purefan/7c0dc2224b4c7b7db5b629448d31b2ac to your computer and use it in GitHub Desktop.
Save purefan/7c0dc2224b4c7b7db5b629448d31b2ac to your computer and use it in GitHub Desktop.
zfs-setup.nix
{
# configuration.nix
systemd.services.zfs-setup = {
description = "ZFS Setup";
after = [ "zfs-import.target" ];
wants = [ "zfs-import.target" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.writeShellScriptBin "zfs-setup" ''
LOGFILE=/tmp/zfs-setup.log
echo "Starting..." >> $LOGFILE 2>&1
echo "Path to zpool is ${pkgs.zfs}/bin/zpool" >> $LOGFILE 2>&1
echo "Path to zfs is ${pkgs.zfs}/bin/zfs" >> $LOGFILE 2>&1
echo "Checking zpool" >> $LOGFILE 2>&1
if ! ${pkgs.zfs}/bin/zpool list pool-01 >> $LOGFILE 2>&1; then
${pkgs.zfs}/bin/zpool create pool-01 /dev/sda /dev/sdb /dev/sdc >> $LOGFILE 2>&1
fi
echo "Checking log device" >> $LOGFILE 2>&1
if ! ${pkgs.zfs}/bin/zpool status pool-01 | grep -q 'log' >> $LOGFILE 2>&1; then
${pkgs.zfs}/bin/zpool add pool-01 log /dev/sdd >> $LOGFILE 2>&1
fi
echo "Checking cache device" >> $LOGFILE 2>&1
if ! ${pkgs.zfs}/bin/zpool status pool-01 | grep -q 'cache' >> $LOGFILE 2>&1; then
${pkgs.zfs}/bin/zpool add pool-01 cache /dev/sde >> $LOGFILE 2>&1
fi
echo "Checking dataset" >> $LOGFILE 2>&1
if ! ${pkgs.zfs}/bin/zfs list pool-01/apps >> $LOGFILE 2>&1; then
${pkgs.zfs}/bin/zfs create pool-01/apps >> $LOGFILE 2>&1
fi
''}";
RemainAfterExit = true;
ExecStartPre = "${pkgs.coreutils}/bin/mkdir -p /tmp";
RootDirectoryStartOnly = true;
};
wantedBy = [ "multi-user.target" ];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment