-----------x86_64-----------------------------------------
参考 http://mgalgs.github.io/2015/05/16/how-to-build-a-custom-linux-kernel-for-qemu-2015-edition.html
下载busybox和linux内核
TOP=/var/www/mytest wget http://distro.ibiblio.org/tinycorelinux/6.x/armv6/release/src/busybox/busybox-1.23.2.tar.bz2 wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.0.3.tar.xz cd $TOP/busybox-1.23.2 #这步骤很重要,busybox不生成动态库 mkdir -pv ../obj/busybox-x86 make O=../obj/busybox-x86 defconfig make O=../obj/busybox-x86 menuconfig -> Busybox Settings -> Build Options [ ] Build BusyBox as a static binary (no shared libs) Go to that location, select it, save, and exit. $ cd ../obj/busybox-x86 $ make -j2 $ make install
把busybox编译好的文件生成 initramfs
$ mkdir -p $TOP/initramfs/x86-busybox $ cd $TOP/initramfs/x86-busybox $ mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}} $ cp -av $TOP/obj/busybox-x86/_install/* .
需要一个启动文件
vim init
------------- #!/bin/sh mount -t proc none /proc mount -t sysfs none /sys echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n" exec /bin/sh --------------------
chmod +x init
find . -print0 | cpio --null -ov --format=newc | gzip -9 > $TOP/obj/initramfs-busybox-x86.cpio.gz
★★★★ 编译内核★★★★★★★★
linux Kernel
$ cd $TOP/linux-4.0.3 $ make O=../obj/linux-x86-basic x86_64_defconfig make O=../obj/linux-x86-basic kvmconfig #支持kvm make O=../obj/linux-x86-basic -j2
$ cd $TOP
运行
qemu-system-x86_64 -kernel obj/linux-x86-basic/arch/x86_64/boot/bzImage -initrd obj/initramfs-busybox-x86.cpio.gz -nographic -append "console=ttyS0" -enable-kvm
------------------------------------------------------------
i386★★★★★★★★★★如果是在64为系统上编译32位,有点特殊
------------------------------------------------------------
这步骤很重要,busybox不生成动态库
cd $TOP/busybox-1.23.2 mkdir -pv ../obj/busybox-i386 make O=../obj/busybox-i386 defconfig make O=../obj/busybox-i386 menuconfig -> Busybox Settings -> Build Options [ ] Build BusyBox as a static binary (no shared libs) Go to that location, select it, save, and exit. $ cd ../obj/busybox-i386
参考
http://blog.csdn.net/xsckernel/article/details/38045783
想编译32为的环境
将/usr/bin/gcc和/usr/bin/ld都改名为*.bin,就是改为: /usr/bin/gcc.bin和/usr/bin/ld.bin
然后写两个脚本:
/usr/bin/gcc: --------------------- #!/bin/sh gcc.bin -m32 $@ ------------------------ /usr/bin/ld: ------------------ #!/bin/sh ld.bin -m elf_i386 $@ ---------------------
还需要
yum install glibc.i686 glibc-devel.i686 glibc-static.i686 glibc-utils.x86_64 -y yum install libgcc.i686 -y
测试是否切换到i386
--------test.c-------- #include <stdio.h> #include <stdlib.h> int main() { printf("sizeof long is %d\n", sizeof(long)); return 0; } ----------------- gcc test.c file a.out a.out: ELF 32-bit LSB executable,
继续以32位的方式编译busybox
$ make -j2
$ make install
生成initramfs:
$ mkdir -p $TOP/initramfs/i386-busybox $ cd $TOP/initramfs/i386-busybox $ mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}} $ cp -av $TOP/obj/busybox-i386/_install/* . vim init ------------- #!/bin/sh mount -t proc none /proc mount -t sysfs none /sys echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n" exec /bin/sh -------------------- chmod +x init find . -print0 | cpio --null -ov --format=newc | gzip -9 > $TOP/obj/initramfs-busybox-i386.cpio.gz
--------------------------
★★★★★★编译32位的内核比较简单,只需要加ARCH=i386
--------------------------
make O=../obj/linux-i386-basic i386_defconfig
make O=../obj/linux-i386-basic kvmconfig
make O=../obj/linux-i386-basic ARCH=i386 -j16
qemu-system-i386 -kernel obj/linux-i386-basic/arch/x86/boot/bzImage -initrd obj/initramfs-busybox-i386.cpio.gz -nographic -append "console=ttyS0" -enable-kvm
-----------------------------------------------------------------------------
系统切换回64位环境
[root@jslinux mytest]# cat gcc64.sh
#!/bin/sh
mv -i /usr/bin/gcc /usr/bin/gcc.sh
mv -i /usr/bin/ld /usr/bin/ld.sh
mv -i /usr/bin/gcc.bin /usr/bin/gcc
mv -i /usr/bin/ld.bin /usr/bin/ld
系统切换回32位环境
[root@jslinux mytest]# cat gcc32.sh
#!/bin/sh
mv -i /usr/bin/gcc /usr/bin/gcc.bin
mv -i /usr/bin/ld /usr/bin/ld.bin
mv -i /usr/bin/gcc.sh /usr/bin/gcc
mv -i /usr/bin/ld.sh /usr/bin/ld
------------------------------------------------------------------------