macOS Mojave(10.14.3)系统QEMU虚拟机运行Clockwork OS

# 安装编译工具macOS Mojave(10.14.3)
$ brew install arm-linux-gnueabihf-binutils

# bison on macOS is too old
$ brew install bison
$ export PATH="/usr/local/opt/bison/bin:$PATH" 

# 安装 crosstool-ng 构建GCC编译环境
$ brew install crosstool-ng

$ export CT_NG_VER=$(brew list --versions crosstool-ng | tr ' ' '\n' | tail -1)

$ export CT_NG_VER_SHORT=${CT_NG_VER%_*}

# 安装的 crosstool-ng 的脚本文件缺少执行权限,导致无法执行,我们需要手工增加执行权限
$ chmod +x "$(brew --cellar crosstool-ng)/${CT_NG_VER}/lib/crosstool-ng-${CT_NG_VER_SHORT}/scripts/crosstool-NG.sh"

# 默认情况下,macOS的文件系统不区分大小写,我们需要手工创建一个区分大小写的分区
$ hdiutil create -volname "ClockworkOS" -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 30g ClockworkOS.dmg

$ hdiutil attach ClockworkOS.dmg.sparseimage -mountpoint /Volumes/ClockworkOS

$ cd /Volumes/ClockworkOS

$ mkdir arm-cortexa9_neon-linux

$ cd arm-cortexa9_neon-linux

$ ct-ng list-samples

# 变更x-tools存储目录
$ export HOME=/Volumes/ClockworkOS

$ ct-ng arm-cortexa9_neon-linux-gnueabihf

# 修复BUG Build failed in step 'Installing m4 for build' 
$ brew uninstall --ignore-dependencies binutils
$ brew install binutils

# 安装依赖工具
$ brew install automake

$ brew uninstall --ignore-dependencies gawk
$ brew install gawk

# 目前编译gettext-0.19.8.1的时候写死依赖automake-1.15,但是最新的已经是automake-1.16,我们通过手工编译安装automake-1.15规避这个问题
$ wget http://ftp.gnu.org/gnu/automake/automake-1.15.tar.gz

# 也可从本站下载 wget http://www.mobibrw.com/wp-content/uploads/2019/03/automake-1.15.tar.gz

$ tar xvf automake-1.15.tar.gz

$ cd automake-1.15

$ bash configure

$ make && make install

$ cd ..

# 修改文件打开数量限制,修正错误 “extra-module.mk:11: *** Too many open files.”
$ ulimit -n 2048

# 'scm_new_port_table_entry' was not declared in this scope
$ sed -i "" "s/CT_GDB_CROSS_EXTRA_CONFIG_ARRAY=.*/CT_GDB_CROSS_EXTRA_CONFIG_ARRAY=\"--with-guile=no\"/g" .config

$ export PATH="/usr/local/bin:$PATH"

$ ct-ng build -j8

编译 u-boot

$ cd /Volumes/ClockworkOS

# 下载u-boot代码
$ git clone https://github.com/qemu/u-boot.git

$ cd u-boot

$ git checkout v2019.01 -b v2019.01

$ export PATH="/Volumes/ClockworkOS/x-tools/arm-cortexa9_neon-linux-gnueabihf/bin:$PATH"

$ export CROSS_COMPILE=arm-cortexa9_neon-linux-gnueabihf-

$ make clean

# R16又名A33 ,R16-J 代表包含Jazelle DBX 
$ make vexpress_ca9x4_defconfig

# fix Undefined symbols for architecture x86_64: "_PyArg_ParseTuple"
$ export HOSTLDFLAGS="-lpython -dynamclib"

$ brew install gnu-sed

# fix ./tools/../lib/bch.c:66:10: fatal error: 'endian.h' file not found
$ gsed -i "s/#include <sys\/endian.h>/#include <sys\/endian.h>\n#elif defined(__APPLE__)\n#include <machine\/endian.h>\n#include <libkern\/OSByteOrder.h>/g" lib/bch.c

$ gsed -i "s/#define cpu_to_be32 htobe32/#if defined(__APPLE__)\n#define cpu_to_be32 OSSwapHostToBigInt32\n#else\n#define cpu_to_be32 htobe32\n#endif/g" lib/bch.c

$ gsed -i "s/#if \!defined(__DragonFly__) \&\& \!defined(__FreeBSD__)/#if \!defined(__DragonFly__) \&\& \!defined(__FreeBSD__) \&\& \!defined(__APPLE__)/g" lib/bch.c

# 无视最后的失败提示,只要u-boot这个文件生成即可
$ make ARCH=arm -j8

编译 Linux 内核

$ cd /Volumes/ClockworkOS

$ brew install aria2

$ aria2c -c https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.14.2.tar.xz

# 也可本站下载 wget http://www.mobibrw.com/wp-content/uploads/2019/03/linux-4.14.2.tar.xz

$ tar xvf linux-4.14.2.tar.xz

$ cd linux-4.14.2

$ export PATH="/Volumes/ClockworkOS/x-tools/arm-cortexa9_neon-linux-gnueabihf/bin:$PATH"

# for mkimage
$ export PATH="/Volumes/ClockworkOS/u-boot/tools:$PATH"
# 或者 brew install u-boot-tools

# elf.h
$ brew install libelf

$ echo "
#include <libelf/libelf.h>
#define R_386_NONE 0
#define R_386_32 1
#define R_386_PC32 2
#define R_ARM_NONE 0
#define R_ARM_PC24 1
#define R_ARM_ABS32 2
#define R_MIPS_NONE 0
#define R_MIPS_16 1
#define R_MIPS_32 2
#define R_MIPS_REL32 3
#define R_MIPS_26 4
#define R_MIPS_HI16 5
#define R_MIPS_LO16 6
#define EF_ARM_EABIMASK 0xFF000000
#define EF_ARM_EABI_VERSION(flags) ((flags) & EF_ARM_EABIMASK)" > /usr/local/include/elf.h

# xargs: illegal option -- r
$ brew install findutils

$ export PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH"

# stat: illegal option -- c
$ ln -s /usr/local/bin/gstat /usr/local/bin/stat

$ export PATH="/usr/local/bin:$PATH"

$ export CROSS_COMPILE=arm-cortexa9_neon-linux-gnueabihf-

$ export ARCH=arm

$ make vexpress_defconfig

$ make -j8

$ mkimage -A arm -O linux -T kernel -C none -a 0x40008000 -e 0x40008000 -n "Linux kernel" -d arch/arm/boot/zImage uImage
$ cd /Volumes/ClockworkOS

$ brew install aria2

# 官方给出的这个地址下不到,只能用镜像地址 http://106.185.33.196/clockworkos_v0.3.img.bz2
$ aria2c -c http://clockworkpi.k15.net/clockworkos_v0.3.img.bz2

$ rm -rf clockworkos_v0.3.img

$ bzip2 -d -k -vvvv clockworkos_v0.3.img.bz2

# 替换镜像中的内核文件
$ hdiutil attach clockworkos_v0.3.img -mountpoint /Volumes/clockworkos_v0.3

$ echo y | cp -i /Volumes/ClockworkOS/linux-4.14.2/uImage /Volumes/clockworkos_v0.3/uImage

$ hdiutil detach /Volumes/clockworkos_v0.3

$ brew install qemu

$ qemu-img convert -f raw -O qcow2 clockworkos_v0.3.img clockworkos_v0.3.qcow2

手工编译 qemu

$ cd /Volumes/ClockworkOS

$ git clone https://github.com/qemu/qemu.git

$ cd qemu

# 从 qemu v2.1.0-rc1 开始,内存需要被映射到0x60000000开始的地址,更低的地址被映射为只读闪存,我们需要取消这种映射行为,否则执行的时候会报告错误
$ sed -i "" "s/\[VE_NORFLASHALIAS\] = 0/\[VE_NORFLASHALIAS\] = -1/g" hw/arm/vexpress.c

$ bash configure

$ make -j8

$ cd ..

# list supported machine `qemu-system-arm -machine help`

$ /Volumes/ClockworkOS/qemu/arm-softmmu/qemu-system-arm -M vexpress-a9 -m 1024M -kernel /Volumes/ClockworkOS/u-boot/u-boot -serial mon:stdio -nographic -sd clockworkos_v0.3.qcow2 -net nic,model=lan9118 -net user

可惜到这一步了,还是没办法成功运行系统。

参考链接


发布者

《macOS Mojave(10.14.3)系统QEMU虚拟机运行Clockwork OS》上有4条评论

  1. 你好,我最近也在弄macos跨平台编译linux内核,发现资料较少,且容易报错,方便加个qq吗,方便以后一起交流;我qq:1378400723

回复 默默 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注