summaryrefslogtreecommitdiffstats
path: root/builder/modules.d/dnbd3-rootfs/module-setup.sh
blob: 1dd16cd4a2a0bbce514e378dfc23f0b6e96718cf (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
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
# region imports
source "$(dirname "${BASH_SOURCE[0]}")/scripts/rebash/core.sh"
core.import exceptions
core.import logging
core.import utils
core.import "$(core_abs_path "$(dirname "${BASH_SOURCE[0]}")/helper/build.inc")"
# endregion
# region forward "build-initrfams.sh" logging configuration if present.
_parse_dracut_args() {
    local __doc__='
    Set log level via dracut logging options and returns current debug state.

    >>> echo "$_debug"
    1
    >>> _parse_dracut_args; echo $?
    1

    >>> _parse_dracut_args --stdlog 3; echo $?

    >>> _parse_dracut_args --stdlog 4; echo $?
    >>> logging.get_commands_level
    >>> logging.get_level
    0
    debug
    debug

    >>> logging.get_level
    critical
    >>> _parse_dracut_args --stdlog 4 --verbose
    >>> logging.get_level
    debug

    >>> _parse_dracut_args --stdlog 4 --unknown-dracut-option; echo $?
    0
    '
    local verbose=false
    local debug=false
    while true; do
        case "$1" in
            --stdlog)
                shift
                local level="$1"
                shift
                [[ "$level" -ge 4 ]] && debug=true
                ;;
            --verbose)
                shift
                verbose=true
                ;;
            '')
                break
                ;;
            *)
                shift
                ;;
        esac
        local level
        $verbose && level=info
        $debug && level=debug
        logging.set_level "$level"
        logging.set_commands_level debug
    done
    $debug
    return $?
}
_debug=0
_parse_dracut_args ${dracut_args[*]} || _debug=$?
# endregion
# region initialize function to build required components

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

    Example:

    `clean`
    '
    . "${moddir}/helper/build.inc"
    clean_components
    return 0
}
# endregion
# region dracut plugin api
check() {
    local __doc__='
    Checks wether all template system assumptions are satisfied.

    Example:

    `check`
    '
    exceptions.activate
    if ! build_initialize_components; then
        logging.error "Failed to initialize components."
        return 1
    fi
    # NOTE: xmount must be compiled before qemu_xmount
    local xmount_is_built=true
    if [[ ! -f "$moddir/binaries/xmount/trunk/build/src/xmount" ]] ||
        [[ ! -f "$moddir/binaries/qemu-xmount/libxmount_input_qemu.so" ]]; then
            xmount_is_built=false
    fi
    $xmount_is_built || logging.warn \
        "Compiling \"xmount\" failed."

    if [[ ! -f "$moddir/binaries/dnbd3/build/dnbd3.ko" ]] || \
       [[ ! -f "$moddir/binaries/dnbd3/build/dnbd3-client" ]]
    then
        CMAKE_FLAGS="-DKERNEL_VERSION=${kernel}" \
            build_compile_dnbd3 "$moddir/binaries/dnbd3/"
        [[ $? != 0 ]] && return 1
    fi
    if [[ ! -f "$moddir/binaries/systemd-preserve-process-marker/systemd-preserve-process-marker" ]]; then
        build_compile_systemd_preserve_process_marker \
            "$moddir/binaries/systemd-preserve-process-marker/"
            [[ $? != 0 ]] && return 1
    fi
    # NOTE: This are workarounds for:
    # - distributions where "systemd-udevd" doesn't lives in "/usr/lib" but in
    #   "/lib".
    local alternate_systemd_udevd_location='/lib/systemd/systemd-udevd'
    if [[ ! -f "${systemdutildir}/systemd-udevd" ]] && \
    [[ -f "$alternate_systemd_udevd_location" ]]; then
        mkdir --parents "${initdir}${systemdutildir}"
        ln --symbolic --force "$alternate_systemd_udevd_location" \
            "${initdir}${systemdutildir}/systemd-udevd"
        #cp "$alternate_systemd_udevd_location" \
        #    "${initdir}${systemdutildir}/systemd-udevd" 1>&2
    fi
    # - "/usr/bin/sh" isn't available but "/bin/sh".
    if [[ ! -f /usr/bin/sh ]] && [[ -f /bin/sh ]]; then
        ln --symbolic --force /bin/sh /usr/bin/sh
    fi
    exceptions.deactivate
    # Tell dracut that this module should only be included if it is required
    # explicitly.
    return 255
}
depends() {
    local __doc__='
    Outputs all dependent dracut modules to make this module work.

    >>> depends
    +doc_test_contains
    base
    '
    #local network_module="network"
    #dracut_module_included "systemd-initrd" && network_module="systemd-networkd-ext"
    echo base bash kernel-modules shutdown slx-dmsetup slx-network # "$network_module"
}
installkernel() {
    local __doc__='
    Copies all needed kernel modules into initramfs file needed work at
    runtime.

    Example:

    `installkernel`
    '
    local block_kmod_dir="/lib/modules/${kernel}/kernel/drivers/block"

    # dnbd3
    mkdir -p "${initdir}/${block_kmod_dir}"
    inst "${moddir}/binaries/dnbd3/build/dnbd3.ko" "${block_kmod_dir}/dnbd3.ko"

    # kqcow2 kernel
    local kqcow_loop_kmod_dir="${moddir}/binaries/kernel-qcow2-linux/drivers/block/loop"
    if [ -e "${kqcow_loop_kmod_dir}/loop.ko" ] && \
        [ -e "${kqcow_loop_kmod_dir}/loop_file_fmt_raw.ko" ] && \
        [ -e "${kqcow_loop_kmod_dir}/loop_file_fmt_qcow.ko" ]; then
            mkdir -p "${initdir}/${block_kmod_dir}/loop"
            for kmod in "${kqcow_loop_kmod_dir}/"*.ko ; do
                inst "$kmod" "${block_kmod_dir}/loop/$(basename $kmod)"
            done
    fi
}
install() {
    local __doc__='
    Copies all needed files into the initramfs image and registers all needed
    dracut hooks.

    Example:

    `install`
    '
    # region binaries
    inst "$moddir/binaries/dnbd3/build/dnbd3-client" /usr/local/bin/dnbd3-client
    inst "$moddir/binaries/systemd-preserve-process-marker/systemd-preserve-process-marker" \
        /usr/local/bin/systemd-preserve-process-marker
    # xmount
    local \
        xmount_installation="$moddir/binaries/xmount/trunk/build/release_build"
    if [[ -f "${xmount_installation}/usr/bin/xmount" ]]; then
        inst "${xmount_installation}/usr/bin/xmount" /usr/bin/xmount
        for file in ${xmount_installation}/usr/lib/xmount/*; do
            inst "$file" /usr/lib/xmount/"$(basename "$file")"
        done
        inst "$moddir/binaries/qemu-xmount/libxmount_input_qemu.so" \
            /usr/lib/xmount/libxmount_input_qemu.so
        # HACK fix missing libraries for some xmount input libs
        # TODO copy the dependencies using dracut functions...
        inst_libdir_file 'libafflib.so*'
        inst_libdir_file 'libewf.so*'
    fi
    # kqcow2 losetup
    local losetup_qcow2_dir="$moddir/binaries/kernel-qcow2-util-linux"
    if [[ -f "${losetup_qcow2_dir}/.libs/losetup" ]]; then
        inst "${losetup_qcow2_dir}/.libs/losetup" /usr/local/bin/losetup
	mkdir -p "${initdir}/usr/local/lib"
        cp -a "${losetup_qcow2_dir}/.libs/"*.so* "${initdir}/usr/local/lib"
    fi
    # endregion
    # region hooks
    inst_hook cmdline 00 "$moddir/hooks/enable-sysrq.sh"
    # NOTE: Can be used to support old style ip append syntax and have an
    # exclusive interface name - required when using dracut's regular
    # 'network' module
    dracut_module_included "network" && inst_hook cmdline 10 \
        "$moddir/hooks/prepare-kernel-command-line-parameter.sh"
    inst_hook cmdline 90 "$moddir/hooks/set-dracut-environment-variables.sh"
    inst_hook pre-udev 00 "$moddir/hooks/load-custom-kernel-modules.sh"
    # Get the openslx config from the servers configured in the kernel command
    # line (${SLX_SERVER}/${SLX_SERVER_BASE}/config).
    inst_hook pre-mount 10 "$moddir/hooks/fetch-config.sh"
    # make the final blockdevice for the root system (dnbd3 -> xmount ->
    # device-mapper)
    if dracut_module_included "systemd-initrd"; then
        inst "$moddir/hooks/prepare-root-partition.sh" \
            /usr/local/bin/dnbd3root
        inst_simple "${moddir}/services/dnbd3root.service" \
            "${systemdsystemunitdir}/dnbd3root.service"
        mkdir --parents \
            "${initdir}/${systemdsystemunitdir}/dracut-mount.service.requires"
        ln_r "${systemdsystemunitdir}/dnbd3root.service" \
            "${systemdsystemunitdir}/dracut-mount.service.requires/dnbd3root.service"
        mkdir --parents \
            "${initdir}/${systemdsystemunitdir}/initrd.target.requires"
        ln_r "${systemdsystemunitdir}/dnbd3root.service" \
            "${systemdsystemunitdir}/initrd.target.requires/dnbd3root.service"
        # Copy systemd services to new root (so they don't get killed after
        # switch_root)
        inst_hook pre-pivot 00 \
            "$moddir/hooks/copy-dnbd3-service-into-newroot.sh"
        inst_hook pre-pivot 00 \
            "$moddir/hooks/copy-dracut-systemd-files-into-newroot.sh"
        inst_hook pre-shutdown 00 "$moddir/hooks/shutdown-umount.sh"
    else
        inst_hook pre-mount 10 "$moddir/hooks/prepare-root-partition.sh"
    fi
    inst_hook mount 10 "$moddir/hooks/mount-root-device.sh"
    inst_hook pre-pivot 00 \
        "$moddir/hooks/copy-openslx-configuration-into-newroot.sh"
    # endregion
    # region scripts
    local file_path
    for file_path in "$moddir/scripts/rebash/"*; do
        inst "$file_path" "/usr/lib/rebash/$(basename "$file_path")"
    done
    #inst "$moddir/scripts/tools.sh" "/usr/lib/openslx/tools.sh"
    # endregion
    # region configuration files
    # Use terminal readline settings from the template system.
    inst /etc/inputrc /etc/inputrc
    # Set some aliases for the initramfs context.
    if [[ "$_debug" == 0 ]]; then
        inst "$moddir/configuration/bash" '/etc/bash.bashrc'
        inst "$moddir/configuration/bash" '/etc/profile.d/aliases'
    fi
    # endregion
    inst_multiple \
        awk \
        basename bash blockdev \
        cat cut curl \
        dd diff dirname dmsetup \
        find \
        grep \
        insmod \
        lsblk \
        mkfifo mktemp mount mountpoint \
        sed sleep sort \
        tee touch tr
}
# endregion
# region vim modline
# vim: set tabstop=4 shiftwidth=4 expandtab:
# vim: foldmethod=marker foldmarker=region,endregion:
# endregion