🌐 🇵🇱 Polski · 🇬🇧 EN
From time to time, you need to perform file system maintenance and clean things up a bit to ensure everything runs efficiently and without issues. Usually, during maintenance, the file system in question is taken offline, i.e., unmounted. Something along the lines of a boss's order: "We need to tidy up the file system on /dev/hdb1 this Wednesday." So, let's get to work.
Step 1 - Unmounting the file system /dev/hdb1
# umount /dev/hdb1
After this, let's look at the tools available to help us perform maintenance and cleanup.
- blkid - Displays device attributes
- df - Checks file system disk space usage
- e2fsck - Verifies an internal file system (ext)
- e2label - Changes the file system label
- findfs - Helps locate specific file systems
- resize2fs - Extends an internal file system - resizing
- tune2fs - Changes file system attribute configuration
First, you need to locate the appropriate file system by the label assigned to it. Using labels makes finding the correct file system faster and easier.
Step 2 - If the file system does not have a label, let's assign one
# e2label /dev/hdb1 CData
Step 3 - Verify that the label has been assigned correctly
# e2label /dev/hdb1
CData
In this example, we don't have many file systems, so finding the right one even without labels isn't a problem. However, if you were working in a much larger environment with specific file systems, you can use the "findfs" tool to help.
findfs tool syntax: # findfs LABEL=<label> | UUID=<uuid>
Step 4 - Let's find a file system knowing its label.
# findfs LABEL=CData
/dev/hdb1
Once we verify that a given label points to a specific file system, we can learn more about it using the "blkid" command.
blkid command syntax: # blkid [options]
blkid command options:
- -s Displays tags
- dev Specifies the device to examine
Step 5 - Using the "blkid" command works well in combination with the "grep" command
# blkid | grep CData
/dev/hdb1: LABEL=”CData” UUID=”2752ffb4-2bca-41c6 a569-f3563f6e884d” TYPE=”ext4”
Step 6 - After maintenance is complete, you can remount the file system with its new label as follows:
# mount LABEL=CData /opt/company_data
In a REDHAT environment, we used the "LABEL" switch with this command. If you wanted to perform the same action in an UBUNTU or DEBIAN environment, you could use the "-L" switch to achieve the same result.
We can update the /etc/fstab file with additional entries related to file system labels. Do not unmount the file systems just yet - we have a few more things to do. Previously, we discussed creating and managing Logical Volumes (LVM) and the benefits of being able to extend partitions. If there is any free disk space in the system, we can use it, or if not, simply add a new disk to increase the available space. You don't get these benefits with basic partitions. To increase the size of a basic partition, you must destroy all current partitions, increase the size, and finally recreate the file system. Since /dev/hdd is configured as LVM, we will use it for the next exercise. We will use the "resize2fs" command to enlarge the file system. Before performing an extension, it is good practice to check the integrity of the file system beforehand using the "e2fsck" command.
e2fsck command syntax: # e2fsck [options] device
e2fsck command options:
- -p - Automatic repair (no prompts)
- -n - Do not make any changes to the file system
- -y - Answer "yes" to all questions
- -f - Force file system check
- -v - Display verbose information
Step 7 - Checking the file system.
# e2fsck -f /dev/vg_group01/lvol0
e2fsck 1.41.12 (17-May-2010)
/dev/vg_group01/lvol0: clean, 11/320000 files, 55366/1280000 blocks
"resize2fs" command syntax: # resize2fs [options] device
resize2fs command options:
- -p - Display percentage progress of the operation
- -f - Force the command
Step 8 - Extending the logical volume :
# lvextend -L 6000 /dev/vg_group01/lvol0
Extending logical volume lvol0 to 5.86 GiB
Logical volume lvol0 successfully resized
Step 9 - Now we will enlarge the file system
# resize2fs -p /dev/vg_group01/lvol0
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_group01/lvol0 to 1536000 (4k)
blocks.
Begin pass 1 (max = 7)
Extending the inode table
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/vg_group01/lvol0 is now 1536000 blocks long.
Step 10 - Now maintenance is complete, let's remount the file system:
# mount LABEL=CData /opt/company_data
Step 11 - Use the "mount" command to verify that mounting was successful
# mount
/dev/mapper/vg_rhel01-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs
(rw,rootcontext=”system_u:object_r:tmpfs_t:s0”)
/dev/hda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc1 on /opt/backup type ext3 (rw)
/dev/hdb1 on /opt/company_data type ext4 (rw)
You can also verify using the " df " command with appropriate switches:
df command syntax: # df [options]
df command options:
- -h - output in human-readable format (mb)
- -l - limit to local file systems
- -T - display file system type
Step 12 - Checking the amount of available space in the system
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_rhel01-lv_root 16G 2.3G 13G 16% /
tmpfs 1004M 0 1004M 0% /dev/shm
/dev/hda1 485M 30M 430M 7% /boot
/dev/hdc1 4.0G 137M 3.7G 4% /opt/backup
/dev/hdb1 7.9G 146M 7.4G 2% /opt/company_data
Comments