by Paul Arnote (parnote)
These days, most of us have thousands of photos and documents stored on our computer's hard drive. As much as we've all heard it, preached to over and over again, most of us are less than diligent about backing up our data -- if we even do it at all. That ol' "it'll never happen to me" attitude tends to rule the day.
One of the most vital parts of your computer -- your hard drive and the file system on it that keeps your data organized and accessible -- probably receives the least amount of attention from users. That is, until something goes wrong. That "something" can involve your file system, and when something does go awry with the file system, panic strikes most users like a lightning bolt out of the sky. So, checking up on and keeping tabs on what's happening with your file system has the potential to save you a LOT of extra worries and headaches down the road. Plus, it can save your precious data from being corrupted -- or worse -- lost forever.
Fortunately, Linux comes with some very useful utilities for helping to ensure the integrity of the data stored in your file system. The easiest way to use them is from the command line in a terminal session. The main commands we're going to be using (in no particular order) are fsck, tune2fs, touch, mount, umount and fdisk. Most of these commands will need to be run as the root user.
fdisk
Before we can use the other commands, we first need to know how the operating system sees the various hard drives on our computer. To do that, we issue the fdisk -l command (that's a lowercase L). It is installed by default in PCLinuxOS, so you have nothing else to install from Synaptic. There will be quite a bit of output, but what we're really interested in is the last part. Below is the output from one of my computers.
Disk /dev/sda: 232.9 GiB, 250059350016 bytes, 488397168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xa33411ea
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 63 8177084 8177022 3.9G 82 Linux swap / Solaris
/dev/sda2 8177085 488392064 480214980 229G 5 Extended
/dev/sda5 8177148 52002404 43825257 20.9G 83 Linux
/dev/sda6 52002468 488392064 436389597 208.1G 83 Linux
Looking at the output, /dev/sda5 is my root partition, and /dev/sda6 is my /home partition. Write these down for handy reference to use with the other commands.
There are a lot more uses for fdisk. You can view the fdisk Linux man page here.
tune2fs
tune2fs allows users to set or adjust tunable filesystem parameters for ext2, ext3 and ext4 filesystems. We'll use a couple of different command line switches to help us achieve our goal of managing the filesystem. You can find a whole list of available command line switches, complete with explanations, by viewing the tune2fs Linux man page here. It is installed by default in PCLinuxOS, so you have nothing else to install from Synaptic.
First, we need to issue the tune2fs -l command (that's a lowercase L, for list), followed by the hard drive partition we want information on. Below is the output from running the command on my /home partition, a.k.a. /dev/sda6. Use the information gleaned from running fdisk -l on your computer, and replace my use of /dev/sda6 with the drive partition from your computer, as reported by fdisk.
For readability, I've highlighted some of the information I'm particularly interested in with red text. Early on in the listing, you'll find the "Filesystem state" information. On this particular hard drive, the filesystem is listed as "clean." A bit further down in the information displayed by the tune2fs command is some important information about the mounting and checking of that particular hard drive and filesystem.
The data shows that my /home partition has been mounted eight times since the last filesystem check (this particular computer tends to stay turned on 24/7). You will notice that "Maximum mount count" is set to -1. With this parameter set to -1 or 0, the number of times my hard drive has been mounted (Mount count) is ignored. The next thing to notice is the date of the last filesystem check. In my case, it's November 28, 2016. Finally, notice that the "Check interval" is set to zero, which means there is no periodic filesystem check interval set for that particular hard drive partition.
Lucky for us, tune2fs does more than just list this information. Tune2fs also allows us to set up a regularly scheduled check of the filesystem. The method you choose to use might be entirely dependent on how you use your computer. If you are the type of person who turns their computer off every night and back on every morning, you might want to set the filesystem check frequency to be every 30 times you mount that particular partition. To do so, issue the command as tune2fs -c 30 /dev/sda6 (in my particular case) to check it every time the mount count increments to 30. If you're like me and have a computer that stays turned on 24/7, you might want to use a time based interval to determine when to perform a filesystem check. The command tune2fs -i 3m /dev/sda6 will cause a filesystem check to occur every three months, the next time the partition is mounted. A number by itself is interpreted as days (you can also use the "d" designation to denote days), while a number with "w" after it is interpreted as weeks, and "m" is interpreted as months.
fsck
The next tool is the utility called fsck, which stands for File System Consistency Check. Others refer to it simply as File System Check. It is installed by default on PCLinuxOS, so you have nothing to install from Synaptic.
Take note that fsck will ONLY check a filesystem that is currently unmounted. Attempting to run the fsck command by itself, with no parameters, will net you this response:
# fsck
fsck from util-linux 2.27.1
e2fsck 1.42.13 (17-May-2015)
/dev/sda5 is mounted.
e2fsck: Cannot continue, aborting.
So, checking the filesystem on ANY partition except the root partition is relatively easy. First, unmount the partition you want to check by issuing the umount command. So, to check the /home partition on my computer that I've been using as an example, the command will be umount /dev/sda6. Then, I can issue the fsck command like this: fsck -p /dev/sda6. Once the filesystem check has been completed, you can mount the partition again, using the command mount /dev/sda6.
The "-p" command line switch tells fsck to automatically repair any errors without asking any questions. If you want to run fsck interactively, use the -r option, instead of -p. If you want to see what's going on, use the -v option (along with the other options) to cause verbose output.
This all works great for any and all partitions, except the root partition. For all other partitions -- except root -- it's pretty easy and straightforward. But what happens when you want or need to run fsck on the root partition? Fortunately, PCLinuxOS users have two options.
The first option is to boot from a Live CD/DVD/USB and run fsck on your hard drive's root partition (/). Just enter the command as outlined here, specifying the name of your root partition (in my case, fsck -p -v /dev/sda5).
The second option is probably easier, though. From the command line, travel to the top level of the root partition (/). Then, issue the command touch forcefsck. Now, the next time you boot your computer, the filesystem on the root partition of your hard drive will be checked. After the filesystem check is completed, the forcefsck file will be deleted.
Summary
With just very little effort, you can keep the filesystems on your hard drive partitions tuned and happy for a very long time to come.
I have to admit that I was a bit shocked and surprised to discover that there are no minimal filesystem checks scheduled by default on PCLinuxOS. Even Ubuntu sets a minimal filesystem check for every 30 times a partition is mounted. But, I'm equally pleased to know that we don't have to accept not having scheduled filesystem checks. That is one thing that anyone can change with relative ease.
|