Setting up an loong64 sbuild environment based on qemu-loongarch64

Installing qemu from the Debian archive

root@loongson01:~# apt install qemu-user-static qemu-system-misc binfmt-support

Manual qemu-user installation

root@loongson01:~# git clone https://github.com/qemu/qemu.git

root@loongson01:~# cd qemu

root@loongson01:~/qemu# ./configure --static --disable-system --target-list=loongarch64-linux-user

root@loongson01:~/qemu# make

root@loongson01:~/qemu# cp build/loongarch64-linux-user/qemu-loongarch64 /usr/bin/qemu-loongarch64-static

Create a binfmt-support config file and register it

root@loongson01:~# cat >/tmp/qemu-loongarch64 <<EOF
package qemu-user-static
type magic
offset 0
magic \x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x02\x01                    
mask \xff\xff\xff\xff\xff\xff\xff\xfc\x00\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff
interpreter /usr/bin/qemu-loongarch64-static
EOF

root@loongson01:~# update-binfmts --import /tmp/qemu-loongarch64 

With this it is now possibe to transparantly run user-mode loongarch64 binaries on another architecture:

root@loongson01:~# uname -a
x86_64

root@loongson01:~# file ./busybox                                                                                      
./busybox: ELF 64-bit LSB executable, LoongArch, version 1 (SYSV), statically linked, BuildID[sha1]=c09d723754265a7e893c64f50f74bfe8b8324082, for GNU/Linux 5.19.0, stripped

root@loongson01:~# ./busybox touch foo

root@loongson01:~# ls foo
foo

Create sbuild environment for loong64

root@loongson01:~# apt install debootstrap sbuild debian-ports-archive-keyring

root@loongson01:~# mkdir /srv/chroots

root@loongson01:~# debootstrap --foreign --arch=loong64 --variant=buildd --include=debian-ports-archive-keyring --verbose --components=main --keyring=/etc/apt/trusted.gpg.d/debian-ports-archive-2023.gpg --resolve-deps --extra-suites=unreleased unstable /srv/chroots/sid-loong64-sbuild/ http://ftp.ports.debian.org/debian-ports 

root@loongson01:~# cd /srv/chroots/sid-loong64-sbuild/

root@loongson01:/srv/chroots/sid-loong64-sbuild/# cp /usr/bin/qemu-loongarch64-static usr/bin/ 

root@loongson01:/srv/chroots/sid-loong64-sbuild/# chroot .

I have no name!@loongson01:/# /debootstrap/debootstrap --second-stage

I have no name!@loongson01:/# exit

root@loongson01:/srv/chroots/sid-loong64-sbuild/# cat >/etc/schroot/chroot.d/sid-loong64-sbuild <<EOF
[sid-loong64-sbuild]
description=Debian sid chroot for loong64
type=directory
directory=/srv/chroots/sid-loong64-sbuild 
groups=root,sbuild,buildd
root-groups=root,sbuild,buildd
EOF

After that, we just need to configure apt in the chroot:

#root@loongson01:~# cat /srv/chroots/sid-loong64-sbuild/etc/apt/sources.list     
# binary default
deb http://ftp.ports.debian.org/debian-ports unstable main 
deb http://ftp.ports.debian.org/debian-ports unreleased main

# source
deb-src http://ftp.cn.debian.org/debian/ sid main

After that, it should be possible to build packages for loong64 for any user in the sbuild group:

root@loongson01:~# sbuild --arch loong64 -d sid hello

Of course, you can just chroot into the loong64 chroot and run any application like you would do on a native system.

For more sbuild options, Please see:

For the use of qemu in the sbuild process of other ports, please see:


CategoryPorts