summaryrefslogtreecommitdiffstats
path: root/builder/modules.d/dnbd3-rootfs/helper/build.inc
blob: 9c935424b5cb2627194e680361c1defec7f85486 (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
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# shellcheck source=./rebash/core.sh

declare -rg _mainmoddir="$(dirname "${BASH_SOURCE[0]}")/.." 2> /dev/null

declare -rg _supported_qcow_handlers=("xmount" "kernel")
# NOTE: expects 'qcow_handler' to be set in the environment, else
# will only build support for xmount
build_initialize_components() {
    _deps_base_dir="${_mainmoddir}/binaries"
    # We might want to move the "binaries" repos from the dnbd3-rootfs module to main repo one day...
    # TODO check for its existence using modinfo -k <kernel_version>
    if [[ ! -f "${_deps_base_dir}/dnbd3/build/dnbd3.ko" ]] || \
        [[ ! -f "${_deps_base_dir}/dnbd3/build/dnbd3-client" ]]; then
            logging.info "Could not find dnbd3, building it..."
            if ! CMAKE_FLAGS="-DKERNEL_DIR=${kernel_headers}" \
                build_compile_dnbd3 "${_deps_base_dir}/dnbd3/"; then
                    logging.error "Failed to build dnbd3."
                    return 1
            fi
    fi

    # process qcow handler
    logging.info "Supported handlers: ${_supported_qcow_handlers[*]}"
    IFS='|' _pattern="^(${_supported_qcow_handlers[*]})$" export _pattern
    if [ -z "$qcow_handler" ]; then
        logging.info "No qcow handler specified, will use xmount."
    elif [[ ! "$qcow_handler" =~ $_pattern ]] ; then
        logging.warn "Unknown qcow handler '$qcow_handler' - will use xmount"
    fi
    if [ -z "$qcow_handler" ] || [ "$qcow_handler" = "xmount" ]; then
        if [[ ! -f "${_deps_base_dir}/xmount/trunk/build/src/xmount" ]]; then
            logging.info "Could not find xmount binary, building it..."
            if ! build_compile_xmount "${_deps_base_dir}/xmount/"; then
                logging.error "Failed to build xmount binary."
                return 1
            fi
        fi
        if [[ ! -f "${_deps_base_dir}/qemu-xmount/libxmount_input_qemu.so" ]]; then
            logging.info "Could not find xmount qemu library, building it..."
            if ! build_compile_qemu_xmount "${_deps_base_dir}/qemu-xmount/"; then
                logging error "Failed to build xmount qemu library."
                return 1
            fi
        fi
    fi
    if [ "$qcow_handler" = "kernel" ]; then
        if [ ! -f "${_deps_base_dir}/kernel-qcow2-linux/drivers/block/loop/loop.ko" ] \
            || [ ! -f "${_deps_base_dir}/kernel-qcow2-linux/drivers/block/loop/loop_file_fmt_qcow.ko" ] \
            || [ ! -f "${_deps_base_dir}/kernel-qcow2-linux/drivers/block/loop/loop_file_fmt_raw.ko" ]; then
            logging.info "Could not find loop kernel modules, building them..."
            if ! build_compile_kernel_qcow "${_deps_base_dir}/kernel-qcow2-linux"; then
                logging.error "Failed to build qcow loop kernel modules."
                return 1
            fi
        fi
        if [ ! -f "${_deps_base_dir}/kernel-qcow2-util-linux/losetup" ]; then
            logging.info "Could not find losetup with qcow2 support, building it..."
            if ! build_compile_losetup_qcow "${_deps_base_dir}/kernel-qcow2-util-linux"; then
                logging.error "Failed to build losetup with qcow support."
                return 1
            fi
        fi
    fi

    # always compile this helper since it does not cost much to do so
    if [[ ! -f "${_deps_base_dir}/systemd-preserve-process-marker/systemd-preserve-process-marker" ]]; then
        logging.info "Could not find systemd-preserve-process-marker binary, building it ..."
        if ! build_compile_systemd_preserve_process_marker \
            "${_deps_base_dir}/systemd-preserve-process-marker/"; then
                    logging.error "Failed to build systemd-preserve-process-marker"
                    return 1
        fi
    fi
    logging.info "Compilation of dnbd3-rootfs dependencies succeeded."
    return 0
}

clean_components() {
    local __doc__='
    Removes all compiled kernel specific files.
    NOTE: This method is triggered manually and not supported by dracut itself.

    Example:

    `clean`
    '
    local _submoddir="${_mainmoddir}/binaries"
    build_clean_xmount "${_submoddir}/xmount/"
    build_clean_qemu_xmount "${_submoddir}/qemu-xmount/"
    build_clean_dnbd3 "${_submoddir}/dnbd3/"
    build_clean_losetup_qcow "${_submoddir}/kernel-qcow2-util-linux"
    build_clean_dnbd3 "${_submoddir}/dnbd3/"
    build_clean_systemd_preserve_process_marker \
        "${_submoddir}/systemd-preserve-process-marker/"
    return 0
}
# endregion

build_compile_qemu_xmount() {
    local __doc__='
    Compiles qemu libxmount.
    NOTE: expects xmount installation under
    $1/../xmount/trunk/build/release_build/

    Provides the following file:
    "$1/libxmount_input_qemu.so"

    Example:

    `build_compile_qemu_xmount /qemu_source /xmount/installation`
    '
    pushd "$1"
    local xmount_installation="../xmount/trunk/build/release_build/usr"
    [ ! -z "$2" ] && xmount_installation="$2"
    ./configure --enable-xmount-input --python="$(which python2)" \
        --extra-cflags="-std=gnu99" \
        --disable-werror \
        --extra-cflags="-fPIC" \
        --extra-cflags="-I${xmount_installation}/include" \
        --extra-cflags="-I${xmount_installation}/include/xmount" \
        --disable-fdt --target-list=""
    make -j libxmount_input_qemu.so
    local ret=$?
    popd
    return $ret
}
build_clean_qemu_xmount() {
    local __doc__='Clean the build of `build_compile_qemu_xmount`.'
    pushd "$1"
    make clean
    local ret=$?
    popd
    return $ret
}
build_compile_xmount() {
    local __doc__='
    Compiles xmount.

    Provides the xmount installation under:
    "$1/trunk/build/release_build/"

    Example:

    `build_compile_xmount /xmount_source /xmount_source/build /usr`
    '
    pushd "$1"

    local destination_directory="./release_build"
    [ ! -z "$2" ] && destination_directory="$2"
    local install_prefix="/usr"
    [ ! -z "$3" ] && install_prefix="$3"

    mkdir --parents trunk/build
    cd trunk/build || return 1
    cmake -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX="$install_prefix" ..
    make -j
    make install DESTDIR="$destination_directory"
    local ret=$?
    popd
    return $ret
}
build_clean_xmount() {
    local __doc__='Clean the build of `build_compile_xmount`.'
    rm --recursive --force "$1/trunk/build"
}
build_compile_dnbd3() {
    local __doc__='
    Compiles dnbd3 kernel module and client.

    Provides the following file:
    "$1/build/dnbd3.ko"
    "$1/build/dnbd3-client"

    Examples:

    `build_compile_dnbd3 path/to/dnbd3/source/`

    Passing the kernel version to cmake:
    `CMAKE_FLAGS="-DKERNEL_VERSION=4.14.6-openslx+" \
        build_compile_dnbd3 path/to/dnbd3/source/`
    '
    pushd "$1"
    # NOTE: The generic way would be: "./build.sh" but this tries to build
    # more than we really need and takes more time.
    mkdir --parents build
    cd build
    # Inject CMAKE_FLAGS as a way to control how cmake is called,
    # e.g. to pass the kernel version
    cmake ${CMAKE_FLAGS} \
        -DBUILD_FUSE_CLIENT=OFF \
        -DBUILD_KERNEL_MODULE=ON \
        -DBUILD_SERVER=OFF \
        -DBUILD_STRESSTEST=OFF \
        ../
    make -j dnbd3 dnbd3-client
    local ret=$?
    popd
    return $ret
}
build_clean_dnbd3() {
    local __doc__='Clean the build of `build_compile_dnbd3`.'
    rm --recursive --force "$1/build"
    return $?
}
build_compile_kernel_qcow() {
    pushd "$1"
    # https://lab.ks.uni-freiburg.de/projects/kernel-qcow2/wiki
    if [ -z "$kernel_headers" ]; then
        logging.error "Kernel header directory not set, ignoring."
        return 1
    fi
    mkdir -p "${kernel_headers}/drivers/block/loop"
    for _file in \
        include/uapi/linux/loop.h \
        drivers/block/Kconfig \
        drivers/block/loop/loop_main.h \
        drivers/block/loop/loop_file_fmt.h \
        drivers/block/loop/Kconfig; do
            cp -f "${_file}" "${kernel_headers}/${_file}"
    done
    (
        set -o errexit
        make -C "$kernel_headers" CONFIG_BLK_DEV_LOOP=m \
            M=$(pwd)/drivers/block/loop loop.ko
        make -C "$kernel_headers" CONFIG_BLK_DEV_LOOP_FILE_FMT_RAW=m \
            M=$(pwd)/drivers/block/loop loop_file_fmt_raw.ko
        make -C "$kernel_headers" CONFIG_BLK_DEV_LOOP_FILE_FMT_QCOW=m \
            M=$(pwd)/drivers/block/loop loop_file_fmt_qcow.ko
    )
    local ret=$?
    popd
    return $ret
}
build_clean_kernel_qcow() {
    if [ -z "$kernel_headers" ]; then
        logging.error "Kernel header directory not set, ignoring."
        return 1
    fi
    pushd "$kernel_headers"
    make -C "$kernel_headers" M=$(pwd)/drivers/block/loop clean
    local ret=$?
    popd
    return $ret
}
build_compile_losetup_qcow() {
    pushd "$1"
    (
        set -o errexit
        ./autogen.sh
        ./configure
        make -j losetup
    )
    local ret=$?
    popd
    return $ret
}
build_clean_losetup_qcow() {
    pushd "$1"
    make clean
    local ret=$?
    popd
    return $ret
}
build_compile_systemd_preserve_process_marker() {
    local __doc__='
    Compiles simple c program.

    Examples:

    `build_compile_systemd_preserve_process_marker path/to/program/folder`
    '
    pushd "$1"
    make
    local ret=$?
    popd
    return $ret
}
build_clean_systemd_preserve_process_marker() {
    local __doc__='
    Clean the build of
    `build_compile_systemd_preserve_process_marker`.
    '
    pushd "$1"
    make clean
    local ret=$?
    popd
    return $ret
}
# region vim modline
# vim: set tabstop=4 shiftwidth=4 expandtab:
# vim: foldmethod=marker foldmarker=region,endregion:
# endregion