How-to-create-Linux.iso
johnchewyy edited this page 11 months ago

How to create Linux.iso from scratch

Create «linux_riscv64.efi»

  1. Setup environment

    export ARCH=riscv 
    export CROSS_COMPILE= <RISCV_tool_chain>
    

    - version 12.0.2

  2. Git clone VisionFive2 Linux repository

    > git clone https://github.com/starfive-tech/linux
    > cd linux
    > git checkout VF2_v2.11.5
    
  3. Compile Linux code

    > make starfive_visionfive2_defconfig
    > make -j$(nproc)
    
  4. Copy and rename Linux image file

    > cp arch/riscv/boot/Image ../linux-riscv64.efi
    > cd ..
    
  5. Create «initramfs.cpio.gz»

    1. Download Busybox

      > wget https://busybox.net/downloads/busybox-1.26.2.tar.bz2
      > tar -xvf busybox-1.26.2.tar.bz2
      
    2. Config Busybox

      > cd busybox-1.26.2
      > make defconfig
      > make menuconfig
      

      In the Busybox Settings menu, select Build Options, and check the box next to Build BusyBox as a static binary (no shared libs).

    3. Build Busybox

      > make
      > make CONFIG_PREFIX=./../busybox_rootfs install
      
    4. Create a directory hierarchy for initramfs

      > mkdir -p initramfs/{bin,dev,etc,home,mnt,proc,sys,usr}
      > cd initramfs/dev
      > sudo mknod sda b 8 0 
      > sudo mknod console c 5 1
      
    5. Copy everything from the busybox_rootfs folder to the initramfs folder.

    6. Create an «init» file in the initramfs folder, and write the following into it:

      #!/bin/sh
      mount -t proc none /proc
      mount -t sysfs none /sys
      exec /bin/sh
      
    7. Make it executable

      > chmod +x init
      
    8. Create and gzip initramfs

      > cd initramfs
      > find . -print0 | cpio --null -ov --format=newc > initramfs.cpio 
      > gzip ./initramfs.cpio
      

    Create «startup.nsh» (Optional)

    1. Create a file name «startup.nsh»
    2. Copy the following and save the file

      initrd initramfs.cpio.gz
      linux-riscv64.efi console=tty1 console=ttyS0,115200 earlycon=sbi rootwait
      

      This script is used for UEFI Shell to auto-load initramfs and linux image during device boot up

    Create «linux.iso»

    1. Query the loop device

      > sudo losetup -f
      
    2. Create a mount device

      > mkfs.msdos -C linux.iso 64000
      > sudo losetup /dev/loop* linux.iso
      > sudo mount /dev/loop* /mnt
      

      Replace loop* with the number shown in previous command

    3. Copy the kernel and initramfs that built previously

      > sudo cp linux-riscv64.efi /mnt
      > sudo cp initramfs.cpio.gz /mnt
      > sudo mkdir /mnt/efi
      > sudo mkdir /mnt/efi/boot
      > sudo cp startup.nsh /mnt/efi/boot
      > sudo umount /mnt
      > sudo losetup -d /dev/$loop
      

      Copy startup.nsh is optional, ignore if not using it