summaryrefslogtreecommitdiffstats
path: root/builder/dnbd3-rootfs/scripts/utils.sh
blob: 881df8207d36ef482699ba0dc84fb20be3451aa2 (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
UTILS_STANDARD_OUTPUT=/dev/null
UTILS_ERROR_OUTPUT=/dev/null
UTILS_VERBOSE=false

function utils_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 [ "$UTILS_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
}
function utils_compile_nbd() {
    # Downloads and compiles nbd.
    #
    # Examples:
    #
    # >>> build_initramfs_compile_nbd path/to/nbd/directory/
    # ...
    # Provides the following file:
    # "$1/nbd.ko"
    pushd "$1" 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" && \
    utils_log 'Compile the nbd kernel module.' && \
    make 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" && \
    popd 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT"
    return $?
    # TODO make clean
}
function utils_compile_dnbd3() {
    # Downloads and compiles dnbd3.
    #
    # Examples:
    #
    # >>> build_initramfs_compile_dnbd3 path/to/dnbd3/directory/
    # ...
    # Provides the following file:
    # "$1/build/dnbd3.ko"
    pushd "$1/.." 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" && \
    rm --recursive --force "$1" 1>"$UTILS_STANDARD_OUTPUT" \
        2>"$UTILS_ERROR_OUTPUT" && \
    git clone git://git.openslx.org/dnbd3.git \
        1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" && \
    cd dnbd3 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" && \
    ./build.sh 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" && \
    popd 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT"
    return $?
    # TODO rm -rf build
}
function utils_compile_systemd_preserve_process_marker() {
    # Compiles simple c program.
    #
    # Examples:
    #
    # >>> utils_compile_systemd_preserve_process_marker path/to/program/folder
    pushd "$1" 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" && \
    make 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT" && \
    popd 1>"$UTILS_STANDARD_OUTPUT" 2>"$UTILS_ERROR_OUTPUT"
    return $?
}
function utils_dependency_check() {
    # This function check if all given dependencies are present.
    #
    # Examples:
    #
    # >>> utils_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
}
function utils_create_partition_via_offset() {
    local device="$1"
    local nameOrUUID="$2"
    local loopDevice=$(losetup -f)

    local sectorSize=$(blockdev --getbsz $device)
    local partitionInfo=$(partx --raw --noheadings --output START,NAME,UUID \
        $device 2>/dev/null| grep $nameOrUUID)
    local offsetSectors=$(echo $partitionInfo | cut -d' ' -f1)
    if [ -z "$offsetSectors" ]; then
        warn "could not find partition with label/uuid '$nameOrUUID' on device $device"
        return 1
    fi
    #warn $(($offsetSectors*512)) # could overflow on 32bit systems
    local offsetBytes=$(echo | awk -v x=$offsetSectors -v y=$sectorSize '{print x * y}')

    # test if mount works directly (problem with btrfs device id)
    #mount -v -o loop,offset=$offsetBytes $device $mountPoint
    losetup -v -o $offsetBytes $loopDevice $device
    echo $loopDevice
}