メインコンテンツへスキップ
見出し画像

育休エンジニアが子供のために、1CD Linuxを自作してみる その5

    前回はこちら

    はじめに

    前回GRUBブートローダを導入しました。今回はいよいよLinuxを起動していきます。

    Linuxのビルド

    まずはLinuxカーネルのビルドに必要なパッケージをインストールします。

    sudo apt install libncurses-dev libelf-dev libssl-dev

    続いて、Linuxカーネルのソースコードをダウンロードし、展開します。

    wget https://ftp.jaist.ac.jp/pub/Linux/kernel.org/linux/kernel/v6.x/linux-6.12.29.tar.xz
    tar xf linux-6.12.29.tar.xz
    cd linux-6.12.29

    まずソースをクリーンな状態にします。

    make mrproper

    続いてカーネルのコンフィグレーションを行います。カーネルには大量の設定項目があり、すべての項目を理解し自力で設定するのは、我々普通の人からすると難しいです。まずはデフォルトの設定をベースに、カスタマイズしていくのが良いでしょう。また、Linux From Scratchのページが参考になります。以下のコンフィギュレーションはLFSの10.3. Linux-6.13.4を参照しました。ただし、バージョン違いにより一部修正しました。

    make menuconfig
    General setup --->
      [ ] Compile the kernel with warnings as errors                        [WERROR]
      CPU/Task time and stats accounting --->
        [*] Pressure stall information tracking                                [PSI]
        [ ]   Require boot parameter to enable pressure stall information tracking
                                                         ...  [PSI_DEFAULT_DISABLED]
      < > Enable kernel headers through /sys/kernel/kheaders.tar.xz      [IKHEADERS]
      [*] Control Group support --->                                       [CGROUPS]
        [*]   Memory controller                                              [MEMCG]
        [ /*] CPU controller --->                                     [CGROUP_SCHED]
          # This may cause some systemd features malfunction:
          [ ] Group scheduling for SCHED_RR/FIFO                    [RT_GROUP_SCHED]
      [ ] Configure standard kernel features (expert users) --->            [EXPERT]
    
    Processor type and features --->
      [*] Build a relocatable kernel                                   [RELOCATABLE]
      [*]   Randomize the address of the kernel image (KASLR)       [RANDOMIZE_BASE]
    
    General architecture-dependent options --->
      [*] Stack Protector buffer overflow detection                 [STACKPROTECTOR]
      [*]   Strong Stack Protector                           [STACKPROTECTOR_STRONG]
    
    [*] Networking support --->                                                [NET]
      Networking options --->
        [*] TCP/IP networking                                                 [INET]
        <*>   The IPv6 protocol --->                                          [IPV6]
    
    Device Drivers --->
      Generic Driver Options --->
        [ ] Support for uevent helper                                [UEVENT_HELPER]
        [*] Maintain a devtmpfs filesystem to mount at /dev               [DEVTMPFS]
        [*]   Automount devtmpfs at /dev, after the kernel mounted the rootfs
                                                               ...  [DEVTMPFS_MOUNT]
        Firmware loader --->
          <  / *> Firmware loading facility                              [FW_LOADER]
          [ ]     Enable the firmware sysfs fallback mechanism
                                                        ...  [FW_LOADER_USER_HELPER]
      Firmware Drivers --->
        [*] Export DMI identification via sysfs to userspace                 [DMIID]
        [*] Mark VGA/VBE/EFI FB as generic system framebuffer       [SYSFB_SIMPLEFB]
      Graphics support --->
        <*> Direct Rendering Manager (XFree86 4.1.0 and higher DRI support) --->
                                                                          ...  [DRM]
          [*]    Display a user-friendly message when a kernel panic occurs
                                                                    ...  [DRM_PANIC]
          (kmsg)   Panic screen formatter                         [DRM_PANIC_SCREEN]
          [*] Enable legacy fbdev support for your modesetting driver
                                                          ...  [DRM_FBDEV_EMULATION]
          <*>    Simple framebuffer driver                           [DRM_SIMPLEDRM]
        Console display driver support --->
          [*] Framebuffer Console support                      [FRAMEBUFFER_CONSOLE]
    
    File systems --->
      [*] Inotify support for userspace                               [INOTIFY_USER]
      Pseudo filesystems --->
        [*] Tmpfs virtual memory file system support (former shm fs)         [TMPFS]
        [*]   Tmpfs POSIX Access Control Lists                     [TMPFS_POSIX_ACL]

    今回スライドショーを表示するFIMは、フレームバッファのデバイスノード(/dev/fb0)を使用します。これを有効にするために追加で以下の設定を行います。

    Device Drivers  --->
      Graphics support  --->
        Frame buffer Devices  --->
          [*] Provide legacy /dev/fb* device

    カーネルをビルドします。かなり時間がかかると思います。

    make -j$(nproc)

    LinuxのインストールおよびGRUBの設定

    まずは以下コマンドによりLinuxカーネルイメージをCDにコピーします。

    cp -iv arch/x86/boot/bzImage ../mydvd/boot/vmlinuz
    cd ..

    続いて、GRUBの設定を行います。以下の通りgrub.cfgを作成することで、GRUBからLinuxを起動することができます。

    vi mydvd/boot/grub/grub.cfg
    # Begin /boot/grub/grub.cfg
    set default=0
    set timeout=5
    
    set gfxpayload=1024x768x32
    
    menuentry "Slideshow on 1CD Linux" {
            linux   /boot/vmlinuz root=/dev/sr0 ro
    }

    grub.cfgを作成したら準備完了です。いつも通りISOイメージを作成しQEMUを起動します。

    mkisofs -iso-level 4 -b boot/grub/i386-pc/eltorito.img -no-emul-boot -r -boot-info-table -o mydvd.iso mydvd
    qemu-system-x86_64 -cdrom mydvd.iso -m 1G

    以下のようにKernel panicと表示され、停止してしまうと思います。これはinitがないためであり、initはこれから用意するため現時点では問題ありません。とりあえずLinuxの起動は成功です。

    [    2.778705] Run /sbin/init as init process
    [    2.779119] Run /etc/init as init process
    [    2.779286] Run /bin/init as init process
    [    2.779435] Run /bin/sh as init process
    [    2.779821] Kernel panic - not syncing: No working init found.  Try passing init= option to kernel. See Linux Documentation.
    [    2.780844] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.12.29 #2
    [    2.781127] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/4
    [    2.781412] Call Trace:
    [    2.783113]  <TASK>
    [    2.783355]  panic+0x326/0x380
    [    2.783645]  ? __pfx_kernel_init+0x10/0x10
    [    2.783712]  kernel_init+0x173/0x1c0
    [    2.783788]  ret_from_fork+0x2f/0x50
    [    2.783835]  ? __pfx_kernel_init+0x10/0x10
    [    2.783878]  ret_from_fork_asm+0x1a/0x30
    [    2.783957]  </TASK>
    [    2.784403] Kernel Offset: 0x2fc00000 from 0xffffffff81000000 (relocation range: 0xffffffff80000000-0xffffffffbfffffff)
    [    2.809341] ---[ end Kernel panic - not syncing: No working init found.  Try passing init= option to kernel. See Linux Docu-

    続く


     
     
    元育休系エンジニア 子育てに奮闘しつつ物理を勉強しながら組込み系の開発をしています。 RISC-Vが好きです。 OS、CPU等低レイヤに興味があります。 Qiitaはこちら https://qiita.com/YoshikiYan

    あなたへのおすすめ