搭建Embedded Linux kernel學習的環境,在PC(Linux) 的QEMU虛擬機上運行Linux系統,以便之後的進一步調試。 .
本文闡述的內容:
在Linux主機上運行QEMU虛擬機,模擬ARM versatile平臺,並在該平臺上啓動Linux系統的步驟
本文大綱:
1. 安裝Toolchain
2. 安裝QEMU
3. Build Linux kernel
4. 準備root filesystem
5. 啓動QEMU
1. 安裝Toolchain
1). 打開以下頁面:
http://www.mentor.com/embedded-software/sourcery-tools/sourcery-codebench/editions/lite-edition/
2). 在Supported processors中選擇ARM processors->Download the GNU/Linux Release
3). 填寫Name, Email, Country的信息,click 'Get Lite'. 下載地址會發到Email郵箱中
4). 在Email中click下載地址,打開下載頁面後click "Download Sourcery CodeBench Lite 2011.09-70"
5). 在列出的Packages中選擇Recommended Packages中IA32 GNU/LINUX Installer, 此時將下載arm-2011.09-70-arm-none-linux-gnueabi.bin,該文件可直接運行圖形界面安裝。(也可選擇Advanced Packages中的TAR包自行安裝,但運行圖形界面安裝非常方便,不需要考慮太多設置)
6). 在shell中運行
$./arm-2011.09-70-arm-none-linux-gnueabi.bin
開始圖形安裝界面,按提示安裝。
7). 安裝完畢後,在shell中確認arm-none-linux-gnueabi-的命令是否已有效。
(如果在安裝中選擇將CodeSourcery安裝到user的home目錄下的話,path將被自動添加到user的bash profile中,可重新運行bash profile來enable codesourcery的path. [centos: source ~/.bash_profile, ubuntu: source ~/.profile]
2. 安裝QEMU
1). 打開http://wiki.qemu.org/Download,下載qemu-1.0.1.tar.gz
2). 解壓縮tar包,編譯qemu-system-arm:
$tar zxvf qemu-1.0.1.tar.gz
$cd qemu-1.0.1
$./configure --target-list=arm-softmmu --prefix=/usr/local/ (必須指定target-list,否則會編譯所有arch類型的binary)
$make;make install
3). 在shell中確認qemu-system-arm命令已有效
3. Build Linux kernel
1). 從kernel.org下載kernel源代碼
方法1:直接得到tar包:
$wget www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.33.7.tar.gz
$tar zxvf linux-2.6.33.7.tar.gz
方法2:用git:
$git clone http://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
$git checkout -B v2.6.33.7 v2.6.33.7 #<---將創建v2.6.33.7的Branch
2). 配置,編譯kernel
進入kernel的目錄
$make mrproper #清除所有配置和編譯產生的文件
$make ARCH=arm versatile_defconfig #使用versatile的default configuration
$make ARCH=arm menuconfig #做微小改動
在Kernel Feature中選中 "Use the ARM EABI to compile the kernel"選項。保存退出。
$make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- #開始編譯
編譯完成後,在arch/arm/boot/下會生成zImage
4. 準備root filesystem
(部分內容來自opencsl.openfoundry.org)
1). 下載busybox
$wget http://www.busybox.net/downloads/
$bunzip2
busybox-1.19.4.tar.bz2
2).配置編譯busybox
進入busybox-1.19.4
$make mrproper #清除所有配置和編譯產生的文件
$make ARCH=arm defconfig #使用default configuration
$make ARCH=arm menuconfig #做微小改動
選中Busybox Settings->Build Options->Build BusyBox as a static binary (no shared libs),保存退出。
$make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
#開始編譯
$make ARCH=arm install #編譯生成的目標可執行文件等將被安裝在_install/目錄下。
3). 創建root file system
進入上述busybox make install後的_install/目錄
創建root file system必要的目錄:
$mkdir etc etc/init.d dev root tmp
在dev/下創建設備開機必須的設備node:
$cd dev
$sudo mknod console c 5 1
$sudo mknod null c 1 3
$sudo mknod ttyAMA0 c 204 64
指定開機要運行的init程序,busybox已包含此功能,只要創建一個指向busybox的init軟link即可:
¥ln -s bin/busybox init
創建開機要執行的script:
$vi etc/inittab,內容以下:
*******************************************************
::sysinit:/etc/init.d/rcS
::respawn:/sbin/getty -L ttyAMA0 115200 xterm
*******************************************************
$vi etc/init.d/rcS,內容以下:
*******************************************************
#! /bin/sh
mkdir -p /proc
mount -t proc proc /proc
mkdir -p /sys
mount -t sysfs sysfs /sys
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
hostname LINUX-EXPLORER
*******************************************************
創建登錄帳號的文件:
$vi etc/passwd,內容如下:
*******************************************************
root::0:0:root:/root:/bin/sh
*******************************************************
$vi etc/group,內容如下:
*******************************************************
root:x:0:
*******************************************************
到此一個簡單的root file system就在_install/下創建好了。因爲每次make install busybox時都會更新_install目錄下的文件,爲了便於管理,我們將製作好的root file system複製到另外的地方。
$mkdir /path/of/rootfs (e.g. ~/rootfs/)
$cp -a /.../busybox-1.19.4/_install/* /path/of/rootfs
4). 使用nfs訪問root file system
因爲系統啓動時將通過nfs訪問文件系統,所以必須使剛剛創建的root file system可以被nfs mount.
安裝nfs server:
每個Linux系統略有不同,這裏不詳述。
將/path/of/rootfs添加到/etc/exportfs中:
$sudo vi /etc/exportfs
添加一行:
/path/of/rootfs *(rw,sync,no_root_squash,insecure)
保存退出。
更新nfs export的目錄:
$sudo exportfs -r
確認剛纔export的目錄能夠被nfs mount:
$sudo mount localhost:/path/of/rootfs /mnt
5. 啓動QEMU
以上在qemu中啓動Linux的準備工作都做好了,下面試着啓動:
在kernel目錄下啓動:
$qemu-system-arm -nographic -cpu arm926 -M versatilepb -kernel arch/arm/boot/zImage -append "console=ttyAMA0 rw ip=on root=/dev/nfs nfsroot=localhost:/path/to/rootfs"
-append後面""中是kernel的啓動參數,詳細解釋請看kernel src目錄下Documentation/kernel-parameters.txt