Skip to content

Setup Arch Linux RISC V Development Environment

Piggy edited this page Feb 8, 2022 · 28 revisions

This documentation helps you set up an Arch Linux RISC-V development environment with QEMU usermode and systemd-nspawn.

This documentation is available for Arch Linux, Debian and Ubuntu for now.

Install Packages

Arch Linux

  1. Add [archlinuxcn] repo

We will install packages from [archlinuxcn] later.

Append to /etc/pacman.conf:

[archlinuxcn]
Server = https://repo.archlinuxcn.org/$arch

There is also a list of public mirrors available.

Add PGP Keys:

$ sudo pacman -Sy && sudo pacman -S archlinuxcn-keyring
  1. Install packages
$ sudo pacman -S qemu-user-static binfmt-qemu-static

where binfmt-qemu-static to register QEMU interpreter for RISC-V ELF files. zstd and systemd-nspawn are in the dependency tree of base meta packages, so there is no need to install.

Debian and Ubuntu

$ sudo apt install zstd qemu-user-static systemd-container

where zstd for decompressing rootfs, systemd-container for running systemd-nspawn.

Prepare rootfs

  1. Download rootfs
$ curl -O https://archriscv.felixc.at/images/archriscv-20210601.tar.zst
$ sha512sum archriscv-20210601.tar.zst
6f012a169fe6f1ea15aeb3283091466e7992f78d823951ee2170940fa030e7fa2394aee11bf67c29943d21579ab42d2262a3d5ca973b5de8be779f338ba1dd44  archriscv-20210601.tar.zst
  1. Decompress rootfs

Arch Linux

$ mkdir archriscv
$ sudo bsdtar -xvf archriscv-20210601.tar.zst -C archriscv

Debian and Ubuntu

$ mkdir archriscv
$ sudo tar -I zstd -xvf archriscv-20210601.tar.zst -C archriscv

Start and Setup Container

$ sudo systemd-nspawn -D ./archriscv -a -U

where -D provides the root directory for the container, -a for preventing processes with PID 1 doesn't reap zombie children, -U for preventing processes in container to use the same UID range as outside the container.

  1. Check the architecture
# uname -m
riscv64
  1. System upgrade
# pacman -Syu
  1. Install necessary packages

For example, install vim:

# pacman -S vim
  1. Set default editor
# echo 'export EDITOR=vim' >> ~/.bashrc && source ~/.bashrc
  1. Create a regular user and allow sudo access
# useradd -m <username>

where -m for create the user's home directory.

Then run visudo, add under ## User privilege specification:

username ALL=(ALL) NOPASSWD: ALL
  1. Switch to the regular user
# exec su username
$ cd ~
$ pwd
/home/username
$ echo 'export EDITOR=vim' >> ~/.bashrc && source ~/.bashrc

Now we are all done.

Compile and Run the First Program

  1. Write the program

In vim hello.c, write:

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello RISC-V!\n");
    return 0;
}
  1. Compile
$ gcc -o hello hello.c
  1. Check the file format
$ file hello
hello: ELF 64-bit LSB pie executable, UCB RISC-V, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv64-lp64d.so.1, BuildID[sha1]=4a75c57e4e99654dca0d6dc91689dffbbe7dc581, for GNU/Linux 4.15.0, not stripped

RISC-V is expected in the output.

  1. Run
$ ./hello
Hello RISC-V!
Clone this wiki locally