Bcachefs: Difference between revisions

From NixOS Wiki
imported>Onny
Update note encryption key bug still affects 23.05
imported>Onny
Updated installation example to UEFI setup
Line 63: Line 63:
Using the installation media generated above, continue the installation as usual following the [https://nixos.org/manual/nixos/stable/index.html#ch-installation instructions of the NixOS manual].  
Using the installation media generated above, continue the installation as usual following the [https://nixos.org/manual/nixos/stable/index.html#ch-installation instructions of the NixOS manual].  


For a BIOS installation, the partitioning needs to be adjusted as following
For a UEFI installation, the partitioning needs to be adjusted as following
 
{{note|Be sure on which disk you'll perform these filesystem operations. All existing data on it will be erased.}}


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# parted /dev/sda -- mklabel msdos
# parted /dev/sda -- mklabel gpt
# parted /dev/sda -- mkpart primary 1MB 512MB
# parted /dev/sda -- mkpart ESP fat32 1MB 512MB
# parted /dev/sda -- set 1 boot on
# parted /dev/sda -- set 1 esp on
# parted /dev/sda -- mkpart primary 512MB 100%
# parted /dev/sda -- mkpart primary 512MB 100%
</syntaxhighlight>
</syntaxhighlight>
Line 75: Line 77:


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# mkfs.ext4 -L boot /dev/sda1
# mkfs.fat -F 32 -n boot /dev/sda1
# mkfs.bcachefs -L nixos /dev/sda2
# mkfs.bcachefs -L nixos /dev/sda2
</syntaxhighlight>
</syntaxhighlight>

Revision as of 17:38, 9 November 2023

Bcachefs is a next-generation CoW filesystem that aims to provide features from Btrfs and ZFS with a cleaner codebase, more stability, greater speed and a GPL-compatible license. It is built upon Bcache and is mainly developed by Kent Overstreet.

Installation

To enable filesystem support and availability of user-space utils, add following line to the system configuration

/etc/nixos/configuration.nix
boot.supportedFilesystems = [ "bcachefs" ];

Usage

Format and mount a single device

# bcachefs format /dev/sda
# mount -t bcachefs /dev/sda /mnt

Format drive with encryption enabled, unlock and mount it afterwards. Following bcachefs commands will ask for a password:

# bcachefs format --encrypt /dev/sda
# bcachefs unlock /dev/sda
# mount -t bcachefs /dev/sda /mnt

Create a subvolume of a mounted bcachefs filesystem. The snapshot of the filesystem state is accessible in the directory /mnt/snap1.

# bcachefs subvolume snapshot /mnt /mnt/snap1

Tips and tricks

Generate bcachefs enabled installation media

Use following Nix expression iso.nix to generate a ISO installation image with a bcachefs enabled kernel

# This module defines a small NixOS installation CD. It does not
# contain any graphical stuff.
{ config, pkgs, lib, ... }:
{
  imports = [
    # Currently fails on NixOS 23.05 to build due to ZFS incompatibility with bcachefs
    #<nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix>
    <nixpkgs/nixos/modules/installer/cd-dvd/installation-cd-minimal-new-kernel-no-zfs.nix>
  ];
  boot.supportedFilesystems = [ "bcachefs" ];
  # kernelPackages already defined in installation-cd-minimal-new-kernel-no-zfs.nix
  boot.kernelPackages = lib.mkOverride 0 pkgs.linuxPackages_testing_bcachefs;
  isoImage.squashfsCompression = "gzip -Xcompression-level 1";
}
# nix-build '<nixpkgs/nixos>' -A config.system.build.isoImage -I nixos-config=iso.nix

NixOS installation on bcachefs

Using the installation media generated above, continue the installation as usual following the instructions of the NixOS manual.

For a UEFI installation, the partitioning needs to be adjusted as following

Note: Be sure on which disk you'll perform these filesystem operations. All existing data on it will be erased.
# parted /dev/sda -- mklabel gpt
# parted /dev/sda -- mkpart ESP fat32 1MB 512MB
# parted /dev/sda -- set 1 esp on
# parted /dev/sda -- mkpart primary 512MB 100%

Formatting the boot partition /dev/sda1 and the root filesystem /dev/sda2

# mkfs.fat -F 32 -n boot /dev/sda1
# mkfs.bcachefs -L nixos /dev/sda2

In case you want to enable filesystem encryption, there's a workaround for a bug affecting NixOS 23.05. Formatting and unlocking the encrypted partition would look like this

# nix-env -iA nixos.keyutils
# keyctl link @u @s
# bcachefs format --encrypt /dev/sda2
# bcachefs unlock /dev/sda2

Mount filesystems

# mount -t bcachefs /dev/sda2 /mnt
# mkdir /mnt/boot
# mount /dev/disk/by-label/boot /mnt/boot

Continue installation as recommended by the NixOS manual.