0

Cách dùng Window cmd để tạo Linux Distro

1. Tổng quan

  • Bài viết này mình sẽ build Linux cho x86. Có 3 phần chính với task build này bao gồm:

    • Kernel
    • User Space (Busy Box)
    • Boot Loader (Syslinux)
  • Quá trình boot của nó gồm các giai đoạn

    Boot → Kernel → User space

2. Gom đồ

3. Quá trình build

Mở Window cmd và bật docker destop lên, sau đó ae mình đến với quá trình build sau:

$ docker run --privileged -it ubuntu
$ apt update
$ apt install bzip2 git vim make gcc libncurses-dev flex bison bc cpio libelf-dev libssl-dev syslinux dosfstools

# tao Image (Kernel)
$ git clone --depth 1 https://github.com/torvalds/linux.git
$ cd linux/
$ make menuconfig

# hien menuconfig thi ban chon Exit -> Yes 
$ make -j 8

# luu Image (Kernel)
$ mkdir /boot-files
$ cp arch/x86/boot/bzImage /boot-files
$ cd ..

# tao initramfs 
$ git clone --depth 1 https://git.busybox.net/busybox
$ cd busybox
$ make menuconfig
-> o day menuconfig hien len, ban di xuong xuong gan cuoi va nhan Y chon config Build static binary (no shared libs)
# sau day Exit va Save nhe
$ make -j 8

# o day neu no hien len loi build fail, ban sua lai code dua theo patch sau (http://lists.busybox.net/pipermail/busybox-cvs/2024-January/041752.html)
-> sau khi dap patch thi make -j 8 lai nhe

# luu initramfs
$ mkdir /boot-files/initramfs

# initramfs la file khoi dong he thong ma kernel load sau khi boot, ae minh de busybox vao file nay
$ make CONFIG_PREFIX=/boot-files/initramfs install   

# tao tien trinh dau tien nhe kkk
$ cd /boot-files/initramfs/
$ ls
$ vim init
# them code sau vao file init

#!/bin/sh
/bin/sh

# thoat va luu file

$ rm linuxrc
$ chmod +x init
# luu initramfs
$ find . | cpio -o -H newc > ../init.cpio
$ cd ..

# tao bootloader ( pwd = /boot-files)
$ apt install syslinux

# tao vung de chua bootloader, kernel, initramfs
$ dd if=/dev/zero of=boot bs=1M count=50

# dinh dang lai filesystem de syslinux ho tro nha
$ apt install dosfstools
$ mkfs -t fat boot
$ syslinux boot
$ mkdir m
$ mount boot m
$ cp bzImage init.cpio m
$ umount m

4. Quá trình boot

Mở tab Window cmd mới để thi triển võ công nhé =)))

# copy do tu docker ve may window cua minh
$ docker ps -a

# lay docker_id cua id docker ae vua dung de build
$ docker cp dockerid:/boot-files/boot .

# nhay vao gia lap test thoi kkk
$ qemu-system-x86_64 boot

# sau khi gia lap hien thi, tab ban thay se la chuong trinh bootloader cua x86 nhe, ban nhap cmd sau de chon Kernel va Initramfs
$ /bzImage -initrd=/init.cpio

# Enter va tan huong thanh qua

5. Kết quả trông như này là oke nhé

image.png


All rights reserved

Bình luận

Đăng nhập để bình luận
Avatar
0
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí