kdave / btrfsmaintenance

Scripts for btrfs maintenance tasks like periodic scrub, balance, trim or defrag on selected mountpoints or directories.
GNU General Public License v2.0
897 stars 79 forks source link

Mountpoint "auto" can fail for read-only mounts #123

Open unterkomplex opened 1 month ago

unterkomplex commented 1 month ago

I encountered one issue while using the option to automatically process all mountpoints BTRFS_SCRUB_MOUNTPOINTS="auto"

The script returns a single mountpoint for each filesystem MNT=$(findmnt --types btrfs --first-only --noheadings --output "TARGET" --source "$DEVICE") https://github.com/kdave/btrfsmaintenance/blob/b1e70f6c99797def13c1d302a603e18cce701a8d/btrfsmaintenance-functions#L25C10-L25C84

This fails if there are both read-only and read-write mounts and findmnt returns a ro mountpoint

In order to fix it I think it would be sufficient to add --options rw MNT=$(findmnt --types btrfs --first-only --options rw --noheadings --output "TARGET" --source "$DEVICE")

Example output from Fedora IoT on BTRFS filesystem with /sysroot mounted as ro

[root@pi ~]# findmnt --types btrfs --noheadings --output "TARGET" --source "/dev/sda1"
/sysroot
/
/etc
/usr
/sysroot/ostree/deploy/fedora-iot/var
/var
/var/mnt/ssd
[root@pi ~]# findmnt --types btrfs --first-only --noheadings --output "TARGET" --source "/dev/sda1"
/sysroot
[root@pi ~]# findmnt --types btrfs --first-only --options rw --noheadings --output "TARGET" --source "/dev/sda1"
/
[root@pi ~]# journalctl -b0 -u btrfs-scrub.service 
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Running scrub on /sysroot
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: RAID level is not 5 or 6, parallel device scrubbing
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: ERROR: scrubbing /sysroot failed for device id 1: ret=-1, errno=30 (Read-only>
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Starting scrub on devid 1
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Scrub device /dev/sda1 (id 1) canceled
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Scrub started:    Mon Jul  8 03:09:39 2024
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Status:           aborted
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Duration:         0:00:00
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Total to scrub:   0.00B
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Rate:             0.00B/s
Jul 08 03:09:39 pi btrfs-scrub.sh[1283]: Error summary:    no errors found