[Turn]romfs

Source: Internet
Author: User

1.1 What is Romfs
Romfs is a read-only file system, mainly used in mainly for initial RAM disks of installation disks. Using the Romfs file system, you can construct a minimal kernel and save memory. In contrast, the early Minix and Xiafs (now obsolete) file systems are larger than 20000 bytes (38502 bytes on the x86 machine) if compiled as modules, while Romfs is less than one page (on a Linux system, a page size of Page_ OFFSET, typically 4K), approximately 4000 bytes (10479 bytes in size on the x86 machine). Under the same conditions, the Msdos file system module is approximately 30K (and does not support device nodes and symbolic links on the x86 machine with a size of 12K). The NTFS and Nfsroot file system modules are approximately 57K in size (102K on the x86 machine).
Note: The values in the above narrative are for i586 machines, the values described in parentheses are the size on the current x86 machine, for the 2.6.28 kernel.
Use of 1.2 Romfs
Romfs The main goal of this design is to construct a minimal kernel that only links the Romfs file system in the kernel, so you can use ROMFS to load other modules later. Romfs can also be used to run programs that determine whether you need a SCSI device, or an IDE device, or if you are using the "INITRD" structure of the kernel, ROMFS can also be used to load the floppy drive later. Another use for Romfs is that when you use the Romfs file system, you can turn off ext2 or Minix or even affs the file system until you're sure you want it to open again.
Performance of 1.3 Romfs
The ROMFS operation is based on block devices, and its underlying structure is very simple. For quick access, each cell is designed to start at a 16-byte boundary. A minimum file is 32 bytes (the file contents are empty and the filename is less than 16 bytes long). The maximum cost for a non-empty file is the file header that precedes the file content and the 16-byte file name after it (since most filenames are longer than 3 bytes and less than 15 bytes, the preset filename is 16 bytes long).
1.4 How to use the ROMFS image
To use an image in a well-crafted ROMFS format, it is mounted on a node in another file system. And there is a very important premise, that is, the kernel to support the Romfs file system. This can be achieved by configuring the kernel, with two methods:
1. Configure the ROMFS to compile directly into the kernel by using the Make Menuconfig command to enter the kernel configuration interface, select "File Systems" and enter, select "Miscellaneous filesystems" and enter, select "ROM file System Support (ROMFS), which is configured as "*" (directly compiled into the kernel). The resulting kernel directly includes support for the Romfs file system.
2. Configure the ROMFS as a module, as in the previous step, and configure it as "M" (compiled as a kernel module) only when you finally select "ROM File system Support (ROMFS)". The compiled kernel does not contain support for the Romfs file system, but only generates the Romfs.ko module (Fs/romfs/romfs.ko), which needs to be loaded into the kernel after booting the system to enable the kernel to support ROMFS file systems.
With the kernel's support for the Romfs file system, it is possible to mount an image of the ROMFS format directly, with the following mounting methods:

[Email protected]:~/kernel/romfs$ ls
Hello.img
[Email protected]:~/kernel/romfs$ file hello.img
HELLO.IMG:ROMFS filesystem, Version 1 208 bytes, named Rom 49e05ac0.
[Email protected]:~/kernel/romfs$ sudo mount-o loop hello.img/mnt
[Email protected]:~/kernel/romfs$ cd/mnt/
[Email protected]:/mnt$ ls
hello.c
[Email protected]:/mnt$

You can see that the Mount command is used to mount the hello.img to the/MNT directory with only one file.
Unmount an already mounted ROMFS format image using the Umount command.
1.5 How to make Romfs File System
In general, we can use some tools to make Romfs file system. After the production is actually a binary file. Production tools generally use "Genromfs", this tool can be downloaded on the Internet, its source code is not many, only less than 900 lines.
The following are the parameters supported by the Genromfs tool:

[Email protected]:~/fs-sys$ genromfs-h

Genromfs 0.5.2 Usage:genromfs [OPTIONS]-fimage

Create a ROMFS filesystem image from a directory

-F Image Output the IMAGE into this file

-D directory Use the This directory as source

-V (Too) verbose operation

-V VOLUME Use the specified VOLUME name

-A ALIGN ALIGN regular file data to ALIGN bytes

-A Align,pattern ALIGN all objects matching PATTERN to at least ALIGN bytes

-X pattern Exclude all objects matching pattern

-H Show This help report bugs to [email protected]

[Email protected]:~/fs-sys$

-f image     specifies the name of the output ROMFS image
-D directory specifies the source directory (the directory is created as a Romfs file system)
-V            Show detailed creation procedure
-v volume    Specify volume label
-A align     specifies an alignment boundary for a normal file (default is 16 bytes)
-A Align,pattern The object that matches the parameter PATTERN is aligned on the ALIGN boundary
-X pattern Objects that match pattern are not included.
-H Displays the help document.
Here's how to make a file system that generates a ROMFS:

[Email protected]:~/fs-sys$ ls

Test

[Email protected]:~/fs-sys$ ls test/

Test Xux Zhwen

[Email protected]:~/fs-sys$ genromfs-v "Xromfs"-F romfs.img-d Test

[Email protected]:~/fs-sys$ ls romfs.img test

[Email protected]:~/fs-sys$ file romfs.img

ROMFS.IMG:ROMFS filesystem, Version 1 592 bytes, named Xromfs.

[Email protected]:~/fs-sys$ sudo mount Romfs.img/mnt-o loop

[Email protected]:~/fs-sys$ ls/mnt/test Xux Zhwen

[Email protected]:~/fs-sys$.

2.romfs Image Structure

In ROMFS format images generated using GENROMFS, files or directories are stored sequentially, each header aligned on a 16-byte boundary, and the basic structure

The ROMFS image begins with 16 bytes corresponding to the struct romfs_super_block struct, followed by the volume label of the ROMFS image. The case shown is that the volume label is less than 16 bytes, and if the volume label is larger than 16 bytes, the order is placed after romfs_super_block and aligned on a 16-byte boundary. Immediately after the label is the first file header Romfs_inode structure, a total of 16 bytes, followed by the file name, is 16-byte alignment, and then the file content. One of the first 4 bytes in the file header of each file (excluding the low 4 bits) is the offset of the next file header in the Romfs image. Romfs is exactly how the entire file is organized together. Below are the romfs_super_block and romfs_inode structures respectively.

2.1 Romfs Super Block structure

In the Linux kernel, the ROMFS super block structure is defined in Include/linux/romfs_fs.h and the source code is:

/* on-disk "Super block" */

struct Romfs_super_block {
__be32 word0;
__be32 word1;
__BE32 size;
__BE32 checksum;
Char name[0]; /* Volume name */
};

(1) word0 and the word1

The values for word0 and word1 are fixed, respectively, "-rom" and "1fs-", and are used to identify the Romfs file system. These two values are defined in the kernel:

#define ROMSB_WORD0 __mk4 ('-', ' R ', ' O ', ' m ')
#define ROMSB_WORD1 __mk4 (' 1 ', ' f ', ' s ', '-')

(2) Size Domain

The Size field represents the sizes that the ROMFS image can access. That is, the end position of the last file (in 16-byte alignment), such as:

[Email protected]:~/romfs$ ls-l romtest
Total 4
-rw-r--r--1 Niutao niutao 2009-04-08 20:15 len.c
[Email protected]:~/romfs$ genromfs-f rom.img-d romtest/-v-v Niutao
0 Niutao [0xFFFFFFFF, 0xFFFFFFFF] 37777777777, SZ 0, at 0x0
1. [0x80e, 0x18aada] 0040755, SZ 0, at 0x20
1.. [0x80e, 0x18aac6] 0040755, SZ 0, at 0x40 [link to 0x20]
1 len.c [0x80e, 0x18aadb] 0100644, SZ, at 0x60
[Email protected]:~/romfs$

For the rom.img image generated above, the last file within Rom.img is LEN.C, its file header is offset to 0x60, the file name is LEN.C (less than 16 bytes), the file size is 44 bytes, so we can calculate the size of the rom.img can be accessed as:
Size = (0x60 + + + +)/0x10 * 0x10 + 0x10 = 0xb0
The first 16 represents the size of the header (struct Romfs_inode structure), and the second 16 indicates the length of the file name occupied. Below we use the File command to verify that the calculation is correct:

[Email protected]:~/romfs$ file rom.img
ROM:ROMFS filesystem, Version 1 176 bytes, named Niutao.
[Email protected]:~/romfs$

You can see that the file is accessible in size exactly b0 bytes (176 bytes). Of course, it is best to look directly at the value of size in the ROMF.IMG super structure (at rom.img offset 0x8):

[Turn]romfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.