How to use the command debugfs (with examples)
- Linux
- December 25, 2023
Debugfs is an interactive ext2/ext3/ext4 filesystem debugger. It allows users to examine and manipulate the internal components of a filesystem. This article will provide examples of various use cases for the debugfs command.
Use case 1: Open the filesystem in read-only mode
Code:
debugfs /dev/sdXN
Motivation: By opening the filesystem in read-only mode, users can view the content of the filesystem without the risk of accidentally modifying or deleting any files.
Explanation: The command debugfs /dev/sdXN
opens the filesystem located on the device /dev/sdXN
in read-only mode. /dev/sdXN
represents the device and partition number of the filesystem.
Example output: Once the command is executed, the debugfs console will be displayed, allowing users to interact with the filesystem. Users can utilize various commands within the console to perform actions.
Use case 2: Open the filesystem in read-write mode
Code:
debugfs -w /dev/sdXN
Motivation: Opening the filesystem in read-write mode allows users to modify and manipulate the files and directories within the filesystem.
Explanation: The command debugfs -w /dev/sdXN
opens the filesystem located on the device /dev/sdXN
in read-write mode. The -w
option specifies that the filesystem should be opened for both reading and writing.
Example output: Once the command is executed, the debugfs console will be displayed as before, but this time with read-write permissions. Users can now perform actions such as creating, modifying, or deleting files and directories.
Use case 3: Read commands from a specified file, execute them, and then exit
Code:
debugfs -f path/to/cmd_file /dev/sdXN
Motivation: This use case allows users to automate a series of commands to be executed within the debugfs console, rather than manually typing each command.
Explanation: The command debugfs -f path/to/cmd_file /dev/sdXN
reads the commands from the specified file path/to/cmd_file
and executes them within the debugfs console. After executing all the commands in the file, the debugfs console will be closed, and the program will exit.
Example output: The commands from the specified file will be executed one by one, and any resulting output or changes to the filesystem will be displayed.
Use case 4: View the filesystem stats in debugfs console
Code:
stats
Motivation: By using the stats
command within the debugfs console, users can obtain detailed information about the underlying filesystem, such as the number of files, directories, blocks, and inodes.
Explanation: Typing stats
within the debugfs console will display various statistics about the filesystem, including the total number of blocks, free blocks, used blocks, directory inodes, and file inodes.
Example output: The output will vary depending on the specific filesystem being examined, but it will provide detailed statistics about the filesystem.
Use case 5: Close the filesystem
Code:
close -a
Motivation: Closing the filesystem is necessary to release any resources associated with it and ensure that any changes made are properly saved.
Explanation: The command close -a
within the debugfs console closes the currently opened filesystem and exits the program. The -a
option specifies that all filesystems currently open should be closed.
Example output: After executing the command, the debugfs console will be closed, and the program will exit.
Use case 6: List all available commands
Code:
lr
Motivation: Listing all available commands provides users with a quick reference of what actions can be performed within the debugfs console.
Explanation: Typing lr
within the debugfs console will display a list of all available commands. This can be helpful for users who may be unfamiliar with the available functionality of debugfs or who want to quickly check if a specific command is available.
Example output: The output will be a list of commands that can be used within the debugfs console, along with a brief description of each command.
Conclusion:
The debugfs command offers a wide range of functionality for examining and manipulating ext2/ext3/ext4 filesystems. By understanding the various use cases and their associated commands, users can effectively interact with the filesystem and perform actions such as viewing filesystem stats, modifying files, and closing the filesystem.