]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - fsck/xfs_fsck.sh
c9fc3eb35fc9281b84faedd750266d5b5d36a80a
[thirdparty/xfsprogs-dev.git] / fsck / xfs_fsck.sh
1 #!/bin/sh -f
2 #
3 # Copyright (c) 2006 Silicon Graphics, Inc. All Rights Reserved.
4 #
5
6 NAME=$0
7
8 # get the right return code for fsck
9 function repair2fsck_code() {
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 ;;
23 *) echo "$NAME error: An unknown return code from xfs_repair '$1'" 1>&2
24 return 4 # something went wrong with xfs_repair
25 esac
26 }
27
28 AUTO=false
29 FORCE=false
30 while getopts ":aApyf" c
31 do
32 case $c in
33 a|A|p|y) AUTO=true;;
34 f) FORCE=true;;
35 esac
36 done
37 eval DEV=\${$#}
38 if [ ! -e $DEV ]; then
39 echo "$0: $DEV does not exist"
40 exit 8
41 fi
42
43 # The flag -f is added by systemd/init scripts when /forcefsck file is present
44 # or fsck.mode=force is used during boot; an unclean shutdown won't trigger
45 # this check, user has to explicitly require a forced fsck.
46 # But first of all, test if it is a non-interactive session.
47 # Invoking xfs_repair via fsck.xfs is only intended to happen via initscripts.
48 # Normal administrative filesystem repairs should always invoke xfs_repair
49 # directly.
50 #
51 # Use multiple methods to capture most of the cases:
52 # The case for *i* and -n "$PS1" are commonly suggested in bash manual
53 # and the -t 0 test checks stdin
54 case $- in
55 *i*) FORCE=false ;;
56 esac
57 if [ -n "$PS1" -o -t 0 ]; then
58 FORCE=false
59 fi
60
61 if $FORCE; then
62 XFS_REPAIR=`command -v xfs_repair`
63 if [ ! -x "$XFS_REPAIR" ] ; then
64 echo "$NAME error: xfs_repair was not found!" 1>&2
65 exit 4
66 fi
67
68 $XFS_REPAIR -e $DEV
69 repair2fsck_code $?
70 exit $?
71 fi
72
73 if $AUTO; then
74 echo "$0: XFS file system."
75 else
76 echo "If you wish to check the consistency of an XFS filesystem or"
77 echo "repair a damaged filesystem, see xfs_repair(8)."
78 fi
79 exit 0