
How to Use the Command 'mkfs.erofs' (with Examples)
- Linux
- December 17, 2024
The mkfs.erofs command is a tool used for creating EROFS (Enhanced Read-Only File System) filesystems. EROFS is a lightweight, high-performance read-only file system primarily used in scenarios where storage efficiency and speedy data access are crucial. It is particularly advantageous in environments like Android systems, predominantly because of its capability to improve boot times while economizing storage space. The following examples demonstrate different use cases for this command.
Use case 1: Creating an EROFS Filesystem Based on the Root Directory
Code:
mkfs.erofs image.erofs root/
Motivation: Creating an EROFS filesystem based on a specific directory, such as the root directory, is often necessary when encapsulating the entire OS state into a read-only image. This can be invaluable for systems that require a stable, unchanging base for either efficiency or security reasons. In environments like embedded systems or appliances, ensuring that the underlying system remains immutable can prevent unauthorized changes and ensure consistent performance.
Explanation:
mkfs.erofs: This is the command to create an EROFS filesystem.image.erofs: The name of the image file that will store the EROFS filesystem. This file will be created or overwritten if it already exists.root/: The source directory from which to create the EROFS filesystem. Here, it refers to the content of the root directory that will be put into the new EROFS image.
Example Output:
Upon successful execution, the command does not produce an explicit output but generates an image.erofs file representing the EROFS filesystem based on the content of the root/ directory.
Use case 2: Creating an EROFS Image with a Specific UUID
Code:
mkfs.erofs -U UUID image.erofs root/
Motivation: Assigning a specific UUID (Universally Unique Identifier) to an EROFS image can be crucial for systems requiring a unique identification method for each filesystem image. This is particularly important in networked environments or complex storage setups where managing and identifying different filesystem images accurately is essential for mounting and system configurations.
Explanation:
-U UUID: This option specifies a custom UUID for the filesystem, replacing the automatically generated one. This UUID is vital for differentiating this filesystem from others.image.erofsandroot/: As previously described, these specify the output image file and the source directory.
Example Output:
The command will complete silently but confirm that an EROFS filesystem with the specified UUID has been created in the image.erofs file.
Use case 3: Creating a Compressed EROFS Image
Code:
mkfs.erofs -zlz4hc image.erofs root/
Motivation: Compression is often employed to reduce the storage footprint of a filesystem, making it a practical choice for systems with limited storage capacity. By compressing an EROFS image, users can store more data in the same space, enhancing efficiency and potentially improving read performance due to reduced disk I/O operations.
Explanation:
-z: Indicates that the filesystem should be compressed.lz4hc: Specifies the compression algorithm to use, in this case, LZ4 in high-compression mode (hc). LZ4hc provides a balance between speed and compression ratio, making it suitable for resource-constrained environments.image.erofsandroot/: The output image file and source directory remain consistent as input and output arguments.
Example Output:
Completing the command results in a compressed EROFS image file named image.erofs, containing the compressed contents from root/.
Use case 4: Creating an EROFS Image Where All Files Are Owned by Root
Code:
mkfs.erofs --all-root image.erofs root/
Motivation: In certain secure or controlled environments, it may be necessary to ensure that all files in a read-only filesystem appear as owned by the root user. This ownership alteration can prevent privilege escalation opportunities that could arise from owned user files, thereby enhancing the security of the system upon deployment.
Explanation:
--all-root: This option ensures that every file within the EROFS image appears as owned by the root user, automatically adjusting the ownership metadata.image.erofsandroot/: These parameters again specify the target output file for the EROFS image and the source directory for the filesystem content.
Example Output: Executing this command results in an EROFS image, with all files and directories showing ownership under the root user.
Conclusion:
Using the mkfs.erofs tool and understanding its various use cases allow system administrators and developers to efficiently manage read-only filesystems tailored to specific needs. By leveraging the examples provided, one can effectively utilize EROFS in environments that prioritize stability, storage efficiency, and enhanced performance.

