Announcement

Collapse
No announcement yet.

Maintenance on Btrfs

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Maintenance on Btrfs

    https://wiki.debian.org/Btrfs


    Maintenance

    As a btrfs volume ages, you may notice performance degrade. This is because btrfs is a Copy On Write file system, and all COW filesystems eventually reach a heavily fragmented state; this includes ZFS. Over time, logs in /var/log/journal will become split across tens of thousands of extents. This is also the case for sqlite databases such as those that are used for Firefox and a variety of common desktop software. Fragmentation is a major contributing factor to why COW volumes become slower over time.

    ZFS addresses the performance problems of fragmentation using an intelligent Adaptive Replacement Cache (ARC); the ARC requires massive amounts of RAM. Btrfs took a different approach, and benefits from—some would say requires—periodic defragmentation. In the future, maintenance of btrfs volumes on Debian systems will be automated using btrfsmaintenance. For now use:

    sudo ionice -c idle btrfs filesystem defragment -t 32M -r $PATH

    This command must be run as root, and it is recommended to ionice it to reduce the load on the system. To further reduce the IO load, flush data after defragmenting each file using:

    sudo ionice -c idle btrfs filesystem defragment -f -t 32M -r $PATH


    Target extent size is a little known, but for practical purposes absolutely essential argument. By default btrfs fi defrag only defrags files of less than 256KiB, because does not touch extents bigger than $SIZE, where $SIZE is by default 256KiB! While argument "-t 1G" would seem to be better than "-t 32M", because most volumes will have 1GiB chunk size, in practise this is not the case. Additionally, if you have a lot of snapshots or reflinked files, please use "-f" to flush data for each file before going to the next file. As of btrfs-progs-4.6.1, "-t 32M" is still necessary, but "-t 32M" is the default after btrfs-progs-4.7. Please consult the following linux-btrfs thread for more information.
    The Btrfs-filesystem man page states:
    defragment [options] <file>|<dir> [<file>|<dir>...]
    Defragment file data on a mounted filesytem.


    If -r is passed, files in dir will be defragmented recursively. The start position and the number of bytes
    to defragment can be specified by start and len using -s and -l options below. Extents bigger than value
    given by -t will be skipped, otherwise this value is used as a target extent size, but is only advisory
    and may not be reached if the free space is too fragmented. Use 0 to take the kernel default, which is
    256kB but may change in the future. You can also turn on compression in defragment operations.


    Warning
    Defragmenting with Linux kernel versions < 3.9 or ≥ 3.14-rc2 as well as with Linux stable kernel
    versions ≥ 3.10.31, ≥ 3.12.12 or ≥ 3.13.4 will break up the ref-links of COW data (for example files
    copied with cp --reflink, snapshots or de-duplicated data). This may cause considerable increase of
    space usage depending on the broken up ref-links.



    Options


    -v
    be verbose, print file names as they’re submitted for defragmentation


    -c[<algo>]
    compress file contents while defragmenting. Optional argument selects the compression algorithm, zlib
    (default) or lzo. Currently it’s not possible to select no compression. See also section EXAMPLES.


    -r
    defragment files recursively in given directories


    -f
    flush data for each file before going to the next file.
    This will limit the amount of dirty data to
    current file, otherwise the amount cumulates from several files and may increase system load.


    -s <start>[kKmMgGtTpPeE]
    defragmentation will start from the given offset, default is beginning of a file


    -l <len>[kKmMgGtTpPeE]
    defragment only up to len bytes, default is the file size


    -t <size>[kKmMgGtTpPeE]
    target extent size, do not touch extents bigger than size



    For start, len, size it is possible to append units designator: 'K', 'M', 'G', 'T', 'P', or 'E', which
    represent KiB, MiB, GiB, TiB, PiB, or EiB, respectively (case does not matter).


    Note
    Directory arguments without -r do not defragment files recursively but will defragment certain
    internal trees (extent tree and the subvolume tree). This has been confusing and could be removed
    in the future.
    My Neon installation is currently using the 4.8.0-42-generic #45~16.04.1-Ubuntu SMP kernel. The warning highlighted in red cautions against using a kernel greater than 3.13.4. This suggests that running a defragmentation would slow the system down, not speed it up, especially if using cp with --rflink is used. However, I suspect that may only apply to the 3.x kernel series. The 4.x series may be safe to use defragmentation.

    [#]BTRFS[/#]
    Last edited by GreyGeek; Sep 22, 2017, 11:57 AM.
    "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
    – John F. Kennedy, February 26, 1962.

    #2
    Thanks GG! It has been years since I checked the online guidance for btrfs -- time goes by and I'm blissfully ignorant of changes. I updated my "notes to self" on how to defrag my 2-drive btrfs filesystem. Hmmm -- now that I think of it, it has also been over a year since I checked anything more significant than the temperature for the health of the hdds. Time to do some smartctl checks!
    Last edited by dibl; Apr 23, 2017, 05:42 AM.

    Comment


      #3
      As to what value "-t" should have perhaps a sorted list of file sizes would suggest an answer. While "-t 32M" is a suggested default value the quoted kernel thread says anything up to 2G will work, and 10G on TB drives.


      Sent from my iPhone using Tapatalk
      "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
      – John F. Kennedy, February 26, 1962.

      Comment


        #4
        I ran it leaving "-t" at the default, on my dual 1TB drive filesystem with roughtly 650 GB of data on it. The file sizes range from tiny to 50GB, and everything in between, probably mostly in the 1 - 5 MB range (photos). I didn't time it, but it was done by the time I finished breakfast, so less than 1 hour. The CPU is an Intel Core i7 4700 and lots of RAM, so that helps.

        Comment


          #5
          Well, I wasn't so fortunate:
          ERROR: defrag failed on /bin/bash: Success
          ERROR: defrag failed on /bin/btrfs: Success
          ERROR: defrag failed on /bin/dash: Success
          ERROR: defrag failed on /lib/systemd/systemd: Success
          ERROR: defrag failed on /lib/systemd/systemd-journald: Success
          ERROR: defrag failed on /lib/systemd/systemd-logind: Success
          ERROR: defrag failed on /lib/systemd/systemd-timesyncd: Success
          ERROR: defrag failed on /lib/systemd/systemd-udevd: Success
          ERROR: defrag failed on /lib/x86_64-linux-gnu/ld-2.23.so: Success
          ERROR: defrag failed on /sbin/agetty: Success
          ERROR: defrag failed on /sbin/dhclient: Success
          ERROR: defrag failed on /sbin/lvmetad: Success
          ERROR: defrag failed on /sbin/wpa_supplicant: Success
          ERROR: defrag failed on /usr/bin/dbus-daemon: Success
          ERROR: defrag failed on /usr/bin/dbus-launch: Success
          ERROR: defrag failed on /usr/bin/pulseaudio: Success
          ERROR: defrag failed on /usr/bin/python3.5: Success
          ERROR: defrag failed on /usr/bin/sddm: Success
          ERROR: defrag failed on /usr/bin/ssh-agent: Success
          ERROR: defrag failed on /usr/bin/sudo: Success
          ERROR: defrag failed on /usr/bin/kactivitymanagerd: Success
          ERROR: defrag failed on /usr/bin/kaccess: Success
          ERROR: defrag failed on /usr/bin/kwin_x11: Success
          ERROR: defrag failed on /usr/bin/ksysguardd: Success
          ERROR: defrag failed on /usr/bin/kuiserver: Success
          ERROR: defrag failed on /usr/bin/kwalletd5: Success
          ERROR: defrag failed on /usr/bin/kdeinit5: Success
          ERROR: defrag failed on /usr/bin/kwrapper5: Success
          ERROR: defrag failed on /usr/bin/baloo_file: Success
          ERROR: defrag failed on /usr/bin/kglobalaccel5: Success
          ERROR: defrag failed on /usr/bin/dolphin: Success
          ERROR: defrag failed on /usr/bin/kgpg: Success
          ERROR: defrag failed on /usr/bin/krunner: Success
          ERROR: defrag failed on /usr/bin/xembedsniproxy: Success
          ERROR: defrag failed on /usr/bin/plasmashell: Success
          ERROR: defrag failed on /usr/bin/ksmserver: Success
          ERROR: defrag failed on /usr/bin/konsole: Success
          ERROR: defrag failed on /usr/lib/accountsservice/accounts-daemon: Success
          ERROR: defrag failed on /usr/lib/at-spi2-core/at-spi-bus-launcher: Success
          ERROR: defrag failed on /usr/lib/at-spi2-core/at-spi2-registryd: Success
          ERROR: defrag failed on /usr/lib/bluetooth/bluetoothd: Success
          ERROR: defrag failed on /usr/lib/bluetooth/obexd: Success
          ERROR: defrag failed on /usr/lib/colord/colord: Success
          ERROR: defrag failed on /usr/lib/dconf/dconf-service: Success
          ERROR: defrag failed on /usr/lib/packagekit/packagekitd: Success
          ERROR: defrag failed on /usr/lib/policykit-1/polkitd: Success
          ERROR: defrag failed on /usr/lib/pulseaudio/pulse/gconf-helper: Success
          ERROR: defrag failed on /usr/lib/rtkit/rtkit-daemon: Success
          ERROR: defrag failed on /usr/lib/snapd/snapd: Success
          ERROR: defrag failed on /usr/lib/udisks2/udisksd: Success
          ERROR: defrag failed on /usr/lib/upower/upowerd: Success
          ERROR: defrag failed on /usr/lib/x86_64-linux-gnu/gconf/gconfd-2: Success
          ERROR: defrag failed on /usr/lib/x86_64-linux-gnu/libexec/kf5/kscreen_backend_launcher: Suc
          cess
          ERROR: defrag failed on /usr/lib/x86_64-linux-gnu/libexec/kf5/klauncher: Success
          ERROR: defrag failed on /usr/lib/x86_64-linux-gnu/libexec/kf5/start_kdeinit: Success
          ERROR: defrag failed on /usr/lib/x86_64-linux-gnu/libexec/kdeconnectd: Success
          ERROR: defrag failed on /usr/lib/x86_64-linux-gnu/libexec/org_kde_powerdevil: Success
          ERROR: defrag failed on /usr/lib/x86_64-linux-gnu/libexec/polkit-kde-authentication-agent-1
          : Success
          ERROR: defrag failed on /usr/lib/x86_64-linux-gnu/sddm/sddm-helper: Success
          ERROR: defrag failed on /usr/lib/xorg/Xorg: Success
          ERROR: defrag failed on /usr/lib/nvidia-378/bin/nvidia-persistenced: Success
          ERROR: defrag failed on /usr/lib/postgresql/9.5/bin/postgres: Success
          ERROR: defrag failed on /usr/sbin/ModemManager: Success
          ERROR: defrag failed on /usr/sbin/NetworkManager: Success
          ERROR: defrag failed on /usr/sbin/acpid: Success
          ERROR: defrag failed on /usr/sbin/avahi-daemon: Success
          ERROR: defrag failed on /usr/sbin/cron: Success
          ERROR: defrag failed on /usr/sbin/cups-browsed: Success
          ERROR: defrag failed on /usr/sbin/irqbalance: Success
          ERROR: defrag failed on /usr/sbin/rsyslogd: Success
          ERROR: defrag failed on /usr/sbin/smartd: Success
          ERROR: defrag failed on /usr/sbin/thermald: Success
          ERROR: defrag failed on /usr/sbin/aiccu: Success
          ERROR: defrag failed on /usr/sbin/atd: Success
          ERROR: defrag failed on /usr/sbin/iio-sensor-proxy: Success
          ERROR: defrag failed on /usr/sbin/dnsmasq: Success
          total 76 failures
          Confusing messages: ERROR: .... Success

          Now to figure out what that means and what do do next, if anything needs to be done.
          "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
          – John F. Kennedy, February 26, 1962.

          Comment


            #6
            Originally posted by GreyGeek View Post
            Well, I wasn't so fortunate:

            Confusing messages: ERROR: ... Success

            Now to figure out what that means and what do do next, if anything needs to be done.
            Those are likely just open files. There's a bug in the error message, it should say:

            ERROR: cannot open /bin/bash: Text file busy

            Try running defrag on an individual file

            Please Read Me

            Comment


              #7
              I should have added that the the subvolume I defragged that gave the errors was "/". The /home subvolume defragged without problems.

              Your explanation makes sense, but if open files gives that kind of error I wonder why defragmentation isn't restricted to unmounted devices, like btrfs check.

              I'm doing a scrub on / right now.

              EDIT: Scrub finished 95GB (my entire disk usage) in 14 minutes and reported no errors.

              Defragging a single file that gave an error:


              :~$ sudo btrfs filesystem defragment -v -f -r /bin/bash
              ERROR: cannot open /bin/bash: Text file busy

              Your experience spoke!

              I haven't read anything about doing defrag on an unmounted device. How else would you close the files? Run defrag on a rw backup subvolume of @? And, why didn't the open files in my home account give the same error?
              Last edited by GreyGeek; Apr 24, 2017, 03:49 PM.
              "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
              – John F. Kennedy, February 26, 1962.

              Comment


                #8
                I haven't found a way yet. You could boot to another install and run it there I guess. I had 80 files come back with errors.

                Also, defrag doesn't parse nested subvols. This is a feature, because if you defrag a snapshot it grows to the original volume size. Defrag works by making a new copy of a fragmented file and deleting the old one. Defraging a snapshot could potentially double the used data space.

                This is a good reason to keep snapshots in a separate folder, because then I can defrag all subvols with:

                $ sudo -i
                # btrfs sub list /subvols/ | while read -r id x gen y top level z path dir; do btrfs fi defrag -r "$dir"; done

                Leaving out my /snapshots/ folder completely.
                Last edited by oshunluvr; Apr 24, 2017, 05:11 PM.

                Please Read Me

                Comment


                  #9
                  Ok, here's what I found out about defrag and snapshots on the btrfs IRC channel:

                  ‎[18:34] ‎<‎oshunluvr‎>‎ debian wiki still says defrag a problem with newer kernels. Is this still true re. 4.4 and above?

                  [18:45] ‎<‎kilobyte‎>‎ oshunluvr: not a real problem, more like a caveat. It works correctly, is safe for the data, but behaves same as if you copied then replaced the file.
                  ‎[18:45] ‎<‎kilobyte‎>‎ oshunluvr: ie, the extents will no longer be reflinked with older snapshots, thus taking extra disk space
                  [18:48] ‎<‎kilobyte‎>‎ if that's a problem, you can defrag then dedup. If both copies are defragged, there's no downside other than the time spent copying then deduping.
                  ‎[18:54] ‎<‎kilobyte‎>‎ the space loss is only if the files were actually fragmented; I guess the vast majority of extents are not

                  So it's safe to do, but it might cause extra space to be used. They also said that defrag isn't commonly or often needed because most files are linearly written.

                  Please Read Me

                  Comment


                    #10
                    I just ran defrag on my home subvol. Only one file locked - dropbox

                    Please Read Me

                    Comment


                      #11
                      Maintenance on Btrfs

                      I always mount my /dev/sda1 to /mnt and my 2nd HD to /backup.
                      Then I make snapshots of @ and @home as @_bkupYYYYMMDD and @home_bkupYYYYMMDD and then I use send & receive to sent them to /backup. When I am done and everything is sync'd, I umount both /mnt and /backup.

                      But, considering what we've discussed today, I am making a slight change to my procedure. After I do the send & receive I am going to delete the @_bkup & @home_bkup snapshots and leave only @ and @home on /mnt. IOW, ALL my archival snapshots will be on the 2nd Drive. I am also going to look into using the btrfs send -f rootfs_YYYMMDD /mnt/@ to create "outfiles"

                      Code:
                      [B][FONT=courier new] btrfs subvolume create /mnt/btrfs/subv1
                       btrfs subvolume snapshot -r /mnt/btrfs/subv1 /mnt/btrfs/ro_snap
                       btrfs send -f output /mnt/btrfs/ro_snap[/FONT][/B]
                      Last edited by GreyGeek; Apr 25, 2017, 08:48 AM.
                      "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                      – John F. Kennedy, February 26, 1962.

                      Comment


                        #12
                        Since bash was one of the files that failed the defrag check I decided to re-install bash and then check its defrag status.
                        Code:
                        [FONT=monospace][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$ [B]sudo apt install --reinstall bash[/B][/COLOR]
                        Reading package lists... Done
                        Building dependency tree        
                        Reading state information... Done
                        Starting pkgProblemResolver with broken count: 0
                        Starting 2 pkgProblemResolver with broken count: 0
                        Done
                        0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded.
                        Need to get 583 kB of archives.
                        After this operation, 0 B of additional disk space will be used.
                        Get:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 bash amd64 4.3-14ubuntu1.1 [583 kB]
                        Fetched 583 kB in 1s (567 kB/s)
                        (Reading database ... 342257 files and directories currently installed.)
                        Preparing to unpack .../bash_4.3-14ubuntu1.1_amd64.deb ...
                        Unpacking bash (4.3-14ubuntu1.1) over (4.3-14ubuntu1.1) ...
                        Processing triggers for install-info (6.1.0.dfsg.1-5) ...
                        Processing triggers for man-db (2.7.5-1) ...
                        Setting up bash (4.3-14ubuntu1.1) ...
                        update-alternatives: using /usr/share/man/man7/bash-builtins.7.gz to provide /usr/share/man/man7/builtins.7.gz (builtins.7.gz) 
                        in auto mode
                        
                        [COLOR=#54FF54][B]jerry@jerry-Aspire-V3-771[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$[B] sudo btrfs filesystem defragment -v -f -r /bin/bash[/B][/COLOR]
                        /bin/bash
                        [COLOR=#54FF54][B]jerry@jerry-Aspire-V3-771[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$ [/COLOR]
                        
                        [/FONT]

                        Looks like reinstalling bash cleared up its defrag error. I am going to reinstall the other 74 files that failed to defrag.

                        "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                        – John F. Kennedy, February 26, 1962.

                        Comment


                          #13
                          When I reinstalled bash and a few other files it gave good defrag results, but 2/3rds of the ones I tried still failed.
                          "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                          – John F. Kennedy, February 26, 1962.

                          Comment


                            #14
                            I experimented with the btrfs send -f.
                            Code:
                            [FONT=monospace][COLOR=#000000]:~# [B]btrfs send -f /backup/@home_output_20170425 /mnt/snapshots/@home_bkup_20170425 [/B]      [/COLOR]
                            At subvol /mnt/snapshots/@home_bkup_20170425
                            
                            root@jerry-Aspire-V3-771:~# vdir
                            total 0
                            root@jerry-Aspire-V3-771:~# vdir /backup
                            total 91816044
                            -rw------- 1 root root 81770110310 Apr 25 13:25 @home_output_20170425
                            -rw------- 1 root root 12249515097 Apr 25 12:42 @output_20170425
                            drwxr-xr-x 1 root root         220 Apr 25 12:26 snapshots
                            -rw-r--r-- 1 root root           0 Apr 20 11:51 snapshots_starting_04-20_are_incremental.txt
                            root@jerry-Aspire-V3-771:~# sync
                            
                            root@jerry-Aspire-V3-771:~#[B] btrfs fi usage /backup[/B]
                            Overall:
                                Device size:                 698.64GiB
                                Device allocated:            188.02GiB
                                Device unallocated:          510.61GiB
                                Device missing:                  0.00B
                                Used:                        182.26GiB
                                Free (estimated):            515.64GiB      (min: 260.34GiB)
                                Data ratio:                       1.00
                                Metadata ratio:                   2.00
                                Global reserve:              255.59MiB      (used: 0.00B)
                            
                            Data,single: Size:185.01GiB, Used:179.98GiB
                               /dev/sdb      185.01GiB
                            
                            Metadata,DUP: Size:1.50GiB, Used:1.14GiB
                               /dev/sdb        3.00GiB
                            
                            System,DUP: Size:8.00MiB, Used:48.00KiB
                               /dev/sdb       16.00MiB
                            
                            Unallocated:
                               /dev/sdb      510.61GiB
                            root@jerry-Aspire-V3-771:~# 
                            [/FONT]


                            If a path is not supplied the "output" filew ill drop into your pwd.

                            Last edited by GreyGeek; Apr 25, 2017, 12:39 PM.
                            "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                            – John F. Kennedy, February 26, 1962.

                            Comment


                              #15
                              I've come to the conclusion that defragmentation errors are essentially meaningless as long a meta data has sufficient space.

                              I checked the status of my btrfs system:
                              Code:
                              [FONT=monospace][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$ cat /sys/block/sda/queue/scheduler  [/COLOR]
                              noop [deadline] cfq  
                              [COLOR=#54FF54][B]jerry@jerry-Aspire-V3-771[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$[FONT=courier new][B] sudo btrfs dev stats /[/B][/FONT]  [/COLOR]
                              [sudo] password for jerry:  
                              [/dev/sda1].write_io_errs   0
                              [/dev/sda1].read_io_errs    0
                              [/dev/sda1].flush_io_errs   0
                              [/dev/sda1].corruption_errs 0
                              [/dev/sda1].generation_errs 0
                              [COLOR=#54FF54][B]jerry@jerry-Aspire-V3-771[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$ [FONT=courier new][B]sudo btrfs dev stats /home[/B][/FONT][/COLOR]
                              [/dev/sda1].write_io_errs   0
                              [/dev/sda1].read_io_errs    0
                              [/dev/sda1].flush_io_errs   0
                              [/dev/sda1].corruption_errs 0
                              [/dev/sda1].generation_errs 0
                              [COLOR=#54FF54][B]jerry@jerry-Aspire-V3-771[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$ [FONT=courier new][B]sudo mount /dev/sda1 /mnt[/B][/FONT][/COLOR]
                              [COLOR=#54FF54][B]jerry@jerry-Aspire-V3-771[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$ [FONT=courier new][B]sudo btrfs dev stats /mnt[/B][/FONT][/COLOR]
                              [/dev/sda1].write_io_errs   0
                              [/dev/sda1].read_io_errs    0
                              [/dev/sda1].flush_io_errs   0
                              [/dev/sda1].corruption_errs 0
                              [/dev/sda1].generation_errs 0
                              [COLOR=#54FF54][B]jerry@jerry-Aspire-V3-771[/B][/COLOR][COLOR=#000000]:[/COLOR][COLOR=#5454FF][B]~[/B][/COLOR][COLOR=#000000]$ [FONT=courier new][B]sudo umount /mnt[/B][/FONT][/COLOR]
                              
                              [/FONT]

                              Zero problems. I'll take that!

                              "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
                              – John F. Kennedy, February 26, 1962.

                              Comment

                              Working...
                              X