Btrfs: Difference between revisions

Transfer snapshot: Add --compressed-data trick
Tags: Mobile edit Mobile web edit
Pigeon (talk | contribs)
 
(5 intermediate revisions by 4 users not shown)
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 of NixOS on btrfs =


{{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.}}
Line 142: Line 142:


== Deduplication ==
== Deduplication ==
 
Files with (partially) equal contents can be deduplicated using [https://github.com/Zygo/bees bees] or [https://github.com/markfasheh/duperemove duperemove]. The [https://btrfs.readthedocs.io/en/latest/Deduplication.html upstream wiki entry] on deduplication provides an overview of available features of these programs.
Files with (partially) equal contents can be deduplicated using [https://github.com/Zygo/bees bees] or [https://github.com/markfasheh/duperemove duperemove].


bees can be configured in <code>configuration.nix</code>:
bees can be configured in <code>configuration.nix</code>:
Line 164: Line 163:
== Subvolumes ==
== Subvolumes ==


Create a subvolume
To display all subvolumes within a mounted btrfs filesystem:
 
<syntaxhighlight lang="bash">
btrfs subvolume list -t /mnt
</syntaxhighlight>
 
To create a new subvolume at a specified location:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 170: Line 175:
</syntaxhighlight>
</syntaxhighlight>


Remove a subvolume
To remove an existing subvolume:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
btrfs subvolume delete /mnt/nixos
btrfs subvolume delete /mnt/nixos
</syntaxhighlight>
</syntaxhighlight>
=== Top-level vs nested subvolumes ===
In btrfs, subvolumes can be created either at the top level of the filesystem or within other subvolumes.
* Top-level subvolumes are created directly under the filesystem's root. By default, the root volume ID is 5. Top-level subvolumes are easier to snapshot, roll back or destroy independently. This is good for things such as <code>/home</code> or <code>/nix</code>.
* Nested subvolumes are created inside an existing subvolume or directory within the filesystem. All nested subvolumes inherit the mount status of their parent unless mounted separately. This layout is useful for organizing related subvolumes under a common namespace. For example, a top-level subvolume such as <code>/srv/nfs</code> can contain multiple nested subvolumes like <code>/srv/nfs/export1</code> and <code>/srv/nfs/export2</code>.


== Snapshots ==
== Snapshots ==
Line 224: Line 239:


= Tips and tricks =
= Tips and tricks =
== Backup ==
[[Btrbk]] is a tool for creating snapshots and remote backups of btrfs subvolumes.


== Installation with encryption ==
== Installation with encryption ==
Line 306: Line 325:
== 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;">
{{Warning|Note that migrating your existing root filesystem can cause data loss or make your system unbootable. Make sure to backup the partition or your files. Proceed only if you know what you're doing!}}
    <div style="color: var(--border-color-error); font-size: 40px; margin-right: 15px; background: var(--background-color-error-subtle); display: flex; line-height: 0;  align-items: center;">⚠</div>
    <div style="color: var(--border-color-error); font-size: 15px; font-style: normal; font-weight: 400; line-height: normal; text-align: left;">Note that migrating your existing root filesystem can cause data loss or make your system unbootable. Make sure to backup the partition or your files. Proceed only if you know what you're doing!</div>
</div>


To convert the existing filesystem (Ext3/4) to Btrfs, boot into a NixOS live system and run the following commandː<syntaxhighlight lang="sh">
To convert the existing filesystem (Ext3/4) to Btrfs, boot into a NixOS live system and run the following commandː<syntaxhighlight lang="sh">
Line 332: Line 348:
btrfs subvolume delete /btrfs/ext2_saved
btrfs subvolume delete /btrfs/ext2_saved
</syntaxhighlight>
</syntaxhighlight>
=== TRIM support ===
{{main|Filesystems#SSD TRIM support}}
Refer to [https://btrfs.readthedocs.io/en/latest/Trim.html btrfs docs] on how Trim works with btrfs through the <code>discard</code> mount option.
[[Category: Configuration]]
[[Category: Configuration]]
[[Category:Filesystem]]
[[Category:Filesystem]]