-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbtrfs-mount-repair
executable file
·78 lines (69 loc) · 2.42 KB
/
btrfs-mount-repair
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
# Jolla btrfs repair and mount script
#
# Copyright (C) 2014 Jolla Ltd.
# Contact: Kalle Jokiniemi <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
print_help() {
echo "Usage: $0 <device> <mount-directory>"
echo " device - btrfs device to be repaired and mounted"
echo " mount-directory - where to mount the device"
echo "NOTE: If repair is succesful, device will be left mounted in"
echo " mount-directory"
}
if [ -z $1 ] || [ ! -e $1 ]; then
echo "$0: Invalid device: \"$1\"" > /dev/kmsg
print_help
exit 1
else
ROOTDEV=$1
fi
if [ -z $2 ] || [ ! -e $2 ]; then
echo "$0: Invalid mount-directory: \"$2\"" > /dev/kmsg
print_help
exit 1
else
TARGET=$2
fi
# First try simple clearing of the free space cache
if mount -t btrfs -o recovery,nospace_cache,clear_cache,autodefrag $ROOTDEV $TARGET; then
echo "$0: Root mounted with recovery options" > /dev/kmsg
exit 0
fi
# in case FS got mounted as read-only..
umount $TARGET
# Try normal btrfs check repair
btrfs check --repair $ROOTDEV
if mount -t btrfs -o recovery,clear_cache,autodefrag $ROOTDEV $TARGET; then
echo "$0: Root mounted after repair" > /dev/kmsg
exit 0
fi
umount $TARGET
# Try rebuilding checksum tree
btrfs check --init-csum-tree $ROOTDEV
if mount -t btrfs -o recovery,clear_cache,autodefrag $ROOTDEV $TARGET; then
echo "$0: Root mounted after rebuilding btrfs checksum tree" > /dev/kmsg
exit 0
fi
umount $TARGET
# Try rebuilding the extent tree, this takes the longest time.
btrfs check --init-extent-tree $ROOTDEV
if mount -t btrfs -o recovery,clear_cache,autodefrag $ROOTDEV $TARGET; then
echo "$0 Root mounted after rebuilding btrfs extent tree" > /dev/kmsg
exit 0
fi
echo "$0: Failed to repair and mount $ROOTDEV" > /dev/kmsg
exit 1