summaryrefslogtreecommitdiff
path: root/make
blob: 6aadbe85a68ed5d26258e1ad2b8c51ed70847520 (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
#!/bin/sh

toppid=$$
die() {
    fmt=$1
    shift
    printf "error building initramfs: $fmt\n" "$@" >&2
    kill "$toppid"
    trap '' EXIT
    exit 1
}

# generate a cpio entry for a command
gen_cmd() {
    cmd=$1
    cmdp="$(command -v "$cmd")" || die 'command not found: %s' "$cmd"
    # ignore builtins
    [ "$cmd" != "$cmdp" ] || return
    printf 'file %s %s 0755 0 0\n' "$cmdp" "$cmdp"
    ldd "$cmdp" | awk '/\// { if ($1~/^\//) file=$1; else file=$3; print "file " file " " file " 0755 0 0" }'
}

# generate the main file list
gen_cpio_list() {
    sed -e '/^#/d' cpio_list.txt
    tr ' ' '\n' < modules.dep | sed -e "s/://;s:.*:file /lib/modules/$KERNVER/& /lib/modules/$KERNVER/& 0644 0 0:"
    gen_cmd e2fsck
    gen_cmd dropbear
    gen_cmd cryptsetup
    ldconfig -p | awk '$1 == "libgcc_s.so.1" { print "file " $4 " " $4 " 0755 0 0" }'
}

# filter the list and insert parent directory entries
gen_dir_ents() {
    awk '{print; while (sub("/[^/]*$", "", $2) && $2) print "dir " $2 " 0755 0 0"; }'
}

# filter the list and generate the make dependency file
gen_depfile() {
    deps='cpio_list.txt make'
    awk -v d=/dev/fd/3 '{print} $1 == "file" { deps=deps " " $3; print $3 ":" > d } END { print "initramfs.img:" deps > d }'
}

if ! [ -e cpio_list.txt ] || ! [ -e gen_init_cpio ]; then
    cd "${0%/*}"
fi

gen_cpio_list | gen_dir_ents | sort -u | gen_depfile 3>initramfs.d | ./gen_init_cpio -t 0 - | $COMPRESSOR > initramfs.img