Bcachefs: Difference between revisions

imported>Krutonium
Add information about multi device formatting, as well as information about mounting and compression options.
Onny (talk | contribs)
NixOS installation on bcachefs: Cleanup instruction
 
(21 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[https://bcachefs.org 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.  
[https://bcachefs.org Bcachefs] is a next-generation CoW filesystem that aims to provide features from [[Btrfs]] and [[ZFS]] with a cleaner codebase<ref name=":0">citation needed</ref>, more stability<ref name=":0" />, greater speed<ref name=":0" /> and a GPL-compatible license. It is built upon Bcache and is mainly developed by Kent Overstreet.  


== Installation ==
== Installation ==
Line 57: Line 57:
     --label=hdd.hdd3 /dev/sde \
     --label=hdd.hdd3 /dev/sde \
     --label=ssd.ssd1 /dev/sdf \
     --label=ssd.ssd1 /dev/sdf \
     --label=ssd.ssd2 /dev/sdg
     --label=ssd.ssd2 /dev/sdg \
     --foreground_target=ssd \
     --foreground_target=ssd \
     --promote_target=ssd \
     --promote_target=ssd \
Line 71: Line 71:
     --label=hdd.hdd3 /dev/sde \
     --label=hdd.hdd3 /dev/sde \
     --label=ssd.ssd1 /dev/sdf \
     --label=ssd.ssd1 /dev/sdf \
     --label=ssd.ssd2 /dev/sdg
     --label=ssd.ssd2 /dev/sdg \
     --foreground_target=ssd \
     --foreground_target=ssd \
     --promote_target=ssd \
     --promote_target=ssd \
Line 82: Line 82:
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# bcachefs subvolume snapshot /mnt /mnt/snap1
# bcachefs subvolume snapshot /mnt /mnt/snap1
</syntaxhighlight>
Filesystem check, fix errors and corruptions where a Bcachefs filesystem is on <code>/dev/sda</code>:
<syntaxhighlight lang="console">
# bcachefs fsck /dev/sda
</syntaxhighlight>Change partition encryption password for <code>/dev/sda1</code><syntaxhighlight lang="console">
# bcachefs set-passphrase /dev/sda1
</syntaxhighlight>
</syntaxhighlight>


Line 106: Line 114:
== Tips and tricks ==
== Tips and tricks ==


=== Generate bcachefs enabled installation media ===
=== Generate bcachefs enabled installation media ===
 
Use following Nix [[Flakes|Flake-expression]] to generate a ISO installation image with a bcachefs enabled kernel
Use following Nix [[Flakes|Flake-expression]] to generate a ISO installation image with a bcachefs enabled kernel
 
{{file|flake.nix|nix|<nowiki>
{{file|flake.nix|nix|<nowiki>
{
{
   description = "Bcachefs enabled installation media";
   description = "Bcachefs enabled installation media";
   inputs.nixos.url = "nixpkgs/nixos-23.11";
   inputs.nixos.url = "nixpkgs/nixos-25.11";
   outputs = { self, nixos }: {
   outputs = { self, nixos }: {
     nixosConfigurations = {
     nixosConfigurations = {
Line 121: Line 131:
           "${nixos}/nixos/modules/installer/cd-dvd/installation-cd-minimal-new-kernel-no-zfs.nix"
           "${nixos}/nixos/modules/installer/cd-dvd/installation-cd-minimal-new-kernel-no-zfs.nix"
           ({ lib, pkgs, ... }: {
           ({ lib, pkgs, ... }: {
            # Required as a workaround for bug
            # https://github.com/NixOS/nixpkgs/issues/32279
            environment.systemPackages = [ pkgs.keyutils ];
             boot.supportedFilesystems = [ "bcachefs" ];
             boot.supportedFilesystems = [ "bcachefs" ];
            boot.kernelPackages = lib.mkOverride 0 pkgs.linuxPackages_latest;
           })
           })
         ];
         ];
Line 132: Line 144:


The following commands will generate the iso-image which will be available in the directory <code>./result/iso</code>
The following commands will generate the iso-image which will be available in the directory <code>./result/iso</code>
 
<syntaxhighlight lang="console">
<syntaxhighlight lang="console"># nix build .#nixosConfigurations.exampleIso.config.system.build.isoImage
# git init
# git add flake.nix
# nix build .#nixosConfigurations.exampleIso.config.system.build.isoImage
</syntaxhighlight>
</syntaxhighlight>


Line 161: Line 170:
</syntaxhighlight>
</syntaxhighlight>


In case you want to enable filesystem encryption, there's a workaround for [https://github.com/NixOS/nixpkgs/issues/32279 a bug] affecting NixOS 23.11. Formatting and unlocking the encrypted partition would look like this
Formatting and unlocking the encrypted partition would look like this


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# nix-env -iA nixos.keyutils
# keyctl link @u @s
# keyctl link @u @s
# bcachefs format --encrypted /dev/sda2
# bcachefs format --label=nixos --encrypted /dev/sda2
# bcachefs unlock /dev/sda2
# bcachefs unlock /dev/sda2
</syntaxhighlight>
</syntaxhighlight>


Mount filesystems
Mount filesystems. Use <code>lsblk -o +uuid,fsType | grep bcachefs</code> to get bcachefs partition uuid.
 
There are two ways to mount a bcachefs filesystem. First, by device:


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# mount /dev/sda2 /mnt
# mount /dev/disk/by-uuid/<...> /mnt
# mkdir /mnt/boot
# mkdir /mnt/boot
# mount /dev/disk/by-label/boot /mnt/boot
# mount /dev/disk/by-label/boot /mnt/boot
</syntaxhighlight>
</syntaxhighlight>


For better reliability as with any other filesystem, one should mount the filesystem by UUID, by replacing any drive specifiers with it.
Continue installation as recommended by the [https://nixos.org/manual/nixos/stable/index.html#ch-installation NixOS manual].
When you formatted, there was an external UUID shown, but if you lost it, you can retrieve it using
 
<syntaxhighlight lang="console">
=== Remote encrypted disk unlocking ===
# lsblk -o +uuid,fsType | grep bcachefs
See article on [[Remote disk unlocking#Bcachefs unlocking|remote disk unlocking]] for a guide on how to enable SSH decryption of Bcachefs enabled systems.
</syntaxhighlight>
 
And using it like <code>UUID=<UUID></code> in place of <code>/dev/sda1:/dev/sdb1</code> or even just <code>/dev/sda</code>.
=== Automatically mount encrypted device on boot ===
Since the Bcachefs mount options do [https://github.com/koverstreet/bcachefs-tools/pull/266 not support supplying a key file yet], we could use the <code>bcachefs</code> command and run it on boot using a [[Systemd]] unit:<syntaxhighlight lang="nix">
systemd.services."bcachefs-mount" = {
  after = [ "local-fs.target" ];
  wantedBy = [ "multi-user.target" ];
  environment = {
    DEVICE_PATH = "/dev/sda1";
    MOUNT_POINT = "/mnt";
  };
  script = ''
    #!${pkgs.runtimeShell} -e
 
    ${pkgs.keyutils}/bin/keyctl link @u @s


Continue installation as recommended by the [https://nixos.org/manual/nixos/stable/index.html#ch-installation NixOS manual].
    # Check if the device path exists
    if [ ! -b "$DEVICE_PATH" ]; then
      echo "Error: Device path $DEVICE_PATH does not exist."
      exit 1
    fi


Before <code>nixos-install</code> you'll have to adapt the generated NixOS configuration to also use the latest Linux kernel supporting Bcachefs. Add the following line to the <code>configuration.nix</code> in your installation root (/mnt):
    # Check if the drive is already mounted
    if ${pkgs.util-linux}/bin/mountpoint -q "$MOUNT_POINT"; then
      echo "Drive already mounted at $MOUNT_POINT. Skipping..."
      exit 0
    fi


{{file|/mnt/etc/nixos/configuration.nix|nix|<nowiki>
    # Wait for the device to become available
boot.supportedFilesystems = [ "bcachefs" ];
    while [ ! -b "$DEVICE_PATH" ]; do
boot.kernelPackages = pkgs.linuxPackages_latest;
      echo "Waiting for $DEVICE_PATH to become available..."
</nowiki>}}
      sleep 5
    done


    # Mount the device
    ${pkgs.bcachefs-tools}/bin/bcachefs mount -f /etc/keyfile_test "$DEVICE_PATH" "$MOUNT_POINT"
  '';
  serviceConfig = {
    Type = "oneshot";
    User = "root";
  };
};
</syntaxhighlight>This example unit mounts the Bcachefs encrypted partition <code>/dev/sda1</code> to the target <code>/mnt</code> by using the key file <code>/etc/keyfile_test</code>.
[[Category:Filesystem]]
[[Category:Filesystem]]