summaryrefslogtreecommitdiffstats
path: root/builder/dnbd3-rootfs/scripts
diff options
context:
space:
mode:
authortorben2015-12-03 13:19:11 +0100
committertorben2015-12-03 13:19:11 +0100
commit91c18206efce73861352a8ffc8fa3ce1b99f3d37 (patch)
tree9c56d9f551f705269d48f9c18394a133af55224c /builder/dnbd3-rootfs/scripts
parentWrite new routine bodys. (diff)
downloadsystemd-init-91c18206efce73861352a8ffc8fa3ce1b99f3d37.tar.gz
systemd-init-91c18206efce73861352a8ffc8fa3ce1b99f3d37.tar.xz
systemd-init-91c18206efce73861352a8ffc8fa3ce1b99f3d37.zip
Refactor utils lib.
Diffstat (limited to 'builder/dnbd3-rootfs/scripts')
-rw-r--r--builder/dnbd3-rootfs/scripts/utils.sh156
1 files changed, 84 insertions, 72 deletions
diff --git a/builder/dnbd3-rootfs/scripts/utils.sh b/builder/dnbd3-rootfs/scripts/utils.sh
index 881df820..599581a9 100644
--- a/builder/dnbd3-rootfs/scripts/utils.sh
+++ b/builder/dnbd3-rootfs/scripts/utils.sh
@@ -1,73 +1,66 @@
-UTILS_STANDARD_OUTPUT=/dev/null
-UTILS_ERROR_OUTPUT=/dev/null
-UTILS_VERBOSE=false
+source "$(dirname "${BASH_SOURCE[0]}")/rebash/core.sh"
+core.import logging
+core.import exceptions
-function utils_log() {
- # Handles logging messages. Returns non zero and exit on log level
- # error to support chaining the message into toolchain.
+exceptions.init
+
+function utils_compile_nbd() {
+ # Downloads and compiles nbd.
#
# 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
+ # >>> utils_compile_nbd path/to/nbd/directory/
+ # ...
+ # Provides the following file:
+ # "$1/nbd.ko"
+ pushd "$1"
+ logging.info 'Compile the nbd kernel module.'
+ make
+ popd
+ return $?
}
-function utils_compile_nbd() {
- # Downloads and compiles nbd.
+function utils_clean_nbd() {
+ # Cleans nbd specific generated files
#
# Examples:
#
- # >>> build_initramfs_compile_nbd path/to/nbd/directory/
+ # >>> utils_clean_nbd path/to/nbd/directory/
# ...
- # Provides the following file:
+ # Removes 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"
+ pushd "$1"
+ make clean
+ popd
return $?
- # TODO make clean
}
function utils_compile_dnbd3() {
# Downloads and compiles dnbd3.
#
# Examples:
#
- # >>> build_initramfs_compile_dnbd3 path/to/dnbd3/directory/
+ # >>> utils_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"
+ pushd "$1/.."
+ rm --recursive --force "$1"
+ git clone git://git.openslx.org/dnbd3.git
+ cd dnbd3
+ ./build.sh
+ popd
+ return $?
+}
+function utils_clean_dnbd3() {
+ # Removes generated dnbd3 specific files.
+ #
+ # Examples:
+ #
+ # >>> utils_clean_dnbd3 path/to/dnbd3/directory/
+ # ...
+ # Removes the following directory:
+ # "$1/build"
+ rm --recursive --force "$1/build"
return $?
- # TODO rm -rf build
}
function utils_compile_systemd_preserve_process_marker() {
# Compiles simple c program.
@@ -75,9 +68,20 @@ function utils_compile_systemd_preserve_process_marker() {
# 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"
+ pushd "$1"
+ make
+ popd
+ return $?
+}
+function utils_clean_systemd_preserve_process_marker() {
+ # Removes compiled simple c program.
+ #
+ # Examples:
+ #
+ # >>> utils_clean_systemd_preserve_process_marker path/to/program/folder
+ pushd "$1"
+ make clean
+ popd
return $?
}
function utils_dependency_check() {
@@ -87,36 +91,44 @@ function utils_dependency_check() {
#
# >>> utils_dependency_check "mkdir pacstrap mktemp"
# ...
- local dependenciesToCheck="$1" && \
- local result=0 && \
- local dependency && \
+ local dependenciesToCheck="$1"
+ local result=0
+ local dependency
for dependency in ${dependenciesToCheck[*]}; do
- if ! hash "$dependency" 1>"$_STANDARD_OUTPUT" 2>/dev/null; then
+ if ! hash "$dependency" 2>/dev/null; then
build_initramfs_log 'critical' \
- "Needed dependency \"$dependency\" isn't available." && \
+ "Needed dependency \"$dependency\" isn't available."
result=1
fi
done
return $result
}
-function utils_create_partition_via_offset() {
+function utils_mount_partition() {
+ # Mounts a partition at given offset.
+ #
+ # Examples:
+ #
+ # >>> utils_mount_partition /dev/DEVICE_NAME PARTITION_IDENTIFIER
+ # ...
local device="$1"
- local nameOrUUID="$2"
- local loopDevice=$(losetup -f)
+ local partition_identifier="$2"
+ local loop_device="$(losetup --find)"
- 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"
+ local sector_size="$(blockdev --getbsz "$device")"
+ local partition_info="(partx --raw --noheadings --output START,NAME,UUID \
+ "$device" 2>/dev/null| grep part)"
+ local offset_sectors="$(echo "$partition_identifier" | \
+ cut --delimiter=' ' --fields=1)"
+ if [ -z "$offset_sectors" ]; then
+ logging.warn "could not find partition with identifier \"$partition_identifier\" 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}')
+ # NOTE: Could overflow on 32bit systems
+ # logging.warn "$(("$offset_sectors"*512))"
+ local offset_bytes="$(echo | awk -v x="$offset_sectors" -v y="$sector_size" '{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
+ # NOTE: Test if mount works directly (problem with btrfs device id).
+ # mount --verbose --options loop,offset="$offset_bytes" "$device" "$mount_point"
+ losetup --verbose --offset "$offset_bytes" "$loop_device" "$device"
+ echo "$loop_device"
}