diff --git a/mkinitramfs-ll.bash b/mkinitramfs-ll.bash index 3a3ca08..d12283d 100755 --- a/mkinitramfs-ll.bash +++ b/mkinitramfs-ll.bash @@ -13,7 +13,7 @@ PKG=( [version]=0.16.0 ) -# @DESCRIPTION: print usages message +# @FUNCTION: Print help message function usage { cat <<-EOH ${PKG[name]}.${PKG[shell]} version ${PKG[version]} @@ -50,26 +50,26 @@ EOH exit $? } -# @DESCRIPTION: print error message to stdout +# @FUNCTION: Print error message to stdout function error { echo -ne " \e[1;31m* \e[0m${PKG[name]}.${PKG[shell]}: $@\n" >&2 } -# @DESCRIPTION: print info message to stdout +# @FUNCTION: Print info message to stdout function info { echo -ne " \e[1;32m* \e[0m${PKG[name]}.${PKG[shell]}: $@\n" } -# @DESCRIPTION: print warning message to stdout +# @FUNCPTION: Print warning message to stdout function warn { echo -ne " \e[1;33m* \e[0m${PKG[name]}.${PKG[shell]}: $@\n" >&2 } -# @DESCRIPTION: call error() to print error message before exiting +# @FUNCPTION: Fatal error helper function die { local ret=$? error "$@" exit $ret } -# @DESCRIPTION: make tmp dir or file in ${TMPDIR:-/tmp} +# @FUNCTION: Temporary dir/file helper # @ARG: -d|-f [-m ] [-o ] [-g ] TEMPLATE function mktmp { local tmp=${TMPDIR:-/tmp}/$1-XXXXXX @@ -77,7 +77,7 @@ function mktmp { echo "$tmp" } -# @DESCRIPTION: add the essential nodes to be able to boot +# @FUNCTION: Device nodes (helper) function donod { pushd dev || die [[ -c console ]] || mknod -m 600 console c 5 1 || die @@ -108,8 +108,7 @@ opt=( opt=($(getopt "${opt[@]}" -- "$@" || usage)) eval set -- "${opt[@]}" -# @DESCRIPTION: declare if not declared while arsing options, -# hold almost every single option/variable +# @VARIABLE: Associative Array holding (almost) every options declare -A opts for (( ; $# > 0; )); do @@ -163,36 +162,36 @@ done source "${PKG[name]}".conf || die "no ${PKG[name]}.conf found" -# @DESCRIPTION: kernel version to pick up +# @VARIABLE: Kernel version if [[ -z "${opts[-kv]}" ]]; then [[ "${opts[-k]}" ]] && opts[-kv]="${opts[-k]}" || opts[-kv]="$(uname -r)" fi -# @DESCRIPTION: initramfs prefx name <$prefix-$kv.$ext> +# @VARIABLE: Initramfs prefx if [[ -z "${opts[-prefix]}" ]]; then [[ "${opts[-p]}" ]] && opts[-prefix]="${opts[-p]}" || opts[-prefix]=initramfs- fi -# @DESCRIPTION: usr dir path, to get extra files +# @VARIABLE: USRDIR path to use if [[ -z "${opts[-usrdir]}" ]]; then [[ "${opts[-u]}" ]] && opts[-usrdir]="${opts[-u]}" || opts[-usrdir]="${PWD}"/usr fi -# @DESCRIPTION: full to initramfs compressed image +# @VARIABLE: Full path to initramfs image opts[-initramfs]=${opts[-prefix]}${opts[-kv]} if [[ -z "${opts[-compressor]}" ]]; then [[ "${opts[-c]}" ]] && opts[-compressor]="${opts[-c]}" || opts[-compressor]="xz -9 --check=crc32" fi -# @DESCRIPTION: kernel architecture +# @VARIABLE: Kernel architecture [[ "${opts[-arch]}" ]] || opts[-arch]=$(uname -m) -# @DESCRIPTION: kernel bit lenght supported +# @VARIABLE: Kernel bit lenght [[ "${opts[-arc]}" ]] || opts[-arc]=$(getconf LONG_BIT) -# @DESCRIPTION: tmp dir where to generate initramfs -# an initramfs compressed image +# @VARIABLE: (initramfs) Tmporary directory opts[-tmpdir]="$(mktmp ${opts[-initramfs]})" -# @DESCRIPTION: configuration directory +# @DESCRIPTION: (initramfs) Configuration directory opts[-confdir]="etc/${PKG[name]}" +# Set up compression declare -a compressor compressor=(bzip2 gzip lzip lzop lz4 xz) @@ -227,7 +226,8 @@ if [[ "${config}" ]]; then unset config xgrep CONFIG COMP compressor fi -# @DESCRIPTION: generate an initramfs image +# @FUNCTION: CPIO image builder +# @ARG: function docpio { local ext=.cpio initramfs=${1:-/boot/${opts[-initramfs]}} local cmd="find . -print0 | cpio -0 -ov -Hnewc" @@ -264,6 +264,7 @@ else rm -fr * fi +# Set up the initramfs if [[ -d "${opts[-usrdir]}" ]]; then cp -ar "${opts[-usrdir]}" . && mv -f {usr/,}root && @@ -302,6 +303,7 @@ cp -a "${opts[-usrdir]}"/../init . && chmod 775 init || die cp -af {/,}lib/modules/${opts[-kv]}/modules.dep || die "failed to copy modules.dep" +# Set up (requested) firmware if [[ "${opts[-F]}" ]] || [[ "${opts[-firmware]}" ]]; then if [[ "${opts[-F]}" == : ]] || [[ "${opts[-firmware]}" == : ]]; then warn "Adding the whole firmware directory" @@ -317,12 +319,14 @@ if [[ "${opts[-F]}" ]] || [[ "${opts[-firmware]}" ]]; then unset firmware fi +# Set up RAID option for bin in dmraid mdadm zfs; do [[ "${opts[-bin]/$bin}" != "${opts[bin]}" ]] || [[ "${opts[-b]/$bin}" != "${opts[-b]}" ]] && opts[-mgrp]+=":$bin" done opts[-mgrp]=${opts[-mgrp]/mdadm/raid} +# Set up (requested) hook for hook in ${opts[-H]//:/ } ${opts[-hook]//:/ }; do for file in ${opts[-usrdir]}/../hooks/*${hook}*; do cp -a "${file}" lib/${PKG[name]}/ @@ -334,6 +338,7 @@ done [[ -f /etc/issue.logo ]] && cp {/,}etc/issue.logo +# Handle & copy BusyBox binary if [[ -x usr/bin/busybox ]]; then mv -f {usr/,}bin/busybox elif type -p busybox >/dev/null; then @@ -348,7 +353,6 @@ elif type -p busybox >/dev/null; then else die "no busybox binary found" fi - if [[ ! -f ${opts[-confdir]}/busybox.applets ]]; then bin/busybox --list-full >${opts[-confdir]}/busybox.applets || die fi @@ -364,6 +368,7 @@ for bin in $(grep '^sbin' ${opts[-confdir]}/busybox.applets); do ln -s ../bin/busybox ${bin} done +# Set up a few options if [[ "${opts[-L]}" ]] || [[ "${opts[-luks]}" ]]; then opts[-bin]+=:cryptsetup opts[-mgrp]+=:dm-crypt fi @@ -383,7 +388,8 @@ if [[ "${opts[-l]}" ]] || [[ "${opts[-lvm]}" ]]; then opts[-bin]+=:lvm opts[-mgrp]+=:device-mapper fi -# @DESCRIPTION: copy kernel module +# @FUNCTION: Kernel module copy helper +# @ARG: [-v|--verbose] function domod { case $1 in (-v|--verbose) @@ -416,7 +422,8 @@ function domod { return ${ret} } -declare -a KEYMAP +# Handle & copy keymap/consolefont +declare -a FONT KEYMAP for keymap in ${opts[-y]//:/ } ${opts[-keymap]//:/ }; do if [[ -f usr/share/keymaps/${keymap}-${opts[-arch]}.bin ]]; then continue @@ -429,9 +436,7 @@ for keymap in ${opts[-y]//:/ } ${opts[-keymap]//:/ }; do (( $? == 0 )) && KEYMAP+=(${keymap}-${opts[-arch]}.bin) done echo "${KEYMAP[0]}" >${opts[-confdir]}/kmap -unset KEYMAP keymap -declare -a FONT for font in ${opts[-f]//:/ } ${opts[-font]//:/ }; do if [[ -f usr/share/consolefonts/${font} ]]; then :; elif [[ -f ${font} ]]; then @@ -448,8 +453,9 @@ for font in ${opts[-f]//:/ } ${opts[-font]//:/ }; do (( $? == 0 )) && FONT+=(${font}) done echo "${FONT[0]}" >${opts[-confdir]}/font -unset FONT font +unset FONT font KEYMAP keymap +# Handle & copy splash themes if [[ "${opts[-s]}" ]] || [[ "${opts[-splash]}" ]]; then opts[-bin]+=:splash_util.static:fbcondecor_helper @@ -468,7 +474,8 @@ if [[ "${opts[-s]}" ]] || [[ "${opts[-splash]}" ]]; then done fi -# @DESCRIPTION: follow and copy link until binary/library is copied +# @FUNCTION: Binary/Library copy helper (handle symlink) +# @ARG: function docp { local link=${1} prefix [[ -n ${link} ]] || return @@ -484,7 +491,8 @@ function docp { done return 0 } -# @DESCRIPTION: copy binary with libraries if not static +# @FUNCTION: (static/dynamic) Binary copy helper +# @ARG: function dobin { local bin=$1 lib docp ${bin} || return @@ -496,6 +504,7 @@ function dobin { done } +# Handle & copy binaries for bin in ${opts[-b]//:/ } ${opts[-bin]//:/ }; do for b in {usr/,}{,s}bin/${bin}; do [ -x ${b} -a ! -h ${b} ] && continue 2 @@ -505,9 +514,9 @@ for bin in ${opts[-b]//:/ } ${opts[-bin]//:/ }; do done unset binary bin b +# Handle & copy kernel module domod ${opts[-m]//:/ } ${opts[-kmod]//:/ } -# Remove module group name from boot group before processing module groups for mod in ${opts[-mboot]//:/ }; do if [[ "${opts[-m$mod]}" ]]; then echo "${mod}" >> ${opts[-confdir]}/boot @@ -528,6 +537,7 @@ for (( i=0; i < ${#env[@]}; i++ )); do done unset env +# Handle GCC libraries symlinks [[ -d usr/lib/gcc ]] && for lib in $(find usr/lib/gcc -iname 'lib*'); do ln -fs /$lib lib/${lib##*/} diff --git a/mkinitramfs-ll.zsh b/mkinitramfs-ll.zsh index 4696b19..f851853 100755 --- a/mkinitramfs-ll.zsh +++ b/mkinitramfs-ll.zsh @@ -13,7 +13,7 @@ PKG=( version 0.16.0 ) -# @DESCRIPTION: print usages message +# @FUNCTION: Print help message function usage { cat <<-EOF ${PKG[name]}.${PKG[shell]} version ${PKG[version]} @@ -50,34 +50,34 @@ EOF exit $? } -# @DESCRIPTION: print error message to stdout +# @FUNCTION: Print error message to stdout function error { print -P " %B%F{red}*%b %1x: %F{yellow}%U%I%u%f: $@" >&2 } -# @DESCRIPTION: print info message to stdout +# @FUNCTION: Print info message to stdout function info { print -P " %B%F{green}*%b%f %1x: $@" } -# @DESCRIPTION: print warning message to stdout +# @FUNCTION: Print warning message to stdout function warn { print -P " %B%F{red}*%b%f %1x: $@" >&2 } -# @DESCRIPTION: call error() to print error message before exiting +# @FUNCTION: Fatal error helper function die { local ret=$? error $@ exit $ret } -# @DESCRIPTION: make tmp dir or file in ${TMPDIR:-/tmp} -# @ARG: TEMPLATE +# @FUNCTION: Temporary dir/file helper +# @ARG: -d|-f [-m ] [-o ] [-g ] TEMPLATE function mktmp { local tmp=${TMPDIR:-/tmp}/$1-XXXXXX mkdir -p $tmp || die "mktmp: failed to make $tmp" print "$tmp" } -# @DESCRIPTION: add the essential nodes to be able to boot +# @FUNCTION: Device nodes (helper) function donod { pushd dev || die [[ -c console ]] || mknod -m 600 console c 5 1 || die @@ -97,8 +97,7 @@ function donod { setopt EXTENDED_GLOB NULL_GLOB unsetopt KSH_ARRAYS -# @DESCRIPTION: declare if not declared while arsing options, -# hold almost every single option/variable +# @VARIABLE: Associative Array holding (almost) every options typeset -A opts typeset -a opt opt=( @@ -159,26 +158,26 @@ if [[ -f "${PKG[name]}".conf ]] { die "no ${PKG[name]}.conf found" } -# @DESCRIPTION: kernel version to pick up +# @VARIABLE: Kernel version : ${opts[-kv]:=${opts[-k]:-$(uname -r)}} -# @DESCRIPTION: initramfs prefx name <$prefix-$kv.$ext> +# @VARIABLE: initramfs prefx : ${opts[-prefix]:=${opts[-p]:-initramfs-}} -# @DESCRIPTION: usr dir path, to get extra files +# @VARIABLE: USRDIR path to use : ${opts[-usrdir]:=${opts[-u]:-"${PWD}"/usr}} -# @DESCRIPTION: compression command +# @VARIABLE: Compression command : ${opts[-compressor]:=${opts[-c]:-xz -9 --check=crc32}} -# @DESCRIPTION: full to initramfs compressed image +# @VARIABLE: Full path to initramfs image : ${opts[-initramfs]:=${opts[-prefix]}${opts[-kv]}} -# @DESCRIPTION: kernel architecture +# @VARIABLE: Kernel architecture : ${opts[-arch]:=$(uname -m)} -# @DESCRIPTION: kernel bit lenght supported +# @VARIABLE: Kernel bit lenght : ${opts[-arc]:=$(getconf LONG_BIT)} -# @DESCRIPTION: tmp dir where to generate initramfs -# an initramfs compressed image +# @VARIABLE: (initramfs) Tmporary directory : ${opts[-tmpdir]:=$(mktmp ${opts[-initramfs]:t})} -# @DESCRIPTION: configuration directory +# @VARIABLE: (initramfs) Configuration directory : ${opts[-confdir]=etc/${PKG[name]}} +# Set up compression typeset -a compressor compressor=(bzip2 gzip lzip lzop lz4 xz) @@ -209,7 +208,8 @@ if (( ${+config} )) { unset config xgrep CONFIG comp compressor } -# @DESCRIPTION: generate an initramfs image +# @FUNCTION: CPIO image builder +# @ARG: function docpio { local ext=.cpio initramfs=${1:-/boot/${opts[-initramfs]}} local cmd="find . -print0 | cpio -0 -ov -Hnewc" @@ -246,6 +246,7 @@ if (( ${+opts[-r]} )) || (( ${+opts[-rebuild]} )) { rm -fr * } +# Set up the initramfs if [[ -d ${opts[-usrdir]} ]] { cp -ar ${opts[-usrdir]} . && mv -f {usr/,}root && @@ -276,11 +277,13 @@ if [[ ${${(pws:.:)opts[-kv]}[1]} -eq 3 ]] && cp -af ${opts[-usrdir]}/../init . && chmod 775 init || die [[ -d root ]] && chmod 0700 root || mkdir -m700 root || die +# Set up RAID option for bin (dmraid mdadm zfs) for opt (${opts[-b]} ${opts[-bin]}) if [[ ${opt/$bin} != $opt ]] { opts[-mgrp]+=:$bin } opts[-mgrp]=${opts[-mgrp]/mdadm/raid} +# Set up (requested) hook for hook (${(pws,:,)opts[-H]} ${(pws,:,)opts[-hook]}) { for file (${opts[-usrdir]:h}/hooks/*${hook}*) { cp -a ${file} lib/${PKG[name]} @@ -292,9 +295,9 @@ for hook (${(pws,:,)opts[-H]} ${(pws,:,)opts[-hook]}) { cp -ar {/,}lib/modules/${opts[-kv]}/modules.dep || die "failed to copy modules.dep" - [[ -f /etc/issue.logo ]] && cp {/,}etc/issue.logo +# Set up (requested) firmware if (( ${+opts[-F]} || ${+opts[-firmware]} )) { if [[ ${opts[-F]} == : ]] || [[ ${opts[-firmware]} == : ]]; then warn "Adding the whole firmware directory" @@ -310,6 +313,7 @@ if (( ${+opts[-F]} || ${+opts[-firmware]} )) { unset firmware } +# Handle & copy BusyBox binary if [[ -x usr/bin/busybox ]] { mv -f {usr/,}bin/busybox } elif (( ${+commands[busybox]} )) { @@ -334,6 +338,7 @@ for bin ($(grep '^bin' ${opts[-confdir]}/busybox.applets)) for bin ($(grep '^sbin' ${opts[-confdir]}/busybox.applets)) ln -s ../bin/busybox ${bin} +# Set up a few options if (( ${+opts[-L]} )) || (( ${+opts[-luks]} )) { opts[-bin]+=:cryptsetup opts[-mgrp]+=:dm-crypt } @@ -351,7 +356,8 @@ if (( ${+opts[-q]} )) || (( ${+opts[-squashd]} )) { opts[-bin]+=:mount.aufs:umount.aufs opts[-mgrp]+=:squashd } -# @DESCRIPTION: copy kernel module +# @FUNCTION: Kernel module copy helper +# @ARG: [-v|--verbose] function domod { case $1 in (-v|--verbose) @@ -383,7 +389,8 @@ function domod { return ${ret} } -typeset -a KEYMAP +# Handle & copy keymap/consolefont +typeset -a FONT KEYMAP for keymap (${(pws,:,)opts[-y]} ${(pws,:,)opts[-keymap]}) { if [[ -f usr/share/keymaps/${keymap}-${opts[-arch]}.bin ]] { :; @@ -396,9 +403,7 @@ for keymap (${(pws,:,)opts[-y]} ${(pws,:,)opts[-keymap]}) { (( $? == 0 )) && KEYMAP+=(${keymap}-${opts[-arch]}.bin) } print ${KEYMAP[1]} >${opts[-confdir]}/kmap -unset KEYMAP keymap -typeset -a FONT for font (${(pws,:,)opts[-f]} ${(pws,:,)opts[-font]}) { if [[ -f usr/share/consolefonts/${font} ]] { :; @@ -414,8 +419,9 @@ for font (${(pws,:,)opts[-f]} ${(pws,:,)opts[-font]}) { (( $? == 0 )) && FONT+=(${font}) } print ${FONT[1]} >${opts[-confdir]}/font -unset FONT font +unset FONT font KEYMAP keymap +# Handle & copy splash themes if (( ${+$opts[-s]} )) || (( ${+opts[-splash]} )) { opts[-bin]+=:splash_util.static:fbcondecor_helper @@ -432,7 +438,8 @@ if (( ${+$opts[-s]} )) || (( ${+opts[-splash]} )) { } else { warn "splash themes does not exist" } } -# @DESCRIPTION: follow and copy link until binary/library is copied +# @FUNCTION: Binary/Library copy helper (handle symlink) +# @ARG: function docp { local link=${1} prefix [[ -n ${link} ]] || return @@ -448,8 +455,8 @@ function docp { done return 0 } - -# @DESCRIPTION: copy binary with libraries if not static +# @FUNCTION: (static/dynamic) Binary copy helper +# @ARG: function dobin { local bin=$1 lib docp ${bin} || return @@ -460,6 +467,7 @@ function dobin { mkdir -p .${lib%/*} && docp ${lib} || die } +# Handle & copy binaries for bin (${(pws,:,)opts[-b]} ${(pws,:,)opts[-bin]}) { for b ({usr/,}{,s}bin/${bin}) [ -x ${b} -a ! -h ${b} ] && continue 2 [[ -x ${bin} ]] && binary=${bin} || binary=${commands[$bin]} @@ -467,7 +475,7 @@ for bin (${(pws,:,)opts[-b]} ${(pws,:,)opts[-bin]}) { } unset -v binary bin b -# Remove module group name from boot group before processing module groups +# Handle & copy kernel module for mod (${(pws,:,)opts[-mboot]}) if (( ${+opts[-m$mod]} )) { print ${mod} >>${opts[-confdir]}/boot @@ -484,6 +492,7 @@ for (( i=1; i <= ${#env[@]}; i++ )) print ${env[i]} >>${opts[-confdir]}/env unset env +# Handle GCC libraries symlinks for lib (usr/lib/gcc/**/lib*.so*(.N)) { ln -fs /$lib lib/$lib:t ln -fs /$lib usr/lib/$lib:t