2011-10-13 00:02:20 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
if [ "$UID" != "0" ]; then
|
|
|
|
echo Need to be root.
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
declare -A results_runlevel
|
|
|
|
declare -A results_priority
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
cat << EOF
|
|
|
|
usage: systemd-sysv-convert [-h] [--save] [--show] [--apply]
|
|
|
|
SERVICE [SERVICE ...]
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
help() {
|
|
|
|
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() {
|
|
|
|
local service
|
|
|
|
local runlevel
|
|
|
|
declare -i priority
|
|
|
|
service=$1
|
|
|
|
runlevel=$2
|
|
|
|
priority=-1
|
2013-05-03 13:48:49 +02:00
|
|
|
for l in $(ls /etc/rc.d/rc$runlevel.d/) ; do
|
2011-10-13 00:02:20 +02:00
|
|
|
initscript=$(basename $l)
|
|
|
|
if [ ${initscript:0:1} != "S" -o ${initscript:3} != "$service" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
if [ ${initscript:1:2} -ge 0 -a ${initscript:1:2} -le 99 -a ${initscript:1:2} -ge $priority ]; then
|
|
|
|
if [ ${initscript:1:1} == 0 ]; then
|
|
|
|
priority=${initscript:2:1}
|
|
|
|
else
|
|
|
|
priority=${initscript:1:2}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ $priority -ge 0 ]; then
|
|
|
|
return $priority
|
|
|
|
else
|
|
|
|
return 255
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
lookup_database() {
|
|
|
|
local services
|
|
|
|
local service
|
2013-04-23 11:39:56 +02:00
|
|
|
local service_file
|
2011-10-13 00:02:20 +02:00
|
|
|
local runlevel
|
|
|
|
local priority
|
|
|
|
local -i k
|
|
|
|
declare -a parsed
|
|
|
|
services=$@
|
|
|
|
k=0
|
|
|
|
results_runlevel=()
|
|
|
|
results_priority=()
|
|
|
|
while read line ; do
|
|
|
|
k+=1
|
|
|
|
parsed=($line)
|
|
|
|
service=${parsed[0]}
|
|
|
|
runlevel=${parsed[1]}
|
|
|
|
priority=${parsed[2]}
|
|
|
|
if [ $runlevel -lt 2 -o $runlevel -gt 5 ]; then
|
|
|
|
echo "Runlevel out of bounds in database line $k. Ignoring" >/dev/stderr
|
2011-08-25 10:07:26 +02:00
|
|
|
continue
|
2011-10-13 00:02:20 +02:00
|
|
|
fi
|
|
|
|
if [ $priority -lt 0 -o $priority -gt 99 ]; then
|
|
|
|
echo "Priority out of bounds in database line $k. Ignoring" >/dev/stderr
|
2011-08-25 10:07:26 +02:00
|
|
|
continue
|
2011-10-13 00:02:20 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
declare -i found
|
|
|
|
found=0
|
|
|
|
for s in $services ; do
|
|
|
|
if [ $s == $service ]; then
|
|
|
|
found=1
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ $found -eq 0 ]; then
|
2011-08-25 10:07:26 +02:00
|
|
|
continue
|
2011-10-13 00:02:20 +02:00
|
|
|
fi
|
|
|
|
results_runlevel[$service]+=" $runlevel"
|
|
|
|
results_priority[$service]+=" $priority"
|
|
|
|
done < /var/lib/systemd/sysv-convert/database
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
-h|--help)
|
|
|
|
help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--save)
|
|
|
|
shift
|
|
|
|
for service in $@ ; do
|
|
|
|
if [ ! -r "/etc/init.d/$service" ]; then
|
|
|
|
echo "SysV service $service does not exist" >/dev/stderr
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
for runlevel in 2 3 4 5; do
|
|
|
|
find_service $service $runlevel
|
|
|
|
priority=$?
|
|
|
|
if [ $priority -lt 255 ]; then
|
|
|
|
echo "$service $runlevel $priority" >> /var/lib/systemd/sysv-convert/database
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
--show)
|
|
|
|
shift
|
|
|
|
services=$@
|
|
|
|
lookup_database $services
|
|
|
|
fail=0
|
|
|
|
for service in $services; do
|
|
|
|
if [ -z "${results_runlevel[$service]}" ]; then
|
|
|
|
echo No information found about service $service found. >/dev/stderr
|
|
|
|
fail=1
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
declare -i count
|
|
|
|
count=0
|
|
|
|
priority=(${results_priority[$service]})
|
|
|
|
for runlevel in ${results_runlevel[$service]}; do
|
|
|
|
echo SysV service $service enabled in runlevel $runlevel at priority ${priority[$count]}
|
|
|
|
count+=1
|
|
|
|
done
|
|
|
|
done
|
|
|
|
exit $fail
|
|
|
|
;;
|
|
|
|
--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
|
|
|
|
lookup_database $services
|
|
|
|
for service in $services; do
|
2013-04-23 11:39:56 +02:00
|
|
|
[ -f "/lib/systemd/system/$service.service" ] && service_file="/lib/systemd/system/$service.service"
|
|
|
|
[ -f "/usr/lib/systemd/system/$service.service" ] && service_file="/usr/lib/systemd/system/$service.service"
|
|
|
|
|
2011-10-13 00:02:20 +02:00
|
|
|
if [ -z "${results_runlevel[$service]}" ]; then
|
|
|
|
echo No information found about service $service found. >/dev/stderr
|
|
|
|
fail=1
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
for runlevel in ${results_runlevel[$service]}; do
|
2013-04-23 11:39:56 +02:00
|
|
|
echo ln -sf $service_file /etc/systemd/system/runlevel$runlevel.target.wants/$service.service >/dev/stderr
|
2011-10-13 00:02:20 +02:00
|
|
|
mkdir -p "/etc/systemd/system/runlevel$runlevel.target.wants"
|
2013-04-23 11:39:56 +02:00
|
|
|
/bin/ln -sf $service_file /etc/systemd/system/runlevel$runlevel.target.wants/$service.service
|
2011-10-13 00:02:20 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
*) usage
|
|
|
|
exit 2;;
|
|
|
|
esac
|
2011-08-25 10:07:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|