summaryrefslogtreecommitdiff
path: root/src/mkinitramfs-samizdat
blob: c2e61416e1ea93fb311ae4eb3fe06659b4574800 (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
#!/bin/bash
if [ "$(id -u)" != 0 ]
then
    exec sudo -- "$0" "$@"
    exit 1
fi

loudly()
{
    (set -x
    "$@")
}

deb_arch()
{
    dpkg-architecture  -q DEB_HOST_ARCH
}

up_to_date()
{
    local found
    [ -e "$1" ] || return
    [ -z "$FORCE_BUILD" ] || return
    found=$(find "$samizdat_initramfs_conf_dir" \
                 "$samizdat_initrd_files_dir" \
                 -type f -newer "$1" -print -quit) || return
    [ -z "$found" ]
}

build_image()
{
    OUTPUT=/boot/initrd.img-$1
    # Always regenerate when --system specified.
    # Otherwise, only regenerate if it appears dependencies changed.
    if ! up_to_date "$OUTPUT"
    then
        /usr/sbin/mkinitramfs \
            -d "$samizdat_initramfs_conf_dir" \
            -o "$OUTPUT" \
            -k "$1" || return
    fi
    loudly ln -sf "${OUTPUT#/}" /initrd.img
}

. samizdat-paths.sh

OUTPUT_TAG=
FORCE_BUILD=
while [ $# -gt 0 ]
do
    case "$1" in
        -t|--tag) OUTPUT_TAG=.samizdat;;
        -f|--force) FORCE_BUILD=y;;
        --) shift; break;;
        -*) exit 1;;
        *) break;;
    esac
    shift
done

if [ "$1" ]
then
    k=/boot/vmlinuz-$1
    if ! [ -e "$k" ]
    then
        printf 'Error: No such file: %s\n' "$k" >&2
        exit 1
    fi
    build_image "$1$OUTPUT_TAG" || exit
    ln -sf "${k#/}" /vmlinuz
elif [ -L /vmlinuz ]
then
    l=$(readlink /vmlinuz)
    mv=${l#boot/vmlinuz-}
    for v in "$mv" "$mv-$(deb_arch)"
    do
        [ -e "/boot/vmlinuz-$v" ] || continue
        build_image "$v"
        exit
    done
    exit 1
else
    printf 'Error: Could not determine kernel version.  Create /vmlinuz or specify a version.' >&2
    exit 1
fi