summaryrefslogtreecommitdiff
path: root/Meta/shell_include.sh
blob: 11da2e1999e61d8a01f51ebdd42a4c441cc47f9d (plain)
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
# shellcheck shell=bash
# shellcheck disable=SC2034
# SC2034: "Variable appears unused. Verify it or export it."
#         Those are intentional here, as the file is meant to be included elsewhere.

# NOTE: If using another privilege escalation binary make sure it is configured or has the appropiate flag
#       to keep the current environment variables in the launched process (in sudo's case this is achieved
#       through the -E flag described in sudo(8).
SUDO="sudo -E"

if [ "$(uname -s)" = "SerenityOS" ]; then
    SUDO="pls -E"
fi

die() {
    echo "die: $*"
    exit 1
}

exit_if_running_as_root() {
    if [ "$(id -u)" -eq 0 ]; then
       die "$*"
    fi
}

find_executable() {
  paths=("/usr/sbin" "/sbin")

  if [ "$(uname -s)" = "Darwin" ]; then
    if [ -n "${HOMEBREW_PREFIX}" ]; then
      paths+=("${HOMEBREW_PREFIX}/opt/e2fsprogs/bin" "${HOMEBREW_PREFIX}/opt/e2fsprogs/sbin")
    elif command -v brew > /dev/null 2>&1; then
      if prefix=$(brew --prefix e2fsprogs 2>/dev/null); then
        paths+=("${prefix}/bin" "${prefix}/sbin")
      fi
    fi
  fi

  executable="${1}"

  # Prefer tools from PATH over fallback paths
  if command -v "${executable}"; then
    return 0
  fi

  for path in "${paths[@]}"; do
    if command -v "${path}/${executable}"; then
      return 0
    fi
  done

  # We return the executable's name back to provide meaningful messages on future failure
  echo "${executable}"
}

FUSE2FS_PATH="$(find_executable fuse2fs)"
RESIZE2FS_PATH="$(find_executable resize2fs)"
E2FSCK_PATH="$(find_executable e2fsck)"
MKE2FS_PATH="$(find_executable mke2fs)"

get_number_of_processing_units() {
  number_of_processing_units="nproc"
  SYSTEM_NAME="$(uname -s)"

  if [ "$SYSTEM_NAME" = "OpenBSD" ]; then
      number_of_processing_units="sysctl -n hw.ncpuonline"
  elif [ "$SYSTEM_NAME" = "FreeBSD" ]; then
      number_of_processing_units="sysctl -n hw.ncpu"
  elif [ "$SYSTEM_NAME" = "Darwin" ]; then
      number_of_processing_units="sysctl -n hw.ncpu"
  fi

  ($number_of_processing_units)
}