Btrfs: Difference between revisions

Pigs (talk | contribs)
Compression: talk about compression algos and levels
Pigs (talk | contribs)
Fix section headers, update swap file section to let nixos handle swap file creating
Line 3: Line 3:
{{note| Use [https://github.com/nix-community/disko/ disko] to manage your NixOS storage layout declaratively. The following shows a manual approach as seen in traditional Linux distributions.}}
{{note| Use [https://github.com/nix-community/disko/ disko] to manage your NixOS storage layout declaratively. The following shows a manual approach as seen in traditional Linux distributions.}}


== Installation ==
= Installation =


{{note|The following example is for EFI enabled systems. Adjust commands accordingly for a BIOS installation.}}
{{note|The following example is for EFI enabled systems. Adjust commands accordingly for a BIOS installation.}}


=== Partition the disk ===
== Partition the disk ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# printf "label: gpt\n,550M,U\n,,L\n" | sfdisk /dev/sdX
# printf "label: gpt\n,550M,U\n,,L\n" | sfdisk /dev/sdX
</syntaxhighlight>
</syntaxhighlight>


=== Format partitions and create subvolumes ===
== Format partitions and create subvolumes ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# nix-shell -p btrfs-progs
# nix-shell -p btrfs-progs
Line 26: Line 26:
</syntaxhighlight>
</syntaxhighlight>


=== Mount the partitions and subvolumes ===
== Mount the partitions and subvolumes ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# mount -o compress=zstd,subvol=root /dev/sdX2 /mnt
# mount -o compress=zstd,subvol=root /dev/sdX2 /mnt
Line 37: Line 37:
</syntaxhighlight>
</syntaxhighlight>


=== Install NixOS ===
== Install NixOS ==
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# nixos-generate-config --root /mnt
# nixos-generate-config --root /mnt
Line 44: Line 44:
</syntaxhighlight>
</syntaxhighlight>


== Configuration ==
= Configuration =


=== Compression ===
== Compression ==


<code>nixos-generate-config</code> doesn't detect mount options automatically. To enable compression, you must specify them manually and other mount options in your <code>configuration.nix</code>:
<code>nixos-generate-config</code> doesn't detect mount options automatically. To enable compression, you must specify them manually and other mount options in your <code>configuration.nix</code>:
Line 73: Line 73:
{{note| Compression is applied only to newly written data. Existing data won't be compressed unless rewritten. (e.g., <code>btrfs filesystem defrag -r -v -czstd /path</code>)}}
{{note| Compression is applied only to newly written data. Existing data won't be compressed unless rewritten. (e.g., <code>btrfs filesystem defrag -r -v -czstd /path</code>)}}


=== Swap file ===
== Swap file ==
 
Creating a separate subvolume for the swap file is optional. It is not required for functionality but can help with organization or snapshot management. Be sure to regenerate your <code>hardware-configuration.nix</code> if you choose to do this.


Optionally, create a separate subvolume for the swap file. Be sure to regenerate your <code>hardware-configuration.nix</code> if you choose to do this.
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
# mkdir -p /mnt
# mkdir -p /mnt
Line 83: Line 84:
# mkdir /swap
# mkdir /swap
# mount -o noatime,subvol=swap /dev/sdXY /swap
# mount -o noatime,subvol=swap /dev/sdXY /swap
# nixos-generate-config
</syntaxhighlight>
</syntaxhighlight>


Then, create the swap file and adjust its size as desired:
Finally, define a swap file in your configuration and run <code>nixos-rebuild switch</code>:


<syntaxhighlight lang="console">
{{file|/etc/nixos/configuration.nix|nix|
# btrfs filesystem mkswapfile --size 8g --uuid clear /swap/swapfile
<nowiki>
</syntaxhighlight>
swapDevices = [{
  device = "/swap/swapfile";
  size = 8*1024; # Creates an 8GB swap file
}];
</nowiki>
}}
 
NixOS will automatically create the swap file with the appropriate attributes for Btrfs including disabling copy on write.
 
{{note| On systems where you do need to manually prepare a swap file on Btrfs, you can use <code>btrfs filesystem mkswapfile</code> utility, e.g.: <br><code># btrfs filesystem mkswapfile --uuid clear /swap/swapfile</code>}}


Finally, add the swap file to your configuration and <code>nixos-rebuild switch</code>:
For more NixOS swap configuration options, see [[Swap]]. Additonal Btrfs swapfile usage can be found at [https://btrfs.readthedocs.io/en/latest/Swapfile.html the Btrfs docs].
<syntaxhighlight lang="nix">
swapDevices = [ { device = "/swap/swapfile"; } ];
</syntaxhighlight>


=== Scrubbing ===
== Scrubbing ==


Btrfs filesystems by default keep checksums for all files, to monitor if the file has changed due to hardware malfunctions or other external effects.
Btrfs filesystems by default keep checksums for all files, to monitor if the file has changed due to hardware malfunctions or other external effects.
Line 133: Line 141:
You can check the status of the ongoing scrubbing process with the same <code>status</code> command as above.
You can check the status of the ongoing scrubbing process with the same <code>status</code> command as above.


=== Deduplication ===
== Deduplication ==


Files with (partially) equal contents can be deduplicated using [https://github.com/Zygo/bees bees] or [https://github.com/markfasheh/duperemove duperemove].
Files with (partially) equal contents can be deduplicated using [https://github.com/Zygo/bees bees] or [https://github.com/markfasheh/duperemove duperemove].
Line 152: Line 160:
This will run the daemon in the background. To disable auto-start, use <code>systemd.services."beesd@root".wantedBy = lib.mkForce [ ];</code> for each filesystem.
This will run the daemon in the background. To disable auto-start, use <code>systemd.services."beesd@root".wantedBy = lib.mkForce [ ];</code> for each filesystem.


== Usage ==
= Usage =


=== Subvolumes ===
== Subvolumes ==


Create a subvolume
Create a subvolume
Line 168: Line 176:
</syntaxhighlight>
</syntaxhighlight>


=== Snapshots ===
== Snapshots ==


A snapshot in btrfs is simply a subvolume that shares its data (and metadata) with some other subvolume, using btrfs's CoW capabilities. Because of that, there is no special location for snapshots and you can decide where you want to store them. It can be a simple directory inside the root subvolume, or a directory inside a dedicated "snapshots" subvolume.
A snapshot in btrfs is simply a subvolume that shares its data (and metadata) with some other subvolume, using btrfs's CoW capabilities. Because of that, there is no special location for snapshots and you can decide where you want to store them. It can be a simple directory inside the root subvolume, or a directory inside a dedicated "snapshots" subvolume.
Line 204: Line 212:
</syntaxhighlight>
</syntaxhighlight>
After this you can mount <code>/home</code> again.
After this you can mount <code>/home</code> again.
=== Transfer snapshot ===
 
== Transfer snapshot ==


Sending the snapshot <code>/snapshots/nixos_snapshot_202302</code> compressed to a remote host via ssh at <code>root@192.168.178.110</code> and saving it to a subvolume mounted on a directory at <code>/mnt/nixos</code>
Sending the snapshot <code>/snapshots/nixos_snapshot_202302</code> compressed to a remote host via ssh at <code>root@192.168.178.110</code> and saving it to a subvolume mounted on a directory at <code>/mnt/nixos</code>
Line 212: Line 221:
</syntaxhighlight>
</syntaxhighlight>


== Tips and tricks ==
= Tips and tricks =
 
== Installation with encryption ==


=== Installation with encryption ===
Using [https://en.wikipedia.org/wiki/Linux_Unified_Key_Setup Luks2]:
Using [https://en.wikipedia.org/wiki/Linux_Unified_Key_Setup Luks2]:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 226: Line 236:
<code>mkfs.vfat -n BOOT "$DISK"p1</code>
<code>mkfs.vfat -n BOOT "$DISK"p1</code>


==== Creating subvolumes ====
=== Creating subvolumes ===


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 292: Line 302:
</syntaxhighlight >
</syntaxhighlight >


=== Convert Ext3/Ext4 system partition to Btrfs ===
== Convert Ext3/Ext4 system partition to Btrfs ==


<div style="border: 1px solid var(--border-color-error); background: var(--background-color-error-subtle); padding: 30px; border-radius: 5px; margin: 10px 0px; display: flex; align-items: center;">
<div style="border: 1px solid var(--border-color-error); background: var(--background-color-error-subtle); padding: 30px; border-radius: 5px; margin: 10px 0px; display: flex; align-items: center;">