Add a file system in Linux

Source: Internet
Author: User

2.1 Add a file system named myext2 that is identical to ext2.
To add a file system named myext2 that is exactly the same as ext2, first determine which files are used to implement the kernel source code of the ext2 file system. The Linux source code structure clearly tells us that all files in the FS/ext2 directory belong to the ext2 file system. Check the header files contained in these files. We can preliminarily summarize the following in the Linux source code:

FS/ext2/ACL. c
FS/ext2/ACL. h
FS/ext2/balloc. c
FS/ext2/bitmap. c
FS/ext2/Dir. c
FS/ext2/ext2.h
FS/ext2/file. c
......
Include/Linux/ext2_fs.h
Include/Linux/ext2_fs_sb.h

Next, add the source code of the myext2 File System to the Linux source code. Clone the source code of the ext2 part to myext2, that is, copy the ext2 source code file listed above for myext2. According to the Linux source code structure, store the source code of the myext2 File System Under fs/myext2, and put the header file under include/Linux. In the Linux Shell, perform the following operations (you may need to convert it to the root user first ):

# Cd/usr/src/Linux
# Cd FS
# Cp-r ext2 myext2
# CD ../include/Linux
# Cp ext2_fs.h myext2_fs.h
# Cp ext2_fs_sb.h myext2_fs_sb.h
# Cd/usr/src/Linux/fs/myext2
# Mv ext2.h myext2.h

This completes the first step of cloning the file system-source code replication. This is certainly not enough for the clone file system, because the data structure name, function name, and related Macros in the file have not been changed according to myext2, compilation fails.
Next, start cloning the file system. Step 2: modify the content of the file added above. For the sake of simplicity, a simple replacement is made: replace the original "ext2" with "myext2", and replace the original "ext2" with "myext2 ".

You can also use the following script to replace the string in the file fs/myext2:

#! /Bin/sh

Script = substitute. Sh

For f in *;
Do
If [$ F = $ script]; then
Echo "Skip $ F"
Continue
Fi

Echo-n "substitute ext2 to myext2 in $ f ..."
Cat $ f | SED's/ext2/myext2/G' >$ {f} _ TMP
MV $ {f} _ TMP $ F
Echo "done"

Echo-n "substitute ext2 to myext2 in $ f ..."
Cat $ f | SED's/ext2/myext2/G' >$ {f} _ TMP
MV $ {f} _ TMP $ F
Echo "done"

Done

Name the script substitute. sh, put it under fs/myext2, and add executable permissions, after running, you can replace "ext2" and "ext2" in all files in the current directory with the corresponding "myext2" and "myext2 ".

Tip: errors may occur during kernel compilation, and some code needs to be modified. These codes are in different files. Be careful and patient when doing experiments.

The modification of source code ends here. The third step is to compile the source code. Add myext2 to the compilation options so that you can add this option when making menuconfig. To do this, you only need to modify three files:
FS/kconfig
FS/makefile
ARCH/i386/defconfig
Copy the corresponding definition and help information of the ext2 macro in FS/kconfig, so that you can view the Help content of this option when making menuconfig. The modification of FS/makefile is to inform the kernel compilation system. When the macro corresponding to myext2 is selected, compile the myext2 File System in the FS/myext2 directory. Defconfig is used to change the default compilation options, for example:

--- Fs/kconfig. bak2007-08-15 07:14:11. 000000000-0400
++ Fs/Kconfig2007-08-15 07:21:29. 000000000-0400
@-62,6 + 62,62 @@
If you do not use a block device that is capable of using this,
Or if unsure, say n.

+ Config myext2_fs
+ Tristate "my second extended FS Support"
+ Help
+ This is a test of adding a self-defined filesystem.
+
+ To compile this file system support as a module, choose m here:
+ Module will be called myext2.
+
+ If unsure, say y.
...
--- Fs/makefile. bak2007-08-15 08:37:03. 000000000-0400
++ Fs/Makefile2007-08-15 08:37:21. 000000000-0400
@-54,6 + 54,7 @@
OBJ-$ (config_ext3_fs) + = ext3/# Before ext2 so root FS can be ext3
OBJ-$ (config_jbd) + = jbd/
OBJ-$ (config_ext2_fs) + = ext2/
+ Obj-$ (config_myext2_fs) + = myext2/
OBJ-$ (config_cramfs) + = cramfs/
OBJ-$ (config_squashfs) + = squashfs/
OBJ-$ (config_ramfs) + = ramfs/
...
--- ARCH/i386/defconfig. bak2007-08-15 07:16:34. 000000000-0400
+ + ARCH/i386/defconfig2007-08-15 07:17:07. 000000000-0400
@-Example 3, 6 + Example 3, 8 @@
#
Config_ext2_fs = y
# Config_ext2_fs_xattr is not set
+ Config_myext2_fs = y
+ # Config_myext2_fs_xattr is not set
Config_ext3_fs = y
Config_ext3_fs_xattr = y
# Config_ext3_fs_posix_acl is not set

Everything is ready. Use make menuconfig to select myext2, as shown below:
# Cd/usr/src/Linux
# Make menuconfig

Select the option corresponding to my ext2:

Save the changes and run the "make" command to compile the connection to generate a new kernel file:
Compile all OK, but the following error occurs during connection:

...
LD fs/built-in.o
GEN. Version
Chk include/Linux/compile. h
UPD include/Linux/compile. h
CC init/version. o
LD init/built-in.o
LD. tmp_vmlinux1
FS/built-in.o: In function 'Grab _ Block': fs/myext2/balloc. C: 271: Undefined reference to 'myext2 _ test_bit'
: Fs/myext2/balloc. C: 285: Undefined reference to 'myext2 _ find_next_zero_bit'
: Fs/myext2/balloc. C: 302: Undefined reference to 'myext2 _ test_bit'
: Fs/myext2/balloc. C: 307: Undefined reference to 'myext2 _ find_next_zero_bit'
: Fs/myext2/balloc. C: 314: Undefined reference to 'myext2 _ set_bit_atomic'
FS/built-in.o: In function 'myext2 _ free_blocks ': fs/myext2/balloc. C: 239: Undefined reference to 'myext2 _ clear_bit_atomic'
FS/built-in.o: In function 'myext2 _ new_block ': fs/myext2/balloc. C: 490: Undefined reference to 'myext2 _ set_bit_atomic'
FS/built-in.o: In function 'myext2 _ free_inode ': fs/myext2/ialloc. C: 151: Undefined reference to 'myext2 _ clear_bit_atomic'
FS/built-in.o: In function 'myext2 _ new_inode ': fs/myext2/ialloc. C: 495: Undefined reference to 'myext2 _ find_next_zero_bit'
: Fs/myext2/ialloc. C: 510: Undefined reference to 'myext2 _ set_bit_atomic'
Make: *** [. tmp_vmlinux1] Error 1
[Root @ localhost Linux] #

As long as there is no problem with compilation, the connection error is easy to handle. From the error analysis shown, it is estimated that the definitions of these functions are missing. According to the reverse thinking method, you only need to search for functions such as ext2_test_bit and ext2_find_next_zero_bit in the Linux source code, and then copy them to myext2_test_bit and myext2_find_next_zero_bit. For example, if you search for ext2_test_bit, you can find these function groups in include/asm-i386/bitops. h.

--- Include/asm-i386/bitops. H. bak2007-08-15 08:58:19. 000000000-0400
++ Include/asm-i386/bitops. h2007-08-15 08:58:04. 000000000-0400
@-449,6 + 449,19 @@
Find_first_zero_bit (unsigned long *) ADDR, size)
# Define ext2_find_next_zero_bit (ADDR, size, Off )/
Find_next_zero_bit (unsigned long *) ADDR, size, off)
+ # Define myext2_set_bit (NR, ADDR )/
+ _ Test_and_set_bit (NR), (unsigned long *) ADDR)
+ # Define myext2_set_bit_atomic (lock, NR, ADDR )/
+ Test_and_set_bit (NR), (unsigned long *) ADDR)
+ # Define myext2_clear_bit (NR, ADDR )/
+ _ Test_and_clear_bit (NR), (unsigned long *) ADDR)
+ # Define myext2_clear_bit_atomic (lock, NR, ADDR )/
+ Test_and_clear_bit (NR), (unsigned long *) ADDR)
+ # Define myext2_test_bit (NR, ADDR) test_bit (NR), (unsigned long *) ADDR)
+ # Define myext2_find_first_zero_bit (ADDR, size )/
+ Find_first_zero_bit (unsigned long *) ADDR, size)
+ # Define myext2_find_next_zero_bit (ADDR, size, Off )/
+ Find_next_zero_bit (unsigned long *) ADDR, size, off)

/* Bitmap functions for the minix filesystem .*/
# Define minix_test_and_set_bit (NR, ADDR) _ test_and_set_bit (NR, (void *) ADDR)

After adding, save and exit. Go back to the Linux directory and make again.
Part 1: Cloning the ext2 file system has been completed.
The newly compiled kernel restarts the system.

Next we will test the added myext2 File System:

# Pwd
#/Root
# Dd If =/dev/Zero of = myfs BS = 1 m COUNT = 1
#/Sbin/mkfs. ext2 myfs
# Cat/proc/filesystems | grep ext
Ext2
Ext3
Myext2
# Mount-T myext2-o loop./myfs/mnt
# Mount
/Root/myfs on/mnt type myext2 (RW, loop =/dev/loop0)
# Umount/mnt
# Mount-T ext2-o loop./myfs/mnt
# Mount
/Root/myfs on/mnt type ext2 (RW, loop =/dev/loop0)
#

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.