PinePhone(ARM) Build Environment setup

So as most of you already know i have ordered a PinePhone. I want to be able to contribute and test as much as I can so I wanted to be able to build packages for it. I figured building direct on the device would be painfully slow so wanted to set something up on my desktop to do so.

I bounced around a few ideas. Cross Compile, Chroot to cross compile in, Emulate ARM with Qemu... In the end I decided to give the Qemu option a go first as it seems like the easiest to setup/maintain. I wasn't completely wrong but it was also a little more complicated than i had assumed at first.

My first idea was just to run one of the pinephone images using qemu. Turns out that can't really be done as qemu can't fully emulate the pinephone. So my next attempt was to run ArchARM using qemu. This is what i will detail here.

So first step is to download the latest generic ARM package found here.

I have setup a folder to host all the files related to ArchARM. So we will want to create an image to hold the ArchARM file system. We do this like so

qemu-img create -f qcow2 -o preallocation=full ArchARM.img 64G

This will create a 64GB image and preallocate the space. This will improve performance.

Next we will create a filesystem and mount this image to copy the base ArchARM system to it. To do this we need to install libguestfs. It is in the AUR and i think soon to be in the community repo. With that installed we can:

Create the filesystem inside the image virt-format --filesystem=ext4 -a ArchARM.img

make the folder to mount it on sudo mkdir /mnt/virtfs

Mount the image sudo guestmount -m /dev/sda1 -a ArchARM.img /mnt/virtfs/

The -m option specifies the partition inside the image to mount. This is not the sda1 on your actual system.

Now we can extract the ArchARM archive to the image

This should be done as root(Not using sudo)

bsdtar -xpf ArchLinuxARM-aarch64-latest.tar.gz -C /mnt/virtfs

Now we need the kernel and initrd from the image so we can boot it with qemu

cp /mnt/virtfs/boot/Image.gz .

cp /mnt/virtfs/boot/initramfs-linux.img .

This will need to be done anytime the kernel is updated in the Virtual Machine.

Now we can unmount the image and we should be able to boot our ARM VirtualMachine.

After some trial and error the proper command to do this is:

qemu-system-aarch64 -machine virt -cpu cortex-a53 -nographic -m 2048 -smp cores=4 -kernel /media/Storage/ArchARM/Image.gz -initrd /media/Storage/ArchARM/ArchARM/boot/initramfs-linux.img -append 'root=/dev/vda1 rw quiet' -drive if=none,file=/media/Storage/ArchARM/ArchARM.img,format=qcow2,id=hd -device virtio-blk-pci,drive=hd -netdev user,id=mynet -device virtio-net-pci,netdev=mynet

This will give the VM 2GB of ram and 4 processor cores. Adjust if your system can't provide that comfortably.

If all goes well you'll get into ArchARM

Starting version 243.162-2-arch
/dev/vda1: clean, 34582/4194304 files, 635873/16777184 blocks

Arch Linux 5.4.1-1-ARCH (ttyAMA0)

alarm login: alarm
[alarm@alarm ~]$ uname -a
Linux alarm 5.4.1-1-ARCH #1 SMP Sat Nov 30 18:54:05 UTC 2019 aarch64 GNU/Linux
[alarm@alarm ~]$

YAY!! now have a fully working ARM system.

Only other thing I'm doing is creating a function in fish(my shell. Bash users could create an alias) so that i can start up this VM by just typing strarm.

Hope this was helpful to some. Let me know if you have any questions or feedback.

Have a great day


#Tech #ARM #PinePhone