Linux 添加檔案系統

來源:互聯網
上載者:User

2.1 添加一個和ext2完全相同的檔案系統myext2
要添加一個與ext2完全相同的檔案系統myext2,首先是確定實現ext2檔案系統的核心源碼是由哪些檔案組成。Linux原始碼結構很清楚地告訴我們:fs/ext2目錄下的所有檔案是屬於ext2檔案系統的。再檢查一下這些檔案所包含的標頭檔,可以初步總結出來Linux原始碼中屬於ext2檔案系統的有:

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

接下來開始添加myext2檔案系統的原始碼到Linux原始碼。把ext2部分的原始碼複製到myext2去,即複製一份以上所列的ext2原始碼檔案給myext2用。按照Linux原始碼的組織圖,把myext2檔案系統的原始碼存放到fs/myext2下,標頭檔放到include/linux下。在Linux的shell下,執行如下操作(也許需要先轉成root使用者):

#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

這樣就完成了複製檔案系統工作的第一步——原始碼複製。對於複製檔案系統來說,這樣當然還遠遠不夠,因為檔案裡面的資料結構名、函數名、以及相關的一些宏等內容還沒有根據myext2改掉,連編譯都通不過。
下面開始複製檔案系統的第二步:修改上面添加的檔案的內容。為了簡單起見,做了一個最簡單的替換:將原來“EXT2”替換成“MYEXT2”;將原來的“ext2”替換成“myext2”。

對於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

把這個指令碼命名為substitute.sh,放在fs/myext2下面,加上可執行許可權,運行之後就可以把目前的目錄裡所有檔案裡面的“ext2”和“EXT2”都替換成對應的“myext2”和“MYEXT2”。

提示:在編譯核心的過程中會出現錯誤,還有一些代碼的需要修改,這些代碼在不同的檔案中。做實驗時要仔細和耐心。

原始碼的修改工作到此結束。接下來就是第三步工作——編譯原始碼。首先要把myext2加到編譯選項中去,以便在做make menuconfig的時候,可以將該選項加上去。做這項工作只需要修改三個檔案:
fs/Kconfig
fs/Makefile
arch/i386/defconfig
fs/Kconfig中拷貝一份對應的對EXT2檔案宏的定義和協助資訊,這樣在做make menuconfig的時候可以查看該選項的有關協助的內容。fs/Makefile的修改是告核心編譯系統,當myext2對應的宏被選擇上的時候,到fs/myext2目錄下去編譯myext2檔案系統。defconfig是改動預設的編譯選項,比如:

--- 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: the
+ 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
@@ -1073,6 +1073,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

一切都準備就緒了,使用make menuconfig選擇上myext2,如下:
# cd /usr/src/linux
# make menuconfig

選中MY EXT2對應的選項:

儲存修改,然後使用“make”命令編譯串連產生新核心檔案:
編譯一切OK,只是在串連的時候出現了以下錯誤:

...
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]#

只要編譯不出現問題,串連錯誤還是比較好辦的。從顯示出來的錯誤分析,估計是缺了這些函數的定義。根據逆向思維方法,只要在Linux原始碼中搜尋ext2_test_bit,ext2_find_next_zero_bit等函數,找到它們之後,同樣複製一份,改成myext2_test_bit,myext2_find_next_zero_bit等函數名就可以了。如搜尋ext2_test_bit,在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)

添加完了以後,儲存,退出。回到linux目錄下,再次做make。
第一部分工作——複製ext2檔案系統已經完成了。
新編譯出來的核心重新啟動系統。

下面我們來對添加的myext2檔案系統進行一下測試:

#pwd
#/root
#dd if=/dev/zero of=myfs bs=1M 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)
#

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.