summaryrefslogtreecommitdiffstats
path: root/mkinitcpio
blob: 714b152ee51abc2691a3e9dc752a090194b154e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/sh
# mkinitcpio - modular tool for building an init ramfs cpio image
#
# IMPORTANT: We need to keep a common base syntax here
# because some of these hooks/scripts need to run under
# the klibc shell or even busybox's ash - therefore, the
# following constraints should be enforced:
#   variables should be quoted and bracketed "${SOMEVAR}"
#   inline execution should be done with $() instead of backticks
#   use "x${var}" = "x" to test for nulls/empty strings
#   incase of embedded spaces, quote all path names and string comaprissons
#
# TODO


# Settings
BASEDIR=""
KERNELVERSION="$(uname -r)"
CONFIG="mkinitcpio.conf"
HOOKDIR="hooks"
SAVELIST=""
GENIMG=""
APPEND=""
QUIET="n"

APPNAME=$(basename "${0}")

usage ()
{
    echo "${APPNAME}: usage"
    echo "  -c CONFIG        Use CONFIG file. default: /etc/mkinitcpio.conf"
    echo "  -k KERNELVERSION Use KERNELVERSION. default: $(uname -r)"
    echo "  -s               Save filelist. default: no"
    echo "  -b BASEDIR       Use BASEDIR. default: /"
    echo "  -g IMAGE         Generate a cpio image as IMAGE. default: no"
    echo "  -a               Append to an existing filelist. default: no"
    echo "  -q               Quiet output. Default: no"
    echo "  -h               This message."
    exit 1
}

while getopts 'c:k:sb:g:aqh' arg; do
    case "$arg" in
        c) CONFIG="$OPTARG" ;;
        k) KERNELVERSION="$OPTARG" ;;
        s) SAVELIST="y" ;;
        b) BASEDIR="$OPTARG" ;;
        g) GENIMG="$OPTARG" ;;
        a) APPEND="y" ;;
        q) QUIET="y" ;;
        h|?) usage ;;
        *) echo "invalid argument '$arg'"; usage ;;
    esac
done
shift $(($OPTIND - 1))

# append a trailing / if needed
if [ "${BASEDIR:${#BASEDIR}}" == "/" ]; then
    BASEDIR="${BASEDIR:0:${#BASEDIR}-1}"
fi

MODULEDIR="${BASEDIR}/lib/modules/${KERNELVERSION}"
FILELIST=${1:-"initcpio.filelist"}

if [ "x${BASEDIR}" != "x" ]; then
    if [ "${BASEDIR:0:1}" != "/" ]; then
        echo "base directory '${BASEDIR}' must be an absolute path"
        exit 1
    elif [ ! -d "${BASEDIR}" ]; then
        echo "base directory '${BASEDIR}' does not exist or is not a directory"
        exit 1
    fi
fi

if [ ! -f "${CONFIG}" ]; then
	echo "config file '${CONFIG}' cannot be found, aborting..."
    exit 1
fi
source "${CONFIG}"

if [ -f "${FILELIST}" -a "x${APPEND}" == "x" ]; then
    echo "destination file list '${FILELIST}' exists - remove before running"
    exit 1
elif [ -f "${DESTIMG}" ]; then
    echo "destination image '${DESTIMG}' exists - remove before running"
    exit 1
else
    touch "${FILELIST}"
fi

# Helper functions, just to make syntax clearer
auto_modules ()
{
    aliases=$(find /sys/devices/ -name modalias -exec cat {} \;)
    modprobe --show-depends -a $aliases 2>/dev/null |\
        sed "s|insmod \(.*\)|\1|" | grep "${1}" | sort -u
}

all_modules ()
{
    find ${MODULEDIR} -name *.ko 2>/dev/null |\
        grep "${1}" | sort -u
}

msg() { [ "${QUIET}" = "n" ] && echo "${@}"; }
err() { [ echo "ERROR: ${@}" >2; }

add_dir()
{
    local dir
    dir="$(dirname ${1})"
    if [ "x${dir}" != "x" -a "${dir}" != "/" ]; then
        dir="${dir}" #this got stripped off above... put it back

        if ! grep "dir ${dir} " "${FILELIST}" 2>&1 > /dev/null; then
            add_dir "${dir}"
            msg "  adding dir ${dir}"
            echo "dir ${dir} 755 0 0" >> "${FILELIST}"
        fi
    fi
}

# add_devnode /dev/foo type major minor [permissions]
add_devnode()
{
    if [ $# -ge 4 ]; then
        local perms
        perms="${5:-644}"
        add_dir "${1}"
        if ! grep "nod ${1}" "${FILELIST}" 2>&1 > /dev/null; then
            msg "       adding device node ${1}"
            echo "nod ${1} ${perms} 0 0 ${2} ${3} ${4}" >> "${FILELIST}"
        fi
    else
        err "invalid device node format: $@" >2
        return 1
    fi
}

add_symlink ()
{
    local fil dest dir
    if [ -L ${1} ]; then
        fil="${1}"
        dest="${fil##$BASEDIR}"
        add_dir "${dest}"
        if ! grep "slink ${fil} " "${FILELIST}" 2>&1 > /dev/null; then
            msg "  adding symlink ${dest}"
            echo "slink ${dest} ${fil} $(stat -c '%a %u %g' ${fil})" >> "${FILELIST}"
        fi
    else
        err "ERROR: file '${1}' is not a symlink" >2
        return 1
    fi
}

add_file ()
{
    local fil lnk dir dest
    if [ -e "${1}" ]; then
        fil="${1}"
        lnk=$(readlink -f "${fil}")
        if [ -n "${lnk}" ]; then
            add_symlink "${fil}"
            fil="${lnk}"
        fi
        if [ $# -eq 2 ]; then
            dest="${2}"
        else
            dest="${fil##$BASEDIR}"
        fi

        add_dir "${dest}"

        if ! grep "file ${fil} " "${FILELIST}" 2>&1 > /dev/null; then
            msg "  adding file ${dest}"
            echo "file ${dest} ${fil} $(stat -c '%a %u %g' ${fil})" >> "${FILELIST}"
        fi
    else
        err "file '${1}' does not exist" >2
        return 1
    fi
}

HAS_MODULES="n"
add_module()
{
    local fil path mod deps
    #cleanup - remove .ko, replace - and _ with [-_] to match either
    fil=$(basename "${1}" | sed -e "s|[-_]|\[-_\]|g" -e "s|\.ko$||g")

    for path in $(find "${MODULEDIR}" -type f -name "${fil}.ko"); do
        for mod in $(modinfo -F depends "${path}" | tr ',' ' '); do
            if [ "x${mod}" != "x" ]; then
                add_module "${mod}"
                HAS_MODULES="y"
            fi
        done
        add_file "${path}"
    done
}

add_binary()
{
    local bin type lib
    bin=$(which "${1}")
    if [ $? -ne 0 ]; then
        bin="${1}"
    fi

    if [ ! -f "${bin}" ]; then
        err "'${bin}' is not a file" >2
        return 1
    fi

    if [ $? -eq 0 ]; then
        type=$(file -b "${bin}")
        case "${type}" in
            *script*)
            msg "   adding '${type}' script, ensure proper interp exists..."
            add_file "${bin}"
            ;;
            *executable*)
            add_file "${bin}"
            #note, this will also handle 'not a dynamic executable' spit out by
            # static binaries... the deps will produce nothing
            for lib in $(ldd ${bin} 2>/dev/null | sed "s|.*=>\(.*\)|\1|"); do
                if [ "x${lib}" != "x" ]; then
                    #remove TLS libraries
                    notls=$(echo ${lib} | sed 's|/lib/tls.*/\(lib.*\)|/lib/\1|')
                    [ -e "${notls}" ] && lib="${notls}"
                    [ -f "${lib}" ] && add_file "${lib}"
                fi
            done
            ;;
            *)
            err "unknown type '${type}' for binary '${bin}'" >2
            return 1
            ;;
        esac
    fi
}

function parse_hook()
{
    local mod bin fil
    for mod in $MODULES; do
        if [ "x${mod}" != "x" ]; then
            add_module "${mod}"
        fi
    done

    for bin in $BINARIES; do
        if [ "x${bin}" != "x" ]; then
            add_binary "${bin}"
        fi
    done

    for fil in $FILES; do
        if [ "x${fil}" != "x" ]; then
            add_file "${fil}"
        fi
    done
}

#parse 'global' hook, as defined in ${CONFIG}
parse_hook

for hook in $HOOKS; do
    unset MODULES
    unset BINARIES
    unset FILES
    install () { msg "${hook}: no install function..."; }
    source "${HOOKDIR}/${hook}"
    install
    parse_hook
    #quick test to check for existance... need a better way...
    # note, this will only pick up valid run_hooks - a space is required
    if grep "run_hook ()" "${HOOKDIR}/${hook}" 2>&1>/dev/null; then
        add_file "${HOOKDIR}/${hook}" "/hooks/${hook}"
    fi
done

if [ "${HAS_MODULES}" == "y" ]; then
    add_file "${MODULEDIR}/modules.dep"
    add_file "${MODULEDIR}/modules.alias"
    add_file "${MODULEDIR}/modules.symbols"
fi

if [ "x$GENIMG" != "x" ]; then
    if ! gen_init_cpio ${FILELIST} | gzip -9 > "${GENIMG}"; then
        err "ERROR: Failed to create '${GENIMG}' image" >2
    fi

    if [ "x${SAVELIST}" == "x" ]; then
        rm ${FILELIST}
    fi
fi
#vim:set ft=sh ts=4 sw=4 noet: