make: kernel image, rootfs image
authorNikita Shubin <maquefel@gmail.com>
Sat, 27 Jul 2019 11:47:07 +0000 (14:47 +0300)
committerNikita Shubin <maquefel@gmail.com>
Thu, 1 Aug 2019 12:53:54 +0000 (15:53 +0300)
making:

- minimal linux kernel image
- minimal (busybox only) rootfs
- packing rootfs to initramfs image

Makefile

index fc438e6b71a633f44b299b25b9db30bc9b0d090d..a3b8703482ed95c5b1aa0e35ac7d1d229fcc0ea6 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -19,24 +19,122 @@ endif
 endif
 
 KERNEL_TREE ?= ${CURDIR}/linux
+SYSROOT ?= ${CURDIR}/initramfs
 
-build-kernel/arch/x86/configs:
+${SYSROOT}:
+       mkdir -p $@
+
+${SYSROOT}/.mount-stamp:       | ${SYSROOT}
+       touch $@
+
+.PHONY: world
+
+world: ${SYSROOT}/bin/busybox \
+       ${SYSROOT}/etc/group \
+       ${SYSROOT}/etc/passwd \
+       ${SYSROOT}/etc/inittab \
+       ${SYSROOT}/init \
+       ${SYSROOT}/loginroot \
+       build-linux/arch/x86_64/boot/bzImage \
+       initramfs.cpio.xz
+
+# --- kernel
+
+build-linux/arch/x86/configs:
        mkdir -p $@
 
 .PHONY: kernel
 
-build-kernel/arch/x86/configs/x86_64_qemu_defconfig: | build-kernel/arch/x86/configs configs/x86_64_qemu_defconfig
+build-linux/arch/x86/configs/x86_64_qemu_defconfig: | build-linux/arch/x86/configs configs/x86_64_qemu_defconfig
        cp configs/x86_64_qemu_defconfig $@
 
-build-kernel/.config:   | build-kernel/arch/x86/configs/x86_64_qemu_defconfig
-       make ARCH=${TARGET_ARCH} -C ${KERNEL_TREE} O=${CURDIR}/build-kernel x86_64_qemu_defconfig
+build-linux/.config:   | build-linux/arch/x86/configs/x86_64_qemu_defconfig
+       make ARCH=${TARGET_ARCH} -C ${KERNEL_TREE} O=${CURDIR}/build-linux x86_64_qemu_defconfig
 
-kernel: build-kernel/.config
-       make ${PARALLEL} -C ${KERNEL_TREE} O=${CURDIR}/build-kernel ARCH=${TARGET_ARCH}
+build-linux/arch/x86_64/boot/bzImage: build-linux/.config
+       make ${PARALLEL} -C build-linux ARCH=${TARGET_ARCH}
+       make ${PARALLEL} -C build-linux ARCH=${TARGET_ARCH} INSTALL_MOD_PATH=${SYSROOT} modules_install
 
 clean::
-       -make ${PARALLEL} -C build-kernel clean
+       -make ${PARALLEL} -C build-linux clean
        -make ${PARALLEL} -C ${KERNEL_TREE} mrproper
 
 distclean::
-       -rm -rf build-kernel
+       -rm -rf build-linux
+
+# --- initramfs
+
+CREATE_DIRS := \
+        /dev \
+        /dev/pts \
+        /boot \
+        /etc \
+        /home \
+        /mnt \
+        /opt \
+        /proc \
+        /root \
+        /srv \
+        /sys \
+        /usr \
+        /var \
+        /var/log \
+        /run \
+        /tmp \
+        /lib
+
+$(patsubst %,${SYSROOT}%,${CREATE_DIRS}):       ${SYSROOT}/.mount-stamp
+       install -d -m 0755 $@
+
+.PHONY:        populate-dirs
+
+populate-dirs: | $(patsubst %,${SYSROOT}%,${CREATE_DIRS})
+
+${SYSROOT}/etc/passwd: etc/passwd ${SYSROOT}/.mount-stamp | ${SYSROOT}/etc
+       install -m 644 $< $@
+
+${SYSROOT}/etc/group:  etc/group ${SYSROOT}/.mount-stamp | ${SYSROOT}/etc
+       install -m 644 $< $@
+
+${SYSROOT}/etc/inittab:        etc/inittab ${SYSROOT}/.mount-stamp | ${SYSROOT}/etc
+       install -m 644 $< $@
+
+${SYSROOT}/init:       scripts/init | ${SYSROOT}
+       install -m 755 $< $@
+
+${SYSROOT}/loginroot:  scripts/loginroot | ${SYSROOT}
+       install -m 755 $< $@
+
+distclean::
+       rm -rf ${SYSROOT}
+
+# --- busybox
+
+build-busybox:
+       mkdir $@
+
+busybox/configs/qemu_defconfig:        configs/busybox_config
+       cp $< $@
+
+build-busybox/.config: busybox/configs/qemu_defconfig | build-busybox
+       make -C busybox O=../build-busybox ARCH=${TARGET_ARCH} qemu_defconfig
+
+build-busybox/busybox: build-busybox/.config
+       make ${PARALLEL} -C build-busybox ARCH=${TARGET_ARCH}
+
+${SYSROOT}/bin/busybox:        build-busybox/busybox | populate-dirs
+       make ${PARALLEL} -C build-busybox ARCH=${TARGET_ARCH} CONFIG_PREFIX=${SYSROOT} install
+       rm -rf ${SYSROOT}/linuxrc
+
+.PHONY: .install-busybox
+
+.install-busybox : ${SYSROOT}/bin/busybox
+
+clean::
+       make -C build-busybox ARCH=${TARGET_ARCH} clean
+
+distclean::
+       rm -rf build-busybox
+
+initramfs.cpio.xz: ${SYSROOT}/bin/busybox ${SYSROOT}/loginroot ${SYSROOT}/init ${SYSROOT}/etc/inittab ${SYSROOT}/etc/group ${SYSROOT}/etc/passwd
+       (cd ${SYSROOT} && find . -print0 | cpio --null -ov --format=newc | xz -C crc32 > ../initramfs.cpio.xz)