Managing Files and Disk Space
The course is part of this learning path
This course - Linux partitions and filesystems - concludes the first half of our Linux certification series and, not coincidentally, covers the last topics you'll need to know for the LPIC-1 101 exam. The final six courses will get you up to speed on the 102 exam.
Besides getting to know the Linux Filesystem Hierarchy Standard, we'll learn how to:
- Create and maintain secure and reliable partitions and filesystems
- Mount and unmount filesystems
- Limit access to only authorized users.
- Create and manage hard and symbolic linked files.
- Control the disk space allocation.
The previous course covered the Linux command line.
If you have thoughts or suggestions for this course, please contact Cloud Academy at email@example.com.
Linux file systems are pretty strong creatures, but they can occasionally break. As a Linux admin, you'll need to know how to fix it. Just like about everything else in life, it's good to organize your efforts into three categories, monitoring or troubleshooting, preventative maintenance, and repair. In this video, we're going to explore each of these categories. You can get a pretty good view of your file system, using df and du. df will list all the active partitions associated with your system by name, display their total size and how much of that space is still available.
Monitoring Linux filesystem space and inode use
Obviously, if a partition is nearly full, it can be the cause of performance issues and you'll want to address it. -H displays the numbers in human readable form. df -T will also display the file system type with which each partition was formatted. df-I displays inode use rather than disk space, and inode, short for index node, is a data structure used to represent a file system object, like a file or directory.
Since each file and directory must be associated with an inode, and since the total number of available inodes was limited when the file system was created, it is possible, although unlikely that you could actually run out. You'll obviously want to know if this is your problem, and df-I is the place to find out. du will list the total disk usage of the current directory, along with all the subdirectories below it. Again, -H displays the numbers in human readable form. This can be really useful if you've noticed that an unusually high percentage of your overall disk space is being swallowed by something, but you're not sure what. du will tell you which directories are taking up most of the space. du -s will report only the overall total. As always, reading the man file will reveal lots of other useful tools. If your partition uses the ext2 file system type or above, then you can use dumpe2fs to display all kinds of useful information, like the volume name, mount point, file system features and file system state.
So that's it for monitoring and troubleshooting. We might as well review what we've seen so far. df lists the file systems currently active. df -T lists their types, and df -I tells you how many free inodes you still have for each file system. du displays the space usage of a particular directory hierarchy.
Use tools like fsck to prevent or solve Linux filesystem problems
Now, let's turn our attention to preventative maintenance. If you're working with an ext2 or above file system, tune2fs -c lets you set the number of disk mounts before running a system check. If you're concerned that your file system is a bit more prone to corruption than normal, you'll want to set this number rather low, say every 40 mounts. In this example, -C orders an automatic check every 40 mounts.
-i sets the maximum interval between checks, even if there haven't been 40 mounts, to one month. And dev/sda1 is the partition to be checked. Tune2fs -j will add a journal to any ext2 file system, which will, more or less, turn it into ext3. And tune2fs -L will set the file system volume label. Besides packages for ext2, there are also similar tools available for other file system types, like reiserfstune, xfs_admin, and xfs_info.
So what happens when you actually do run into a problem? Your first stop should probably be the fsck (filesystem check) tool - or various equivalents like e2fsck. fsck will first check the specified filesystem, but then apply a range of fixes to clean up the problems it finds. We'll run fsck against the usb drive in /dev/sdc. But we'll first need to make sure that the drive is not mounted.
fsck sees that the dirty bit is set. Now, don't go out and buy some toilet bowl cleaner to take care of it. A dirty bit is just the indicator that a corresponding block of memory has been changed, but not yet saved to disk. It might also be an indication that some data has been corrupted. We can ask fsck to remove or reset the bit and go on. Everything else seems fine. E2fsck also checks for and repairs file system problems, including restoring a corrupted super block. Running E2fsck by itself will give us a helpful list of the most common commands. We can see the -B, when accompanied by the absolute location of an alternative super block file, will restore your super block.
Finally, running debugfs will drop you into a special debug shell. Hitting question mark will print a couple of pages of commands. Running stat on the VM line of this file for instance, which happens to be in the root directory of this partition, will give us some useful diagnostic information about the file. We can also perform many basic file management functions, like creating and deleting directories, and using write, copying files from other file systems. I'm sure you can see the value this might have for restoring troubled partitions.
Let's review. Tune2fs can control the number of mounts that are allowed before an automatic FS check is launched. Tune2fs -j will add journaling to ext2 file system. fsck, when run against a file system, will check for and correct file system problems. E2fsck will repair problems specifically associated with ext2 and newer systems. Finally, the debut FS shell allows you to perform diagnostic repair and file management operations on a file system.
About the Author
David taught high school for twenty years, worked as a Linux system administrator for five years, and has been writing since he could hold a crayon between his fingers. His childhood bedroom wall has since been repainted.
Having worked directly with all kinds of technology, David derives great pleasure from completing projects that draw on as many tools from his toolkit as possible.
Besides being a Linux system administrator with a strong focus on virtualization and security tools, David writes technical documentation and user guides, and creates technology training videos.
His favorite technology tool is the one that should be just about ready for release tomorrow. Or Thursday.