From 7ba6c0a44a44317993cb032cdf0caefbfa08d055 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Tue, 19 May 2015 11:06:54 +0200 Subject: [PATCH] can generate initramfs from busybox Signed-off-by: Nico Schottelius --- hacking/add_kernel_isolinux.sh | 29 --------- .../arch_bootstrap.sh | 0 .../debian_bootstrap.sh | 0 .../file_list_of_packages.sh | 0 .../filelist_from_package.sh | 0 .../filelist_to_dir.sh | 0 hacking/v3-busybox/add_kernel_isolinux.sh | 24 ++++++++ hacking/v3-busybox/all.sh | 8 +++ hacking/v3-busybox/generate.sh | 27 +++++++++ hacking/v3-busybox/init | 60 +++++++++++++++++++ 10 files changed, 119 insertions(+), 29 deletions(-) delete mode 100755 hacking/add_kernel_isolinux.sh rename hacking/{ => v1-debootstrap-pacstrap}/arch_bootstrap.sh (100%) rename hacking/{ => v1-debootstrap-pacstrap}/debian_bootstrap.sh (100%) rename hacking/{archlinux => v2-initramfs-from-os}/file_list_of_packages.sh (100%) rename hacking/{ => v2-initramfs-from-os}/filelist_from_package.sh (100%) rename hacking/{ => v2-initramfs-from-os}/filelist_to_dir.sh (100%) create mode 100755 hacking/v3-busybox/add_kernel_isolinux.sh create mode 100755 hacking/v3-busybox/all.sh create mode 100755 hacking/v3-busybox/generate.sh create mode 100644 hacking/v3-busybox/init diff --git a/hacking/add_kernel_isolinux.sh b/hacking/add_kernel_isolinux.sh deleted file mode 100755 index ec7b610c..00000000 --- a/hacking/add_kernel_isolinux.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -# FIXME: Write cdist type / explorer that finds -# package for a file, distro independent - -if [ "$#" -ne 1 ]; then - echo "$0 dir-out" - exit 1 -fi - -dir=$1; shift -boot=$dir/boot - -mkdir -p "$boot" -cp /boot/vmlinuz-linux \ - /boot/initramfs-linux-fallback.img \ - /usr/lib/syslinux/bios/isolinux.bin \ - "$boot" - -cp /usr/lib/syslinux/bios/ldlinux.c32 \ - "$dir" - -cat > "$dir/isolinux.cfg" << eof -default preos -label preos -title cdist PreOS -linux /boot/vmlinuz-linux -initrd /boot/initramfs-linux-fallback.img -eof diff --git a/hacking/arch_bootstrap.sh b/hacking/v1-debootstrap-pacstrap/arch_bootstrap.sh similarity index 100% rename from hacking/arch_bootstrap.sh rename to hacking/v1-debootstrap-pacstrap/arch_bootstrap.sh diff --git a/hacking/debian_bootstrap.sh b/hacking/v1-debootstrap-pacstrap/debian_bootstrap.sh similarity index 100% rename from hacking/debian_bootstrap.sh rename to hacking/v1-debootstrap-pacstrap/debian_bootstrap.sh diff --git a/hacking/archlinux/file_list_of_packages.sh b/hacking/v2-initramfs-from-os/file_list_of_packages.sh similarity index 100% rename from hacking/archlinux/file_list_of_packages.sh rename to hacking/v2-initramfs-from-os/file_list_of_packages.sh diff --git a/hacking/filelist_from_package.sh b/hacking/v2-initramfs-from-os/filelist_from_package.sh similarity index 100% rename from hacking/filelist_from_package.sh rename to hacking/v2-initramfs-from-os/filelist_from_package.sh diff --git a/hacking/filelist_to_dir.sh b/hacking/v2-initramfs-from-os/filelist_to_dir.sh similarity index 100% rename from hacking/filelist_to_dir.sh rename to hacking/v2-initramfs-from-os/filelist_to_dir.sh diff --git a/hacking/v3-busybox/add_kernel_isolinux.sh b/hacking/v3-busybox/add_kernel_isolinux.sh new file mode 100755 index 00000000..ac5d495b --- /dev/null +++ b/hacking/v3-busybox/add_kernel_isolinux.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# FIXME: distro specific kernel location + +if [ "$#" -ne 1 ]; then + echo "$0 dir-out" + exit 1 +fi + +dir=$1; shift +boot=$dir/boot + +mkdir -p "$boot" +cp /boot/vmlinuz-linux "$boot/linux" +cp /usr/lib/syslinux/bios/isolinux.bin "$boot" +cp /usr/lib/syslinux/bios/ldlinux.c32 "$dir" + +cat > "$dir/isolinux.cfg" << eof +default preos +label preos +title cdist PreOS +linux /boot/linux +initrd /boot/initramfs +eof diff --git a/hacking/v3-busybox/all.sh b/hacking/v3-busybox/all.sh new file mode 100755 index 00000000..51eac9af --- /dev/null +++ b/hacking/v3-busybox/all.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +rm -rf preos +mkdir -p preos/boot + +./generate.sh > preos/boot/initramfs +./add_kernel_isolinux.sh preos + diff --git a/hacking/v3-busybox/generate.sh b/hacking/v3-busybox/generate.sh new file mode 100755 index 00000000..00227a57 --- /dev/null +++ b/hacking/v3-busybox/generate.sh @@ -0,0 +1,27 @@ +#!/bin/sh +set -ex + + +initramfs_dir=$(mktemp -d /tmp/cdist-preos.XXXXXXX) +# initramfs_dir=$1 + +for dir in bin sbin etc proc sys newroot; do + mkdir -p ${initramfs_dir}/$dir +done +touch ${initramfs_dir}/etc/mdev.conf + +cp init "${initramfs_dir}/init" +cp $(which busybox) "${initramfs_dir}/bin" +ln -fs busybox "${initramfs_dir}/bin/sh" + +cd "${initramfs_dir}" +find . | cpio -H newc -o | gzip + +exit 0 + +# TODO: +# - Kernel modules +# - ssh +# - various mkfs +# - libs + diff --git a/hacking/v3-busybox/init b/hacking/v3-busybox/init new file mode 100644 index 00000000..40507339 --- /dev/null +++ b/hacking/v3-busybox/init @@ -0,0 +1,60 @@ +#!/bin/sh + +#Mount things needed by this script +mount -t proc proc /proc +mount -t sysfs sysfs /sys + +#Disable kernel messages from popping onto the screen +echo 0 > /proc/sys/kernel/printk + +#Create all the symlinks to /bin/busybox +busybox --install -s + +#Create device nodes +mknod /dev/null c 1 3 +mknod /dev/tty c 5 0 +mdev -s + +#Function for parsing command line options with "=" in them +# get_opt("init=/sbin/init") will return "/sbin/init" +get_opt() { + echo "$@" | cut -d "=" -f 2 +} + +#Defaults +init="/sbin/init" +root="/dev/hda1" + +#Process command line options +for i in $(cat /proc/cmdline); do + case $i in + root\=*) + root=$(get_opt $i) + ;; + init\=*) + init=$(get_opt $i) + ;; + esac +done + + +exec sh + +# Skipping the rest + +#Mount the root device +mount "${root}" /newroot + +#Check if $init exists and is executable +if [[ -x "/newroot/${init}" ]] ; then + #Unmount all other mounts so that the ram used by + #the initramfs can be cleared after switch_root + umount /sys /proc + + #Switch to the new root and execute init + exec switch_root /newroot "${init}" +fi + +#This will only be run if the exec above failed +echo "Failed to switch_root, dropping to a shell" +exec sh