I've answered the "How much space does a snapshot take?" question many times. When I give the answer "None initially", this invariably leads to some confusion and more questions. I'll try and explain this as plainly as possible.
If @data contains a file named "abc.txt", that same file appears in @data2 when the snapshot is taken - the file is now "owned" by both subvolumes. If I delete abc.txt from @data, it still resides in @data2. The deletion of abc.txt didn't actually delete the file, it just deleted the ownership from @data. @data2 still owns it so the file remains. If I then delete it from @data2 also, the file is now actually deleted from the file system and the space it occupied is now free.
Pictures can be worth 1000 words so I made a 256MB file system with nothing on it:
Then I made the subvolume @data and put ten 15M files in it:
You can see the small file system is almost full at 87% used.
Note that the "btrfs filesystem df" command shows a more accurate depiction of the amount of space actually used. You can see that space is consumed by the System, Metadata, and GlobalReserve areas. This doesn't change unless you resize your file system or "balance" it to distribute the space differently. I just used the default settings when I made the example file system
Now let's take a snapshot and look again:
and lets look inside:
Two subvolumes, two sets of files listed, but occupying the exact same space and the exact same amount - 167M.
Now I'll delete 2 files from @data:
No additional free space but clearly, @data only has eight files remaining. You can see that @data2 still has all 10 files and that's why no additional free space is available. If I had deleted the files from @data2 instead, the results would have been the same.
Now let's delete those same two files from @data2. If you've been following along and understanding, you would expect to see 30MB of new free space:
No surprises here. DF shows the 30M is now free.
An interesting effect of multiple snapshots of the same source subvolume is how the used data space accumulates. When any file gets added to any subvolume (remember snapshots are also subvolumes) free space is consumed, but no free space is added until all copies of any given file (or the snapshot contain the file) are deleted. This is why good housekeeping practices are necessary when making snapshots. All your snapshots will grow the amount of space they consume as changes are made to the source (the original) subvolume.
I hope this helps explain how snapshots use your drive space.
- I have a subvolume named "@data" and I take a snapshot of it as "@data2".
- At this moment, both @data and @data2 contain exactly the same files but I have not used any additional data space.
- This is because BTRFS doesn't make a copy of any files when you take a snapshot.
- BTRFS assigns ownership of all files in @data to both @data and @data2.
- Snapshots are sort of like dependent subvolumes. They act as subvolumes, but share all or a portion of the data space on the file system with another.
- If I delete a file from either subvolume that file remains in the other, so no free space is created.
- If I add a file to @data, it does not appear in @data2 (or vice-versa) because the new file was added after the snapshot was taken.
- This newly added file consumes the expected amount of space.
If @data contains a file named "abc.txt", that same file appears in @data2 when the snapshot is taken - the file is now "owned" by both subvolumes. If I delete abc.txt from @data, it still resides in @data2. The deletion of abc.txt didn't actually delete the file, it just deleted the ownership from @data. @data2 still owns it so the file remains. If I then delete it from @data2 also, the file is now actually deleted from the file system and the space it occupied is now free.
Pictures can be worth 1000 words so I made a 256MB file system with nothing on it:
Code:
[FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test/@data[/B][/COLOR][COLOR=#000000]$ df[/COLOR] Filesystem Size Used Avail Use% Mounted on [/FONT][FONT=monospace][COLOR=#000000]/dev/sda2 256M 17M 175M 9% /mnt/test[/COLOR] [COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ ll[/COLOR] total 16 drwxr-xr-x 1 root root 0 Jun 5 11:09 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 194 Jun 5 11:11 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] [/FONT][FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ sudo btrfs fi sh /mnt/test[/COLOR] Label: none uuid: 9405e441-c232-4e62-9318-177fb9f3621c Total devices 1 FS bytes used 128.00KiB devid 1 size 256.00MiB used 88.00MiB path /dev/sda2[/FONT]
Code:
[FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test/@data[/B][/COLOR][COLOR=#000000]$ df[/COLOR] Filesystem Size Used Avail Use% Mounted on [/FONT][COLOR=#000000][FONT=monospace]/dev/sda2 256M 167M 25M 87% /mnt/test [/FONT][/COLOR] [FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test/@data[/B][/COLOR][COLOR=#000000]$ ll[/COLOR] total 153616 drwxr-xr-x 1 root root 100 Jun 5 11:23 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 10 Jun 5 11:21 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file0 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file1 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file2 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file3 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file4 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file5 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file6 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file7 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file8 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file9 [COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test/@data[/B][/COLOR][COLOR=#000000]$ sudo btrfs fi df /mnt/test[/COLOR] Data, single: total=175.00MiB, used=150.00MiB System, DUP: total=8.00MiB, used=16.00KiB Metadata, DUP: total=32.00MiB, used=320.00KiB GlobalReserve, single: total=16.00MiB, used=0.00B [COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test/@data[/B][/COLOR][COLOR=#000000]$ [/COLOR][/FONT]
Note that the "btrfs filesystem df" command shows a more accurate depiction of the amount of space actually used. You can see that space is consumed by the System, Metadata, and GlobalReserve areas. This doesn't change unless you resize your file system or "balance" it to distribute the space differently. I just used the default settings when I made the example file system
Now let's take a snapshot and look again:
Code:
[COLOR=#54FF54][FONT=monospace][B]stuart@office[/B][/FONT][/COLOR][COLOR=#000000][FONT=monospace]:[/FONT][/COLOR][COLOR=#5454FF][FONT=monospace][B]/mnt/test/@data[/B][/FONT][/COLOR][COLOR=#000000][FONT=monospace]$ df[/FONT][/COLOR] [FONT=monospace]Filesystem Size Used Avail Use% Mounted on[/FONT] [FONT=monospace][COLOR=#000000]/dev/sda2 256M 167M 25M 87% /mnt/test [/COLOR][/FONT][COLOR=#54FF54][FONT=monospace][B]stuart@office[/B][/FONT][/COLOR][COLOR=#000000][FONT=monospace]:[/FONT][/COLOR][COLOR=#5454FF][FONT=monospace][B]/mnt/test[/B][/FONT][/COLOR][COLOR=#000000][FONT=monospace]$ sudo btrfs su sn @data @data2[/FONT][/COLOR][FONT=monospace] [COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ ll[/COLOR] total 16 drwxr-xr-x 1 root root 22 Jun 5 11:28 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 194 Jun 5 11:11 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 100 Jun 5 11:23 [COLOR=#5454FF][B]@data[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 100 Jun 5 11:23 [COLOR=#5454FF][B]@data2[/B][/COLOR][COLOR=#000000]/[/COLOR][/FONT][FONT=monospace] [/FONT][COLOR=#54FF54][FONT=monospace][B]stuart@office[/B][/FONT][/COLOR][COLOR=#000000][FONT=monospace]:[/FONT][/COLOR][COLOR=#5454FF][FONT=monospace][B]/mnt/test/@data[/B][/FONT][/COLOR][COLOR=#000000][FONT=monospace]$ df[/FONT][/COLOR] [FONT=monospace]Filesystem Size Used Avail Use% Mounted on[/FONT] [FONT=monospace][COLOR=#000000]/dev/sda2 256M 167M 25M 87% /mnt/test[/COLOR][/FONT]
Code:
[FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ ll @data[/COLOR] total 153616 drwxr-xr-x 1 root root 100 Jun 5 11:23 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 22 Jun 5 11:28 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file0 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file1 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file2 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file3 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file4 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file5 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file6 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file7 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file8 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file9 [COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ ll @data2[/COLOR] total 153616 drwxr-xr-x 1 root root 100 Jun 5 11:23 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 22 Jun 5 11:28 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file0 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file1 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file2 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file3 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file4 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file5 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file6 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file7 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file8 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file9[/FONT]
Now I'll delete 2 files from @data:
Code:
[FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test/@data[/B][/COLOR][COLOR=#000000]$ df[/COLOR] Filesystem Size Used Avail Use% Mounted on [/FONT][COLOR=#000000][FONT=monospace]/dev/sda2 256M 167M 25M 87% /mnt/test [/FONT][/COLOR][FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ sudo rm @data/file8[/COLOR] [/FONT][FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ sudo rm @data/file9[/COLOR] [/FONT][FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ ll @data[/COLOR] total 122896 drwxr-xr-x 1 root root 80 Jun 5 12:09 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 22 Jun 5 11:28 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file0 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file1 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file2 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file3 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file4 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file5 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file6 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file7 [COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ ll @data2[/COLOR] total 153616 drwxr-xr-x 1 root root 100 Jun 5 11:23 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 22 Jun 5 11:28 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file0 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file1 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file2 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file3 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file4 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file5 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file6 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file7 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file8 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file9 [/FONT][FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test/@data[/B][/COLOR][COLOR=#000000]$ df[/COLOR] Filesystem Size Used Avail Use% Mounted on [/FONT][COLOR=#000000][FONT=monospace]/dev/sda2 256M 167M 25M 87% /mnt/test [/FONT][/COLOR][FONT=monospace] [/FONT]
Now let's delete those same two files from @data2. If you've been following along and understanding, you would expect to see 30MB of new free space:
Code:
[FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test/@data[/B][/COLOR][COLOR=#000000]$ df[/COLOR] Filesystem Size Used Avail Use% Mounted on [/FONT][COLOR=#000000][FONT=monospace]/dev/sda2 256M 167M 25M 87% /mnt/test [/FONT][/COLOR][FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ sudo rm @data2/file8[/COLOR] [/FONT][FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ sudo rm @data2/file9[/COLOR][/FONT][FONT=monospace] [COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ ll @data[/COLOR] total 122896 drwxr-xr-x 1 root root 80 Jun 5 12:09 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 22 Jun 5 11:28 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file0 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file1 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file2 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file3 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file4 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file5 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file6 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file7 [COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ ll @data2[/COLOR] total 122896 drwxr-xr-x 1 root root 80 Jun 5 12:15 [COLOR=#5454FF][B].[/B][/COLOR][COLOR=#000000]/[/COLOR] drwxr-xr-x 1 root root 22 Jun 5 11:28 [COLOR=#5454FF][B]..[/B][/COLOR][COLOR=#000000]/[/COLOR] -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file0 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file1 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file2 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file3 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file4 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file5 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file6 -rw-r--r-- 1 root root 15728640 Jun 5 11:23 file7 [/FONT][COLOR=#54FF54][FONT=monospace][B]stuart@office[/B][/FONT][/COLOR][COLOR=#000000][FONT=monospace]:[/FONT][/COLOR][COLOR=#5454FF][FONT=monospace][B]/mnt/test/@data[/B][/FONT][/COLOR][COLOR=#000000][FONT=monospace]$ df[/FONT][/COLOR] [FONT=monospace]Filesystem Size Used Avail Use% Mounted on [/FONT][FONT=monospace][COLOR=#000000]/dev/sda2 256M 137M 55M 72% /mnt/test [/COLOR][/FONT][FONT=monospace][COLOR=#54FF54][B]stuart@office[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]/mnt/test[/B][/COLOR][COLOR=#000000]$ sudo btrfs fi df /mnt/test[/COLOR] [sudo] password for stuart: Data, single: total=139.00MiB, used=120.00MiB System, DUP: total=8.00MiB, used=16.00KiB Metadata, DUP: total=32.00MiB, used=288.00KiB GlobalReserve, single: total=16.00MiB, used=0.00B [/FONT]
An interesting effect of multiple snapshots of the same source subvolume is how the used data space accumulates. When any file gets added to any subvolume (remember snapshots are also subvolumes) free space is consumed, but no free space is added until all copies of any given file (or the snapshot contain the file) are deleted. This is why good housekeeping practices are necessary when making snapshots. All your snapshots will grow the amount of space they consume as changes are made to the source (the original) subvolume.
I hope this helps explain how snapshots use your drive space.
Comment