summaryrefslogtreecommitdiffstats
path: root/builder/dnbd3-rootfs/module-setup.sh
blob: 05bb877216907d6c5ac2e6d033056584672126fa (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
#!/bin/bash

source "$(dirname "${BASH_SOURCE[0]}")/scripts/rebash/core.sh"
core.import exceptions
core.import logging
core.import utils

# TODO set log level via dracut logging options
logging.set_log_level debug
logging.set_commands_log_level debug

core.import "$(core_abs_path "$(dirname "${BASH_SOURCE[0]}")/scripts/build.sh")"

clean() {
    # NOTE: This method is currently triggered manually and not supported by
    # dracut itself.

    if [[ -f "$moddir/binaries/nbd/nbd.ko" ]]; then
        build_clean_nbd "$moddir/binaries/nbd/"
    fi
    if [[ -f "$moddir/binaries/dnbd3/build/dnbd3.ko" || ! -f "$moddir/binaries/dnbd3/build/dnbd3-client" ]]; then
        build_clean_dnbd3 "$moddir/binaries/dnbd3/"
    fi
    if [[ -f "$moddir/binaries/systemd-preserve-process-marker/systemd-preserve-process-marker" ]]; then
        build_clean_systemd_preserve_process_marker \
            "$moddir/binaries/systemd-preserve-process-marker/"
    fi
    return 0
}

check() {

    exceptions.activate

    if [[ ! -f "$moddir/binaries/nbd/nbd.ko" ]]; then
        build_compile_nbd "$moddir/binaries/nbd/"
    fi
    if [[ ! -f "$moddir/binaries/dnbd3/build/dnbd3.ko" ]] || \
       [[ ! -f "$moddir/binaries/dnbd3/build/dnbd3-client" ]]
    then
        build_compile_dnbd3 "$moddir/binaries/dnbd3/"
    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/"
    fi
    if [[ ! -f "$moddir/binaries/qemu/qemu-nbd" ]]; then
        build_compile_qemu_nbd "$moddir/binaries/qemu/"
    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"
    fi
    # - "/usr/bin/sh" isn't available.
    if [[ ! -f /usr/bin/sh ]] && [[ -f /bin/sh ]]; then
        ln --symbolic --force /bin/sh /usr/bin/sh
    fi

    exceptions.deactivate

    return 0
}

depends() {
    # DEBUG: echo base network bash kernel-modules systemd-initrd dracut-systemd fs-lib rootfs-block
    echo base network bash kernel-modules
    # NOTE: btrfs module should be used if it would work.
}

installkernel() {
    inst "$moddir/binaries/dnbd3/build/dnbd3.ko" \
        /usr/lib/modules/current/extra/dnbd3.ko
    inst "$moddir/binaries/nbd/nbd.ko" \
        /usr/lib/modules/current/extra/nbd.ko

    # Debugging:
    instmods cfg80211 rfkill vboxsf nls_iso8859_1 ppdev nls_cp437 iosf_mbi \
        vfat fat snd_intel8x0 crct10dif_pclmul crc32_pclmul input_leds \
        led_class aesni_intel evdev aes_x86_64 lrw joydev gf128mul \
        snd_ac97_codec glue_helper ablk_helper cryptd psmouse ac97_bus \
        snd_pcm vboxvideo mousedev serio_raw pcspkr mac_hid snd_timer battery \
        video snd ac acpi_cpufreq processor button parport_pc e1000 parport \
        drm intel_agp soundcore intel_gtt vboxguest i2c_piix4 sch_fq_codel \
        ip_tables x_tables hid_generic usbhid hid xor raid6_pq sr_mod \
        cdrom sd_mod ata_generic pata_acpi atkbd libps2 i8042 serio ohci_pci \
        ohci_hcd crc32c_intel usbcore ahci usb_common libahci ata_piix libata \
        scsi_mod \
        ext4 dm_snapshot btrfs crc32c
    # Production:
    # instmods ext4 dm_snapshot btrfs crc32c
    # NOTE: btrfs crc32c should usually be included by the btrfs dracut module.
}

install() {

    # region binaries

    inst "$moddir/binaries/dnbd3/build/dnbd3-client" /usr/bin/dnbd3-client
    inst "$moddir/binaries/systemd-preserve-process-marker/systemd-preserve-process-marker" \
        /usr/bin/systemd-preserve-process-marker
    # TODO: static linked qemu-nbd still needed?
    inst "$moddir/binaries/qemu/qemu-nbd" /usr/bin/qemu-nbd

    # endregion

    # region hooks

    inst_hook cmdline 00 "$moddir/hooks/enable-sysrq.sh"
    inst_hook cmdline 10 "$moddir/hooks/prepare-kernel-command-line-parameter.sh"
    inst_hook cmdline 90 "$moddir/hooks/set-dracut-environment-variables.sh"

    # load nbd.ko, dnbd3.ko
    inst_hook pre-udev 00 "$moddir/hooks/load-custom-kernel-modules.sh"

    if dracut_module_included "systemd-initrd"; then
        # TODO analyze qemu-nbd pivot-root bug!
        inst_script "$moddir/services/dnbd3-generator.sh" \
            $systemdutildir/system-generators/dnbd3-generator
        inst "$moddir/hooks/prepare-root-partition.sh" \
            /sbin/dnbd3root
        inst "$moddir/hooks/fetch-config.sh" \
            /sbin/fetch-config
        #region old approach
        #inst_simple "${moddir}/services/dnbd3root.service" \
            #"${systemdsystemunitdir}/dnbd3root.service"
        #mkdir -p "${initdir}/${systemdsystemunitdir}/sysinit.target.wants"
        #ln_r "${systemdsystemunitdir}/dnbd3root.service" \
            #"${systemdsystemunitdir}/sysinit.target.wants/dnbd3root.service"
        # don't kill at switch_root
        #mkdir -p "${initdir}/${systemdsystemunitdir}/initrd-root-fs.target.wants"
        #ln_r "${systemdsystemunitdir}/dnbd3root.service" \
            #"${systemdsystemunitdir}/initrd-root-fs.target.wants/dnbd3root.service"
        # todo don't kill after switch_root
        #works if initrd-root-fs wants dnbd3.service (on /sysroot)
        #endregion
    else
        # get the openslx config from the servers configured in the kernel command
        # line (${SLX_SERVER}/${SLX_SERVER_BASE}/config)
        inst_hook pre-mount 00 "$moddir/hooks/fetch-config.sh"
        # make the final blockdevice for the root system (dnbd3 -> qemu-nbd ->
        # device-mapper)
        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"
    #inst_hook pre-pivot 00 "$moddir/hooks/mount-tmp.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

    # TODO currently not used
    # This script is triggered by udev upon finding the right partitions for
    # persistent sessions.
    #inst "$moddir/scripts/prepare-persistent-disks" \
        #/sbin/prepare-persistent-disks

    # endregion

    # region configuration files

    # TODO currently not used
    # Udev rules detecting 44, 45, 46 partitions and running
    # "prepare-persistent-disks" to prepare persistent partitions.
    #inst "$moddir/udev/70-openslx-disk.rules" /etc/udev/rules.d/70-openslx-disk.rules

    # Use terminal readline settings from the template system.
    inst /etc/inputrc /etc/inputrc

    # endregion

    # Debugging:
    inst_multiple lsblk ping ip ifconfig sshd htop tail head vim touch sed \
        lsmod sleep route wget find lsof strace chroot switch_root pivot_root \
        mount fdisk mkfs.xfs env busybox ps agetty tree \
        insmod blockdev partx dmsetup sed cut awk tr insmod \ #qemu-nbd \
        blockdev partx dmsetup cat cut awk losetup dd mkfs.ext4 grep \
        basename dirname sort mktemp diff qemu-img
    # Production:
    # inst_multiple insmod blockdev partx dmsetup sed cat cut awk tr touch \
    #     losetup grep basename dirname mktemp diff sleep wget mount sort \
    #     qemu-nbd bash
}

# region vim modline

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

# endregion