Skip to content

Instantly share code, notes, and snippets.

@doevelopper
Last active July 31, 2018 12:35
Show Gist options
  • Save doevelopper/712630caee143d2c9120b49d79361bf3 to your computer and use it in GitHub Desktop.
Save doevelopper/712630caee143d2c9120b49d79361bf3 to your computer and use it in GitHub Desktop.
Raspberry Pi 3 System from scratch using ubuntu-base FS

Compile RPI3 Kernel

Using Ubuntu-base arm64 rootfs for Raspberry Pi 3 Courtesy to https://a-delacruz.github.io/ubuntu/rpi3-setup-filesystem

>$ WORKING_DIR=$(readlink -f `pwd`)
>$ RTOS64=${WORKING_DIR}/rtos64
>$ export CONCURRENCY_LEVEL=$(nproc)
>$ ROOTFS=${WORKING_DIR}/rootfs

>$ mkdir -pv kbuild && cd kbuild
>$ rm -rf linux
>$ git clone --depth 1 --branch rpi-4.14.y-rt https://github.com/raspberrypi/linux.git rpi-preempt-4.14.y-rt

>$ make O=${RTOS64}/ ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- distclean
>$ make O=${RTOS64}/ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig
>$ make O=${RTOS64}/ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
>$ nice -n 19 make O=${RTOS64}/ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \
    CFLAGS="${CFLAGS} -O3 -march=armv8-a+crc -mtune=cortex-a53 -mcpu=cortex-a53 \
    -mfpu=neon-fp-armv8 -mfloat-abi=hard -funsafe-math-optimizations" \
    LDFLAGS="${LDFLAGS} -O3 -march=armv8-a+crc -mtune=cortex-a53 -mcpu=cortex-a53 \
    -mfpu=neon-fp-armv8 -mfloat-abi=hard -funsafe-math-optimizations" modules dtbs -j$(nproc)  #zImage not working

>$ wget http://cdimage.ubuntu.com/ubuntu-base/releases/17.10/release/ubuntu-base-17.10-base-arm64.tar.gz
>$ mkdir ${ROOTFS}
>$ sudo tar xzvf ubuntu-base-17.10-base-arm64.tar.gz -C ${ROOTFS}

Install the kernel module and firmware into the rootfs folder that we just created above.

Go to the compiled Raspberry Pi 3 kernel,

>$ sudo make -C linux-rpi-4.14.y-rt/ ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- \
    O=${rtos64} modules_install INSTALL_MOD_PATH=${ROOTFS}

Starting kernel > 4.14 use your debian based "linux-firmware" package:

For kernel < 4.13 include below commands for firmware:

>$ sudo make -C linux-rpi-4.14.y-rt/ ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- \
   O=${rtos64} firmware_install INSTALL_FW_PATH=${ROOTFS}/lib/firmware/

Remove build and source links.

>$ ls ${ROOTFS}/lib/modules/4.11~
>$ sudo find ${ROOTFS}/ -name build | xargs rm -rf
>$ sudo find ${ROOTFS}/ -name source | xargs rm -rf 

Well use chroot to further setup our filesystem

copy resolv.conf from host machine for internet connection to ${ROOTFS}/etc/

>$ sudo cp -av /run/systemd/resolve/stub-resolv.conf ${ROOTFS}/etc/resolv.conf

Enter chroot environment:

>$ sudo chroot ${ROOTFS}/
>$ useradd -G sudo -m -s /bin/bash pi3
>$ echo pi3:pi3 | chpasswd

setup your hostname

>$ echo rtos64 > /etc/hostname
>$ echo 127.0.0.1	localhost > /etc/hosts
>$ echo 127.0.1.1	rtos64 >> /etc/hosts

Fetch the latest package lists from server then upgrade.

>$ apt-get update
>$ apt-get upgrade

Help to correct some error messages about locale

>$ apt-get install dialog perl
>$ locale-gen "en_US.UTF-8"

Install minimal packages:

>$ apt-get install sudo ifupdown net-tools ethtool udev wireless-tools iputils-ping 
>$ apt-get install resolvconf wget apt-utils wpasupplicant initramfs-tools

Create our Ramdisk.

>$  --mkinitramfs -o /boot/initrd.img /lib/modules/4.11~ --

When everything you want are done, exit chroot:

>$ exit

add /etc/fstab file entry below coz’ if not…it will run on read-only mode. It is Tab separated not spacebar.

>$ echo /dev/mmcblk0p2	/	ext4	defaults,noatime	0	1 >> $HOME/rootfs/etc/fstab

To reduce the rootfs/ size we can remove some unwanted files.

Create a file /etc/dpkg/dpkg.cfg.d/01_nodoc which specifies the desired filters.

path-exclude /usr/share/doc/*
# we need to keep copyright files for legal reasons
path-include /usr/share/doc/*/copyright
path-exclude /usr/share/man/*
path-exclude /usr/share/groff/*
path-exclude /usr/share/info/*
# lintian stuff is small, but really unnecessary
path-exclude /usr/share/lintian/*
path-exclude /usr/share/linda/*
> sudo find rootfs/usr/share/doc -depth -type f ! -name copyright|xargs rm || true
> sudo find rootfs/usr/share/doc -empty|xargs rmdir || true
> sudo rm -rf rootfs/usr/share/man/* rootfs/usr/share/groff/* rootfs/usr/share/info/*
> sudo rm -rf rootfs/usr/share/lintian/* rootfs/usr/share/linda/* rootfs/var/cache/man/*

copy the $HOME/rootfs/* content to the 2nd partition of your MicroSD card.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment