一、Scratchbox简介
Scratchbox is a configuration and compilation environment for building Linux software and entire Linux distributions. The basic idea of Scratchbox is to offer developers an environment that works and looks like the target environment before the target environment is available.
二、安装Scratchbox
1.添加软件源
lingd@Ubuntu:~$ sudo vi /etc/apt/sources.list
/etc/apt/sources.list最后面增加接下来内容:
deb http://scratchbox.org/debian/ stable main
2.更新软件源
lingd@ubuntu:~$ sudo apt-get update
3.安装软件包
lingd@ubuntu:~$ sudo apt-get install scratchbox-core scratchbox-libs scratchbox-devkit-qemu
lingd@ubuntu:~$ sudo apt-get install scratchbox-devkit-debian scratchbox-devkit-perl scratchbox-toolchain-cs2007q3-glibc2.5-arm7
注:
以下是必须安装的
scratchbox-core: scratchbox环境, 常用工具及本地编译器.
scratchbox-libs: scratchbox-core, devkits和toolchains所需的库.
scratchbox-devkit-qemu: scratchbox的ARM指令集仿真器. scratchbox中提供了qemu和sbrsh两种仿真手段. 要使用qemu仿真, 需安装scratchbox-devkit-cputransp包,不过现在scratchbox-devkit-cputransp包已被scratchbox-devkit-qemu取代了
以下是可选的
scratchbox-devkit-debian: 用于Debian包开发的环境和工具.
scratchbox-devkit-perl: 额外的perl模块.
scratchbox-toolchain-cs2007q3-glibc2.5-arm7: 交叉编译工具包,也可以选择其他交叉编译工具包
scratchbox-devkit-doctools: 文档生成工具. 这个包很大,我没有安装
三、禁止VDSO
VDSOs (Virtual Dynamically-linked Shared Objects 虚拟动态共享对象) are a way to export kernel space routines to user space applications, using standard mechanisms for linking and loading (i.e. standard ELF format).
禁止Ubuntu 10.04的VDSO功能
lingd@ubuntu:~$ sudo vi /etc/sysctl.conf
编辑/etc/sysctl.conf, 最后面增加接下来两行:
vm.vdso_enabled = 0
vm.mmap_min_addr = 4096
使结果生效,运行
lingd@ubuntu:~$ sudo sysctl -p
vm.vdso_enabled = 0
vm.mmap_min_addr = 4096
为了长期生效,编辑/etc/rc.local,在最后一行(exit 0)前面, 增加:sysctl -p
四、添加scratchbox用户
1.增加用户到scratchbox中
lingd@ubuntu:~$ sudo /scratchbox/sbin/sbox_adduser lingd
[sudo] password for lingd:
Add user lingd to group 'sbox'? [yes/no] (yes): yes
正在添加用户"lingd"到"sbox"组...
Adding user lingd to group sbox
完成。
Scratchbox user account for user lingd added
2.查看用户lingd是否已在sbox组中
lingd@ubuntu:~$ groups
lingd adm dialout cdrom plugdev lpadmin admin sambashare sbox
一旦没有看到sbox组,那就退出后重新登录;否则,/scratchbox/login会出现
lingd@ubuntu:~$ /scratchbox/login
bash: /scratchbox/login: 权限不够
五、配置scratchbox
1.进入scratchbox环境
lingd@ubuntu:~$ /scratchbox/login 或 scratchbox
You dont have active target in scratchbox chroot.
Please create one by running "sb-menu" before continuing
Welcome to Scratchbox, the cross-compilation toolkit!
Use 'sb-menu' to change your compilation target.
See /scratchbox/doc/ for documentation.
[sbox-NO-TARGET: ~] >
2.配置编译目标
[sbox-NO-TARGET: ~] > sb-menu
出现交互式配置菜单,选择建立一个target
Setup Setup a target
选择创建一个新target
<NEW> Create a new target --> 输入target名字: arm
选择compiler(编译器)
cs2007q3-glibc2.5-arm7 cross
选择devkits(开发辅助工具),一定要选择qemu,其他根据自身需要
qemu
选择好devkits后,选择
DONE Done selecting
选择模拟器,arm模拟器为qemu-arm
qemu-arm-sb Emulation
Do you wish extract a rootstrap on the target ? --> no
Do you wish install files to the target ? --> yes
直接按照默认选择(或也可以通过按空格来选择自己想要安装的文件)--> OK
Do you wish to select the target --> yes
此时sb-menu会退出。
参考文献4有图解scratchbox的配置过程
六、编译测试
1.HelloWorld
在/scratchbox/packages有一些例子代码,HelloWorld就是其中一个。因为在/scratchbox/packages没有足够权限,所以只能拷贝hello.c到用户主目录再编译
[sbox-arm: ~] > cp /scratchbox/packages/hello.c hello.c
[sbox-arm: ~] > gcc hello.c -o hello
[sbox-arm: ~] > ls
hello hello.c
测试文件类型:
[sbox-arm: ~] > file hello
hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), for GNU/Linux 2.6.14, dynamically linked (uses shared libs), not stripped
可以看到可执行文件hello的运行平台为 ARM
2.PC上运行HelloWorld(在scratchbox环境下)
[sbox-arm: ~] > ./hello
hello world
上面显示的结果,实际上,是在qemu模拟器上运行该程序的结果
在 Selected devkits(辅助开发工具)中要选择qemu配置模拟器时,一定要选为qemu-arm-XX(我选的是qemu-arm-sb);否则运行./hello会出现:
[sbox-arm: ~] > ./hello
Error -8 while loading ./hello
3.arm开发板上运行HelloWorld
hello 文件下载到开发板当前目录,运行:
chmod a+x hello
./hello
可以在开发板上看到程序的运行结果。
七、关于登陆scratchbox出现Scratchbox is not properly set up的解决方法
官网的解释:
If Scratchbox was installed from Debian or RPM packages, the /etc/init.d/scratchbox-core init script was installed and Scratchbox should start automatically when the system is rebooted. However, if you installed Scratchbox from tarballs then rebooting your machine will clear away all the mounts and binfmt_misc registrations that Scratchbox requires to work. To get your Scratchbox working again after reboot, you have to run the following command as root:
# /scratchbox/sbin/sbox_ctl start
简单意思就是:以tar方式安装scratchbox,重启pc后,scratchbox正常工作所需要的mounts和binfmt_misc信息都会被清除。所以每次开机第一次运行scratchbox或/scratchbox/login前,必须先运行“/scratchbox/sbin/sbox_ctl start”命令
lingd@ubuntu:~$ scratchbox
ERROR: Scratchbox is not properly set up!
lingd@ubuntu:~$ sudo /scratchbox/sbin/sbox_ctl start
[sudo] password for lingd:
Starting Scratchbox: binfmt_misc, mount.
lingd@ubuntu:~$ scratchbox
Welcome to Scratchbox, the cross-compilation toolkit!
Use 'sb-menu' to change your compilation target.
See /scratchbox/doc/ for documentation.
[sbox-arm: ~] >
不过,我是通过deb包安装的,/scratchbox/sbin/sbox_ctl start解决不了我的问题。我估计是虚拟机Vmware不正常关闭导致的。重新安装一下scratchbox:
lingd@ubuntu:~$ sudo apt-get --no-download --reinstall install scratchbox-core scratchbox-libs scratchbox-devkit-qemu
scratchbox又恢复正常了!