blob: 2bc64896176145e614611a4f9f77cda5952f4d6a [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/sh
2echo "Started $0 $*"
3
4ROOT=
5
6# parse command line params
7action=
8while [ $# != 0 ]; do
9 opt="$1"
10
11 case "$opt" in
12 enable)
13 shift
14
15 action="$opt"
16 services="$1"
17 cmd_args="1"
18 shift
19 ;;
20 disable)
21 shift
22
23 action="$opt"
24 services="$1"
25 cmd_args="1"
26 shift
27 ;;
28 mask)
29 shift
30
31 action="$opt"
32 services="$1"
33 cmd_args="1"
34 shift
35 ;;
36 preset)
37 shift
38
39 action="$opt"
40 services="$1"
41 cmd_args="1"
42 shift
43 ;;
44 --root=*)
45 ROOT=${opt##--root=}
46 cmd_args="0"
47 shift
48 ;;
49 *)
50 if [ "$cmd_args" = "1" ]; then
51 services="$services $opt"
52 shift
53 else
54 echo "'$opt' is an unkown option; exiting with error"
55 exit 1
56 fi
57 ;;
58 esac
59done
60if [ "$action" = "preset" -a "$service_file" = "" ]; then
61 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)
62 services="$services $opt"
63 presetall=1
64fi
65
66for service in $services; do
67 if [ "$presetall" = "1" ]; then
68 action="preset"
69 fi
70 if [ "$action" = "mask" ]; then
71 if [ ! -d $ROOT/etc/systemd/system/ ]; then
72 mkdir -p $ROOT/etc/systemd/system/
73 fi
74 cmd="ln -s /dev/null $ROOT/etc/systemd/system/$service"
75 echo "$cmd"
76 $cmd
77 exit 0
78 fi
79
80 service_base_file=`echo $service | sed 's/\(@\).*\(\.[^.]\+\)/\1\2/'`
81 if [ -z `echo $service | sed '/@/p;d'` ]; then
82 echo "Try to find location of $service..."
83 service_template=false
84 else
85 echo "Try to find location of template $service_base_file of instance $service..."
86 service_template=true
Brad Bishopec8c5652018-11-05 19:23:07 -050087 instance_specified=`echo $service | sed 's/^.\+@\(.*\)\.[^.]\+/\1/'`
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 fi
89
90 # find service file
91 for p in $ROOT/etc/systemd/system \
92 $ROOT/lib/systemd/system \
93 $ROOT/usr/lib/systemd/system; do
94 if [ -e $p/$service_base_file ]; then
95 service_file=$p/$service_base_file
96 service_file=${service_file##$ROOT}
97 fi
98 done
99 if [ -z "$service_file" ]; then
100 echo "'$service_base_file' couldn't be found; exiting with error"
101 exit 1
102 fi
103 echo "Found $service in $service_file"
104
105 # If any new unit types are added to systemd they should be added
106 # to this regular expression.
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107 unit_types_re='\.\(service\|socket\|device\|mount\|automount\|swap\|target\|target\.wants\|path\|timer\|snapshot\)\s*$'
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 if [ "$action" = "preset" ]; then
109 action=`egrep -sh $service $ROOT/etc/systemd/user-preset/*.preset | cut -f1 -d' '`
110 if [ -z "$action" ]; then
111 globalpreset=`egrep -sh '\*' $ROOT/etc/systemd/user-preset/*.preset | cut -f1 -d' '`
112 if [ -n "$globalpreset" ]; then
113 action="$globalpreset"
114 else
115 action="enable"
116 fi
117 fi
118 fi
119 # create the required symbolic links
120 wanted_by=$(sed '/^WantedBy[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
121 | tr ',' '\n' \
122 | grep "$unit_types_re")
123
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500124 required_by=$(sed '/^RequiredBy[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
125 | tr ',' '\n' \
126 | grep "$unit_types_re")
127
128 for dependency in WantedBy RequiredBy; do
129 if [ "$dependency" = "WantedBy" ]; then
130 suffix="wants"
131 dependency_list="$wanted_by"
132 elif [ "$dependency" = "RequiredBy" ]; then
133 suffix="requires"
134 dependency_list="$required_by"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500136 for r in $dependency_list; do
137 echo "$dependency=$r found in $service"
Brad Bishopec8c5652018-11-05 19:23:07 -0500138 if [ -n "$instance_specified" ]; then
139 # substitute wildcards in the dependency
140 r=`echo $r | sed "s/%i/$instance_specified/g"`
141 fi
142
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143 if [ "$action" = "enable" ]; then
144 enable_service=$service
Brad Bishopec8c5652018-11-05 19:23:07 -0500145 if [ "$service_template" = true -a -z "$instance_specified" ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146 default_instance=$(sed '/^DefaultInstance[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file")
147 if [ -z $default_instance ]; then
148 echo "Template unit without instance or DefaultInstance directive, nothing to enable"
149 continue
150 else
151 echo "Found DefaultInstance $default_instance, enabling it"
152 enable_service=$(echo $service | sed "s/@/@$(echo $default_instance | sed 's/\\/\\\\/g')/")
153 fi
154 fi
155 mkdir -p $ROOT/etc/systemd/system/$r.$suffix
156 ln -s $service_file $ROOT/etc/systemd/system/$r.$suffix/$enable_service
157 echo "Enabled $enable_service for $r."
158 else
Brad Bishopec8c5652018-11-05 19:23:07 -0500159 if [ "$service_template" = true -a -z "$instance_specified" ]; then
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500160 disable_service="$ROOT/etc/systemd/system/$r.$suffix/`echo $service | sed 's/@/@*/'`"
161 else
162 disable_service="$ROOT/etc/systemd/system/$r.$suffix/$service"
163 fi
164 rm -f $disable_service
165 [ -d $ROOT/etc/systemd/system/$r.$suffix ] && rmdir --ignore-fail-on-non-empty -p $ROOT/etc/systemd/system/$r.$suffix
166 echo "Disabled ${disable_service##$ROOT/etc/systemd/system/$r.$suffix/} for $r."
167 fi
168 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 done
170
171 # create the required symbolic 'Alias' links
172 alias=$(sed '/^Alias[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
173 | tr ',' '\n' \
174 | grep "$unit_types_re")
175
176 for r in $alias; do
177 if [ "$action" = "enable" ]; then
178 mkdir -p $ROOT/etc/systemd/system
179 ln -s $service_file $ROOT/etc/systemd/system/$r
180 echo "Enabled $service for $alias."
181 else
182 rm -f $ROOT/etc/systemd/system/$r
183 echo "Disabled $service for $alias."
184 fi
185 done
186
187 # call us for the other required scripts
188 also=$(sed '/^Also[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \
189 | tr ',' '\n')
190 for a in $also; do
191 echo "Also=$a found in $service"
192 if [ "$action" = "enable" ]; then
193 $0 --root=$ROOT enable $a
194 fi
195 done
196done