]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/99base/dracut-dev-lib.sh
63265f21013183fbc94eaff6fb8a9e27b6bca279
[thirdparty/dracut.git] / modules.d / 99base / dracut-dev-lib.sh
1 #!/bin/sh
2
3 # replaces all occurrences of 'search' in 'str' with 'replacement'
4 #
5 # str_replace str search replacement
6 #
7 # example:
8 # str_replace ' one two three ' ' ' '_'
9 str_replace() {
10 local in="$1"
11 local s="$2"
12 local r="$3"
13 local out=''
14
15 while [ "${in##*"$s"*}" != "$in" ]; do
16 chop="${in%%"$s"*}"
17 out="${out}${chop}$r"
18 in="${in#*"$s"}"
19 done
20 printf -- '%s' "${out}${in}"
21 }
22
23 # get a systemd-compatible unit name from a path
24 # (mimicks unit_name_from_path_instance())
25 dev_unit_name() {
26 local dev="$1"
27
28 if command -v systemd-escape > /dev/null; then
29 systemd-escape -p -- "$dev"
30 return $?
31 fi
32
33 if [ "$dev" = "/" -o -z "$dev" ]; then
34 printf -- "-"
35 return 0
36 fi
37
38 dev="${1%%/}"
39 dev="${dev##/}"
40 # shellcheck disable=SC1003
41 dev="$(str_replace "$dev" '\' '\x5c')"
42 dev="$(str_replace "$dev" '-' '\x2d')"
43 if [ "${dev##.}" != "$dev" ]; then
44 dev="\x2e${dev##.}"
45 fi
46 dev="$(str_replace "$dev" '/' '-')"
47
48 printf -- "%s" "$dev"
49 }
50
51 # set_systemd_timeout_for_dev [-n] <dev> [<timeout>]
52 # Set 'rd.timeout' as the systemd timeout for <dev>
53 set_systemd_timeout_for_dev() {
54 local _name
55 local _needreload
56 local _noreload
57 local _timeout
58
59 [ -z "$DRACUT_SYSTEMD" ] && return 0
60
61 if [ "$1" = "-n" ]; then
62 _noreload=1
63 shift
64 fi
65
66 if [ -n "$2" ]; then
67 _timeout="$2"
68 else
69 _timeout=$(getarg rd.timeout)
70 fi
71
72 _timeout=${_timeout:-0}
73
74 _name=$(dev_unit_name "$1")
75 if ! [ -L "${PREFIX}/etc/systemd/system/initrd.target.wants/${_name}.device" ]; then
76 [ -d "${PREFIX}"/etc/systemd/system/initrd.target.wants ] || mkdir -p "${PREFIX}"/etc/systemd/system/initrd.target.wants
77 ln -s ../"${_name}".device "${PREFIX}/etc/systemd/system/initrd.target.wants/${_name}.device"
78 type mark_hostonly > /dev/null 2>&1 && mark_hostonly /etc/systemd/system/initrd.target.wants/"${_name}".device
79 _needreload=1
80 fi
81
82 if ! [ -f "${PREFIX}/etc/systemd/system/${_name}.device.d/timeout.conf" ]; then
83 mkdir -p "${PREFIX}/etc/systemd/system/${_name}.device.d"
84 {
85 echo "[Unit]"
86 echo "JobTimeoutSec=$_timeout"
87 echo "JobRunningTimeoutSec=$_timeout"
88 } > "${PREFIX}/etc/systemd/system/${_name}.device.d/timeout.conf"
89 type mark_hostonly > /dev/null 2>&1 && mark_hostonly /etc/systemd/system/"${_name}".device.d/timeout.conf
90 _needreload=1
91 fi
92
93 if [ -z "$PREFIX" ] && [ "$_needreload" = 1 ] && [ -z "$_noreload" ]; then
94 /sbin/initqueue --onetime --unique --name daemon-reload systemctl daemon-reload
95 fi
96 }
97
98 # wait_for_dev <dev> [<timeout>]
99 #
100 # Installs a initqueue-finished script,
101 # which will cause the main loop only to exit,
102 # if the device <dev> is recognized by the system.
103 wait_for_dev() {
104 local _name
105 local _noreload
106
107 if [ "$1" = "-n" ]; then
108 _noreload=-n
109 shift
110 fi
111
112 _name="$(str_replace "$1" '/' '\x2f')"
113
114 type mark_hostonly > /dev/null 2>&1 && mark_hostonly "$hookdir/initqueue/finished/devexists-${_name}.sh"
115
116 [ -e "${PREFIX}$hookdir/initqueue/finished/devexists-${_name}.sh" ] && return 0
117
118 printf '[ -e "%s" ]\n' "$1" \
119 >> "${PREFIX}$hookdir/initqueue/finished/devexists-${_name}.sh"
120 {
121 printf '[ -e "%s" ] || ' "$1"
122 printf 'warn "\"%s\" does not exist"\n' "$1"
123 } >> "${PREFIX}$hookdir/emergency/80-${_name}.sh"
124
125 set_systemd_timeout_for_dev $_noreload "$@"
126 }
127
128 cancel_wait_for_dev() {
129 local _name
130 _name="$(str_replace "$1" '/' '\x2f')"
131 rm -f -- "$hookdir/initqueue/finished/devexists-${_name}.sh"
132 rm -f -- "$hookdir/emergency/80-${_name}.sh"
133 if [ -n "$DRACUT_SYSTEMD" ]; then
134 _name=$(dev_unit_name "$1")
135 rm -f -- "${PREFIX}/etc/systemd/system/initrd.target.wants/${_name}.device"
136 rm -f -- "${PREFIX}/etc/systemd/system/${_name}.device.d/timeout.conf"
137 /sbin/initqueue --onetime --unique --name daemon-reload systemctl daemon-reload
138 fi
139 }