手搓一个Linux

下载源码

wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.19.3.tar.xz
sudo tar -vxf linux-6.19.3.tar.xz
cd linux-6.19.3

安装依赖

sudo apt install build-essential libncurses-dev flex bison openssl libssl-dev libelf-dev

配置config

sudo make defconfig
sudo make menuconfig

-> Device Drivers
-> Graphics support
-> Frame buffer Devices

-> Support for frame buffer device drivers (FB [=y])
  -> EFI-based Framebuffer Support (FB_EFI [=y])

编译

sudo make -j$(nproc)

安装shell

wget_https://www.busybox.net/downloads/busybox-1.37.0.tar.bz2
tar -xf busybox-1.37.0.tar.bz2 
cd busybox-1.37.0
make menuconfig

Settings->Build static binary (no shared libs) (y)
Networking Utilities-> tc (8.3 kb)(n)

make install CONFIG_PREFIX=../busybox 
cd ..

生成512M的空白镜像

dd if=/dev/zero of=linux.img bs=1M count=512

格式化

gdisk linux.img

GPT fdisk (gdisk) version 1.0.10

Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present

Creating new GPT entries in memory.

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-1048542, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-1048542, default = 1046527) or {+-}size{KMGTP}: 50M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): EF00
Changed type of partition to 'EFI system partition'

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-1048542, default = 104448) or {+-}size{KMGTP}:
Last sector (104448-1048542, default = 1046527) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to linux.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

验证一下

fdisk -l linux.img

设备 起点 末尾 扇区 大小 类型
设备 起点 末尾 扇区 大小 类型
linux.img1 2048 102400 100353 49M EFI 系统
linux.img2 104448 1046527 942080 460M Linux 文件系统

sudo losetup -f -P linux.img
sudo mkfs.fat -F32 /dev/loop10p1
sudo mkfs.ext4 /dev/loop10p2

查询uuid

sudo blkid /dev/loop10p2

/dev/loop10p2: UUID="98ed4f6b-8a87-414b-a7e7-c79edac1bfe2" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Linux filesystem" PARTUUID="af7ce5e2-205f-4e6f-a254-924775bfb65e"

安装grub

mkdir mnt
sudo mount /dev/loop10p1 mnt
sudo apt update && sudo apt install grub-efi-amd64-bin
sudo grub-install --target=x86_64-efi --efi-directory=$(realpath mnt) --bootloader-id=GRUB --removable --recheck
cd mnt
sudo mkdir -p boot/grub
sudo cp EFI/BOOT/grub.cfg boot/grub/
sudo nano boot/grub/grub.cfg
cd .. && sudo umount mnt

安装内核和shell

sudo mount /dev/loop10p2 mnt
sudo su
mkdir mnt/boot/grub sys proc -p
cp linux-7.0-rc1/arch/x86/boot/bzImage mnt/boot/
cp busybox/* mnt -r
cd mnt
nano boot/grub/grub.cfg
menuentry 'Linux' {
        insmod gzio
        insmod part_gpt
        insmod ext2
        insmod fat
        insmod normal
        search --no-floppy --fs-uuid --set=root 98ed4f6b-8a87-414b-a7e7-c79edac1bfe2
        linux /boot/bzImage root=PARTUUID=af7ce5e2-205f-4e6f-a254-924775bfb65e rw init=/boot/init rootdelay=3
}
nano boot/init
#!/bin/sh
mount -t sysfs none /sys
mount -t proc none /proc
mount -t devtmpfs devtmpfs /dev
exec /bin/sh
chmod +x boot/init

包管理器apk
https://gitlab.alpinelinux.org/alpine/apk-tools/-/releases

wget https://gitlab.alpinelinux.org/api/v4/projects/5/packages/generic/v3.0.4/x86_64/apk.static
mkdir -p mnt/etc/apk/keys mnt/var/lib/apk mnt/var/cache/apk mnt/var/lock
nano mnt/etc/apk/repositories
http://dl-cdn.alpinelinux.org/alpine/latest-stable/main
http://dl-cdn.alpinelinux.org/alpine/latest-stable/community

git clone https://gitlab.alpinelinux.org/alpine/aports.git
cp aports/main/alpine-keys/* mnt/etc/apk/keys
cp apk.static mnt/bin/apk
chmod +x mnt/bin/apk
perlong
“ 一天一个新发现 ”
 喜欢文章
头像