source[2]
sudo apt install bc binutils bison dwarves flex gcc git gnupg2 gzip libelf-dev libncurses5-dev libssl-dev make openssl pahole perl-base rsync tar xz-utils
git clone --depth 1 https://github.com/torvalds/linux
cd linux
make tinyconfig
make menuconfig (if you want to change the config of the kernel)
64-bit kernel ---> yes
General setup ---> Initial RAM filesystem and RAM disk (initramfs/initrd) support ---> yes
General setup ---> Configure standard kernel features ---> Enable support for printk ---> yes
Executable file formats / Emulations ---> Kernel support for ELF binaries ---> yes
Executable file formats / Emulations ---> Kernel support for scripts starting with #! ---> yes
Device Drivers ---> Generic Driver Options ---> Maintain a devtmpfs filesystem to mount at /dev ---> yes
Device Drivers ---> Generic Driver Options ---> Automount devtmpfs at /dev, after the kernel mounted the rootfs ---> yes
Device Drivers ---> Character devices ---> Enable TTY ---> yes
Device Drivers ---> Character devices ---> Serial drivers ---> 8250/16550 and compatible serial support ---> yes
Device Drivers ---> Character devices ---> Serial drivers ---> Console on 8250/16550 and compatible serial port ---> yes
File systems ---> Pseudo filesystems ---> /proc file system support ---> yes
File systems ---> Pseudo filesystems ---> sysfs file system support ---> yes
make -j# (#being the number of core you want to compile the kernel with)
mv arch/x86/boot/bzImage ..
cd ..
download busybox source[3]
git clone --depth 1 https://git.busybox.net/busybox/
cd busybox
make menuconfig
(setting)1st option
star build static binary (we are not going to fuck around with libs)
networking -> tc Disable (not compatible with newer kernel)
had to install bzip2
make -j8
mkdir ../initramfs
make CONFIG_PREFIX=../initramfs install
cd ../initramfs
rm linuxrc (if you want)
nano init
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
exec /bin/sh
chmod +x init
find . -print0 | cpio --null -ov --format=newc > initramfs.cpio
gzip ./initramfs.cpio
mv initramfs.cpio.gz ..
cd ..
we need a bootloader[1]
dd if=/dev/zero of=disk.img bs=1M count=1024
fdisk disk.img
n - p - 1 (leave default 2048sectors)
a
w
lsblk (for free loobback device)
sudo losetup /dev/loop15 disk.img
sudo losetup /dev/loop16 disk.img -o 1M
sudo mkfs.ext4 /dev/loop16
sudo mount /dev/loop16 /mnt
sudo cp bzImage /mnt
sudo cp initramfs.cpio.gz /mnt
sudo grub-install --target=i386-pc --root-directory=/mnt --no-floppy --modules="normal part_msdos ext2 multiboot" /dev/loop15
sudo nano /mnt/boot/grub/grub.cfg
menuentry 'my OS' {
set root='(hd0,1)'
linux /bzImage
initrd /initramfs.cpio.gz
}
sudo apt install qemu-system-x86
qemu-system-x86_64 disk.img
[1]: https://wiki.osdev.org/GRUB#Disk_image_instructions
[2]: https://itsfoss.com/compile-linux-kernel/
[3]: https://medium.com/@kiky.tokamuro/creating-initramfs-5cca9b524b5a