Repairing file system on linux

How to Check/Repair Linux File System on Boot

An operating system user that is still a learner in the OS world can never escape the challenges the current elite users have overcome. The user impact of such challenges depends on the operating system flavor or distribution you are under.

For Linux users, you might think you are facing an impossible challenge only to realize that there are multiple valid solutions for every single OS problem faced.

The Linux file system is a maze that most users hesitate to enter. It is because this file system is responsible for the performance consistency of your Linux operating system and any slight modification to its hierarchy or file system structure can be a nightmare for any user.

The FSCK Linux Command

The Linux operating system handles file system issues through the fsck command-line utility. It is a prepackaged file system check toolkit that interactively checks and repairs corrupted files that might affect the performance of your operating system.

Most of the time, it is these corrupt files that are responsible for failed system boots and partition mounts.

FSCK Command Usage

The standard syntax for fsck command-line utility is as follows:

You need to be a sudoer user or have sudo privileges to effectively use the FSCK tool.

FSCK adheres to some predefined rules.

Linux fstab File

  • It references the /etc/fstab file when a user does not provide [FILESYSTEM] as a command argument.

The column for the (/) and (ext4) is set to 1 because its file system checks and repair are prioritized.

It is not recommended to use the fsck command with mounted partitions as there is a high likelihood of permanent file system damage.

Repairing A Corrupted Linux File System

The first step is to identify the device partition that holds the Linux file system you would like to check and repair.

List Linux File System Partitions

The second step is to unmount the device whose file system you want to check and repair. e.g.

The third step is to check and possibly repair the device’s file system using FSCK.

The -p command option is for automatic repair without any user intervention.

The final step is to re-mount your device once the fsck file system check and repair is complete.

Repairing Linux File System on Boot-Time

On a running machine, it is not possible to simultaneously check and repair your Linux file system while using the OS. In this case, we have the options of using a live CD/bootable usb drive or checking and repairing the Linux file system on boot (For installed Linux systems).

Linux File System Repair on Boot

1. Restart your machine, press [Esc] or [Shift] and select Advanced Options on the resulting boot menu.

Ubuntu Advanced Options

2. Select Recovery Mode and choose the fsck option from the resulting list of menu options.

Читайте также:  Nvme linux низкая скорость

Ubuntu Recovery Mode

3. Hit [Enter] and choose the fsck menu option.

Ubuntu Fsck Option

4. Choose Yes when asked whether to remount your OS root file system.

Mount Ubuntu Filesystem

5. Once the file system check and repair is complete, direct your system to resume normal boot.

Resume Ubuntu Normal Boot

Live CD/USB File System Repair

  1. Boot into your machine from the live distribution (CD or USB).
  2. Open the Linux terminal and use sudo fdisk -l command to find the targeted Linux root partition name.
  3. If the root partition is identified by a name like /dev/sda1, running the following command will check and possibly repair its associated file system.

FSCK is a very effective tool to get you started on understanding the nature and performance of your Linux system. The man fsck command has more options on using the FSCK utility to check and optionally repair broken or corrupt Linux file systems.

Источник

Basic filesystem checks and repairs

The most common method of checking filesystem’s health is by running what’s commonly known as the fsck utility. This tool should only be run against an unmounted filesystem to check for possible issues. Nearly all well established filesystem types have their fsck tool. e.g.: ext2/3/4 filesystems have the e2fsck tool. Most notable exception until very recently was btrfs. There are also filesystems that do not need a filesystem check tool i.e.: read-only filesystems like iso9660 and udf.

e2fsprogs — ext2, ext3, ext4 filesystems

Ext2/3/4 have the previously mentioned e2fsck tool for checking and repairing filesystem. This is a part of e2fsprogs package — the package needs to be installed to have the fsck tool available. Unless one removes it in aptitude during installation, it should already be installed.

  1. it runs automatically during computer bootup every X days or Y mounts (whichever comes first). This is determined during the creation of the filesystem and can later be adjusted using tune2fs.
  2. it runs automatically if a filesystem has not been cleanly unmounted (e.g.: powercut)
  3. user runs it against an unmounted filesystem
  4. user makes it run at next bootup

case 1

When filesystem check is run automatically X days after the last check or after Y mounts, Ubuntu gives user the option to interrupt the check and continue bootup normally. It is recommended that user lets it finish the check.

case 2

If a filesystem has not been cleanly unmounted, the system detects a dirty bit on the filesystem during the next bootup and starts a check. It is strongly recommended that one lets it finish. It is almost certain there are errors on the filesystem that fsck will detect and attempt to fix. Nevertheless, one can still interrupt the check and let the system boot up on a possibly corrupted filesystem.

  1. fsck dies — If fsck dies for whatever reason, you have the option to press ^D (Ctrl + D) to continue with an unchecked filesystem or run fsck manually. See e2fsck cheatsheet for details how.
  2. fsck fails to fix all errors with default settings — If fsck fails to fix all errors with default settings, it will ask to be run manually by the user. See e2fsck cheatsheet for details how.

case 3

User may run fsck against any filesystem that can be unmounted on a running system. e.g. if you can issue umount /dev/sda3 without an error, you can run fsck against /dev/sda3.

case 4

You can make your system run fsck by creating an empty ‘forcefsck’ file in the root of your root filesystem. i.e.: touch /forcefsck Filesystems that have 0 or nothing specified in the sixth column of your /etc/fstab, will not be checked

Читайте также:  Winrar for linux 64

Till Ubuntu 6.06 you can also issue shutdown -rF now to reboot your filesystem and check all partitions with non-zero value in sixth column of your /etc/fstab. Later versions of Ubuntu use Upstart version of shutdown which does not support the -F option any more.

Refer to man fstab for what values are allowed.

e2fsck cheatsheet

  • fsck.ext4 -p /dev/sda1 — will check filesystem on /dev/sda1 partition. It will also automatically fix all problems that can be fixed without human intervention. It will do nothing, if the partition is deemed clean (no dirty bit set).
  • fsck.ext4 -p -f /dev/sda1 — same as before, but fsck will ignore the fact that the filesystem is clean and check+fix it nevertheless.
  • fsck.ext4 -p -f -C0 /dev/sda1 — same as before, but with a progress bar.
  • fsck.ext4 -f -y /dev/sda1 — whereas previously fsck would ask for user input before fixing any nontrivial problems, -y means that it will simply assume you want to answer «YES» to all its suggestions, thus making the check completely non-interactive. This is potentially dangerous but sometimes unavoidable; especially when one has to go through thousands of errors. It is recommended that (if you can) you back up your partition before you have to run this kind of check. (see dd command for backing up filesystems/partitions/volumes)
  • fsck.ext4 -f -c -C0 /dev/sda1 — will attempt to find bad blocks on the device and make those blocks unusable by new files and directories.
  • fsck.ext4 -f -cc -C0 /dev/sda1 — a more thorough version of the bad blocks check.
  • fsck.ext4 -n -f -C0 /dev/sda1 — the -n option allows you to run fsck against a mounted filesystem in a read-only mode. This is almost completely pointless and will often result in false alarms. Do not use.

dosfstools — FAT12, FAT16 and FAT32 (vfat) filesystem

In order to create and check/repair these Microsoft(TM)’s filesystems, dosfstools package needs to be installed. Similarly to ext filesystems’ tools, dosfsck has softlinks too — fsck.msdos and fsck.vfat. Options, however, vary slightly.

dosfsck cheatsheet

  • fsck.vfat -n /dev/sdc1 — a simple non-interactive read-only check
  • fsck.vfat -a /dev/sdc1 — checks the file system and fixes non-interactively. Least destructive approach is always used.
  • fsck.vfat -r /dev/sdc1 — interactive repair. User is always prompted when there is more than a single approach to fixing a problem.
  • fsck.vfat -l -v -a -t /dev/sdc1 — a very verbose way of checking and repairing the filesystem non-interactively. The -t parameter will mark unreadable clusters as bad, thus making them unavailable to newly created files and directories.

Recovered data will be dumped in the root of the filesystem as fsck0000.rec, fsck0001.rec, etc. This is similar to CHK files created by scandisk and chkdisk on MS Windows.

ntfs-3g (previously also ntfsprogs) — NTFS filesystem

Due to the closed sourced nature of this filesystem and its complexity, there is no fsck.ntfs available on GNU/Linux (ntfsck isn’t being developed anymore). There is a simple tool called ntfsfix included in ntfs-3g package. Its focus isn’t on fixing NTFS volumes that have been seriously corrupted; its sole purpose seems to be making an NTFS volume mountable under GNU/Linux.

  • ntfsfix /dev/sda1 — will attempt to fix basic NTFS problems. e.g.: detects and fixes a Windows XP bug, leading to a corrupt MFT; clears bad cluster marks; fixes boot sector problems
  • ntfsfix -d /dev/sda1 — will clear the dirty bit on an NTFS volume.
  • ntfsfix -b /dev/sda1 — clears the list of bad sectors. This is useful after cloning an old disk with bad sectors to a new disk. Windows 8 and GNU/Linux cohabitation problems This segment is taken from http://www.tuxera.com/community/ntfs-3g-advanced/ When Windows 8 is restarted using its fast restarting feature, part of the metadata of all mounted partitions are restored to the state they were at the previous closing down. As a consequence, changes made on Linux may be lost. This can happen on any partition of an internal disk when leaving Windows 8 by selecting “Shut down” or “Hibernate”. Leaving Windows 8 by selecting “Restart” is apparently safe. To avoid any loss of data, be sure the fast restarting of Windows 8 is disabled. This can be achieved by issuing as an administrator the command : powercfg /h off
Читайте также:  Linux distro at work

reiserfstools — reiserfs

  • fsck.reiserfs /dev/sda1 — a readonly check of the filesystem, no changes made (same as running with —check). This is what you should run before you include any other options.
  • fsck.reiserfs —fix-fixable /dev/sda1 — does basic fixes but will not rebuild filesystem tree
  • fsck.reiserfs —scan-whole-partition —rebuild-tree /dev/sda1 — if basic check recommends running with —rebuild-tree, run it with —scan-whole-partition and do NOT interrupt it! This will take a long time. On a non-empty 1TB partition, expect something in the range of 10-24 hours.

xfsprogs — xfs

  • xfs_repair -n /dev/sda — will only scan the volume and report what fixes are needed. This is the no modify mode and you should run this first.
    • xfs_repair will exit with exit status 0 if it found no errors and with exit status 1 if it found some. (You can check exit status with echo $?)

    XFS filesystem has a feature called allocation groups (AG) that enable it to use more parallelism when allocating blocks and inodes. AGs are more or less self contained parts of the filesystem (separate free space and inode management). mkfs.xfs creates only a single AG by default.

    xfs_repair checks and fixes your filesystems by going through 7 phases. Phase 3 (inode discovery and checks) and Phase 4 (extent discovery and checking) work sequentially through filesystem’s allocation groups (AG). With multiple AGs, this can be heavily parallelised. xfs_repair is clever enough to not process multiple AGs on same disks.

    • you created your XFS filesystem with only a single AG.
    • your xfs_repair is older than version 2.9.4 or you will make the checks even slower on GNU/Linux. You can check your version with xfs_repair -V
    • your filesystem does not span across multiple disks
    • xfs_repair -o ag_stride=8 -t 5 -v /dev/sda — same as previous example but reduces the check/fix time by utilising multiple threads, reports back on its progress every 5 minutes (default is 15) and its output is more verbose.
      • if your filesystem had 32 AGs, the -o ag_stride=8 would start 4 threads, one to process AGs 0-7, another for 8-15, etc. If ag_stride is not specified, it defaults to the number of AGs in the filesystem.

      Missing superblock

      Bad blocks

      Sources and further reading

      Источник

Оцените статью
Adblock
detector