2011-10-13 00:02:20 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
if [ "$UID" != "0" ]; then
|
|
|
|
echo Need to be root.
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2017-07-17 09:07:55 +02:00
|
|
|
declare -A results_target
|
2011-10-13 00:02:20 +02:00
|
|
|
|
|
|
|
usage() {
|
2017-06-20 09:33:30 +02:00
|
|
|
cat << EOF
|
2011-10-13 00:02:20 +02:00
|
|
|
usage: systemd-sysv-convert [-h] [--save] [--show] [--apply]
|
|
|
|
SERVICE [SERVICE ...]
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
help() {
|
2017-06-20 09:33:30 +02:00
|
|
|
usage
|
|
|
|
cat << EOF
|
2013-04-23 11:39:56 +02:00
|
|
|
Save and Restore SysV Service Runlevel Information
|
2011-10-13 00:02:20 +02:00
|
|
|
|
|
|
|
positional arguments:
|
|
|
|
SERVICE Service names
|
|
|
|
|
|
|
|
optional arguments:
|
|
|
|
-h, --help show this help message and exit
|
|
|
|
--save Save SysV runlevel information for one or more services
|
|
|
|
--show Show saved SysV runlevel information for one or more services
|
|
|
|
--apply Apply saved SysV runlevel information for one or more services
|
|
|
|
to systemd counterparts
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
find_service() {
|
2017-07-17 09:07:55 +02:00
|
|
|
local service=$1
|
|
|
|
local rcnd=$2
|
2017-06-20 09:33:30 +02:00
|
|
|
|
2017-07-17 09:07:55 +02:00
|
|
|
case $rcnd in
|
|
|
|
boot.d) [ -L /etc/rc.d/$rcnd/S??boot.$service ] ;;
|
|
|
|
*) [ -L /etc/rc.d/$rcnd/S??$service ]
|
|
|
|
esac
|
2011-10-13 00:02:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lookup_database() {
|
2017-07-17 09:07:55 +02:00
|
|
|
local services=$@
|
2017-06-20 09:33:30 +02:00
|
|
|
local service
|
|
|
|
local runlevel
|
|
|
|
local priority
|
|
|
|
|
2017-07-17 09:07:55 +02:00
|
|
|
# 'priority' field is not used but is kept for backward compat
|
|
|
|
# reason.
|
|
|
|
while read service runlevel priority; do
|
2011-10-13 00:02:20 +02:00
|
|
|
for s in $services ; do
|
|
|
|
if [ $s == $service ]; then
|
2017-07-17 09:07:55 +02:00
|
|
|
results_target[$service]+=" runlevel$runlevel.target"
|
|
|
|
break
|
2011-10-13 00:02:20 +02:00
|
|
|
fi
|
|
|
|
done
|
2017-06-20 09:33:30 +02:00
|
|
|
done < /var/lib/systemd/sysv-convert/database
|
2011-10-13 00:02:20 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 18:55:40 +01:00
|
|
|
declare -i fail=0
|
2017-06-20 09:33:30 +02:00
|
|
|
case "$1" in
|
|
|
|
-h|--help)
|
2011-10-13 00:02:20 +02:00
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--save)
|
|
|
|
shift
|
|
|
|
for service in $@ ; do
|
2017-07-17 09:07:55 +02:00
|
|
|
if [ ! -r /etc/init.d/$service ] && [ ! -r /etc/init.d/boot.$service ]; then
|
2017-06-20 09:33:30 +02:00
|
|
|
echo "SysV service $service does not exist, skipping"
|
2016-11-22 18:55:40 +01:00
|
|
|
continue
|
2011-10-13 00:02:20 +02:00
|
|
|
fi
|
2017-07-17 09:07:55 +02:00
|
|
|
for rcnd in rc2.d rc3.d rc4.d rc5.d boot.d; do
|
|
|
|
case $rcnd in
|
|
|
|
rc*.d) runlevel=${rcnd:2:1} ;;
|
|
|
|
boot.d) runlevel=3 ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Write a dumb priority as it is not used.
|
|
|
|
find_service $service $rcnd &&
|
|
|
|
echo "$service $runlevel 50" >>/var/lib/systemd/sysv-convert/database
|
2017-06-20 09:33:30 +02:00
|
|
|
done
|
2011-10-13 00:02:20 +02:00
|
|
|
done
|
|
|
|
;;
|
|
|
|
--show)
|
|
|
|
shift
|
|
|
|
services=$@
|
|
|
|
lookup_database $services
|
|
|
|
for service in $services; do
|
2017-07-17 09:07:55 +02:00
|
|
|
if [ -z "${results_target[$service]}" ]; then
|
|
|
|
echo "No information about service $service found." >/dev/stderr
|
2016-11-22 18:55:40 +01:00
|
|
|
let fail++
|
2011-10-13 00:02:20 +02:00
|
|
|
continue
|
|
|
|
fi
|
2017-07-17 09:07:55 +02:00
|
|
|
for target in ${results_target[$service]}; do
|
|
|
|
echo "SysV service '$service' is pulled by $target"
|
2011-10-13 00:02:20 +02:00
|
|
|
done
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
--apply)
|
|
|
|
shift
|
|
|
|
services=$@
|
|
|
|
for service in $services; do
|
2013-04-23 11:39:56 +02:00
|
|
|
if [ ! -f "/lib/systemd/system/$service.service" -a ! -f "/usr/lib/systemd/system/$service.service" ]; then
|
2011-10-13 00:02:20 +02:00
|
|
|
echo systemd service $service.service does not exist. >/dev/stderr
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
2013-04-23 11:39:56 +02:00
|
|
|
|
2017-06-20 09:33:30 +02:00
|
|
|
#
|
|
|
|
# The database might no have been created by a previous --save
|
|
|
|
# call. This can happen when:
|
|
|
|
#
|
|
|
|
# - we're upgrading a package which initially didn't
|
|
|
|
# have any unit file nor sysv init script and now
|
|
|
|
# start shipping one or more unit files (bsc#982303).
|
|
|
|
#
|
|
|
|
# - the sysv init service wasn't enabled at all before
|
|
|
|
# being migrated to a native unit file (bsc#982211).
|
|
|
|
#
|
|
|
|
if [ -e /var/lib/systemd/sysv-convert/database ]; then
|
|
|
|
lookup_database $services
|
|
|
|
for service in $services; do
|
2017-07-17 09:07:55 +02:00
|
|
|
[ -f "/lib/systemd/system/$service.service" ] && unit="/lib/systemd/system/$service.service"
|
|
|
|
[ -f "/usr/lib/systemd/system/$service.service" ] && unit="/usr/lib/systemd/system/$service.service"
|
2011-10-13 00:02:20 +02:00
|
|
|
|
2017-06-20 09:33:30 +02:00
|
|
|
# If $service is not present in the database,
|
|
|
|
# then it simply means that the sysv init
|
|
|
|
# service was not enabled at all.
|
2017-07-17 09:07:55 +02:00
|
|
|
for target in ${results_target[$service]}; do
|
|
|
|
echo ln -sf $unit /etc/systemd/system/$target.wants/$service.service >/dev/stderr
|
|
|
|
mkdir -p "/etc/systemd/system/$target.wants"
|
|
|
|
/bin/ln -sf $unit /etc/systemd/system/$target.wants/$service.service
|
2017-06-20 09:33:30 +02:00
|
|
|
done
|
|
|
|
done
|
|
|
|
fi
|
2011-10-13 00:02:20 +02:00
|
|
|
;;
|
2017-06-20 09:33:30 +02:00
|
|
|
*)
|
|
|
|
usage
|
2016-11-22 18:55:40 +01:00
|
|
|
let fail=2
|
|
|
|
;;
|
2011-10-13 00:02:20 +02:00
|
|
|
esac
|
2011-08-25 10:07:26 +02:00
|
|
|
|
2016-11-22 18:55:40 +01:00
|
|
|
exit $fail
|