summaryrefslogtreecommitdiffstats
path: root/build_initramfs.sh
blob: 05d345a0c57a1e64217fbceafdc074ff0d814cf6 (plain) (blame)
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env bash
# -*- coding: utf-8 -*-

# region header

# Copyright Torben Sickert (t.sickert["~at~"]gmail.com) 29.10.2015
#           Janosch Dobler (TODO) 29.10.2015

# License
# -------

# This library written by Torben Sickert and Janosch Dobler stand under a
# creative commons naming 3.0 unported license.
# see http://creativecommons.org/licenses/by/3.0/deed.de

# This tool provides a generic way to install systemd based remote linux
# initramfs.

# Examples
# --------

# Start install progress command (Assuming internet is available):
# >>> ./build_initramfs.sh

# Note that you only get very necessary output until you provide "--verbose" as
# commandline options.

# Dependencies
# ------------

# - bash (or any bash like shell)
# - cpio          - Copies files into or out of a cpio or tar archive. The
#                   archive can be another file on the disk, a magnetic tape,
#                   or a pipe.
# - git           - The stupid content tracker.
# - test          - Check file types and compare values (part of the shell).
# - shift         - Shifts the command line arguments (part of the shell).
# - echo          - Display a line of text (part of coreutils).
# - mktemp        - Create a temporary file or directory (part of coreutils).
# - cat           - Concatenate files and print on the standard output (part of
#                   coreutils).
# - rm            - Remove files or directories (part of coreutils).
# - sed           - Stream editor for filtering and transforming text.
# - gzip          - Compress or expand files.
# - curl          - Transfer a URL
# - tar           - The GNU version of the tar archiving utility.
# - make          - GNU make utility to maintain groups of programs.
# - cmake         - The "cmake" executable is the CMake command-line interface.
# - linux-headers - GNU make utility to maintain groups of programs.
# - grep          - Searches the named input files (or standard input if no
#                   files are named, or if a single hyphen-minus (-) is given
#                   as file name) for lines containing a match to the given
#                   PATTERN. By default, grep prints the matching lines.

# Optional dependencies
# ---------------------

# TODO

# TODO

__NAME__='build_initramfs'

# endregion

function build_initramfs() {
    # Provides the main module scope.

    # region properties

    local _SCOPE='local' && \
    if [[ $(echo "\"$@\"" | grep --extended-regexp \
        '(^"| )(-l|--load-environment)("$| )') != '' ]]
    then
        local _SCOPE='export'
    fi
    # NOTE: Only initialize environment if current scope wasn't set yet.
    if [ "$_VERBOSE" == '' ]; then
        "$_SCOPE" _VERBOSE='no'
        "$_SCOPE" _LOAD_ENVIRONMENT='no'
        local dependencies=(bash cpio git test shift echo mktemp cat rm sed \
            gzip curl tar grep make cmake)
        "$_SCOPE" _DEPENDENCIES="${dependencies[*]}"
        "$_SCOPE" _KERNEL_MODULE_DIRECTORY="builder/dnbd3-qcow2-rootfs/kernel_modules/"
        "$_SCOPE" _STANDARD_OUTPUT=/dev/null
        "$_SCOPE" _ERROR_OUTPUT=/dev/null
    fi

    # endregion

    # region functions

    ## region command line interface

    function build_initramfs_print_usage_message() {
        # Prints a description about how to use this program.
    cat << EOF
$__NAME__ provides a generic way to install systemd based remote linux
initramfs.
EOF
    }
    function build_initramfs_print_usage_examples() {
        # Prints a description about how to use this program by providing
        # examples.
        cat << EOF
    # Start install progress:
    # >>> ./build_initramfs.sh
EOF
    }
    function build_initramfs_print_command_line_option_description() {
        # Prints descriptions about each available command line option.
        cat << EOF
    -h --help Shows this help message.

    -v --verbose Tells you what is going on (default: "$_VERBOSE").

    -d --debug Gives you any output from all tools which are used
        (default: "$_DEBUG").

    -l --load-environment Simple load the install arch linux scope without
        doing anything else.
EOF
    }
    function build_initramfs_print_help_message() {
        # Provides a help message for this module.
        echo -e "\nUsage: $0 [options]\n"
        build_initramfs_print_usage_message "$@"
        echo -e '\nExamples:\n'
        build_initramfs_print_usage_examples "$@"
        echo -e '\nOption descriptions:\n'
        build_initramfs_print_command_line_option_description "$@"
        echo
    }
    function build_initramfs_command_line_interface() {
        # Provides the command line interface and interactive questions.
        while true; do
            case "$1" in
                -h|--help)
                    shift
                    build_initramfs_print_help_message "$0"
                    exit 0
                    ;;
                -v|--verbose)
                    shift
                    _VERBOSE='yes'
                    ;;
                -d|--debug)
                    shift
                    _STANDARD_OUTPUT=/dev/stdout
                    _ERROR_OUTPUT=/dev/stderr
                    ;;
                -l|--load-environment)
                    shift
                    _LOAD_ENVIRONMENT='yes'
                    ;;

                '')
                    shift
                    break
                    ;;
                *)
                    build_initramfs_log 'critical' \
                        "Given argument: \"$1\" is not available." '\n' && \
                    if [[ "$_SCOPE" == 'local' ]]; then
                        build_initramfs_print_help_message "$0"
                    fi
                    return 1
            esac
        done
        if [[ "$UID" != '0' ]]; then
            build_initramfs_log 'critical' \
                "You have to run this script as \"root\" not as \"${USER}\"."
            exit 2
        fi
        return 0
    }
    function build_initramfs_log() {
        # Handles logging messages. Returns non zero and exit on log level
        # error to support chaining the message into toolchain.
        #
        # Examples:
        #
        # >>> build_initramfs_log
        # info: test
        # >>> build_initramfs_log debug message
        # debug: message
        # >>> build_initramfs_log info message '\n'
        #
        # info: message
        local loggingType='info' && \
        local message="$1" && \
        if [ "$2" ]; then
            loggingType="$1"
            message="$2"
        fi
        if [ "$_VERBOSE" == 'yes' ] || [ "$loggingType" == 'error' ] || \
           [ "$loggingType" == 'critical' ]; then
            if [ "$3" ]; then
                echo -e -n "$3"
            fi
            echo -e "${loggingType}: $message"
        fi
        if [ "$loggingType" == 'error' ]; then
            exit 1
        fi
        return 0
    }

    ## endregion

    ## region tools

    function build_initramfs_perform_dependency_check() {
        # This function check if all given dependencies are present.
        #
        # Examples:
        #
        # >>> build_initramfs_perform_dependency_check "mkdir pacstrap mktemp"
        # ...
        local dependenciesToCheck="$1" && \
        local result=0 && \
        local dependency && \
        for dependency in ${dependenciesToCheck[*]}; do
            if ! hash "$dependency" 1>"$_STANDARD_OUTPUT" 2>/dev/null; then
                build_initramfs_log 'critical' \
                    "Needed dependency \"$dependency\" isn't available." && \
                result=1
            fi
        done
        return $result
    }

    ## endregion

    function build_initramfs_initialize_dracut() {
        # Downloads and compiles dracut.
        #
        # Examples:
        #
        # >>> build_initramfs_initialize_dracut
        # ...
        mkdir dracut 1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        build_initramfs_log 'Download and extract dracut.' && \
        curl --location \
            https://www.kernel.org/pub/linux/utils/boot/dracut/dracut-043.tar.gz | \
            tar --extract --gzip --directory dracut --strip-components 1 \
            1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        pushd dracut 1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        # NOTE: On virtualbox shared folder symlinks are not allowed.
        # make the dracut-install binary (dracut-install resolves dependencies etc.)
        build_initramfs_log 'Compile dracut.' && \
        make install/dracut-install 1>"$_STANDARD_OUTPUT" \
            2>"$_ERROR_OUTPUT" && \
        # NOTE: We have to copy the binary to current instead of symlinking
        # them since this feature isn't supported in shared virtual box
        # machine folders. If symlinks would be available we could simply
        # use:
        # >>> make dracut-install
        cp install/dracut-install dracut-install 1>"$_STANDARD_OUTPUT" \
            2>"$_ERROR_OUTPUT" && \
        popd 1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT"
        return $?
    }
    function build_initramfs_compile_nbd() {
        # Downloads and compiles nbd.
        #
        # Examples:
        #
        # >>> build_initramfs_compile_nbd
        # ...
        # Provides the following file:
        # ${_KERNEL_MODULE_DIRECTORY}/nbd/nbd.ko
        pushd "${_KERNEL_MODULE_DIRECTORY}/nbd" \
            1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        build_initramfs_log 'Compile the nbd kernel module.' && \
        make 1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        popd 1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT"
        return $?
        # TODO make clean
    }
    function build_initramfs_compile_dnbd3() {
        # Downloads and compiles dnbd3.
        #
        # Examples:
        #
        # >>> build_initramfs_compile_dnbd3
        # ...
        # Provides the following file:
        # ${_KERNEL_MODULE_DIRECTORY}/dnbd3/build/dnbd3.ko
        rm --recursive --force ${_KERNEL_MODULE_DIRECTORY}/dnbd3 \
            1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        pushd $_KERNEL_MODULE_DIRECTORY \
            1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        git clone git://git.openslx.org/dnbd3.git \
            1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        cd dnbd3 1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        ./build.sh 1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        popd 1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT"
        return $?
        # TODO rm -rf build
    }

    # endregion

    # region controller

    if [[ "$0" == *"${__NAME__}.sh" ]]; then
        build_initramfs_perform_dependency_check "${_DEPENDENCIES[*]}" || \
            build_initramfs_log 'error' 'Satisfying main dependencies failed.'
        build_initramfs_command_line_interface "$@" || return $?

        build_initramfs_log 'Checking dracut.' && \
        if ! [[ -d ./dracut ]]; then
            build_initramfs_log "Dracut isn't available yet loading it." && \
            build_initramfs_initialize_dracut
        fi
        # TODO Provide note if compilation doesn't work that we need
        # "linux-headers".
        build_initramfs_compile_nbd && \
        build_initramfs_compile_dnbd3 && \
        build_initramfs_log 'Build initramfs.' && \
        build_initramfs_log \
            'Copy dnbd3 qcow2 plugin into dracut modules folder.' && \
        cp --recursive builder/dnbd3-qcow2-rootfs/ \
            dracut/modules.d/90dndb3-qcow2-rootfs \
            1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT" && \
        dracut/dracut.sh --local --verbose --force \
            1>"$_STANDARD_OUTPUT" 2>"$_ERROR_OUTPUT"

    fi
    return 0

    # endregion
}

# region footer

if [[ "$0" == *"${__NAME__}.sh" || $(echo "$@" | grep --extended-regexp \
    '(^| )(-l|--load-environment)($| )') ]]; then
    "$__NAME__" "$@"
fi

# endregion

# region vim modline

# vim: set tabstop=4 shiftwidth=4 expandtab:
# vim: foldmethod=marker foldmarker=region,endregion:

# endregion