為linux建立最小的根檔案系統

來源:互聯網
上載者:User

在編譯核心時候,可以指定一個檔案夾作為核心啟動時候的根檔案系統,linux中管這個檔案系統叫做initramfs。

具體做法如下(以i386為例)

1.下載核心檔案

  wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.26.tar.bz2

2.解壓核心

   bzip2 -d linux-2.6.26.tar.bz2  產生一個linux-2.6.26.tar檔案,然後

   tar xvf linux-2.6.26.tar 

   解壓後,將有個linux-2.6.26檔案夾存在

3.準備一個iniramfs檔案系統的檔案夾

  在linux-2.6.26檔案夾下建立一個檔案夾 myinitramfs

   寫一個測試用的hello world,起名為hello.c,如下:

  #include <stdio.h>

  #include <unistd.h>

  int main(int argc,char *argv[])

  {

     int i = 0;

     while (1) {

      printf("hello world (%d)\n",i);

     }

    return 0;

   }

  編譯  gcc -static -o init hello.c

  把init拷貝到myinitramfs檔案夾下。

  cp init myinitramfs/

  由於需要顯示文字,還需要在檔案夾下準備console裝置檔案。

  mkdir myinitramfs/dev

  cp -a /dev/console myinitramfs/

4.編譯核心

   在linux-2.6.26檔案下下,執行make help。

   將看到很多協助資訊,其中有一項是 i386_defconfig

   執行 make i386_defconfig,將產生一個.config檔案。

   為了把之前準備好的檔案夾添加到核心設定檔中,還需要重新設定下config檔案

   make config

    在 General Setup --->

    Initial RAM filesystem and RAM disk (initramfs/initrd) support (BLK_DEV_INITRD) [Y/n/?]

     Initramfs source file(s) (INITRAMFS_SOURCE) [myinitramfs]

   處,輸入準備好的檔案夾.

    配置好後,在.config檔案中會有如下一條定義

   CONFIG_INITRAMFS_SOURCE="myinitramfs"

   儲存.config

   make 編譯核心

5.用qemu測試核心和initramfs

   qemu -kernel  linux-2.6.26/arch/i386/boot/bzImage  -initrd linux-2.6.26/usr/initramfs_data.cpio.gz  /dev/zero

   initramfs_data.cpio.gz 這個檔案是核心自動產生的,具體名字可能不同的系統或者核心有差異,但是尾碼應該是.cpio.gz

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.