summarylogtreecommitdiffstats
path: root/preserve-modules.script
blob: cb9cc9d3ca2eedd7e84e25d1cb408ab41bc86d56 (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
#!/bin/bash

shopt -s nullglob

KERNEL="$(uname -r)"
TARGET="usr/lib/modules/$KERNEL"
PRESERVE_MODULES_DIR=/run/linux-preserve-modules

not() {
    if "$@"; then
        return 1
    else
        return 0
    fi
}

preserve-modules-copy() {
    TARGET_DIR="/$TARGET"
    PRESERVE_DIR="$PRESERVE_MODULES_DIR/modules/$KERNEL"

    # exit early if kernel version empty
    if [[ -z "$KERNEL" ]]; then
        echo "error: unknown kernel version" >&2
        exit 1
    fi

    # exit early if currently running kernel modules untouched
    if not grep -F -q "$TARGET"; then
        echo "info: nothing to copy" >&2
        exit 0
    fi

    # exit early if the target directory does not exist
    if [[ ! -d "$TARGET_DIR" ]]; then
        echo "info: nothing to copy" >&2
        exit 0
    fi

    # clean preserved files before transaction
    if [[ -f "$TARGET_DIR/.preserved" ]]; then
        rm "$TARGET_DIR/.preserved"
        rm "$TARGET_DIR/modules".*

        mountpoint -q "$TARGET_DIR/kernel" && umount "$TARGET_DIR/kernel"
        mountpoint -q "$TARGET_DIR/updates" && umount "$TARGET_DIR/updates"
    fi

    # exit early if kernel modules already copied
    if [[ -d "$PRESERVE_DIR" ]]; then
        echo "info: already copied" >&2
        exit 0
    fi

    # copy over kernel modules and dep files
    mkdir -p "$PRESERVE_DIR"
    cp -r "$TARGET_DIR/modules".* "$PRESERVE_DIR/"
    [[ -d "$TARGET_DIR/kernel" ]] && cp -r "$TARGET_DIR/kernel" "$PRESERVE_DIR/kernel"
    [[ -d "$TARGET_DIR/updates" ]] && cp -r "$TARGET_DIR/updates" "$PRESERVE_DIR/updates"

    echo "info: copied modules" >&2
    exit 0
}

preserve-modules-link() {
    TARGET_DIR="/$TARGET"
    PRESERVE_DIR="$PRESERVE_MODULES_DIR/modules/$KERNEL"

    # exit early if kernel version empty
    if [[ -z "$KERNEL" ]]; then
        echo "error: unknown kernel version" >&2
        exit 1
    fi

    # exit early if no kernel modules copied
    if [[ ! -d "$PRESERVE_DIR" ]]; then
        echo "info: nothing to link" >&2
        exit 0
    fi

    # exit early if mount targets still owned by a package
    OWNERS=(
        $(pacman -Qoq "$TARGET_DIR/kernel" 2>/dev/null)
        $(pacman -Qoq "$TARGET_DIR/updates" 2>/dev/null)
    )
    if [[ "${#OWNERS[@]}" -ne 0 ]]; then
        echo "info: modules still there" >&2
        exit 0
    fi

    # copy over dep files and mark kernel version as preserved
    touch "$TARGET_DIR/.preserved"
    cp -r "$PRESERVE_DIR/modules".* "$TARGET_DIR/"

    # bind mount preserved kernel modules
    mkdir -p "$TARGET_DIR/kernel"
    mkdir -p "$TARGET_DIR/updates"
    [[ -d "$PRESERVE_DIR/kernel" ]] && mount --bind "$PRESERVE_DIR/kernel" "$TARGET_DIR/kernel"
    [[ -d "$PRESERVE_DIR/updates" ]] && mount --bind "$PRESERVE_DIR/updates" "$TARGET_DIR/updates"

    # trigger depmod
    depmod "$KERNEL"

    echo "info: linked modules" >&2
    exit 0
}

usage() {
    echo "usage: linux-preserve-modules <copy|link>" >&2
    exit 1
}

if [[ "$#" -ne 1 ]]; then
    usage
fi

case "$1" in
    copy) preserve-modules-copy;;
    link) preserve-modules-link;;
    *) usage;;
esac