Skip to main content

e2fsck & expect

Sometimes fsck won't run non-interactively, because the filesystem is too damaged to choose sane repair defaults. So the first question will be "Abort?" and if we're brave enough, we'd answer with "no" but all the following zillion questions I'd like to answer with "yes", as in "yes do whatever it takes to repair the filesystem, I have my backups around anyway". When doing this, the wonderful tool expect comes to mind. But since I have a severely damaged filesystem only once a year (hah!), I always forget how to use it. Here's an example:

#!/usr/bin/expect -f
set timeout -1
spawn /sbin/e2fsck -v $argv
expect {
        "Clear? "             { send "y" ; exp_continue }
        "Abort? "             { send "n" ; exp_continue }
        "Recreate? "          { send "y" ; exp_continue }
        "Fix? "               { send "y" ; exp_continue }
        "Ignore error? "      { send "y" ; exp_continue }
        "Force rewrite? "     { send "y" ; exp_continue }
        "Root inode not allocated.  Allocate?"   { send "y" ; exp_continue }
        "/lost+found not found.  Create?"        { send "y" ; exp_continue }
}