AaylaSecura

mkinitramfs

Aug 15th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.57 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. usage() {
  4.     exec 2>&1
  5.     [[ -n $1 ]] && echo -e "Error: $1\n"
  6.     cat <<EOF
  7. Append the specified (existing) files, directories, nodes, symlinks to a
  8. gen_init_cpio file list <file>, automatically detecting the type, mode
  9. and library dependencies. Optionally create the initramfs.
  10.  
  11. Usage: ${BASH_SOURCE[0]} [options] [<file>]
  12. If <file> is missing, print list to stdout.
  13.  
  14. Options:
  15.   --dyn-bin   | -b <binary>   Add a dynamically linked binary and all of its
  16.                               dependencies.
  17.   --file      | -f <file>     Add a file, automatically detecting the type
  18.                               (file, dir, nod, slink, pipe or sock) and mode.
  19.   --overwrite | -o            Do not append to file.
  20.   --generate  | -g [<name>]   Generate the initramfs after creating/updating
  21.                               the file list. A default name is generated from
  22.                               the currently SELECTED kernel.
  23.   --compress  | -c [<level>]  Compress the initramfs. Default <level> is 4.
  24.   --quiet     | -q            Do not write file list to stdout.
  25. EOF
  26.     [[ -n ${TMPFILE} && -e ${TMPFILE} ]] && rm ${TMPFILE}
  27.     exit 1
  28. }
  29.  
  30. add_file() {
  31.     local OBJ=${1//%/%%}
  32.     stat -c "file ${OBJ} ${OBJ} %a 0 0" "$1" >> ${TMPFILE}
  33. }
  34.  
  35. add_dir() {
  36.     local OBJ=${1//%/%%}
  37.     stat -c "dir ${OBJ} %a 0 0" "$1" >> ${TMPFILE}
  38. }
  39.  
  40. add_char() {
  41.     local OBJ=${1//%/%%}
  42.     stat -c "nod ${OBJ} %a 0 0 c %t %T" "$1" >> ${TMPFILE}
  43. }
  44.  
  45. add_block() {
  46.     local OBJ=${1//%/%%}
  47.     stat -c "nod ${OBJ} %a 0 0 b %t %T" "$1" >> ${TMPFILE}
  48. }
  49.  
  50. add_slink() {
  51.     local OBJ=${1//%/%%}
  52.     stat -c "slink ${OBJ} $(readlink "$1") %a 0 0" "$1" >> ${TMPFILE}
  53. }
  54.  
  55. add_pipe() {
  56.     local OBJ=${1//%/%%}
  57.     stat -c "pipe ${OBJ} %a 0 0" "$1" >> ${TMPFILE}
  58. }
  59.  
  60. add_sock() {
  61.     local OBJ=${1//%/%%}
  62.     stat -c "sock ${OBJ} %a 0 0" "$1" >> ${TMPFILE}
  63. }
  64.  
  65. add_obj_path() {
  66.     local BASE
  67.     BASE=$(dirname "$1")
  68.     BASE=${BASE%/}
  69.     while [[ -n ${BASE} ]] ; do
  70.         [[ -L ${BASE} ]] && add_slink "${BASE}"
  71.         add_dir "$(readlink -f "${BASE}")"
  72.         BASE=${BASE%/*}
  73.     done
  74. }
  75.  
  76. add_dyn_bin() {
  77.     local BIN LIB TRUELIB
  78.     BIN=$(type -p "$1")
  79.     [[ -n ${BIN} ]] || usage "Cannot find binary: $1."
  80.  
  81.     add_obj "${BIN}"
  82.     # linux-vdso.so is provided by the kernel
  83.     ldd "${BIN}" | sed -r -n -e '/ *not a dynamic executable/b' \
  84.                        -e '/linux-vdso.so/b' -e 's/^\t*(.*+> )?([^ ]+) .*/\2/p' \
  85.         | while IFS= read LIB ; do
  86.         TRUELIB=$(readlink -f "${LIB}")
  87.         add_obj "${TRUELIB}"
  88.         [[ ${TRUELIB} == ${LIB} ]] || add_obj "${LIB}"
  89.     done
  90. }
  91.  
  92. add_obj() {
  93.     if [[ -L $1 ]] ; then
  94.         add_slink "$1"
  95.     elif [[ -f $1 ]] ; then
  96.         add_file "$1"
  97.     elif [[ -d $1 ]] ; then
  98.         add_dir "$1"
  99.     elif [[ -c $1 ]] ; then
  100.         add_char "$1"
  101.     elif [[ -b $1 ]] ; then
  102.         add_block "$1"
  103.     elif [[ -p $1 ]] ; then
  104.         add_pipe "$1"
  105.     elif [[ -S $1 ]] ; then
  106.         add_sock "$1"
  107.     elif [[ -z $1 ]] ; then
  108.         usage "Option --file requires a filename."
  109.     elif [[ -e $1 ]] ; then
  110.         usage "Unknown file type: $1."
  111.     else
  112.         usage "No such file or directory: $1."
  113.     fi
  114.     add_obj_path "$1"
  115. }
  116.  
  117. ###############################################################################
  118.  
  119. (( $# )) || usage
  120.  
  121. QUIET=0
  122. APPEND=1
  123. INITRAMFS=""
  124. COMPRESS=""
  125. FILE=""
  126. TMPFILE=$(mktemp --tmpdir gen_init_cpio_flist.XXXX)
  127.  
  128. while (( $# > 0 )) ; do
  129.     case $1 in
  130.         --dyn-bin|-b)
  131.             [[ $2 == *\ * ]] && usage "Spaces are not allowed in filenames."
  132.             add_dyn_bin "$(readlink -f "$2")"
  133.             shift 2
  134.             ;;
  135.         --file|-f)
  136.             [[ $2 == *\ * ]] && usage "Spaces are not allowed in filenames."
  137.             add_obj "$(readlink -f "$2")"
  138.             shift 2
  139.             ;;
  140.         --overwrite|-o)
  141.             APPEND=0
  142.             shift
  143.             ;;
  144.         --generate|-g)
  145.             if [[ -n $2 && $2 != -* ]] ; then
  146.                 INITRAMFS=$2
  147.                 shift
  148.             else
  149.                 KERNEL=$(basename $(readlink -f /usr/src/linux))
  150.                 INITRAMFS=initramfs-${KERNEL}.cpio
  151.             fi
  152.             shift
  153.             ;;
  154.         --compress|-c)
  155.             if [[ $2 =~ ^[1-9]$ ]] ; then
  156.                 COMPRESS=$2
  157.                 shift
  158.             else
  159.                 COMPRESS=4
  160.             fi
  161.             shift
  162.             ;;
  163.         -c1|-c2|-c3|-c4|-c5|-c6|-c7|-c8|-c9)
  164.             COMPRESS=${1#-c}
  165.             shift
  166.             ;;
  167.         --quiet|-q)
  168.             QUIET=1
  169.             shift
  170.             ;;
  171.         -*)
  172.             usage "Unknown option: $1."
  173.             ;;
  174.         *)
  175.             [[ -n ${FILE} ]] && usage "Please specify only one filename."
  176.             FILE=$1
  177.             shift
  178.             ;;
  179.     esac
  180. done
  181.  
  182. if [[ -z ${FILE} ]] ; then
  183.     FILE=$TMPFILE
  184. elif (( APPEND )) ; then
  185.     if [[ -f ${FILE} ]] ; then
  186.         cat "${FILE}" >> ${TMPFILE}
  187.     elif [[ -e ${FILE} ]] ; then
  188.         usage "${FILE} is not a regular file."
  189.     fi
  190. fi
  191. # remove duplicate entries
  192. sort -k2,2 -u -o "${FILE}" ${TMPFILE}
  193. if [[ ${FILE} == ${TMPFILE} ]] ; then
  194.     (( QUIET )) || cat ${FILE}
  195. fi
  196.  
  197. # generate the initramfs
  198. if [[ -n ${INITRAMFS} ]] ; then
  199.     GEN_INIT_CPIO=$(readlink -e /usr/src/linux/usr/gen_init_cpio)
  200.     [[ -z ${GEN_INIT_CPIO} ]] && \
  201.         usage "Cannot find gen_init_cpio binary in /usr/src/linux/usr/."
  202.     ${GEN_INIT_CPIO} "${FILE}" > "${INITRAMFS}"
  203.     [[ -n ${COMPRESS} ]] && gzip -${COMPRESS} "${INITRAMFS}"
  204. fi
  205.  
  206. rm "${TMPFILE}"
  207. exit 0
Add Comment
Please, Sign In to add comment