A Little About Linux: Keeping File Systems Happy

Although for 99% of the time the your file system will be fine, occasionally something will go wrong, or you will find that sometimes the file system needs to be tuned specifically to your needs.  This section will go through these scenarios and cover some of the tools you will need when dealing with Linux file systems.

Tuning Your File System
In case you didn't know, a file system is a way of storing data in a structured way on a disk/usb drive or other device.  Every group or person who designs a file system will make some design decisions that make their file system different to others, it may perform exceptionally well with large files or with lots of small files... either way, they all have their own unique selling point.  Now, in order to meet their design aim, something else will have no doubt been compromised and this may affect performance in the way you want to use the file system.  If you're lucky, the designers of the file system may have allowed you to tweak some parameters to change how it performs (specifically ext2, ext3 and ext4) and to do this the following tools can be used;

dumpe2fs -  Shows file system information for ext2, ext3 and ext4 file systems and can be run on mounted or unmounted file systems.

dumpe2fs -h /dev/sdb1

It displays information such as...

Last mounted on:          /data1
Filesystem UUID:          4bc5430c-fb5c-4fdb-b7d0-7785254a9e1d
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Filesystem created:       Sat Jun 28 07:31:03 2014
Last mount time:          Tue Oct 21 20:40:21 2014
Last write time:          Tue Oct 21 20:40:21 2014
Mount count:              74
Maximum mount count:      33
Last checked:             Sat Jun 28 07:31:03 2014
Check interval:           15552000 (6 months)
Next check after:         Thu Dec 25 06:31:03 2014
Lifetime writes:          240 GB
Directory Hash Seed:      4b2b6b77-44da-4fdc-a1c1-8e21421eafe3
Journal backup:           inode blocks
Journal features:         journal_incompat_revoke
Journal size:             128M
Journal length:           32768
Journal sequence:         0x0001282d
Journal start:            0

Now, with the information gleaned from the above command, you can alter many aspects of the file system using;

tune2fs - Enables you to change certain ext2, ext3 and ext4 file system parameters

There are many parameters and options, but the ones you will use most are below...

Sets the number of times the file system can be mounted before re-check (50 in this case)
tune2fs -c 50 /dev/sdb1

tune2fs 1.41.12 (17-May-2010)


Setting maximal mount count to 50

Setting -c 0 or -c -1 means that the check is disabled although this is strongly discouraged.

Adjust the time period between checks.  This means it will re-check after a certain number of days, months or weeks regardless of how many times its mounted (every 4 weeks in this case)
tune2fs -i 4w /dev/sdb1

tune2fs 1.41.12 (17-May-2010)
Setting interval between checks to 2419200 seconds

debugfs - Interactively debug and change the states of ext2, ext3 and ext4 file systems.

debugfs /dev/sdb1

debugfs 1.41.12 (17-May-2010)
debugfs:

You can type options at the debugfs: command prompt such as stat filename to display the inode data of a file, or extract a file from damaged and unmountable file system using write internal-file external-file.

e2fsck - Checks and corrects health issues on ext2, ext3 or ext4 file systems.  On journalling systems (ext3 and ext4) that have been shut down uncleanly, this will attempt to replay the journal first  before attempting other checks. fsck is a tool with similar aims as e2fsck but will work with a wider range of file systems.

Run an automatic repair on the specified file system, add any bad blocks found to the bad blocks list and be verbose. Only run when the file system is un-mounted.

e2fsck -pcv /dev/sdb1

You can also run the commands without any parameters...

fsck /dev/sdb1

fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/sdb1 has been mounted 78 times without being checked, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 95366/244195328 files (3.0% non-contiguous), 62290712/976754176 blocks

Monitoring Disk Usage
The next two commands aren't really maintenance tools, but are away for a System Administrator to find out how much of the file system is being utilised and/or how much of it is remaining.

df - Shows disk space usage by partition

The most used version of this command will be to show space information for all physical partitions (remove -h to have sizes in bytes)
df -h

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_box-lv_root
                       50G  2.6G   45G   6% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             485M  127M  333M  28% /boot
/dev/mapper/vg_box-lv_home
                      405G  315M  385G   1% /home

/dev/sdb1             3.6T  192G  3.3T   6% /data1

As above but with the file system type specified also
df -hT

Filesystem           Type   Size  Used Avail Use% Mounted on
/dev/mapper/vg_box-lv_root
                     ext4    50G  2.6G   45G   6% /
tmpfs                tmpfs  1.9G     0  1.9G   0% /dev/shm
/dev/sda1            ext4   485M  127M  333M  28% /boot
/dev/mapper/vg_box-lv_home
                     ext4   405G  315M  385G   1% /home

/dev/sdb1            ext4   3.6T  192G  3.3T   6% /data1

Show available and used inodes rather than blocks
df -i

Filesystem              Inodes  IUsed     IFree IUse% Mounted on
/dev/mapper/vg_cutebox-lv_root
                       3276800  86155   3190645    3% /
tmpfs                   489354      1    489353    1% /dev/shm
/dev/sda1               128016     62    127954    1% /boot
/dev/mapper/vg_cutebox-lv_home
                      26968064   3217  26964847    1% /home
/dev/sdb1            244195328 100750 244094578    1% /data1

du - Shows disk space usage by directory

List all directories from / with the directory size in human readable form (A really long list!)
du / -h

List all directories below the current location with directory size with total size at end (in human readable format)
du . -hc

List the cumulative size of directories below current point (in human readable format)
du . -hs

No comments :