summarylogtreecommitdiffstats
path: root/linux-preserve-modules
blob: 3727499fad642617d13d3a3c60a9647aa0d8bcdb (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
#!/bin/bash

shopt -s nullglob

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

set-vars() {
    KERNEL="$1"
    TARGET="usr/lib/modules/$KERNEL"
    PRESERVE_MODULES_DIR=/run/linux-preserve-modules

    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
}

preserve-modules-copy() {
    if [[ $# -gt 1 ]]; then
        echo "usage: linux-preserve-modules copy [release]" >&2
        exit 1
    elif [[ $# -eq 1 ]]; then
        set-vars "$1"
    fi

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

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

    # exit early if kernel modules already copied
    if [[ -f "$TARGET_DIR/.preserved" ]]; then
        echo "info: already linked" >&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() {
    if [[ $# -gt 1 ]]; then
        echo "usage: linux-preserve-modules link [release]" >&2
        exit 1
    elif [[ $# -eq 1 ]]; then
        set-vars "$1"
    fi

    # exit early if no kernel modules copied
    if [[ ! -f "$PRESERVE_DIR/modules.builtin" ]]; 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

    # exit early if kernel modules already copied
    if [[ -f "$TARGET_DIR/.preserved" ]]; then
        echo "info: already linked" >&2
        exit 0
    fi

    # copy over dep files and mark kernel version as preserved
    mkdir -p "$TARGET_DIR/"
    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
}

preserve-modules-unlink() {
    if [[ $# -gt 1 ]]; then
        echo "usage: linux-preserve-modules unlink [release]" >&2
        exit 1
    elif [[ $# -eq 1 ]]; then
        set-vars "$1"
    fi

    # exit early if kernel modules not already copied
    if [[ ! -f "$TARGET_DIR/.preserved" ]]; then
        echo "info: not linked" >&2
        exit 0
    fi

    # clean preserved files
    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"

    # remove mount points if empty
    rmdir --ignore-fail-on-non-empty "$TARGET_DIR/kernel"
    rmdir --ignore-fail-on-non-empty "$TARGET_DIR/updates"
    rmdir --ignore-fail-on-non-empty "$TARGET_DIR"

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

preserve-modules-unlink-all() {
    if [[ $# -gt 0 ]]; then
        echo "usage: linux-preserve-modules unlink-all" >&2
        exit 1
    fi

    # find kernels with .preserved marker file
    LINKED_KERNELS=( $(find /usr/lib/modules -maxdepth 2 -type f -name .preserved | cut -d/ -f5) )
    echo "info: found ${#LINKED_KERNELS[@]} kernels with linked modules" >&2

    # unlink modules for each
    for kernel in ${LINKED_KERNELS[@]}; do
        linux-preserve-modules unlink "$kernel"
    done

    exit 0
}

preserve-modules-list() {
    if [[ $# -gt 0 ]]; then
        echo "usage: linux-preserve-modules list" >&2
        exit 1
    fi

    # find kernels with .preserved marker file
    LINKED_KERNELS=( $(find /usr/lib/modules -maxdepth 2 -type f -name .preserved | cut -d/ -f5) )
    echo "info: found ${#LINKED_KERNELS[@]} kernels with linked modules" >&2

    # list kernels
    echo "${LINKED_KERNELS[@]}"

    exit 0
}

usage() {
    echo "usage: linux-preserve-modules <copy|link|unlink|unlink-all|list> [release]" >&2
    exit 1
}

main() {
    if [[ "$(id -u)" -ne 0 ]]; then
        echo "error: this script needs to run as root" >&2
        usage
    fi

    set-vars "$(uname -r)"

    if [[ "$#" -eq 0 ]]; then
        usage
    fi

    CMD="$1"
    shift
    case "$CMD" in
        copy) preserve-modules-copy "$@";;
        link) preserve-modules-link "$@";;
        unlink) preserve-modules-unlink "$@";;
        unlink-all) preserve-modules-unlink-all "$@";;
        list) preserve-modules-list "$@";;
        *) usage;;
    esac
}

main "$@"