简体   繁体   中英

How to convert systemv startup scripts to systemd services?

Advice on converting SysVinit file to Systemd services will be helpful.

Currently, I am using a systemv init script which will be executed after every boot on my STM32MP1 based Avenger96 board. Now I have to switch to Systemd from SysVinit. But I am not sure how to convert the init file to relevant systemd files. I am using Yocto with Ubuntu20.04 as build system. If someone help me to get started would be really great. Below is the init script and image recipe which establishes symlink to the init script.

custom-script.sh which is installed in etc/init.d/ directory of the rootfs.

#!/bin/sh

DAEMON="swupdate"
PIDFILE="/var/run/$DAEMON.pid"

PART_STATUS=$(sgdisk -A 4:get:2 /dev/mmcblk0)
if test "${PART_STATUS}" = "4:2:1" ; then
    ROOTFS=rootfs-2
else
    ROOTFS=rootfs-1
fi

if test -f /update-ok ; then
    SURICATTA_ARGS="-c 2"
    rm -f /update-ok
fi

start() {
    printf 'Starting %s: ' "$DAEMON"
    # shellcheck disable=SC2086 # we need the word splitting
    start-stop-daemon -b -q -m -S -p "$PIDFILE" -x "/usr/bin/$DAEMON" \
        -- -f /etc/swupdate/swupdate.cfg -L -e rootfs,${ROOTFS} -u "${SURICATTA_ARGS}"
    status=$?
    if [ "$status" -eq 0 ]; then
        echo "OK"
    else
        echo "FAIL"
    fi
    return "$status"
}

stop() {
    printf 'Stopping %s: ' "$DAEMON"
    start-stop-daemon -K -q -p "$PIDFILE"
    status=$?
    if [ "$status" -eq 0 ]; then
        rm -f "$PIDFILE"
        echo "OK"
    else
        echo "FAIL"
    fi
    return "$status"
}

restart() {
    stop
    sleep 1
    start
}

case "$1" in
        start|stop|restart)
        "$1";;
    reload)
        # Restart, since there is no true "reload" feature.
        restart;;
        *)
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac

Image recipe which creates init.d dir and installs above script and also establishes symlink to rc4.d dir.

custom-image.bb
.
.
inherit update-rc.d

SRC_URI = "file://custom-script.sh \
          "

S = "${WORKDIR}"

INITSCRIPT_PACKAGES = "${PN}"
INITSCRIPT_NAME = "custom-script.sh"
INITSCRIPT_PARAMS = "start 99 2 3 4 5 . "

do_install_append() {
    install -d ${D}${sysconfdir}/rc4.d
    install -d 644 ${D}${sysconfdir}/init.d
    install -m 0755 ${WORKDIR}/custom-script.sh ${D}${sysconfdir}/init.d
    ln -sf ../init.d/custom-script.sh ${D}${sysconfdir}/rc4.d/S99custom-script.sh
    ln -sf ../init.d/custom-script.sh ${D}${sysconfdir}/rc4.d/K99custom-script.sh
}

FILES_${PN} += "${sysconfdir}/init.d"

Now I am trying to do the same functionality of custom-script.sh with systemd . Is it possible to make use of systemd-sysv-generator in this case?

Also, will the dir init.d completely removed once we switch to "systemd"? What will happen to other files which are present in etc/init.d ?

Can anyone please help me get started?

Your help will be much appreciated.

Thanks in advance.

PS: Please let me know if any info is missing here.

/etc/init.d will not get deleted by using systemd

Have you checked /etc/systemd and /usr/lib/systemd on your Ubuntu machine for examples of systemd scripts. Along the manual pages of systemd, you should have enough examples to convert your sysv init script to systemd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM