Created
October 27, 2020 18:32
-
-
Save r4space/013189f3870726b63fdb2dffc3fbb710 to your computer and use it in GitHub Desktop.
QEMU based emulation of a RISC-V64b core running Debian. Installed and executed on Ubuntu 20.04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #STEP1: Install requirements. | |
| ## Assumes a clean Ubuntu 20.04 host OS | |
| sudo apt-get install debian-ports-archive-keyring | |
| sudo apt-get update | |
| sudo apt-get install gcc-riscv64-linux-gnu qemu-system-misc opensbi qemu-user-static binfmt-support debootstrap libguestfs-tools | |
| wget http://za.archive.ubuntu.com/ubuntu/pool/universe/u/u-boot/u-boot-qemu_2020.04+dfsg-2ubuntu1_all.deb | |
| sudo dpkg -i u-boot-qemu_2020.04+dfsg-2ubuntu1_all.deb | |
| #STEP2: Create a RISC-V filesystem using chroot | |
| ## Basically a space on your local HD that looks like an install of linux with all the libraraies and executables you expect to see from / availble from a designated folder. Provides the ability to have an alternative filesystem accessible from within a host filesystem | |
| sudo debootstrap --arch=riscv64 --keyring /usr/share/keyrings/debian-ports-archive-keyring.gpg --include=debian-ports-archive-keyring unstable /tmp/riscv64-chroot http://deb.debian.org/debian-ports | |
| #STEP3: Configure this new file system by 'logging in' and running some commands | |
| sudo chroot /tmp/riscv64-chroot | |
| ## Update chroot FS | |
| apt-get update | |
| ## Configure a 'network' | |
| cat >>/etc/network/interfaces <<EOF | |
| ## Copy and paste the following lines into the prompt you now have. You're writing the following lines to the interfaces file using cat. | |
| ## 'EOF' is "End Of File" and is a signals to cat that you're done | |
| auto lo | |
| iface lo inet loopback | |
| auto eth0 | |
| iface eth0 inet dhcp | |
| EOF | |
| ## Set a root password | |
| passwd | |
| ## Disable the getty on hvc0 as hvc0 and ttyS0 share the same console device in qemu. | |
| ln -sf /dev/null /etc/systemd/system/[email protected] | |
| ## Install kernel and bootloader infrastructure | |
| apt-get install linux-image-riscv64 u-boot-menu | |
| ## Install and configure ntp tools | |
| apt-get install openntpd ntpdate openssh-server | |
| sed -i 's/^DAEMON_OPTS="/DAEMON_OPTS="-s /' /etc/default/openntpd | |
| ## Configure syslinux-style boot menu | |
| cat >>/etc/default/u-boot <<EOF | |
| U_BOOT_PARAMETERS="rw noquiet root=/dev/vda1" | |
| U_BOOT_FDT_DIR="noexist" | |
| EOF | |
| u-boot-update | |
| exit | |
| #STEP4: Turn the Chroot FS you just made into a disk image (like an iso or CD you'd install an OS from) | |
| sudo virt-make-fs --partition=gpt --type=ext4 --size=10G /tmp/riscv64-chroot/ rootfs.img | |
| sudo chown ${USER} rootfs.img | |
| #STEP5: Boot it and login with username:root and the password you set above | |
| qemu-system-riscv64 -nographic -machine virt -m 1.9G \ | |
| -bios /usr/lib/riscv64-linux-gnu/opensbi/qemu/virt/fw_jump.elf \ | |
| -kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \ | |
| -object rng-random,filename=/dev/urandom,id=rng0 -device virtio-rng-device,rng=rng0 \ | |
| -append "console=ttyS0 rw root=/dev/vda1" \ | |
| -device virtio-blk-device,drive=hd0 -drive file=rootfs.img,format=raw,id=hd0 \ | |
| -device virtio-net-device,netdev=usernet -netdev user,id=usernet,hostfwd=tcp::22222-:22 | |
| #STEP5: Setup ssh | |
| ## While logged into the emulated machine run the following to install ssh, sudo and make youself a non-root user | |
| apt install openssh-server sudo | |
| service ssh restart | |
| adduser yourname | |
| usermod -aG sudo yourname | |
| #STEP6: Test your new user and ssh work: | |
| ## Logout of the emulation or open a new terminal and from the host run: | |
| ssh yourname@localhost -p 22222 | |
| #STEP7: On your host OS, cross-compile a helloworld (or whatever code you want) and copy the object file over to the emulator to run it. | |
| ## EG: | |
| *****main.c****** | |
| #include <stdio.h> | |
| int main() { | |
| printf("Hello, World!\n"); | |
| return 0; | |
| } | |
| ****************** | |
| riscv64-linux-gnu-gcc -o mainRV main.c | |
| scp -P 22222 mainRV yourname@localhost: | |
| ## Login to the emulator and run the code: | |
| ./mainRV | |
| ------------------ | |
| #LASTLY Some general notes: | |
| - To Exit QEMU and shutdown the emulation: | |
| Ctrl-A x | |
| - The way this has been done means you have access to a limited repo from debian of packages that have already been compiled and ported to RISC-V | |
| - You can search this repo using apt eg: | |
| $ apt-cache search keyword | |
| (Or easier - install aptitude and search with it if you're familiar with it) | |
| See here for docs on Debian port: https://wiki.debian.org/RISC-V | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment