diff options
author | Simon Rettberg | 2024-02-08 16:55:49 +0100 |
---|---|---|
committer | Simon Rettberg | 2024-02-08 16:55:49 +0100 |
commit | 5a75d2127e131ebad2314b6922ecfad4d2d8aa82 (patch) | |
tree | 264ffd1a6996211616ef9426c4848a2958043c6e | |
parent | [dnbd3-rootfs] Make sure to copy dnbd3-client binary again (diff) | |
download | systemd-init-5a75d2127e131ebad2314b6922ecfad4d2d8aa82.tar.gz systemd-init-5a75d2127e131ebad2314b6922ecfad4d2d8aa82.tar.xz systemd-init-5a75d2127e131ebad2314b6922ecfad4d2d8aa82.zip |
Add slx_service helper function to add service-script to init
This should be called in a module install script, to add a script
in <moddir>/hooks/ to the init, and create a service file with
configurable dependencies.
slx_service <script name without .sh> <service description> [options]
Options are:
--before, --after, --wants, --requires, --wbefore --wafter
--wbefore is shorthand for doing
--before <something> --wants <something>
same for --wafter.
For example, assuming a script ./hooks/s3-init-foo.sh, call
slx_service "s3-init-foo" "Initialize all foo" --wafter \
s3-finish-bar.service --before s3-something.service
-rwxr-xr-x | build-initramfs.sh | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/build-initramfs.sh b/build-initramfs.sh index e41a642a..5b5e9597 100755 --- a/build-initramfs.sh +++ b/build-initramfs.sh @@ -348,6 +348,76 @@ initialize_dracut() { ./configure || exit 1 make dracut-install || exit 1 make dracut-util + # add a few handy functions to dracut + if ! [ -s dracut-init.sh ]; then + echo "no dracut-init.sh" + exit 1 + fi + # One tab then 4 spaces, tab will get removed by -EOF + cat >> dracut-init.sh <<-"EOF" + + slx_service() { + local _name _desc _tmpfile + declare -a _before=() _after=() _requires=() _wants=() + _name="$1" + _desc="$2" + shift 2 + while (( $# > 0 )); do + case "$1" in + --wbefore) + _wants+=("$2") + ;& + --before) + _before+=("$2") + ;; + --wafter) + _wants+=("$2") + ;& + --after) + _after+=("$2") + ;; + --wants) + _wants+=("$2") + ;; + --requires) + _requires+=("$2") + ;; + *) + dfatal "Invalid option: '$1'" + exit 10 + esac + shift 2 + done + if [ -z "$_desc" ]; then + dfatal "Cannot install service without description" + exit 1 + fi + mkdir --parents "${initdir}/usr/local/slx-services" + _tmpfile="/tmp/dracut-service-$RANDOM" + inst "$moddir/hooks/${_name}.sh" \ + "/usr/local/slx-services/${_name}.sh" || exit 10 + { + echo "[Unit]" + echo "Description=${_desc}" + [ -n "${_wants}" ] && echo "Wants=${_wants[*]}" + [ -n "${_requires}" ] && echo "Requires=${_requires[*]}" + [ -n "${_before}" ] && echo "Before=${_before[*]}" + [ -n "${_after}" ] && echo "After=${_after[*]}" + echo "" + echo "[Service]" + echo "Type=oneshot" + echo "RemainAfterExit=yes" + echo "ExecStart=/usr/local/slx-services/${_name}.sh" + } > "${_tmpfile}" + inst_simple "${_tmpfile}" \ + "${systemdsystemunitdir}/${_name}.service" || exit 10 + rm -f -- "${_tmpfile}" + mkdir --parents \ + "${initdir}/${systemdsystemunitdir}/initrd.target.wants" || exit 10 + ln_r "${systemdsystemunitdir}/${_name}.service" \ + "${systemdsystemunitdir}/initrd.target.wants/${_name}.service" || exit 10 + } + EOF popd || exit 1 return $? } |