Linux administration

Maintain the Integrity of Filesystems

Nguyen Hai Chau
Vietnam National University

Filesystem Concepts

  • The files stored on a disk partition are organised into a filesystem
  • There are several filesystem types; the common Linux one is called ext4
  • A filesystem contains a fixed number of inodes
    • An inode is the data structure that describes a file on disk
    • It contains information about the file, including its type (file/directory/device), modification time, permissions, etc.
  • A file name refers to an inode, not to the file directly
    • This allows hard links: many file names referring to the same inode

Potential Problems

  • Over time, an active filesystem can develop problems:
    • It can fill up, causing individual programs or even the entire system to fail
    • It can become corrupted, perhaps due to a power failure or a system crash
    • It can run out of space for inodes, so no new files or directories can be created
  • Monitoring and checking filesystems regularly can help prevent and correct problems like these

Monitoring Space: df

  • Run df with no arguments to get a listing of free space on all mounted filesystems
  • Usually better to use the -h option, which displays space in human-readable units:
$ df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 2.0G     0  2.0G   0% /dev
tmpfs                    2.1G  7.2M  2.0G   1% /dev/shm
tmpfs                    2.1G  1.8M  2.0G   1% /run
tmpfs                    2.1G     0  2.1G   0% /sys/fs/cgroup
/dev/mapper/fedora-root  104G   91G  7.6G  93% /
/dev/sda3                237G  230G  7.7G  97% /media/D
/dev/sda2                129G  124G  5.3G  96% /media/C
/dev/sda5                500M  253M  217M  54% /boot
/dev/mapper/fedora-home  516G  356G  134G  73% /home
tmpfs                    401M   17k  401M   1% /run/user/42
tmpfs                    401M   33k  401M   1% /run/user/1000

Monitoring Space: df

  • The Use% column shows what percentage of the filesystem is in use
  • You can give df directories as extra arguments to make it show space on the filesystems those directories are mounted on

Monitoring Inodes: df

  • Filesystems rarely run out of inodes, but it would be possible if the filesystem contains many small files
  • Run df -i to get information on inode usage on all mounted filesystems:
$ df -i
Filesystem                Inodes   IUsed    IFree IUse% Mounted on
devtmpfs                  485603     575   485028    1% /dev
tmpfs                     488509      12   488497    1% /dev/shm
tmpfs                     488509     919   487590    1% /run
tmpfs                     488509      16   488493    1% /sys/fs/cgroup
/dev/mapper/fedora-root  6438912 1103134  5335778   18% /
/dev/sda3                7527856   44154  7483702    1% /media/D
/dev/sda2                5487484  338700  5148784    7% /media/C
/dev/sda5                 128016     437   127579    1% /boot
/dev/mapper/fedora-home 32006144  743868 31262276    3% /home
tmpfs                     488509      16   488493    1% /run/user/42
tmpfs                     488509      25   488484    1% /run/user/1000

Monitoring Inodes: df

  • In this example, every filesystem has used a smaller percentage of its inodes (IUse%) than of its file space

    • This is a good sign!
    • If percentage of inode is larger than percentage of space, the file system may have a lot of small files, reducing IO performance

Monitoring Disk Usage: du

  • df shows a summary of the free space on a partition
  • du, on the other hand, shows information about disk space used in a directory tree
  • Takes one or more directories on the command line:
$ du /usr/share/vim
2156    /usr/share/vim/vim58/doc
2460    /usr/share/vim/vim58/syntax
36  /usr/share/vim/vim58/tutor
16  /usr/share/vim/vim58/macros/hanoi
16  /usr/share/vim/vim58/macros/life
40  /usr/share/vim/vim58/macros/maze
20  /usr/share/vim/vim58/macros/urm
156 /usr/share/vim/vim58/macros
100 /usr/share/vim/vim58/tools
5036    /usr/share/vim/vim58
5040    /usr/share/vim

du Options

Option Description
-a Show all files, not just directories
-c Print a cumulative total for all directories named on the command line
-h Print disk usage in human-readable units
-s Print only a summary for each directory named on the command line
-S Make the size reported for a directory be the size of only the files in that directory, not the total including the sizes of its subdirectories

Finding and Repairing Filesystem Corruption: fsck

  • Sometimes filesystems do become corrupted
    • Perhaps there was a power failure
    • Or maybe your kernel version has a bug in it
  • The fsck program checks the integrity of a filesystem
    • And can make repairs if necessary
  • Actually has two main parts:
    • A 'driver program', fsck, which handles any filesystem type
    • One 'backend program' for each specific filesystem type
  • The backend program for ext2 is e2fsck, but it is always invoked through fsck

Running fsck

  • fsck is normally run at system startup
    • So it gets run automatically if the system was shut down uncleanly
  • It can also be run manually:
# fsck /dev/sdb3
  • Interactively asks whether to fix problems as they are found
    • Use -f to force checking the filesystem, even if fsck thinks it was cleanly umounted
    • Use -y to automatically answer 'yes' to any question
    • Usually a bad idea to run fsck on a mounted filesystem!

Monitor system I/O

iostat
## Linux 4.14.8-300.fc27.x86_64 (dino)  01/03/2018  _x86_64_    (4 CPU)
## 
## avg-cpu:  %user   %nice %system %iowait  %steal   %idle
##           16.59
## 
## Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
## sda               6.30        73.55       118.44    7275617   11716428
## dm-0              4.16        30.64        21.91    3031089    2166960
## dm-1              0.00         0.03         0.00       3284          0
## dm-2              6.18        42.80        96.54    4234180    9549464

Exercise 1

  • a. Check the free disk space on the computer.
  • b. Display just the usage information for the partition that contains /usr/. Display this in human-readable units.
  • c. Look at the free space and inodes of the partition of /var/tmp first. Then run these commands:
$ mkdir /var/tmp/foo
$ seq -f '/var/tmp/foo/bar-%04.f' 0 2000 | xargs touch

What has happened? Look at the free space and inodes again. Remove the files when you have finished.

Exercise 2

Go into the /var/ directory. Run each of the following commands as root, and explain the difference in their output:

  • a. # du
  • b. # du -h
  • c. # du -h *
  • d. # du -hs
  • e. # du -hs *
  • f. # du -hsS *
  • g. # du -hsc *
  • h. # du -bsc *