Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- usage() {
- exec 2>&1
- [[ -n $1 ]] && echo -e "Error: $1\n"
- cat <<EOF
- Append the specified (existing) files, directories, nodes, symlinks to a
- gen_init_cpio file list <file>, automatically detecting the type, mode
- and library dependencies. Optionally create the initramfs.
- Usage: ${BASH_SOURCE[0]} [options] [<file>]
- If <file> is missing, print list to stdout.
- Options:
- --dyn-bin | -b <binary> Add a dynamically linked binary and all of its
- dependencies.
- --file | -f <file> Add a file, automatically detecting the type
- (file, dir, nod, slink, pipe or sock) and mode.
- --overwrite | -o Do not append to file.
- --generate | -g [<name>] Generate the initramfs after creating/updating
- the file list. A default name is generated from
- the currently SELECTED kernel.
- --compress | -c [<level>] Compress the initramfs. Default <level> is 4.
- --quiet | -q Do not write file list to stdout.
- EOF
- [[ -n ${TMPFILE} && -e ${TMPFILE} ]] && rm ${TMPFILE}
- exit 1
- }
- add_file() {
- local OBJ=${1//%/%%}
- stat -c "file ${OBJ} ${OBJ} %a 0 0" "$1" >> ${TMPFILE}
- }
- add_dir() {
- local OBJ=${1//%/%%}
- stat -c "dir ${OBJ} %a 0 0" "$1" >> ${TMPFILE}
- }
- add_char() {
- local OBJ=${1//%/%%}
- stat -c "nod ${OBJ} %a 0 0 c %t %T" "$1" >> ${TMPFILE}
- }
- add_block() {
- local OBJ=${1//%/%%}
- stat -c "nod ${OBJ} %a 0 0 b %t %T" "$1" >> ${TMPFILE}
- }
- add_slink() {
- local OBJ=${1//%/%%}
- stat -c "slink ${OBJ} $(readlink "$1") %a 0 0" "$1" >> ${TMPFILE}
- }
- add_pipe() {
- local OBJ=${1//%/%%}
- stat -c "pipe ${OBJ} %a 0 0" "$1" >> ${TMPFILE}
- }
- add_sock() {
- local OBJ=${1//%/%%}
- stat -c "sock ${OBJ} %a 0 0" "$1" >> ${TMPFILE}
- }
- add_obj_path() {
- local BASE
- BASE=$(dirname "$1")
- BASE=${BASE%/}
- while [[ -n ${BASE} ]] ; do
- [[ -L ${BASE} ]] && add_slink "${BASE}"
- add_dir "$(readlink -f "${BASE}")"
- BASE=${BASE%/*}
- done
- }
- add_dyn_bin() {
- local BIN LIB TRUELIB
- BIN=$(type -p "$1")
- [[ -n ${BIN} ]] || usage "Cannot find binary: $1."
- add_obj "${BIN}"
- # linux-vdso.so is provided by the kernel
- ldd "${BIN}" | sed -r -n -e '/ *not a dynamic executable/b' \
- -e '/linux-vdso.so/b' -e 's/^\t*(.*+> )?([^ ]+) .*/\2/p' \
- | while IFS= read LIB ; do
- TRUELIB=$(readlink -f "${LIB}")
- add_obj "${TRUELIB}"
- [[ ${TRUELIB} == ${LIB} ]] || add_obj "${LIB}"
- done
- }
- add_obj() {
- if [[ -L $1 ]] ; then
- add_slink "$1"
- elif [[ -f $1 ]] ; then
- add_file "$1"
- elif [[ -d $1 ]] ; then
- add_dir "$1"
- elif [[ -c $1 ]] ; then
- add_char "$1"
- elif [[ -b $1 ]] ; then
- add_block "$1"
- elif [[ -p $1 ]] ; then
- add_pipe "$1"
- elif [[ -S $1 ]] ; then
- add_sock "$1"
- elif [[ -z $1 ]] ; then
- usage "Option --file requires a filename."
- elif [[ -e $1 ]] ; then
- usage "Unknown file type: $1."
- else
- usage "No such file or directory: $1."
- fi
- add_obj_path "$1"
- }
- ###############################################################################
- (( $# )) || usage
- QUIET=0
- APPEND=1
- INITRAMFS=""
- COMPRESS=""
- FILE=""
- TMPFILE=$(mktemp --tmpdir gen_init_cpio_flist.XXXX)
- while (( $# > 0 )) ; do
- case $1 in
- --dyn-bin|-b)
- [[ $2 == *\ * ]] && usage "Spaces are not allowed in filenames."
- add_dyn_bin "$(readlink -f "$2")"
- shift 2
- ;;
- --file|-f)
- [[ $2 == *\ * ]] && usage "Spaces are not allowed in filenames."
- add_obj "$(readlink -f "$2")"
- shift 2
- ;;
- --overwrite|-o)
- APPEND=0
- shift
- ;;
- --generate|-g)
- if [[ -n $2 && $2 != -* ]] ; then
- INITRAMFS=$2
- shift
- else
- KERNEL=$(basename $(readlink -f /usr/src/linux))
- INITRAMFS=initramfs-${KERNEL}.cpio
- fi
- shift
- ;;
- --compress|-c)
- if [[ $2 =~ ^[1-9]$ ]] ; then
- COMPRESS=$2
- shift
- else
- COMPRESS=4
- fi
- shift
- ;;
- -c1|-c2|-c3|-c4|-c5|-c6|-c7|-c8|-c9)
- COMPRESS=${1#-c}
- shift
- ;;
- --quiet|-q)
- QUIET=1
- shift
- ;;
- -*)
- usage "Unknown option: $1."
- ;;
- *)
- [[ -n ${FILE} ]] && usage "Please specify only one filename."
- FILE=$1
- shift
- ;;
- esac
- done
- if [[ -z ${FILE} ]] ; then
- FILE=$TMPFILE
- elif (( APPEND )) ; then
- if [[ -f ${FILE} ]] ; then
- cat "${FILE}" >> ${TMPFILE}
- elif [[ -e ${FILE} ]] ; then
- usage "${FILE} is not a regular file."
- fi
- fi
- # remove duplicate entries
- sort -k2,2 -u -o "${FILE}" ${TMPFILE}
- if [[ ${FILE} == ${TMPFILE} ]] ; then
- (( QUIET )) || cat ${FILE}
- fi
- # generate the initramfs
- if [[ -n ${INITRAMFS} ]] ; then
- GEN_INIT_CPIO=$(readlink -e /usr/src/linux/usr/gen_init_cpio)
- [[ -z ${GEN_INIT_CPIO} ]] && \
- usage "Cannot find gen_init_cpio binary in /usr/src/linux/usr/."
- ${GEN_INIT_CPIO} "${FILE}" > "${INITRAMFS}"
- [[ -n ${COMPRESS} ]] && gzip -${COMPRESS} "${INITRAMFS}"
- fi
- rm "${TMPFILE}"
- exit 0
Add Comment
Please, Sign In to add comment