Btrfs

如何通過 journalctl 在 BTRFS 上找到損壞文件的完整路徑?

  • December 15, 2019

執行後btrfs scrub,可以檢測到損壞的文件。為了確定無法糾正的文件,我們可以嘗試讀取磁碟上的每個文件:

sudo find / -type f -exec cp -v {} /dev/null \; 2> corrupted-files.txt

該命令的輸出滿足需要。但是需要幾個小時才能完成。

Arch Linux wiki 建議使用以下命令來確定損壞的文件:

sudo journalctl --output cat | grep 'BTRFS .* i/o error' | sort | uniq 

它可以快速獲取結果。但是,文件路徑是相對於它們的子卷路徑的,因此對於從備份中修復文件非常寶貴:

BTRFS warning (device dm-2): i/o error at logical 69945753600 on dev /dev/mapper/zeytin-root, sector 142920640, root 636, inode 172280, offset 238682112, length 4096, links 1 (path: home/aea/couchdb/couchdb/data/shards/60000000-7fffffff/client2.1533443496.couch)
BTRFS warning (device dm-2): i/o error at logical 70045958144 on dev /dev/mapper/zeytin-root, sector 143116352, root 422, inode 172296, offset 307662848, length 4096, links 1 (path: home/aea/couchdb/couchdb/data/shards/e0000000-ffffffff/client2.1533443496.couch)
BTRFS warning (device dm-2): i/o error at logical 70045958144 on dev /dev/mapper/zeytin-root, sector 143116352, root 636, inode 172296, offset 307662848, length 4096, links 1 (path: home/aea/couchdb/couchdb/data/shards/e0000000-ffffffff/client2.1533443496.couch)
BTRFS warning (device dm-2): i/o error at logical 70138376192 on dev /dev/mapper/zeytin-root, sector 143296856, root 423, inode 100548, offset 688631808, length 4096, links 1 (path: var/log/auth.log)
BTRFS warning (device dm-2): i/o error at logical 70138425344 on dev /dev/mapper/zeytin-root, sector 143296952, root 423, inode 100548, offset 688644096, length 4096, links 1 (path: var/log/auth.log)
BTRFS warning (device dm-2): i/o error at logical 70138441728 on dev /dev/mapper/zeytin-root, sector 143296984, root 423, inode 100548, offset 688648192, length 4096, links 1 (path: var/log/auth.log)
BTRFS warning (device dm-2): i/o error at logical 70138449920 on dev /dev/mapper/zeytin-root, sector 143297000, root 419, inode 818783, offset 95145984, length 4096, links 1 (path: var/log/daemon.log)
BTRFS warning (device dm-2): i/o error at logical 70138564608 on dev /dev/mapper/zeytin-root, sector 143297224, root 419, inode 818783, offset 95154176, length 4096, links 1 (path: var/log/daemon.log)
BTRFS warning (device dm-2): i/o error at logical 70138884096 on dev /dev/mapper/zeytin-root, sector 143297848, root 419, inode 818783, offset 95174656, length 4096, links 1 (path: var/log/daemon.log)

有沒有辦法通過使用值來確定子卷路徑sector XXXXXXX, root XXX, inode XXXXXXX, offset XXXXXXXX

這裡的root XXX值是子卷的根 ID。所以對應子卷的路徑可以由下式確定:

sudo btrfs sub list / | awk '$2 == 419 {print $9}'
var/lib/lxc/foo/rootfs

進行內部連接的有用腳本如下:

#!/bin/bash

sub_list=/tmp/subvolume-list-of-root.txt

sudo btrfs sub list / > $sub_list

while read a; do
   root_id=$(echo $a | awk '{print $16}' | sed -r 's/,//')
   rel_path=$(echo $a | sed -r 's/.*path:\s(.+)\)/\1/g')
   subvol_path=$(cat $sub_list | awk '$2 == '"$root_id"' {print $9}')
   [[ -z $subvol_path ]] && subvol_path="????"
   echo "/$subvol_path/$rel_path"
done < <( cat << HELLO
BTRFS warning (device dm-2): i/o error at logical 69945753600 on dev /dev/mapper/zeytin-root, sector 142920640, root 636, inode 172280, offset 238682112, length 4096, links 1 (path: home/aea/couchdb/couchdb/data/shards/60000000-7fffffff/client2.1533443496.couch)
BTRFS warning (device dm-2): i/o error at logical 70045958144 on dev /dev/mapper/zeytin-root, sector 143116352, root 422, inode 172296, offset 307662848, length 4096, links 1 (path: home/aea/couchdb/couchdb/data/shards/e0000000-ffffffff/client2.1533443496.couch)
BTRFS warning (device dm-2): i/o error at logical 70045958144 on dev /dev/mapper/zeytin-root, sector 143116352, root 636, inode 172296, offset 307662848, length 4096, links 1 (path: home/aea/couchdb/couchdb/data/shards/e0000000-ffffffff/client2.1533443496.couch)
BTRFS warning (device dm-2): i/o error at logical 70138376192 on dev /dev/mapper/zeytin-root, sector 143296856, root 423, inode 100548, offset 688631808, length 4096, links 1 (path: var/log/auth.log)
BTRFS warning (device dm-2): i/o error at logical 70138425344 on dev /dev/mapper/zeytin-root, sector 143296952, root 423, inode 100548, offset 688644096, length 4096, links 1 (path: var/log/auth.log)
BTRFS warning (device dm-2): i/o error at logical 70138441728 on dev /dev/mapper/zeytin-root, sector 143296984, root 423, inode 100548, offset 688648192, length 4096, links 1 (path: var/log/auth.log)
BTRFS warning (device dm-2): i/o error at logical 70138449920 on dev /dev/mapper/zeytin-root, sector 143297000, root 419, inode 818783, offset 95145984, length 4096, links 1 (path: var/log/daemon.log)
BTRFS warning (device dm-2): i/o error at logical 70138564608 on dev /dev/mapper/zeytin-root, sector 143297224, root 419, inode 818783, offset 95154176, length 4096, links 1 (path: var/log/daemon.log)
BTRFS warning (device dm-2): i/o error at logical 70138884096 on dev /dev/mapper/zeytin-root, sector 143297848, root 419, inode 818783, offset 95174656, length 4096, links 1 (path: var/log/daemon.log)
HELLO
)

輸出:

./test.sh 

/????/home/aea/couchdb/couchdb/data/shards/60000000-7fffffff/client2.1533443496.couch
/var/lib/lxc/foo-couch2/rootfs/home/aea/couchdb/couchdb/data/shards/e0000000-ffffffff/client2.1533443496.couch
/????/home/aea/couchdb/couchdb/data/shards/e0000000-ffffffff/client2.1533443496.couch
/var/lib/lxc/foo-git/rootfs/var/log/auth.log
/var/lib/lxc/foo-git/rootfs/var/log/auth.log
/var/lib/lxc/foo-git/rootfs/var/log/auth.log
/var/lib/lxc/foo1/rootfs.bak/var/log/daemon.log
/var/lib/lxc/foo1/rootfs.bak/var/log/daemon.log
/var/lib/lxc/foo1/rootfs.bak/var/log/daemon.log

因此,整體工具get-corrupted-files.sh變為:

#!/bin/bash

sub_list=/tmp/subvolume-list-of-root.txt

sudo btrfs sub list / > $sub_list

while read a; do
   root_id=$(echo $a | awk '{print $16}' | sed -r 's/,//')
   rel_path=$(echo $a | sed -r 's/.*path:\s(.+)\)/\1/g')
   subvol_path=$(cat $sub_list | awk '$2 == '"$root_id"' {print $9}')
   [[ -z $subvol_path ]] && subvol_path="????"
   echo "/$subvol_path/$rel_path"
done < <( sudo journalctl --output cat | grep 'BTRFS .* i/o error' | sort | uniq )

引用自:https://unix.stackexchange.com/questions/557213