Thursday, March 13, 2014

Build Raspibian Linux

I'm gonna share how to create debian system on Raspberry PI rev B.
My computer is running debian 7 (wheezy), so this tutor will only work with the same type of linux system. Note that Raspberry Pi had arm v6 processor, the debian armhf itself was build for arm v7, so the armhf distribution of debian will not work on Raspberry Pi, you may use armel instead, regardless performance since soft floating point is used. At last the Raspibian were debian armhf build for arm v6 specially for Raspberry Pi.

1. Cross-toolchains

Download the toolchains from
extract somewhere in your home directory, mostly people put the toolchains on /opt directory.
Export the toolchains path to your working environment like this:
export PATH:$PATH:/arm-bcm2708 /gcc-linaro-arm-linux-gnueabihf-raspbian / bin

test your toolchains by typing: 
arm-linux-gnueabihf-gcc --version

2. Kernel Source
install git
> apt-get install git
get kernel source
> git init
> git fetch git:// rpi-3.6.y:refs/remotes/origin/rpi-3.6.y
> git checkout rpi-3.6.y
You got kernel source,the kernel you're going to create is two, one is the kernel.img as normal kernel and kernel_emergency.img for somekind of failsafe rescue system.

2.1 Kernel Emergency
To create kernel_emergency, you should download the target_fs of first from:
extract with name target_fs same level as linux-rpi-3.6.y, the kernel source will get those filesystem.
> make ARCH=armhf CROSS_COMPILE=arm-linux-gnueabihf bcmrpi_emergency_defconfig
> make ARCH=armhf CROSS_COMPILE=arm-linux-gnueabihf all
> cp arch/arm/boot/zImage kernel_emergency.img

2.2 Kernel
 > make ARCH=armhf CROSS_COMPILE=arm-linux-gnueabihf bcmrpi_defconfig
> make ARCH=armhf CROSS_COMPILE=arm-linux-gnueabihf all
> cp arch/arm/boot/zImage kernel.img

3. Raspbian File system
Use the debootstrap, if you don't have it just install it:
> sudo apt-get install debootstrap
> sudo debootstrap --arch=armhf --foreign --no-check-gpg --include=ca-certificates wheezy rootfs/

To finish the debootstrap you should be able to chroot to arm filesystem, user the qemu-user-static package
> sudo apt-get install qemu-user-static
copy the qemu-statuc-arm
> cp /usr/bin/qemu-static-arm rootfs/usr/bin/.
chroot to rootfs and execute following command
> sudo chroot rootfs

> mount proc /proc -t proc
> export LC_ALL=C LANGUAGE=C LANG=Cnow finish up the debootstrap
> ./debootstrap/debootstrap --second-stage

after finished installing filesystem package, add your username and password to enable login.
> adduser raspibian
> password raspibian

add following repository ro /etc/apt/sources.list.
deb wheezy main contrib non-free firmware rpi
deb-src wheezy main contrib non-free firmware rpi

Install the gpg key
wget -O - | apt-key add -
apt-key add raspbian.public.key

Do update the package repository
> apt-get update
below is my recommendation package, you may add yours.
> apt-get install sudo vim bzip2 ntfs-3g

add your username to sudoers
> usermod -a -G sudo raspibian

to change to root, just execute this command:
> sudo -i

edit /etc/modules add this modules below to be loaded at boot time



edit /etc/inittab find serial line part, uncomment and modifies like this:
# Example how to put a getty on a serial line (for a terminal)
T0:2345:respawn:/sbin/getty -L ttyAMA0 115200 vt100
#T1:23:respawn:/sbin/getty -L ttyS1 9600 vt100

add a host name on /etc/hostname, you change it with your own name
add the hostname to /etc/hosts at the first lines   raspibian   localhost

4. Install Kernel modules
Exit the chroot,  To install the kernel modules kernel to your filesystem do
> sudo make ARCH=armhf CROSS_COMPILE=arm-linux-gnueabihf INSTALL_MOD_PATH= modules_install
Check the files at /lib/modules/linux-xxx, if exist then the installation was succesfull.

Now you got the a complete base file system. The next step is how to put the raspibian file system into your sd-card.

Make at least two partition on at least 2GB sd-card, first ones is dos partition for bootloader with 50MByte size (that's should be enough), and rest were formated with linux ext4 for root filesystem. You may also add small amount of  swap space at the end.


To get the bootloader partition to work, first download the firmware of the bootloader section from this link:
or you may retain a working copy from available Rpi sd-card images.

You'll get these files:

Add these files:
> gedit cmdline.txt
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

> gedit config.txt
# Set sdtv mode to PAL (as used in Europe)
# Force the monitor to HDMI mode so that sound will be sent over HDMI cable
# Set monitor mode to DMT
# Set monitor resolution to 1024x768 XGA 60 Hz (HDMI_DMT_XGA_60)
# Make display smaller to stop text spilling off the screen
# for more options see

Now copy kernel.img and kernel_emergency from previous step to this partition. And copy all rootfs files to ext4 partition of the sd-card.

Plug the sd-card, connect the serial to RS232 port (you should build it first), run a terminal with 115200 baud rate and turn the power on. Your Raspibian should be up and running!. Use the previous username and password to log in.