-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile.kernel
More file actions
144 lines (114 loc) · 5.06 KB
/
Makefile.kernel
File metadata and controls
144 lines (114 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Makefile.kernel: Build a minimal Linux distro with custom Go utilities
ARCH ?= x86_64
KERNEL_VERSION ?= 6.14
KERNEL_URL = "https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-$(KERNEL_VERSION).tar.xz"
KERNEL_ARCHIVE = linux-$(KERNEL_VERSION).tar.xz
KERNEL_DIR = linux-$(KERNEL_VERSION)
ROOTFS_DIR = rootfs
INITRAMFS = initramfs.cpio.gz
# Directories for host system files - Be cautious with these!
BUSYBOX_DIR ?= /usr/bin/
GCC_DIR ?= /usr/bin/
MAKE_DIR ?= /usr/bin/
LIBS_DIR ?= /lib/
H_DIR=/usr/include/
# Set this to the password you want for dosu (required)
DOSU_PASS ?=
ISO_IMAGE = nebula.iso
ISO_DIR = iso
.PHONY: all kernel utils rootfs image clean iso quickreimage
all: kernel utils rootfs image iso
quickreimage: quickclean rootfs image iso
# Download and extract the Linux kernel
$(KERNEL_ARCHIVE):
wget -c $(KERNEL_URL)
$(KERNEL_DIR): $(KERNEL_ARCHIVE)
tar -xf "$<"
# Helper function to get the correct kernel image name based on ARCH
KERNEL_IMAGE_NAME = $(if $(filter x86_64,$(ARCH)),bzImage,Image)
kernel: $(KERNEL_DIR)
@echo "--- Compiling the kernel for $(ARCH) ---"
# Always start with a clean state to prevent stale files from causing issues
@cd "$(KERNEL_DIR)" && make ARCH=$(ARCH) mrproper
# Create a default configuration if it doesn't exist
@cd "$(KERNEL_DIR)" && make ARCH=$(ARCH) defconfig
# Compile the kernel using GCC 12 and required flags
cd "$(KERNEL_DIR)" && make ARCH=$(ARCH) $(KERNEL_IMAGE_NAME) modules -j$(shell nproc) \
CC=gcc \
KBUILD_CFLAGS="-std=gnu11 -Wno-unterminated-string-initialization -Wno-error=attributes"
# Build all Go utilities using the main Makefile
utils:
@echo "--- Building Go utilities ---"
$(MAKE) -f Makefile
# Create a minimal root filesystem and copy utilities
rootfs: utils
@echo "--- Creating root filesystem ---"
rm -rf $(ROOTFS_DIR)
mkdir -p $(ROOTFS_DIR)/bin $(ROOTFS_DIR)/etc $(ROOTFS_DIR)/root $(ROOTFS_DIR)/dev $(ROOTFS_DIR)/proc $(ROOTFS_DIR)/sys
# Create necessary device nodes
mknod -m 622 $(ROOTFS_DIR)/dev/console c 5 1
mknod -m 666 $(ROOTFS_DIR)/dev/null c 1 3
# Copy all built binaries from bin/ to rootfs/bin
find bin -type f -executable -exec cp {} $(ROOTFS_DIR)/bin/ \;
# Set up dosu password and permissions
@if [ -z "$(DOSU_PASS)" ]; then \
echo "ERROR: DOSU_PASS must be set (e.g. make rootfs DOSU_PASS=yourpassword)"; \
exit 1; \
fi
echo -n "$(DOSU_PASS)" | sha256sum | awk '{print $$1}' > $(ROOTFS_DIR)/etc/dosu_passwd
chmod 600 $(ROOTFS_DIR)/etc/dosu_passwd
chown 0:0 $(ROOTFS_DIR)/etc/dosu_passwd
# Set dosu binary permissions
chown 0:0 $(ROOTFS_DIR)/bin/dosu || true
chmod u+s $(ROOTFS_DIR)/bin/dosu || true
# Prep emergency shell using built BusyBox
cp "$(BUSYBOX_DIR)/busybox" "$(ROOTFS_DIR)/bin/sh"
cp "$(GCC_DIR)/gcc" "$(ROOTFS_DIR)/bin/gcc"
cp "$(MAKE_DIR)/make" "$(ROOTFS_DIR)/bin/make"
mkdir -p "$(ROOTFS_DIR)/$(LIBS_DIR)"
cp -r $(LIBS_DIR)/* "$(ROOTFS_DIR)/$(LIBS_DIR)"
mkdir -p "$(ROOTFS_DIR)/$(H_DIR)"
cp -r $(H_DIR)/* "$(ROOTFS_DIR)/$(H_DIR)"
chmod +x $(ROOTFS_DIR)/bin/sh
chmod +x $(ROOTFS_DIR)/bin/gcc
chmod +x $(ROOTFS_DIR)/bin/make
# Create init script
echo '#!/bin/sh' > $(ROOTFS_DIR)/init
echo '# Basic init script for gutils Linux' >>$(ROOTFS_DIR)/init
echo 'mount -t proc none /proc' >>$(ROOTFS_DIR)/init
echo 'mount -t sysfs none /sys' >>$(ROOTFS_DIR)/init
echo 'mount -t devtmpfs none /dev' >>$(ROOTFS_DIR)/init
echo 'export PATH=/bin:/sbin:/usr/bin:/usr/sbin' >> $(ROOTFS_DIR)/init
echo 'export HOME=/root' >>$(ROOTFS_DIR)/init
echo 'export TERM=linux' >>$(ROOTFS_DIR)/init
echo 'echo "Booting gutils Linux..."' >>$(ROOTFS_DIR)/init
echo 'echo "Starting highway shell..."' >>$(ROOTFS_DIR)/init
echo 'exec /bin/highway || exec /bin/sh' >>$(ROOTFS_DIR)/init
chmod +x $(ROOTFS_DIR)/init
# Create an initramfs image
image: rootfs
@echo "--- Creating initramfs image ---"
cd $(ROOTFS_DIR) && find . | cpio -o -H newc | gzip > ../$(INITRAMFS)
clean:
@echo "--- Cleaning project ---"
# Use make mrproper for a thorough cleanup of the kernel source tree
-cd "$(KERNEL_DIR)" && make mrproper
rm -rf $(KERNEL_DIR) $(KERNEL_ARCHIVE) $(ROOTFS_DIR) bin $(INITRAMFS) $(ISO_DIR) $(ISO_IMAGE)
quickclean:
@echo "--- Quick cleaning rootfs and images ---"
rm -rf $(ROOTFS_DIR) $(INITRAMFS) $(ISO_DIR) $(ISO_IMAGE)
iso: image kernel
@echo "--- Creating bootable ISO image ---"
rm -rf $(ISO_DIR)
mkdir -p $(ISO_DIR)/boot/grub $(ISO_DIR)/EFI/BOOT
# Copy kernel and initramfs to ISO directory
cp "$(KERNEL_DIR)/arch/$(ARCH)/boot/$(KERNEL_IMAGE_NAME)" "$(ISO_DIR)/boot/vmlinuz"
cp "$(INITRAMFS)" "$(ISO_DIR)/boot/initramfs.cpio.gz"
# Create GRUB config
printf 'set default=0\nset timeout=5\n\nmenuentry "NebuLa" { \n\tlinux /boot/vmlinuz root=/dev/ram0 rw \n\tinitrd /boot/initramfs.cpio.gz \n}\n' > $(ISO_DIR)/boot/grub/grub.cfg
# Copy the GRUB EFI bootloader
cp /usr/lib/grub/$(ARCH)-efi/grubx64.efi $(ISO_DIR)/EFI/BOOT/
# Create a dummy grub.cfg for the EFI bootloader
cp $(ISO_DIR)/boot/grub/grub.cfg $(ISO_DIR)/EFI/BOOT/
# Create the ISO with grub-mkrescue
grub-mkrescue -o $(ISO_IMAGE) $(ISO_DIR) --xorriso=/usr/bin/xorriso --install-modules="linux normal efi_gop"