Fsck

From TheBestLinux.com
Revision as of 05:22, 15 December 2022 by Jamie (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

FSCK stands for File System ChecK, and does exactly that, checks a filesystem! This is one of the many beauties of Linux, in that most of the time, you can figure out what a command does by it's name, though that's definitely NOT always the case!

As there are many types of file systems on many operating systems, including Linux, there are many variations of fsck to support many different filesystems types, and a quick way to see which files systems are supported by the current Linux system you happen to be logged into right now, by typing this(note the DOT after "fsck."!) and then tapping the TAB key twice:

[jamie@fc33hw01.dawgland.com:~]$ fsck.
fsck.btrfs   fsck.exfat   fsck.ext3    fsck.fat     fsck.msdos   fsck.vfat
fsck.cramfs  fsck.ext2    fsck.ext4    fsck.minix   fsck.ntfs    fsck.xfs

[jamie@fc33hw01.dawgland.com:~]$ fsck.


As you can see, not only do my particular fsck packages installed support the most common and current Linux filesystems, EXT2, EXT3, EXT4, BTRFS and XFS.

Also, as you can see, Microsoft filesystems MSDOS, FAT, VFAT and NTFS are supported as well.

HFS is short for Hierarchical File System, and is used on MAC's.

CramFS is mostly used on embedded systems & Minix is used on the Minix operating system, and designed to be more UNIX-like.

ExFAT is the latest, greatest filesystem that does it all, supporting HUGE file sizes, and is usable by both Windows and Linux!

Each fsck command has different switches and arguments. To see how to use a particular fsck tool, just type the entire fsck.xxx command by itself and hit the ENTER key, and it should give you the "Usage" page.

For instance, if you want to find out how to use the EXT4 filesystem, just type the command on a blank line without any arguments or switches:

[jamie@fc33hw01.dawgland.com:~]$ fsck.ext4


This should produce the following usage output:

Usage: fsck.ext4 [-panyrcdfktvDFV] [-b superblock] [-B blocksize]
		[-l|-L bad_blocks_file] [-C fd] [-j external_journal]
		[-E extended-options] [-z undo_file] device

Emergency help:
 -p                   Automatic repair (no questions)
 -n                   Make no changes to the filesystem
 -y                   Assume "yes" to all questions
 -c                   Check for bad blocks and add them to the badblock list
 -f                   Force checking even if filesystem is marked clean
 -v                   Be verbose
 -b superblock        Use alternative superblock
 -B blocksize         Force blocksize when looking for superblock
 -j external_journal  Set location of the external journal
 -l bad_blocks_file   Add to badblocks list
 -L bad_blocks_file   Set badblocks list
 -z undo_file         Create an undo file
[jamie@fc33hw01.dawgland.com:~]$


Other fsck tools point you to a different location to get the usage page. For instance, here is what the fsck.btrfs command on a blank line by itself does:

[jamie@fc33hw01.dawgland.com:~]$ fsck.btrfs 
If you wish to check the consistency of a BTRFS filesystem or
repair a damaged filesystem, see btrfs(8) subcommand 'check'.
[jamie@fc33hw01.dawgland.com:~]$


The above hint indicating to see the 'check' subcommand tells me to type this command to get the usage for fsck.btrfs. I always try "--help" without the quotes to look for built-in help in most every command-line tool:

[jamie@fc33hw01.dawgland.com:~]$ btrfs check --help


Which produces the following usage output I am looking for:

[jamie@fc33hw01.dawgland.com:~]$ btrfs check --help
usage: btrfs check [options] <device>

    Check structural integrity of a filesystem (unmounted).

    Check structural integrity of an unmounted filesystem. Verify internal
    trees' consistency and item connectivity. In the repair mode try to
    fix the problems found. 
    WARNING: the repair mode is considered dangerous and should not be used
             without prior analysis of problems found on the filesystem.
    Options:
      starting point selection:
           -s|--super <superblock>     use this superblock copy
           -b|--backup                 use the first valid backup root copy
           -r|--tree-root <bytenr>     use the given bytenr for the tree root
           --chunk-root <bytenr>       use the given bytenr for the chunk tree root
      operation modes:
           --readonly                  run in read-only mode (default)
           --repair                    try to repair the filesystem
           --force                     skip mount checks, repair is not possible
           --mode <MODE>               allows choice of memory/IO trade-offs
                                       where MODE is one of:
                                       original - read inodes and extents to memory (requires
                                                  more memory, does less IO)
                                       lowmem   - try to use less memory but read blocks again
                                                  when needed (experimental)
      repair options:
           --init-csum-tree            create a new CRC tree
           --init-extent-tree          create a new extent tree
           --clear-space-cache v1|v2   clear space cache for v1 or v2
           --clear-ino-cache 	    clear ino cache leftover items
      check and reporting options:
           --check-data-csum           verify checksums of data blocks
           -Q|--qgroup-report          print a report on qgroup consistency
           -E|--subvol-extents <subvolid>
                                       print subvolume extents and sharing state
           -p|--progress               indicate progress

[jamie@fc33hw01.dawgland.com:~]$


My other favorite file system, especially on servers with huge files, is the XFS filesystem, and the fsck.xfs command on a blank line by itself with no switches or arguments produces this:

[jamie@fc33hw01.dawgland.com:~]$ fsck.xfs
If you wish to check the consistency of an XFS filesystem or
repair a damaged filesystem, see xfs_repair(8).
[jamie@fc33hw01.dawgland.com:~]$


So, when I then type the command "xfs_repair"(NOTE the required under_score in the command name!) without the quotes on a blank line without any switches or arguments, I get the following output:

[jamie@fc33hw01.dawgland.com:~]$ xfs_repair
Usage: xfs_repair [options] device

Options:
  -f           The device is a file
  -L           Force log zeroing. Do this as a last resort.
  -l logdev    Specifies the device where the external log resides.
  -m maxmem    Maximum amount of memory to be used in megabytes.
  -n           No modify mode, just checks the filesystem for damage.
               (Cannot be used together with -e.)
  -P           Disables prefetching.
  -r rtdev     Specifies the device where the realtime section resides.
  -v           Verbose output.
  -c subopts   Change filesystem parameters - use xfs_admin.
  -o subopts   Override default behaviour, refer to man page.
  -t interval  Reporting interval in seconds.
  -d           Repair dangerously.
  -e           Exit with a non-zero code if any errors were repaired.
               (Cannot be used together with -n.)
  -V           Reports version and exits.
[jamie@fc33hw01.dawgland.com:~]$


In the case of fsck.ntfs, the fsck tool for NTFS filesystems, there is no help, and typing "fsck.ntfs" without the quotes on a blank line without any switches or arguments produces no output at all! However, upon further examination, we find that "fsck.ntfs" is just a pointer, what in Linux is known as a "Symlink", pointing to another command called "ntfsck". Using the Linux command/tool "which", we can find where this "Symlink" lives on the hard drive. If it were just a regular file, the "which" command also would show it's location:

[jamie@fc33hw01.dawgland.com:~]$ which fsck.ntfs
/usr/sbin/fsck.ntfs
[jamie@fc33hw01.dawgland.com:~]$


So, when I do a long file listing, to show all file attributes, you can see the actual symlink and where it points to:
<pre style="color:blue">
[jamie@fc33hw01.dawgland.com:~]$ ls -l /usr/sbin/fsck.ntfs 
lrwxrwxrwx. 1 root root 13 Jul 28  2020 /usr/sbin/fsck.ntfs -> ../bin/ntfsck
[jamie@fc33hw01.dawgland.com:~]$


So now, if I type ntfsck --help, I get this:

[jamie@fc33hw01.dawgland.com:~]$ ntfsck --help
Failed to access '--help': No such file or directory
Error opening partition device: No such file or directory
[jamie@fc33hw01.dawgland.com:~]$
 

So, I see that I must specify an actual device, which is the partition containing the non-mounted NTFS filesystem.  I can't stress enough how important it is to only run all of these tools on partitions that are NOT mounted.  This usually means you have to reboot the computer with an alternate device such as a USB flash drive, or other bootable media which contains a "Live operating system" which includes the fsck tools we are referring to.