Experiment

Run on WSL

Setup WSL X11 environment

Run the image

# load asterinas 0.11.0
docker load -i .\asterinas.tar

# run the image with X11 forwarding
sudo docker run -it --privileged -v $(pwd)/asterinas:/root/asterinas -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix asterinas/asterinas:0.11.0

Get X11 library

sudo apt update
sudo apt-get install libghc-x11-dev # get x11 lib

X11 Test

Here we provide a simple example from https://blog.csdn.net/tianziczj/article/details/6601959 to test if X11 works on WSL:

#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
   Display *d;
   Window w;
   XEvent e;
   char *msg = "Hello, World!";
   int s;

   d = XOpenDisplay(NULL);
   if (d == NULL) {
      fprintf(stderr, "Cannot open display\n");
      exit(1);
   }

   s = DefaultScreen(d);
   w = XCreateSimpleWindow(d, RootWindow(d, s), 100, 100, 500,500, 1,
                           777215, 111111);
               printf("BlackPixel(d, s) is %d\n",(int)BlackPixel(d, s));
               printf("WhitePixel(d, s) is %d\n",(int)WhitePixel(d, s));
   XSelectInput(d, w, ExposureMask | KeyPressMask);
   XMapWindow(d, w);

   while (1) {
      XNextEvent(d, &e);
      if (e.type == Expose) {
         XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
         XDrawString(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg));
      }
      if (e.type == KeyPress)
         break;
   }

   XCloseDisplay(d);
   return 0;
}

Compile it using X11 library:

gcc hello_x11.c -o HelloX11 -lX11

if it works well, it will output a screen with "hello world":
a091ddae8f24749bb2bbd8d4314c342.png

Extra: X11 Debug

  1. vscode
MoTTY X11 proxy: Unsupported authorisation protocol
Cannot open display

Qemu Recompilation

To enable graphic in Qemu, we need to recompile the qemu simulator for asterinas.

# get qemu source code 
wget https://download.qemu.org/qemu-8.2.1.tar.xz
tar xf qemu-8.2.1.tar.xz
cd qemu-8.2.1

# ensure necessary package for qemu
sudo apt install libgtk-3-dev
sudo apt install libglib2.0-dev
sudo apt install ninja-build

# configure qemu display 
 ./configure \
    --target-list=x86_64-softmmu \
    --prefix=/usr/local/qemu \
    --enable-slirp \
    --enable-gtk \

# compile and build
make -j$(nproc)
make install

This configuration will enable GTK.

GTK is a widget toolkit. Each user interface created by GTK consists of widgets. GTK (formerly known as GIMP Toolkit and GTK+) is a free and open-source, cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License, allowing both free and proprietary software to use it. It is one of the most popular toolkits for the Wayland and X11 window systems.

User Networking (SLiRP) is the default networking backend for QEMU that provides a full TCP/IP stack within QEMU

Test GTK Window on Asterinas

cd asterinas
make build 
make run

And we can see the qemu is starting using a GTK Window.
271b5791bdda282e7ff4332ec2e17d9.png

Then, it will appear with all screen green:

9c34773ee6cccfcd92c3279364fe6ea.png

This is because

Reference

GTK
QEMU -display gtk and -display sdl not available | Ubuntu 20.04.1 LTS - Ask Ubuntu