how to delete a file in a snapper btrfs snapshot

This document (000019594) is provided subject to the disclaimer at the end of this document.

Environment

SUSE Linux Enterprise Server 15 SP2
SUSE Linux Enterprise Server 15 SP1
SUSE Linux Enterprise Server 15
SUSE Linux Enterprise Server 12 SP5
SUSE Linux Enterprise Server 12 SP4
 

Situation

Sometimes one has to delete files to save badly needed space on the filesystem. Though it is beneficial in most cases that snapper keeps a copy of these files in a snapshot,  it prevents from freeing space which might be needed for other tasks.

Resolution

To delete a file in a snapshot it has to be made writable:

btrfs property set /.snapshots/<version>/snapshot ro false
rm /.snapshots/<version>/snapshot/<file>


Make it read-only again:

btrfs property set /.snapshots/<version>/snapshot ro true

WARNING:
- The initial Btrfs snapshot 1 /.snapshots/1/snapshot should be handled differently.
- /.snapshots/1/snapshot is writable by default and should be kept writable.
- So in case you need to remove any files from the initial Btrfs snapshot 1, please DO NOT run "btrfs property set /.snapshots/1/snapshot ro false" and "btrfs property set /.snapshots/1/snapshot ro true" - it should be sufficient to just run "rm /.snapshots/1/snapshot/<file>"
- If you set /.snapshots/1/snapshot to read-only, the entire Btrfs filesystem would become read-only - if that happens, you can fix that with "btrfs property set /.snapshots/1/snapshot ro false"

Additional Information

Following an example. In this test the following will happen:
  • create a large file, 
  • create snapshots with invoking zypper 
  • clean up afterwards.

At first please check the available space:

china:/ # df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G   12K  1.9G   1% /dev/shm
tmpfs           1.9G  9.0M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/vda2        55G  3.1G   51G   6% /
/dev/vda2        55G  3.1G   51G   6% /boot/grub2/x86_64-efi
/dev/vda2        55G  3.1G   51G   6% /opt
/dev/vda2        55G  3.1G   51G   6% /root
/dev/vda2        55G  3.1G   51G   6% /tmp
/dev/vda2        55G  3.1G   51G   6% /boot/grub2/i386-pc
/dev/vda2        55G  3.1G   51G   6% /usr/local
/dev/vda2        55G  3.1G   51G   6% /srv
/dev/vda2        55G  3.1G   51G   6% /.snapshots
/dev/vda2        55G  3.1G   51G   6% /var
tmpfs           381M     0  381M   0% /run/user/0


Create a large file:

dd if=/dev/urandom of=/test.img bs=1M count=10000

Check again disk usage:

china:/ # df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G   12K  1.9G   1% /dev/shm
tmpfs           1.9G  9.0M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/vda2        55G   13G   41G  25% /
/dev/vda2        55G   13G   41G  25% /boot/grub2/x86_64-efi
/dev/vda2        55G   13G   41G  25% /opt
/dev/vda2        55G   13G   41G  25% /root
/dev/vda2        55G   13G   41G  25% /tmp
/dev/vda2        55G   13G   41G  25% /boot/grub2/i386-pc
/dev/vda2        55G   13G   41G  25% /usr/local
/dev/vda2        55G   13G   41G  25% /srv
/dev/vda2        55G   13G   41G  25% /.snapshots
/dev/vda2        55G   13G   41G  25% /var
tmpfs           381M     0  381M   0% /run/user/0


Install any package ( this will invoke snapper and snapper will create two snapshots pre and post )

zypper in emacs
....


and a second one :

zypper in virt-viewer
...


Find out in which snapshots the file is present:

china:/ # find /.snapshots/ -name test.img
.snapshots/1/snapshot/test.img
.snapshots/34/snapshot/test.img
.snapshots/35/snapshot/test.img
.snapshots/36/snapshot/test.img
.snapshots/37/snapshot/test.img


To delete a file in a snapshot it has to be made writable:

btrfs property set /.snapshots/34/snapshot ro false
rm /.snapshots/34/snapshot/test.img


Make it read-only again:

btrfs property set /.snapshots/34/snapshot ro true

After repeating those steps for all snapshots containing the file, the disk space is free again:

china:/ # df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G   12K  1.9G   1% /dev/shm
tmpfs           1.9G  9.0M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/vda2        55G  3.6G   51G   7% /
/dev/vda2        55G  3.6G   51G   7% /boot/grub2/x86_64-efi
/dev/vda2        55G  3.6G   51G   7% /opt
/dev/vda2        55G  3.6G   51G   7% /root
/dev/vda2        55G  3.6G   51G   7% /tmp
/dev/vda2        55G  3.6G   51G   7% /boot/grub2/i386-pc
/dev/vda2        55G  3.6G   51G   7% /usr/local
/dev/vda2        55G  3.6G   51G   7% /srv
/dev/vda2        55G  3.6G   51G   7% /.snapshots
/dev/vda2        55G  3.6G   51G   7% /var
tmpfs           381M     0  381M   0% /run/user/0



Of course doing all this manually is no fun:

file=$1

while read a
        do snapshot=$(echo ${a%%/$file})
           btrfs property set $snapshot ro false
           rm -f $a
           btrfs property set $snapshot ro true
        done < <(find /.snapshots/ -name $file)


Here is a link to a third party script:
https://github.com/paskalito/btrfs-delete

Note: SUSE is not responsible for the script or its outcome. Customers should validate script to ensure it will meet needs. 

 

Disclaimer

This Support Knowledgebase provides a valuable tool for SUSE customers and parties interested in our products and solutions to acquire information, ideas and learn from one another. Materials are provided for informational, personal or non-commercial use within your organization and are presented "AS IS" WITHOUT WARRANTY OF ANY KIND.

  • Document ID:000019594
  • Creation Date: 14-Aug-2023
  • Modified Date:22-Aug-2023
    • SUSE Linux Enterprise Server
    • SUSE Linux Enterprise Server for SAP Applications

< Back to Support Search

For questions or concerns with the SUSE Knowledgebase please contact: tidfeedback[at]suse.com

SUSE Support Forums

Get your questions answered by experienced Sys Ops or interact with other SUSE community experts.

Join Our Community

Support Resources

Learn how to get the most from the technical support you receive with your SUSE Subscription, Premium Support, Academic Program, or Partner Program.


SUSE Customer Support Quick Reference Guide SUSE Technical Support Handbook Update Advisories
Support FAQ

Open an Incident

Open an incident with SUSE Technical Support, manage your subscriptions, download patches, or manage user access.

Go to Customer Center