• 11
name Punditsdkoslkdosdkoskdo

How to delete a directory with multiple btrfs subvolumes?

My root file system is on a btrfs partition. My /chroot directory has a number of btrfs subvolumes in it which prevent me from deleting it with rm -rf /chroot. In an attempt to delete all the subvolumes, I tried listing just the subvolumes below /chroot by using the -o, which the man page says "print only subvolumes below specified path".

# btrfs subvolume list -o /chroot
ID 593 gen 6616 top level 5 path chroot/base-devel/root
ID 594 gen 6618 top level 5 path chroot/multilib-devel/root
ID 595 gen 6620 top level 5 path chroot/base/root
ID 597 gen 6624 top level 5 path chroot/twm/root
ID 599 gen 6628 top level 5 path chroot/lxde/root
ID 601 gen 6655 top level 5 path chroot/wheezy/root
ID 602 gen 6684 top level 5 path chroot/sid/root
ID 603 gen 6862 top level 5 path var/lib/machines

To my surprise, this returns the subvolume in /var/lib/macines (which I do not want to delete). This leads me to 3 questions:

  1. What does the -o option do?

  2. How do I list only subvolumes below /chroot

  3. How can I delete a directory with an unknown number of subvolumes?

I am running Arch Linux with the 4.2.5 kernel and btrfs-progs v4.3.1

    • I would file a bug report with btrfs. In the mean time could you pipe the list of directories through grep chroot?
      • 1
    • What kernel are you running and what version of the btrfs tools do you have installed? This is probably a bug in the tools.

The following command removes all subvolumes in the /chrootdirectory:

ls /chroot | xargs btrfs subvolume delete

In case there are files or directories that are not btrfs subvolumes present, an error will be printed, but they are not removed.

(Tested on Debian with the 4.6.0 kernel and btrfs-progs v3.17)

  • 2
Reply Report

The path in btrfs subvolume delete <path> supports globbing.

For example, I had a subvolume that was being snapshot and each snapshot was its own subvolume. (ie /mnt/newDrive/.snapshots/1/snapshot/new_subvol was its own subvolume) :

user@machine:/mnt/newDrive/.snapshots$ tree
??? 1
?   ??? info.xml
?   ??? snapshot
?       ??? new_subvol
??? 104
?   ??? info.xml
?   ??? snapshot
?       ??? new_subvol
??? 111
?   ??? info.xml
?   ??? snapshot
?       ??? new_subvol

To delete all these subvolumes all I had to do was

sudo btrfs subvolume delete /mnt/newDrive/.snapshots/*/snapshot/new_subvol

After that, all the subvolumes were gone and and I could easily delete /mnt/newDrive/.snapshots with

rm -rf /mnt/newDrive/.snapshots

Hope this is helpful!

  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags