Btrfs: Difference between revisions

Ponder (talk | contribs)
m Scrubbing: Copyedit for grammar & missing small words
Ponder (talk | contribs)
m Usage: Copyedit incl combine short paras
Line 140: Line 140:
== Usage ==
== Usage ==


=== Subvolume ===
=== Subvolumes ===


Create a subvolume
Create a subvolume
Line 148: Line 148:
</syntaxhighlight>
</syntaxhighlight>


Removing a subvolume
Remove a subvolume


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 156: Line 156:
=== 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.
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.


Because of that, there is no special location for snapshots - you need to decide where you want to store them for yourself. It can be a simple directory inside root subvolume, or a directory inside a dedicated "snapshots" subvolume.
For this example we are going to store snapshots in a directory <code>/snapshots</code>, that has to be created beforehand with <code>sudo mkdir /snapshots</code>


For this example we are going to store snapshots in a simple directory <code>/snapshots</code>, that has to be created beforehand with <code>sudo mkdir /snapshots</code>
To take a read-only (<code>-r</code>) snapshot called <code>home_snapshot_202302</code> of the subvolume mounted at <code>/home</code>
 
Taking a read-only (<code>-r</code>) snapshot called <code>home_snapshot_202302</code> of the subvolume mounted at <code>/home</code>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 168: Line 166:
</syntaxhighlight>
</syntaxhighlight>


You can also snapshot the root subvolume. But keep in mind, that nested subvolumes are '''not''' part of a snapshot. So if you have subvolumes <code>/nix /home</code>, taking snapshot of <code>/</code> will not include them.
You can also snapshot the root subvolume. But keep in mind that nested subvolumes are '''not''' part of a snapshot. So if you have subvolumes <code>/nix /home</code>, taking a snapshot of <code>/</code> will not include them.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 174: Line 172:
</syntaxhighlight>
</syntaxhighlight>


Make snapshot read-write again
Make snapshot read-write againː


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 180: Line 178:
</syntaxhighlight>
</syntaxhighlight>


However, changing read-only property of a snapshot in-place may [//lore.kernel.org/linux-btrfs/06e92a0b-e71b-eb21-edb5-9d2a5513b718@gmail.com/ causes issues] with any future incremental send/receive.
However, changing read-only property of a snapshot in-place may [//lore.kernel.org/linux-btrfs/06e92a0b-e71b-eb21-edb5-9d2a5513b718@gmail.com/ causes issues] with any future incremental send/receive. Instead, a read-only snapshot itself (being a simple subvolume) can be snapshotted again as a read-write snapshot like this:<syntaxhighlight lang="bash">
 
Instead, a read-only snapshot itself (being a simple subvolume) can be snapshoted again as a read-write snapshot like this:
<syntaxhighlight lang="bash">
btrfs subvolume snapshot /snapshots/home_snapshot_202302 /snapshots/home_snapshot_202302_rw
btrfs subvolume snapshot /snapshots/home_snapshot_202302 /snapshots/home_snapshot_202302_rw
</syntaxhighlight>
</syntaxhighlight>


Or it can be restored directly to <code>/home</code> straight away like this:
Or it can be restored directly to <code>/home</code> straight away like this:
{{warning|1=this will delete current <code>/home</code> and restore the snapshot! <code>/home</code> must be unmounted for this operation}}
{{warning|1=this will delete the current <code>/home</code> and restore the snapshot! <code>/home</code> must be unmounted for this operation}}


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 194: Line 189:
btrfs subvolume snapshot /snapshots/home_snapshot_202302 /home
btrfs subvolume snapshot /snapshots/home_snapshot_202302 /home
</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 or 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>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 215: Line 208:
</syntaxhighlight>
</syntaxhighlight>


You can use any device paritition for your bootloader # Notice that this bootloader is unencrypted on default:
You can use any device partition for your bootloader. Note that this bootloader is unencrypted by default:


<code>
<code>mkfs.vfat -n BOOT "$DISK"p1</code>
mkfs.vfat -n BOOT "$DISK"p1  
</code>


==== Creating Subvolumes ====
==== Creating subvolumes ====


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 241: Line 232:
Unmount to mount on the subvolumes for the next steps:
Unmount to mount on the subvolumes for the next steps:


<code>
<code>umount /mnt</code>
umount /mnt  
 
</code>
Once the subvolumes have been created, mount them with the desired options.


Once the subvolumes has been created, mount them with the options.
Example with [https://facebook.github.io/zstd/ Zstandard compression] and noatime:
Example with [https://facebook.github.io/zstd/ Zstandard compression] with noatime:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
mount -o subvol=root,compress=zstd,noatime /dev/mapper/enc /mnt  
mount -o subvol=root,compress=zstd,noatime /dev/mapper/enc /mnt