From 3fdba9f45e50d8bd82b622317d210fca0dd5b101 Mon Sep 17 00:00:00 2001 From: Ivan Davidov Date: Sun, 13 Sep 2015 16:04:01 +0300 Subject: [PATCH] Added experimental scripts which build Kernel + ToyBox with simple DHCP network. DNS is still broken due to glibc issues. --- src/experimental/.config | 12 ++++ src/experimental/0_prepare.sh | 8 +++ src/experimental/1_get_kernel.sh | 24 +++++++ src/experimental/2_build_kernel.sh | 23 +++++++ src/experimental/3_get_toybox.sh | 24 +++++++ src/experimental/4_build_toybox.sh | 29 ++++++++ src/experimental/5_generate_rootfs.sh | 71 ++++++++++++++++++++ src/experimental/6_pack_rootfs.sh | 14 ++++ src/experimental/7_generate_iso.sh | 19 ++++++ src/experimental/build_minimal_linux_live.sh | 10 +++ src/experimental/qemu32.sh | 4 ++ src/experimental/qemu64.sh | 4 ++ 12 files changed, 242 insertions(+) create mode 100644 src/experimental/.config create mode 100755 src/experimental/0_prepare.sh create mode 100755 src/experimental/1_get_kernel.sh create mode 100755 src/experimental/2_build_kernel.sh create mode 100755 src/experimental/3_get_toybox.sh create mode 100755 src/experimental/4_build_toybox.sh create mode 100755 src/experimental/5_generate_rootfs.sh create mode 100755 src/experimental/6_pack_rootfs.sh create mode 100755 src/experimental/7_generate_iso.sh create mode 100755 src/experimental/build_minimal_linux_live.sh create mode 100755 src/experimental/qemu32.sh create mode 100755 src/experimental/qemu64.sh diff --git a/src/experimental/.config b/src/experimental/.config new file mode 100644 index 000000000..a082ead59 --- /dev/null +++ b/src/experimental/.config @@ -0,0 +1,12 @@ +# You can find the latest Linux kernel source bundles here: +# +# http://kernel.org +# +KERNEL_SOURCE_URL=https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.1.6.tar.xz + +# You can find the latest BusyBox source bundles here: +# +# http://busybox.net +# +TOYBOX_SOURCE_URL=http://landley.net/toybox/downloads/toybox-0.6.0.tar.gz + diff --git a/src/experimental/0_prepare.sh b/src/experimental/0_prepare.sh new file mode 100755 index 000000000..b1f8e4ed3 --- /dev/null +++ b/src/experimental/0_prepare.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +rm -rf work +mkdir work + +# -p stops errors if the directory already exists +mkdir -p source + diff --git a/src/experimental/1_get_kernel.sh b/src/experimental/1_get_kernel.sh new file mode 100755 index 000000000..0d79b79a1 --- /dev/null +++ b/src/experimental/1_get_kernel.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# Grab everything after the '=' character +DOWNLOAD_URL=$(grep -i KERNEL_SOURCE_URL .config | cut -f2 -d'=') + +# Grab everything after the last '/' character +ARCHIVE_FILE=${DOWNLOAD_URL##*/} + +cd source + +# Downloading kernel file +# -c option allows the download to resume +wget -c $DOWNLOAD_URL + +# Delete folder with previously extracted kernel +rm -rf ../work/kernel +mkdir ../work/kernel + +# Extract kernel to folder 'work/kernel' +# Full path will be something like 'work/kernel/linux-3.16.1' +tar -xvf $ARCHIVE_FILE -C ../work/kernel + +cd .. + diff --git a/src/experimental/2_build_kernel.sh b/src/experimental/2_build_kernel.sh new file mode 100755 index 000000000..1734a0270 --- /dev/null +++ b/src/experimental/2_build_kernel.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +cd work/kernel + +# Change to the first directory ls finds, e.g. 'linux-3.18.6' +cd $(ls -d *) + +# Cleans up the kernel sources, including configuration files +make mrproper + +# Create a default configuration file for the kernel +make defconfig + +# Changes the name of the system +sed -i "s/.*CONFIG_DEFAULT_HOSTNAME.*/CONFIG_DEFAULT_HOSTNAME=\"minimal\"/" .config + +# Compile the kernel with optimization for "parallel jobs" = "number of processors" +# Good explanation of the different kernels +# http://unix.stackexchange.com/questions/5518/what-is-the-difference-between-the-following-kernel-makefile-terms-vmlinux-vmlinux +make bzImage -j $(grep ^processor /proc/cpuinfo | wc -l) + +cd ../../.. + diff --git a/src/experimental/3_get_toybox.sh b/src/experimental/3_get_toybox.sh new file mode 100755 index 000000000..26eda6591 --- /dev/null +++ b/src/experimental/3_get_toybox.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# Grab everything after the '=' character +DOWNLOAD_URL=$(grep -i TOYBOX_SOURCE_URL .config | cut -f2 -d'=') + +# Grab everything after the last '/' character +ARCHIVE_FILE=${DOWNLOAD_URL##*/} + +cd source + +# Downloading toybox source +# -c option allows the download to resume +wget -c $DOWNLOAD_URL + +# Delete folder with previously extracted toybox +rm -rf ../work/toybox +mkdir ../work/toybox + +# Extract toybox to folder 'toybox' +# Full path will be something like 'work/toybox/toybox-0.6.0' +tar -xvf $ARCHIVE_FILE -C ../work/toybox + +cd .. + diff --git a/src/experimental/4_build_toybox.sh b/src/experimental/4_build_toybox.sh new file mode 100755 index 000000000..2367057ea --- /dev/null +++ b/src/experimental/4_build_toybox.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +cd work/toybox + +# Change to the first directory ls finds, e.g. 'toybox-0.6.0' +cd $(ls -d *) + +# Remove previously generated artefacts +make distclean + +# Create a default configuration file +make allyesconfig + +export LDFLAGS="--static" + +# Compile toybox with optimization for "parallel jobs" = "number of processors" +make toybox -j $(grep ^processor /proc/cpuinfo | wc -l) + +unset LDFLAGS + +rm -rf rootfs +mkdir rootfs +export PREFIX=rootfs +# Create the symlinks for toybox +make install_flat +unset PREFIX + +cd ../../.. + diff --git a/src/experimental/5_generate_rootfs.sh b/src/experimental/5_generate_rootfs.sh new file mode 100755 index 000000000..70a80781e --- /dev/null +++ b/src/experimental/5_generate_rootfs.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +cd work + +rm -rf rootfs +mkdir rootfs + +cd toybox +cd $(ls -d *) + +# Copy all toybox generated stuff to the location of our "initramfs" folder. +cp -R rootfs ../../rootfs/bin +cd ../../rootfs + +# Create root FS folders +mkdir dev +mkdir etc +mkdir proc +mkdir root +mkdir src +mkdir sys +mkdir tmp + +# "1" means that only the owner of a file/directory (or root) can remove it. +chmod 1777 tmp + +cd etc + +# The file "/etc/welcome.txt" is displayed on every boot of the system in each +# available terminal. +cat > welcome.txt << EOF + + ##################################### + # # + # Welcome to "Minimal Linux Live" # + # # + ##################################### + +EOF + +cd .. + +# Problem setting default path +cat > init << EOF +#!/bin/sh +dmesg -n 1 + +mount -t devtmpfs none /dev +mount -t proc none /proc +mount -t sysfs none /sys + +ifconfig eth0 up +dhcp -i eth0 + +cat /etc/welcome.txt + +sh + +poweroff +EOF + +chmod +rx init + +# Copy all source files to "/src". Note that the scripts won't work there. +cp ../../*.sh src +cp ../../.config src +chmod +r src/*.sh +chmod +r src/.config + +cd ../.. + diff --git a/src/experimental/6_pack_rootfs.sh b/src/experimental/6_pack_rootfs.sh new file mode 100755 index 000000000..f9e16e523 --- /dev/null +++ b/src/experimental/6_pack_rootfs.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +cd work + +# Remove the old initramfs archive if it exists. +rm -f rootfs.cpio.gz + +cd rootfs + +# Packs the current folder structure in "cpio.gz" archive. +find . | cpio -H newc -o | gzip > ../rootfs.cpio.gz + +cd ../.. + diff --git a/src/experimental/7_generate_iso.sh b/src/experimental/7_generate_iso.sh new file mode 100755 index 000000000..38c951585 --- /dev/null +++ b/src/experimental/7_generate_iso.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +rm -f minimal_linux_live.iso + +cd work/kernel +cd $(ls -d *) + +# Edit Makefile to look for genisoimage instead of mkisofs. This was added as a +# workaround for some "Debian" and "Arch Linux" distributions. In general this +# fix should be harmless. +sed -i 's/mkisofs/genisoimage/g' arch/x86/boot/Makefile + +# Generate the ISO image with optimization for "parallel jobs" = "number of processors" +make isoimage FDINITRD=../../rootfs.cpio.gz -j $(grep ^processor /proc/cpuinfo | wc -l) + +cp arch/x86/boot/image.iso ../../../minimal_linux_live.iso + +cd ../../.. + diff --git a/src/experimental/build_minimal_linux_live.sh b/src/experimental/build_minimal_linux_live.sh new file mode 100755 index 000000000..01644c044 --- /dev/null +++ b/src/experimental/build_minimal_linux_live.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +sh 0_prepare.sh +sh 1_get_kernel.sh +sh 2_build_kernel.sh +sh 3_get_busybox.sh +sh 4_build_busybox.sh +sh 5_generate_rootfs.sh +sh 6_pack_rootfs.sh +sh 7_generate_iso.sh diff --git a/src/experimental/qemu32.sh b/src/experimental/qemu32.sh new file mode 100755 index 000000000..2e4d546c4 --- /dev/null +++ b/src/experimental/qemu32.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +qemu-system-i386 -cdrom minimal_linux_live.iso + diff --git a/src/experimental/qemu64.sh b/src/experimental/qemu64.sh new file mode 100755 index 000000000..8e5174a01 --- /dev/null +++ b/src/experimental/qemu64.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +qemu-system-x86_64 -cdrom minimal_linux_live.iso +