Total Pageviews

Monday 11 July 2016

在树莓派2上,安装ubuntu 14.04

不太喜欢原配的raspbian系统,占用空间太大(4GB+),太多杂七杂八的应用。如果在树莓派上面使用ubuntu社区版本,就会令我觉得更轻量(无X-desktop),更通用(与桌面版命令一致)。因此记下自己折腾ubuntu的过程。

与原系统性能差别

结论:由于ubuntu内核默认开启了LSM系统防护(AppArmor)模块,导致性能略逊于raspbian(它默认没有该模块),如果能关掉LSM那ubuntu性能明显强于后者。
参考ubuntu的AppArmor说明文档

安装ubuntu

内存卡至少2GB,参考ubuntu 14.04 社区版 博客 安装指引,下载该文件解压。
sudo apt-get install bmap-tools
sudo bmaptool copy --bmap 2015-04-06-ubuntu-trusty.bmap 2015-04-06-ubuntu-trusty.img /dev/sdX
不建议dd或者ddrescure恢复。会丢启动信息导致ubuntu halt

开启ssh

进入树莓派的ubuntu使用网线连接路由,安装ssh服务器
sudo apt-get update
sudo apt-get install openssh-server 
小设置
sudo vi /etc/ssh/sshd_config
# 监听
ListenAddress 0.0.0.0
# Authentication:
# 允许root登陆
PermitRootLogin yes
# 关闭严格模式
#StrictModes yes
# 重定义失败次数
MaxAuthTries 6
#MaxSessions 10
重启sshd
sudo /etc/init.d/ssh restart

开启无线连接

我愿意开启root用户。先改root密码
sudo passwd root
切换root
su
安装wpa支持无线加密
apt-get install wpasupplicant
插入无线网卡(RTL 8188CU/8192CU),启动接口
ifup wlan0
wpa_passphrase <SSID> <WIFI-PASSWORD> > /etc/wpa_supplicant.conf
开启连接
wpa_supplicant -B -iwlan0 -c/etc/wpa_supplicant.conf -Dwext
# 可能出现三行错误,忽略它
dhclient wlan0
测试一下是否连接成功,看wlan0的ip:
ifconfig
然后写入到/etc/rc.local作为启动脚本,开机即可使用无线网
vi /etc/rc.local
# 增加到exit 0前
wpa_supplicant -B -iwlan0 -c/etc/wpa_supplicant.conf -Dwext
dhclient wlan0
exit 0

交叉编译

工具链(树莓派2:BCM2836 ARMv7)
sudo apt-get install gcc-arm-linux-gnueabihf
arm-linux-gnueabihf-gcc -march=armv7 -o main main.c 
然后scp到树莓派看看能否运行。
scp ./main root@192.168.1.101:/tmp
使用GPIO库(C语言)(默认是动态库,可以选择编译静态库)
git clone git://git.drogon.net/wiringPi
cd wiringPi && sudo ./build
使用GPIO库(Python)
http://sourceforge.net/p/raspberry-gpio-python/wiki/Home/

其他

安装中文语言

sudo apt-get -y install language-pack-zh-hans language-pack-zh-hans-base
-------------

Ubuntu 14.04 (Trusty Tahr) on the Raspberry Pi 2

My Raspberry Pi 2 arrived yesterday, and I started playing with it today. Unlike the original Raspberry Pi which had an ARMv6 CPU, the Raspberry Pi 2 uses a Broadcom BCM2836 (ARMv7) CPU, which allows for binary compatibility with many distributions' armhf ports. However, it's still early early in the game, and since ARM systems have little standardization, there isn't much available yet. Raspbian works, but its userland still uses ARMv6-optimized binaries. Ubuntu has an early beta of Ubuntu Snappy, but Snappy is a much different environment than "regular" Ubuntu.
I found this post by Sjoerd Simons detailing getting Debian testing (jessie) on the Pi 2, and he did a good job of putting together the needed software, which I used to get a clean working install of Ubuntu trusty on my Pi 2. This is meant as a rough guide, mostly from memory -- I'll let better people eventually take care of producing a user-friendly system image. This procedure should work for trusty, utopic, and vivid, and might work for earlier distributions. 1) Install and boot Raspbian on one MicroSD card, and get the system access to a second MicroSD card, e.g. via a USB adapter.
2) Partition and format the card. #1 must be a vfat partition (64MB should be fine), and for the rest of this post I'm going to assume #2 is a swap partition and #3 is the rest of the card as ext4 for the root partition. Mount the root partition at e.g. /mnt/ubuntu and the vfat partition at e.g. /mnt/ubuntu-boot
3) Bootstrap a trusty system.
# apt-get install debootstrap
# ln -sf gutsy /usr/share/debootstrap/scripts/trusty
# debootstrap trusty /mnt/ubuntu
4) Chroot into the bootstrapped system.
# mount -t proc none /mnt/ubuntu/proc
# mount -t sysfs none /mnt/ubuntu/sys
# mount --bind /dev /mnt/ubuntu/dev
# chroot /mnt/ubuntu
5) Configure the base system. At the very least you should add a user, set passwords, and edit /etc/fstab as so:
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p3  /               ext4    defaults,noatime  0       1
/dev/mmcblk0p2  none            swap    sw                0       0
6) Install all the necessary packages in Sjoerd's repository. You can either add the apt repository, or just do as I did and download/dpkg install them manually. However, there's a problem. These are built for jessie, and the linux-image package requires initramfs-tools 0.110 or greater, which is not yet available in Ubuntu. But since the kernel we're using doesn't actually need an initramfs, I've put together an "initramfs-tools 0.110~fake1" package which fakes it. Simply install that package ahead of time.
7) Outside the chroot, copy /boot/config.txt from the Raspbian installation to /mnt/ubuntu/boot/firmware/config.txt. Then create /mnt/ubuntu/boot/firmware/cmdline.txt with the following:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p3 rootwait
8) Copy the firmware files to the actual boot device:
# cp -a /mnt/ubuntu/boot/firmware/* /mnt/ubuntu-boot/
9) Umount everything, shut down the system, swap the new card into place, and boot. Once you are successfully booted into the Ubuntu system, I'd recommend installing the ubuntu-standard metapackage to get everything not pulled in by the base debootstrap.
ryan@raspberrypi:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:        14.04
Codename:       trusty

ryan@raspberrypi:~$ uname -a
Linux raspberrypi 3.18.0-trunk-rpi2 #1 SMP PREEMPT Debian 3.18.5-1~exp1.co1 (2015-02-02) armv7l armv7l armv7l GNU/Linux

ryan@raspberrypi:~$ cat /proc/cpuinfo 
processor       : [0,1,2,3]
model name      : ARMv7 Processor rev 5 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm 
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xc07
CPU revision    : 5

Hardware        : BCM2709
Revision        : 0000
Serial          : 0000000000000000

ryan@raspberrypi:~$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.8/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --enable-multilib --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --disable-werror --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.8.2 (Ubuntu/Linaro 4.8.2-19ubuntu1)
from http://www.finnie.org/2015/02/14/ubuntu-14-04-trusty-tahr-on-the-raspberry-pi-2/
---------------

Raspberry Pi 2 update - (unofficial) Ubuntu 14.04 image available

If you downloaded an older image than the current one, you shouldn't need to reinstall, but be sure to review the changelog in the link above.
Note that this blog post originally contained a bunch more information, which has been moved to adedicated page on wiki.ubuntu.com.
I've closed comments on this blog post. If you are looking for help, please see this post on the raspberrypi.org forums. If you post there, you'll be reaching a wider audience of people (including myself) who can help you. Thanks for all of your comments!

After my last post, I went and ported Sjoerd's Raspberry Pi 2 Debian kernel patchset to Ubuntu's kernel package base (specifically 3.18.0-14.15). The result is an RPi2-compatible 3.18.7-based kernel which not only installs in Ubuntu, but has all the Ubuntu bells and whistles. I also re-ported flash-kernel based on Ubuntu's package, recompiled raspberrypi-firmware-nokernel, created a linux-meta-rpi2 package, and put it all in a PPA.
With that all done, I decided to go ahead and produce a base Ubuntu trusty image. It's 1.75GB uncompressed so you can put it on a 2GB or larger MicroSD card, and includes a full ubuntu-standard setup. Also included in the zip is a .bmap file; if you are writing the image in Linux you can use bmap-tools package to write only the non-zero bytes, saving some time. Otherwise it's the same procedure as other Raspberry Pi images.
(PS: If this image becomes popular, I should point out ahead of time: This is an unofficial image and is in no way endorsed by my employer, who happens to be the company who produces Ubuntu. This is a purely personal undertaking.)
from http://www.finnie.org/2015/02/16/raspberry-pi-2-update-ubuntu-14-04-image-available/