Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reynir/a7215a437becb41734605c0a8941b91e to your computer and use it in GitHub Desktop.
Save reynir/a7215a437becb41734605c0a8941b91e to your computer and use it in GitHub Desktop.

This can be used as a starting point for developing your own image e.g. for running MirageOS on an Pi3

You will need an arm64 / aarch64 cross compiler. You can use e.g. the cross compiler shipped by Ubuntu or get the toolchain of your choice for your OS.

apt-get install gcc-aarch64-linux-gnu

Now we setup the SD card. We need to create two partitions like shown below

Device                                Boot Start     End Sectors  Size Id Type
2018-03-13-raspbian-stretch-lite.img1       8192   93802   85611 41,8M  c W95 FAT32 (LBA)
2018-03-13-raspbian-stretch-lite.img2      98304 3629055 3530752  1,7G 83 Linux

last sector of second partition can be max size of card

You can use fdisk or any other partition tool you like. Now we need filesystems. The first one needs to be fat32. The second one can be anything a linux kernel can open. We use ext4 here.

sudo mkfs.vfat /dev/sdc1
sudo mkfs.ext4 /dev/sdc2

you can know give the paritions names for your convinince.

sudo fatlabel /dev/sdc1 boot
sudo tune2fs -L root /dev/sdc2

This is a good time to get a litte familar with the boot process. I found these to posts helpfull https://raspberrypi.stackexchange.com/questions/39959/raspbian-boot-process-and-the-partition-table?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa https://raspberrypi.stackexchange.com/questions/10442/what-is-the-boot-sequence

As explained in the linked artickels we need some blobs as this is a broadcom graphics card used as an CPU....

Check out the firmware files you will need

git clone --depth=1 https://github.com/raspberrypi/firmware

Copy the content of boot to your first partition. There will be some files you dont need like dtb's for older pis and some overlays but for the sake of easy updates in the future and personal lazyness lets ignore that for now.

cp -r boot/* /<boot partition mount>/

You will find a config.txt which you can alter if you want. https://elinux.org/RPiconfig gives an good overview on the options.

You can start with a default config.ini

wget https://github.com/RPi-Distro/pi-gen/raw/master/stage1/00-boot-files/files/config.txt

You may want to add

enable_uart=1
arm_control=0x200
kernel=Image

to enable the serial console (This will disable you bluetooth for now.) , set the CPU to 64bit mode and choose the name of your kernel image.

and

gpu_mem=16

to reduce the memory for the GPU to the minimum.

We also need a cmdline.txt to tell the kernel some option. We can get a default by

wget https://github.com/RPi-Distro/pi-gen/raw/master/stage1/00-boot-files/files/cmdline.txt

here we want to set the rootfs to

root=/dev/mmcblk0p2 

and you may also want to get rid of predictable device names by adding

net.ifnames=0

Now we need a root filesystem. We use qemu-debootrap for this as it will give us very plain debian. For this mount the second partition somewhere.

mount /dev/sdc2 /mnt

and run the qemu debootstrap wrapper

You may want to read the deboostrap manpage at this point

sudo qemu-debootstrap --arch arm64 stretch /mnt

As the kernel that we got from the firmware repo is an rusty old 4.9 with 32bit and no Virtualization we need to build our own.

git clone --depth=1 https://github.com/raspberrypi/linux.git -b rpi-4.18.y

Note that we check out the branch 4.18 which my be outdate at the time you read this. So you may want to use a newer one.

We use CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm64 make bcmrpi3_defconfig to start with an kernel condfig fitting to the raspi.

Now we need to enable Virtualization.

  CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm64 make menuconfig
  -> Virtualization -> 
				-> Kernel-based Virtual Machine (KVM) support *
				-> Host kernel accelerator for virtio net  M

and we are good to go to build our kernel. You may want adjust the -j4 to the number of CPU cores you want to use for this.

CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm64 make -j4 Image dtbs modules

We now need to copy the kernel image and the dtbs file to the SD card. Note that we here copy the dtb for an raspberrypi 3 +, so if you use a different pi you may want to copy a different dtb file.

cp arch/arm64/boot/Image /<boot partition mount>/
cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b-plus.dtb /<boot partition mount>/

Now we need to copy the modules to the root filesystem

sudo CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm64 INSTALL_MOD_PATH=/mnt make modules_install

As debootstrap gives us an unconfigured debian you may want to edit

/etc/network/interfaces

auto eth0
iface eth0 inet dhcp

/etc/fstab

UUID=31c566e0-0f1d-475d-9908-4740c8ca3653 / ext4    errors=remount-ro 0       1

you can get the uuid by running blkid

/etc/hostname

finally you want to set a root password

sudo chroot /mnt
passwd 
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment