Using e2fsck to Find and Repair Disk Errors On ext File Systems

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

What is e2fsck?

e2fsck is a utility that examines ext2, ext3, and ext4 filesystems for errors, and attempts to repair them if possible. It is the backend tool that the popular fsck frontend utility calls for a combination of tasks related to ext filesystems.

On some systems, e2fsck runs automatically after an unclean shutdown or after a certain number of reboots.

Note
When booting into rescue mode on the Linode Platform, the e2fsck tool will be installed as the default disk repair utility. For instructions specific to performing a filesystem check on the Linode platform, see our Troubleshooting Guide: Booting into Rescue Mode for guidance.

When to Use e2fsck

You can use fsck to check your file system if your system fails to boot, if files on a specific disk become corrupt, or if an attached drive does not act as expected. Unmount the disks you intend to work on before attempting to check or repair them.

Caution
Unmount the target disk first. You risk corrupting your file system and losing data if you run fsck on an active disk.

e2fsck Options and Arguments

OptionAction
-aAutomatically repair (“preen”) the file system. This option is provided for backwards compatibility purposes only, and it is recommended that the -p option is used instead wherever possible.
-b superblockUses an alternative superblock, replacing superblock as noted in the option.
-B blocksizeAllows you to define a block size when performing e2fsck, instead of having e2fsck attempt multiple block sizes to find the correct one. Replace blocksize with the block size you would like to attempt a repair with.
-cUses the badblocks program to perform a read-only scan to find any bad blocks, and then add any back blocks to the inode to prevent them from being allocated.
-C fdEnsures that e2fsck writes completion information to a specified file descriptor to track the progress of the file system check. Replace fd with the file descriptor you would like to use.
-dPrints debugging output
-DEnsures the e2fsck attempts to optimize all directories by re-indexing them or by sorting and compressing directories for smaller directories, or traditional linear directories.
-E extended_optionsSets extended options for e2fsck. These are generally more advanced options. For more information, see the official man page for e2fsck.
-fForce filesystem checking even if the file system appears clean.
-FFlushes the file system device’s buffer caches before beginning. This is generally only useful for benchmarking purposes.
-j external-journalSets the path name where the external-journal for the defined file system can be found. Replace external-journal with the path you will be using.
-kUsed in combination with the -c option, uses the badblocks program, ensures that any existing bad blocks are preserved, and any new bad blocks found are added to the bad blocks list.
-l filenameAdds block numbers listed in the file specified by filename to the list of bad blocks.
-L filenameSame as the l option, however clears the bad blocks list before the blocks listed in the file are added.
-nOpens the filesystem as read-only and assumes an answer of no to any prompts. This option may not be specified at the same time as the -p or -y options.
-pEntered to automatically repair (“preen”) the file system. Automatically attempts to fix any filesystem issues without human intervention.
-tPrints statistics for e2fsck related to timing.
-vEnables verbose output.
-VPrints the version information for e2fsck without performing any scan.
-yAutomatically selects the yes option to any prompts when performing e2fsck tasks. This option may not be specified at the same time as the -n or -p options.

Unmount the Disk

Boot into Rescue Mode

If you are using e2fsck on a Linode, the easiest and safest way to unmount your disk is to use Rescue Mode. Visit our Rescue and Rebuild guide for instructions on how to boot your Linode into Rescue Mode. If you’re working on a local machine, consider using the distribution’s recovery mode or a live distribution to avoid working on a mounted disk. e2fsck should be run only as a user with root permissions.

View Mounted Disks and Verify Disk Location

  1. Run df to view a list of currently mounted disks. If you are using Rescue Mode, the disk you want to check should not be listed:

    df -h
    
  2. Use fdisk to view disk locations:

    fdisk -l
    

    Copy the location of the target disk to use with the fsck command.

Configuration Profile

If you are working on a Linode but do not wish to use Rescue Mode, shut down the Linode from the Linode Manager. Unmount the disk from the Configuration Profile. Apply the changes and reboot the Linode.

Manual Unmount

If you are working on a local machine, unmount the disk manually.

  1. Use umount to unmount the disk location copied in the previous step:

    umount /dev/sdb
    
  2. If the disk is declared in /etc/fstab, change the mount point to none there as well.

How to Check for Errors on a Disk

Run fsck on the target disk, using the desired options. This example forces a file system check and attempts to fix any issues without human intervention (-fp) on /dev/sda:

e2fsck -pf /dev/sda

Understand fsck Error Codes

The error codes that fsck returns can be understood with the following table from man7.org:

CodeError Code Meaning
0No errors
1Filesystem errors corrected
2System should be rebooted
4Filesystem errors left uncorrected
8Operational error
16Usage or syntax error
32Checking canceled by user request
128Shared-library error

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.