buildrootでGUIを起動させる

# 環境

| 項目 | 値 |備考 |
|-|-|-|
|ビルド環境| fedora 40 ||
| 実行環境 | qemu-x86_64 ||

## download

commit idは指定しなくても動作する可能性が高いが
こちらで動作確認できたものを念の為記載している。

```shell
git clone git@github.com:buildroot/buildroot.git gui_sample
cd gui_sample
git checkout 4a9a4c3cd5f416b44b8ab6c3c7cdfc3cc991a5de
```
## buildrootの設定

```shell
make qemu_x86_64_defconfig
make linux-menuconfig
```

マシンによるが、最初の`linux-menuconfig`は10分くらいかかる。  
以下を有効
```
Device Drivers > Input device support > Mouse interface
Device Drivers > Input device support > Mouse interface > Provide legacy /dev/psaux device
```
save -> exit

```shell
make menuconfig
```

以下を有効
```
Toolchain > Enable C++ support
Target Packages > Hardware handling > evtest
Target Packages > Graphic libraries and applications > mesa3d
Target Packages > Graphic libraries and applications > X.org X Window System
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Servers > xorg-server
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Libraries > libXaw 
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Applications > twm
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Applications > xclock
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Applications > xeyes (確認用)
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Applications > xhost
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Applications > xinit
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Applications > xinput
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Applications > xkill
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Drivers > xf86-input-mouse
```
save -> exit

### 強制udev

多分他にやり方があると思うが、調べきれなかったので以下で

`vim .config`で  
BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_EUDEVを強制的に変更

```diff
diff --git a/.config b/.config
index dd199f1c81..1ddb922224 100644
--- a/.config
+++ b/.config
@@ -489,9 +489,9 @@ BR2_INIT_BUSYBOX=y
 # BR2_INIT_TINYINIT is not set
 # BR2_INIT_NONE is not set
 # BR2_ROOTFS_DEVICE_CREATION_STATIC is not set
-BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_DEVTMPFS=y
+#BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_DEVTMPFS=y
 # BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_MDEV is not set
-# BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_EUDEV is not set
+BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_EUDEV=y
 BR2_ROOTFS_DEVICE_TABLE="system/device_table.txt"
 # BR2_ROOTFS_DEVICE_TABLE_SUPPORTS_EXTENDED_ATTRIBUTES is not set
 # BR2_ROOTFS_MERGED_USR is not set
```

```
make menuconfig
```
後、すぐにexitするとconfigを変更するか聞かれるのでそのままEnter


修正した部分以外にも`.config`が自動で修正される。

### Overlayファイルの作成

以下を作成
- board/qemu/x86_64/overlay/etc/mdev.conf
- board/qemu/x86_64/overlay/etc/X11/xorg.conf
- board/qemu/x86_64/overlay/etc/init.d/S99startx

```shell
mkdir -p board/qemu/x86_64/overlay/etc/X11/
mkdir -p board/qemu/x86_64/overlay/etc/init.d/

cat <<EOF > board/qemu/x86_64/overlay/etc/mdev.conf
^event[0-9]+$         root:input 0660
^event[0-9]+$         root:input 0660
EOF

cat <<EOF > board/qemu/x86_64/overlay/etc/X11/xorg.conf
Section "InputClass"
    Identifier "Mouse"
    MatchIsPointer "yes"
    Driver "libinput"
EndSection

Section "InputClass"
    Identifier "Keyboard"
    MatchIsKeyboard "yes"
    Driver "libinput"
EndSection
EOF

cat <<EOF > board/qemu/x86_64/overlay/etc/init.d/S99startx
#!/bin/sh

ARGV=\$1
export DISPLAY=:0.0
case "\$ARGV" in
    start)
        xterm -geometry 100x24+100+100 &
        ;;
    stop)
        killall X
        ;;
    restart)
        "\$0" stop
        "\$0" start
        ;;
    *)
        echo "ARGV:\$ARGV"
        exit 1
esac
exit 0
EOF

chmod +x board/qemu/x86_64/overlay/etc/init.d/S99startx
```

再度
```
make menuconfig
```
し、以下を変更(有効)
2箇所キーボード入力が必要(ディスクサイズとoverlayディレクトリ)

```
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Libraryies > xcb-util
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Libraryies > xcb-util-cursor
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Libraryies > xcb-util-keysyms
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Drivers > xf86-input-evdev
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Drivers > xf86-input-libinput
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Drivers > xf86-input-vmmouse
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Drivers > xf86-video-fbdev
Target Packages > Graphic libraries and applications > X.org X Window System > X11R7 Drivers > xf86-video-vesa
Target Packages > Graphic libraries and applications > directfb
Target Packages > Graphic libraries and applications > directfb > directfb multi application
Target Packages > Graphic libraries and applications > linux-fusion
Target Packages > Hardware handling > acpid
Target Packages > Hardware handling > dbus
Target Packages > Hardware handling > mdevd
Target Packages > Libraries > Text and terminal handling > ncurses
Target Packages > Libraries > Text and terminal handling > ncurses > enable wide char support
Target Packages > Graphic libraries and applications > xterm
System configuration > Use symlinks to /usr for /bin, /sbin and /lib
System configuration > Root filesystem overlay directories(board/qemu/x86_64/overlay)キーボード入力
Toolchain > Install glibc utilities
Toolchain > Copy gconv libraries

Filesystem images > exact size (256M)キーボード入力
```



## ビルド
```
make
```

### 不具合回避

#### cpp -> g++
多分fedora環境だけかもしれないが、c++のコンパイラに
g++ではなくcppが選択されてうまく動かない場合がある。

`output/host/bin/x86_64-buildroot-linux-gnu-g++`
が存在しないといわれたら以下を実行し、再ビルド
```shell
pushd output/host/bin/
ln -s /usr/bin/g++ x86_64-buildroot-linux-gnu-g++
popd
```

## 実行

```
qemu-system-x86_64 -M q35 -enable-kvm -kernel output/images/bzImage -append "root=/dev/vda console=ttyS0" -drive file=./output/images/rootfs.ext2,if=virtio,format=raw -net nic,model=virtio -serial stdio -device qemu-xhci
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3786381/25c7e524-0183-0780-fcef-f13f171d415a.png)

### Window managerを起動させて、背景変更

fehで背景画像を指定し、Window Managerにtwmを使用するとこんな感じ

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3786381/60612f92-ffed-3ae3-674f-2cc81d45b51c.png)

#### 変更内容
S99startxの以下部分を変更し、
```
    start)
        twm &
        feh --bg-scale /usr/share/backgrounds/bg1.jpg &
        xterm -geometry 100x24+100+100 &
        ;;

```
board/qemu/x86_64/overlay/usr/share/backgrounds/bg1.jpg
を置く

Target Packages > Graphic libraries and applications > feh
も有効にする。