2013-01-19 22:47:07 +00:00
|
|
|
#!/bin/sh
|
|
|
|
echo "Started $0 $*"
|
|
|
|
|
|
|
|
ROOT=
|
|
|
|
|
|
|
|
# parse command line params
|
|
|
|
action=
|
|
|
|
while [ $# != 0 ]; do
|
|
|
|
opt="$1"
|
|
|
|
|
|
|
|
case "$opt" in
|
|
|
|
enable)
|
|
|
|
shift
|
|
|
|
|
|
|
|
action="$opt"
|
|
|
|
services="$1"
|
|
|
|
cmd_args="1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
disable)
|
|
|
|
shift
|
|
|
|
|
|
|
|
action="$opt"
|
|
|
|
services="$1"
|
|
|
|
cmd_args="1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
mask)
|
|
|
|
shift
|
|
|
|
|
2013-11-23 06:28:44 +00:00
|
|
|
action="$opt"
|
|
|
|
services="$1"
|
|
|
|
cmd_args="1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
preset)
|
|
|
|
shift
|
|
|
|
|
2013-01-19 22:47:07 +00:00
|
|
|
action="$opt"
|
|
|
|
services="$1"
|
|
|
|
cmd_args="1"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--root=*)
|
|
|
|
ROOT=${opt##--root=}
|
|
|
|
cmd_args="0"
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
if [ "$cmd_args" = "1" ]; then
|
|
|
|
services="$services $opt"
|
|
|
|
shift
|
|
|
|
else
|
|
|
|
echo "'$opt' is an unkown option; exiting with error"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2013-11-23 06:28:44 +00:00
|
|
|
if [ "$action" = "preset" -a "$service_file" = "" ]; then
|
|
|
|
services=$(for f in `find $ROOT/etc/systemd/system $ROOT/lib/systemd/system $ROOT/usr/lib/systemd/system -type f 2>1`; do basename $f; done)
|
|
|
|
services="$services $opt"
|
|
|
|
presetall=1
|
|
|
|
fi
|
2013-01-19 22:47:07 +00:00
|
|
|
|
|
|
|
for service in $services; do
|
2013-11-23 06:28:44 +00:00
|
|
|
if [ "$presetall" = "1" ]; then
|
|
|
|
action="preset"
|
|
|
|
fi
|
2013-01-19 22:47:07 +00:00
|
|
|
if [ "$action" = "mask" ]; then
|
|
|
|
if [ ! -d $ROOT/etc/systemd/system/ ]; then
|
|
|
|
mkdir -p $ROOT/etc/systemd/system/
|
|
|
|
fi
|
|
|
|
cmd="ln -s /dev/null $ROOT/etc/systemd/system/$service"
|
|
|
|
echo "$cmd"
|
|
|
|
$cmd
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2014-12-12 15:50:51 +00:00
|
|
|
service_base_file=`echo $service | sed 's/\(@\).*\(\.[^.]\+\)/\1\2/'`
|
|
|
|
if [ -z `echo $service | sed '/@/p;d'` ]; then
|
|
|
|
echo "Try to find location of $service..."
|
|
|
|
service_template=false
|
|
|
|
else
|
|
|
|
echo "Try to find location of template $service_base_file of instance $service..."
|
|
|
|
service_template=true
|
|
|
|
if [ -z `echo $service | sed 's/^.\+@\(.*\)\.[^.]\+/\1/'` ]; then
|
|
|
|
instance_specified=false
|
|
|
|
else
|
|
|
|
instance_specified=true
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-01-19 22:47:07 +00:00
|
|
|
# find service file
|
|
|
|
for p in $ROOT/etc/systemd/system \
|
|
|
|
$ROOT/lib/systemd/system \
|
|
|
|
$ROOT/usr/lib/systemd/system; do
|
2014-12-12 15:50:51 +00:00
|
|
|
if [ -e $p/$service_base_file ]; then
|
|
|
|
service_file=$p/$service_base_file
|
2013-01-19 22:47:07 +00:00
|
|
|
service_file=${service_file##$ROOT}
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ -z "$service_file" ]; then
|
2014-12-12 15:50:51 +00:00
|
|
|
echo "'$service_base_file' couldn't be found; exiting with error"
|
2013-01-19 22:47:07 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
echo "Found $service in $service_file"
|
|
|
|
|
2013-06-21 04:12:57 +00:00
|
|
|
# If any new unit types are added to systemd they should be added
|
|
|
|
# to this regular expression.
|
|
|
|
unit_types_re='\.\(service\|socket\|device\|mount\|automount\|swap\|target\|path\|timer\|snapshot\)$'
|
2013-11-23 06:28:44 +00:00
|
|
|
if [ "$action" = "preset" ]; then
|
|
|
|
action=`egrep -sh $service $ROOT/etc/systemd/user-preset/*.preset | cut -f1 -d' '`
|
|
|
|
if [ -z "$action" ]; then
|
|
|
|
globalpreset=`egrep -sh '\*' $ROOT/etc/systemd/user-preset/*.preset | cut -f1 -d' '`
|
|
|
|
if [ -n "$globalpreset" ]; then
|
|
|
|
action="$globalpreset"
|
|
|
|
else
|
|
|
|
action="enable"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2013-01-19 22:47:07 +00:00
|
|
|
# create the required symbolic links
|
2013-04-24 11:37:21 +00:00
|
|
|
wanted_by=$(sed '/^WantedBy[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
|
2013-01-19 22:47:07 +00:00
|
|
|
| tr ',' '\n' \
|
2013-06-21 04:12:57 +00:00
|
|
|
| grep "$unit_types_re")
|
2013-01-19 22:47:07 +00:00
|
|
|
|
|
|
|
for r in $wanted_by; do
|
|
|
|
echo "WantedBy=$r found in $service"
|
|
|
|
if [ "$action" = "enable" ]; then
|
2014-12-12 15:50:51 +00:00
|
|
|
enable_service=$service
|
|
|
|
if [ "$service_template" = true -a "$instance_specified" = false ]; then
|
|
|
|
default_instance=$(sed '/^DefaultInstance[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file")
|
|
|
|
if [ -z $default_instance ]; then
|
|
|
|
echo "Template unit without instance or DefaultInstance directive, nothing to enable"
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
echo "Found DefaultInstance $default_instance, enabling it"
|
2016-01-12 12:55:43 +00:00
|
|
|
enable_service=$(echo $service | sed "s/@/@$(echo $default_instance | sed 's/\\/\\\\/g')/")
|
2014-12-12 15:50:51 +00:00
|
|
|
fi
|
|
|
|
fi
|
2013-01-19 22:47:07 +00:00
|
|
|
mkdir -p $ROOT/etc/systemd/system/$r.wants
|
2014-12-12 15:50:51 +00:00
|
|
|
ln -s $service_file $ROOT/etc/systemd/system/$r.wants/$enable_service
|
|
|
|
echo "Enabled $enable_service for $wanted_by."
|
2013-01-19 22:47:07 +00:00
|
|
|
else
|
2014-12-12 15:50:51 +00:00
|
|
|
if [ "$service_template" = true -a "$instance_specified" = false ]; then
|
|
|
|
disable_service="$ROOT/etc/systemd/system/$r.wants/`echo $service | sed 's/@/@*/'`"
|
|
|
|
else
|
|
|
|
disable_service="$ROOT/etc/systemd/system/$r.wants/$service"
|
|
|
|
fi
|
|
|
|
rm -f $disable_service
|
2015-03-13 18:41:40 +00:00
|
|
|
[ -d $ROOT/etc/systemd/system/$r.wants ] && rmdir --ignore-fail-on-non-empty -p $ROOT/etc/systemd/system/$r.wants
|
2014-12-12 15:50:51 +00:00
|
|
|
echo "Disabled ${disable_service##$ROOT/etc/systemd/system/$r.wants/} for $wanted_by."
|
2013-01-19 22:47:07 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# create the required symbolic 'Alias' links
|
2013-04-24 11:37:21 +00:00
|
|
|
alias=$(sed '/^Alias[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
|
2013-01-19 22:47:07 +00:00
|
|
|
| tr ',' '\n' \
|
2013-06-21 04:12:57 +00:00
|
|
|
| grep "$unit_types_re")
|
2013-01-19 22:47:07 +00:00
|
|
|
|
|
|
|
for r in $alias; do
|
|
|
|
if [ "$action" = "enable" ]; then
|
|
|
|
mkdir -p $ROOT/etc/systemd/system
|
|
|
|
ln -s $service_file $ROOT/etc/systemd/system/$r
|
|
|
|
echo "Enabled $service for $alias."
|
|
|
|
else
|
|
|
|
rm -f $ROOT/etc/systemd/system/$r
|
|
|
|
echo "Disabled $service for $alias."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# call us for the other required scripts
|
2013-04-24 11:37:21 +00:00
|
|
|
also=$(sed '/^Also[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
|
2013-01-19 22:47:07 +00:00
|
|
|
| tr ',' '\n')
|
|
|
|
for a in $also; do
|
|
|
|
echo "Also=$a found in $service"
|
|
|
|
if [ "$action" = "enable" ]; then
|
|
|
|
$0 --root=$ROOT enable $a
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|