]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - fsck/xfs_fsck.sh
xfsprogs: Release v6.8.0
[thirdparty/xfsprogs-dev.git] / fsck / xfs_fsck.sh
CommitLineData
21f63869
NS
1#!/bin/sh -f
2#
3# Copyright (c) 2006 Silicon Graphics, Inc. All Rights Reserved.
4#
5
04a2d5dc
JT
6NAME=$0
7
8# get the right return code for fsck
7a58ab01 9repair2fsck_code() {
04a2d5dc
JT
10 case $1 in
11 0) return 0 # everything is ok
12 ;;
13 1) echo "$NAME error: xfs_repair could not fix the filesystem." 1>&2
14 return 4 # errors left uncorrected
15 ;;
16 2) echo "$NAME error: The filesystem log is dirty, mount it to recover" \
17 "the log. If that fails, refer to the section DIRTY LOGS in the" \
18 "xfs_repair manual page." 1>&2
19 return 4 # dirty log, don't do anything and let the user solve it
20 ;;
21 4) return 1 # The fs has been fixed
22 ;;
e3f20e60
DW
23 127)
24 echo "$NAME error: xfs_repair was not found!" 1>&2
25 return 4
26 ;;
04a2d5dc
JT
27 *) echo "$NAME error: An unknown return code from xfs_repair '$1'" 1>&2
28 return 4 # something went wrong with xfs_repair
29 esac
30}
31
21f63869 32AUTO=false
04a2d5dc 33FORCE=false
79ba1e15 34REPAIR=false
04a2d5dc 35while getopts ":aApyf" c
21f63869
NS
36do
37 case $c in
79ba1e15
S
38 a|A|p) AUTO=true;;
39 y) REPAIR=true;;
04a2d5dc 40 f) FORCE=true;;
21f63869
NS
41 esac
42done
ec16267e
BN
43eval DEV=\${$#}
44if [ ! -e $DEV ]; then
45 echo "$0: $DEV does not exist"
46 exit 8
47fi
04a2d5dc
JT
48
49# The flag -f is added by systemd/init scripts when /forcefsck file is present
50# or fsck.mode=force is used during boot; an unclean shutdown won't trigger
51# this check, user has to explicitly require a forced fsck.
52# But first of all, test if it is a non-interactive session.
53# Invoking xfs_repair via fsck.xfs is only intended to happen via initscripts.
54# Normal administrative filesystem repairs should always invoke xfs_repair
55# directly.
56#
57# Use multiple methods to capture most of the cases:
58# The case for *i* and -n "$PS1" are commonly suggested in bash manual
59# and the -t 0 test checks stdin
60case $- in
61 *i*) FORCE=false ;;
62esac
63if [ -n "$PS1" -o -t 0 ]; then
64 FORCE=false
65fi
66
67if $FORCE; then
e3f20e60 68 xfs_repair -e $DEV
79ba1e15
S
69 error=$?
70 if [ $error -eq 2 ] && [ $REPAIR = true ]; then
71 echo "Replaying log for $DEV"
72 mkdir -p /tmp/repair_mnt || exit 1
73 for x in $(cat /proc/cmdline); do
74 case $x in
75 root=*)
76 ROOT="${x#root=}"
77 ;;
78 rootflags=*)
79 ROOTFLAGS="-o ${x#rootflags=}"
80 ;;
81 esac
82 done
83 test -b "$ROOT" || ROOT=$(blkid -t "$ROOT" -o device)
84 if [ $(basename $DEV) = $(basename $ROOT) ]; then
85 mount $DEV /tmp/repair_mnt $ROOTFLAGS || exit 1
86 else
87 mount $DEV /tmp/repair_mnt || exit 1
88 fi
89 umount /tmp/repair_mnt
90 xfs_repair -e $DEV
91 error=$?
92 rm -d /tmp/repair_mnt
93 fi
94 repair2fsck_code $error
04a2d5dc
JT
95 exit $?
96fi
97
21f63869
NS
98if $AUTO; then
99 echo "$0: XFS file system."
100else
101 echo "If you wish to check the consistency of an XFS filesystem or"
12a48f5d 102 echo "repair a damaged filesystem, see xfs_repair(8)."
21f63869
NS
103fi
104exit 0