Skip to content

Commit

Permalink
use a zfs mirror for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
erikarvstedt committed Jul 5, 2022
1 parent 187527a commit 986503b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 17 deletions.
1 change: 1 addition & 0 deletions base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ with lib;
imports = [ ./hardware.nix ];

networking.hostName = "nixbitcoin";
networking.hostId = "d1af0f9b";
time.timeZone = "UTC";

services.openssh = {
Expand Down
70 changes: 70 additions & 0 deletions deployment/2-format-storage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash
set -euxo pipefail

disk1=/dev/sda
disk2=/dev/sdb
bootSize=512MiB
# `swapSize` should equal system RAM.
# This setup will create one swap partition on each disk.
swapSize=${swapSize:-32GiB}

# Remount already formatted storage
mountStorage() {
if ! zfs get type rpool &>/dev/null; then
zpool import -f -N rpool
fi
mount -t zfs -o x-mount.mkdir rpool/root /mnt
mount -t zfs -o x-mount.mkdir rpool/nix /mnt/nix
mount -o x-mount.mkdir ${disk1}2 /mnt/boot1
mount -o x-mount.mkdir ${disk2}2 /mnt/boot2
}

if [[ ${1:-} == remount ]]; then
mountStorage
exit
fi

formatDisk() {
disk=$1
sgdisk --zap-all \
-n 0:0:+1MiB -t 0:ef02 -c 0:bios-boot \
-n 0:0:+$bootSize -t 0:8300 -c 0:boot \
-n 0:0:+$swapSize -t 0:8200 -c 0:swap \
-n 0:0:0 -t 0:bf01 -c 0:root $disk
}
formatDisk $disk1
formatDisk $disk2
mkfs.fat -n boot1 ${disk1}2
mkfs.fat -n boot2 ${disk2}2
mkswap -L swap1 ${disk1}3
mkswap -L swap2 ${disk2}3

# ashift=12
# Set pool sector size to 2^12 to optimize performance for storage devices with 4K sectors.
# Auto-detection of physical sector size (/sys/block/sdX/queue/physical_block_size) can be unreliable.
#
# acltype=posixacl
# Required for / and the systemd journal
#
# xattr=sa
# Improve performance of certain extended attributes
#
# normalization=formD
# Enable UTF-8 normalization for file names
#
zpool create -f \
-o ashift=12 \
-O acltype=posixacl \
-O xattr=sa \
-O normalization=formD \
-O relatime=on \
-O compression=lz4 \
-O dnodesize=auto \
rpool mirror ${disk1}4 ${disk2}4

zfs create -o mountpoint=legacy rpool/root
zfs create -o mountpoint=legacy rpool/nix
zfs create -o mountpoint=none -o refreservation=1G rpool/reserved
zfs set com.sun:auto-snapshot=true rpool/root

mountStorage
73 changes: 57 additions & 16 deletions hardware.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
# Do not modify this file! It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations. Please make changes
# to /etc/nixos/configuration.nix instead.
# Storage deployment is defined in ./deployment/2-format-storage.sh

{ config, lib, pkgs, modulesPath, ... }:

let
# When a device marked with `nonessentialDevice` is unavailable,
# booting succeeds and systemd marks the system as degraded.
# This allows the system to boot when one device fails.
nonessentialDevice = [
"nofail"
"x-systemd.device-timeout=10s"
];

bindMount = srcPath: {
device = srcPath;
options = [ "bind" ];
# Silence warning:
# systemd-fstab-generator: Checking was requested for "<srcPath>", but it is not a device.
noCheck = true;
};
in
{
imports =
[ (modulesPath + "/installer/scan/not-detected.nix")
Expand All @@ -13,21 +29,46 @@
boot.kernelModules = [ "kvm-intel" ];
boot.extraModulePackages = [ ];

boot.loader.grub.enable = true;
boot.loader.grub.version = 2;
boot.loader.grub.device = "/dev/sda";
boot.supportedFilesystems = [ "zfs" ];

# Not needed
systemd.services.zfs-mount.enable = false;

fileSystems."/" =
{ device = "/dev/disk/by-uuid/d070abe5-6a51-4ea5-9365-57828fd0dd30";
fsType = "btrfs";
};
# TODO-EXTERNAL:
# The filesystem definitions below can be replaced with zfs `mountpoint` attributes when
# https://github.com/NixOS/nixpkgs/issues/62644 has been implemented
fileSystems."/" = {
device = "rpool/root";
fsType = "zfs";
};

fileSystems."/boot" =
{ device = "/dev/disk/by-uuid/66958700-90e2-4428-aed5-1cc850f202aa";
fsType = "ext2";
};
fileSystems."/nix" = {
device = "rpool/nix";
fsType = "zfs";
};

swapDevices = [ ];
fileSystems."/boot1" = {
device = "/dev/disk/by-label/boot1";
fsType = "vfat";
options = nonessentialDevice;
};

powerManagement.cpuFreqGovernor = lib.mkDefault "powersave";
fileSystems."/boot2" = {
device = "/dev/disk/by-label/boot2";
fsType = "vfat";
options = nonessentialDevice;
};

swapDevices = [
{ device = "/dev/disk/by-label/swap1"; options = nonessentialDevice; }
{ device = "/dev/disk/by-label/swap2"; options = nonessentialDevice; }
];

boot.loader.grub = {
enable = true;
mirroredBoots = [
{ path = "/boot1"; devices = [ "/dev/sda" ]; }
{ path = "/boot2"; devices = [ "/dev/sdb" ]; }
];
};
}
2 changes: 1 addition & 1 deletion website/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<h1>nix-bitcoin demo node</h1>

This webpage is hosted on a nix-bitcoin node (<a href="https://github.com/fort-nix/nixbitcoin.org/">source code</a>) running bitcoind, clightning, clboss, electrs, btcpayserver, and joinmarket. The node is deployed using btrfs on Hetzner Online.
This webpage is hosted on a nix-bitcoin node (<a href="https://github.com/fort-nix/nixbitcoin.org/">source code</a>) running bitcoind, clightning, clboss, electrs, btcpayserver, and joinmarket. The node is deployed using ZFS on Hetzner Online.

<h4>clightning</h4>
<code>
Expand Down

0 comments on commit 986503b

Please sign in to comment.