diff options
author | Aaron Ball <nullspoon@iohq.net> | 2016-01-08 14:39:53 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2016-01-25 20:47:25 -0700 |
commit | 544246217a49ce66480691a2312c90062d613868 (patch) | |
tree | a42447eec6aa7cb00fc9d1450812b86cc8414bc4 /mkinitramfs | |
download | mkinitramfs-544246217a49ce66480691a2312c90062d613868.tar.gz mkinitramfs-544246217a49ce66480691a2312c90062d613868.tar.xz |
Initial commit
Mostly functioning, but missing a few binaries and libraries
required to boot a system's kernel.
Diffstat (limited to 'mkinitramfs')
-rwxr-xr-x | mkinitramfs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/mkinitramfs b/mkinitramfs new file mode 100755 index 0000000..e11adf7 --- /dev/null +++ b/mkinitramfs @@ -0,0 +1,103 @@ +#!/usr/bin/env bash + +function get_bin_deps { + [[ -z ${1} ]] && echo 'Binary path required.\n' & exit 1 + + ldd ${1} +} + + +# +# Gets first path locatable in a string +# +function get_first_path { + local str=${1} + + local out='' + local cursor='' + + for ((i=0; i<${#str}; i++)); do + if [[ ${str:$i:1} == '/' ]]; then + cursor=${i} + break + fi + done + + # Exit if no path found + [[ ${cursor} == '' ]] && return + + while [[ ${str:$cursor:1} != ' ' ]] && [[ ${cursor} -lt ${#str} ]]; do + out="${out}${str:$cursor:1}" + cursor=$((${cursor} + 1)) + done + + echo "${out}" +} + + +function get_deps { + local bin=${1} + + for dep in $(ldd ${bin}); do + get_first_path "${dep}" + done +} + + +function mkcpio { + [[ -z ${1} ]] && echo "Initramfs cache path required." && exit 1 + + cd ${1} + fsname="initramfs-$(uname -m)-$(uname -r).cpio.xz" + find . -print0 | cpio --null -ov --format=newc | xz -v -9 -c > ../${fsname} +} + + +function main { + local cache='initramfs' + + # Clean the cache so we start fresh + rm -rf ${cache} && mkdir ${cache} + + # List of binaries to exist in the new initramfs + local bins=( + /bin/bash + #/bin/sh + /sbin/cryptsetup + /bin/chmod + /bin/chown + /usr/bin/vi + /usr/bin/less + #/bin/ps + #/sbin/shutdown + /sbin/switch_root + ) + + # # Create the temporary directory structure + mkdir -p ./${cache}/{bin,dev,etc,lib,lib64,mnt/root,proc,root,sbin,sys,usr,usr/lib,usr/lib64,usr/lib32,usr/bin} + + # # Copy in live system devices + # cp -a /dev/{null,console,tty,sda1} ./${cache}/dev/ + + # Copy binary and dependencies locally + for bin in ${bins[@]}; do + bindirname=$(dirname ${bin}) + # Copy the binary of interest + cp -u ${bin} "${cache}/${bindirname}/" + + deps=$(get_deps ${bin}) + # Copy each of the binary's deps + for dep in ${deps[@]}; do + local depdirname=$(dirname ${dep}) + cp -u ${dep} "${cache}/${depdirname}/" + done + done + + cp init ${cache}/init && chmod +x ${cache}/init + + + mkcpio ${cache} +} + + +main ${@} |