Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | echo "Started $0 $*" |
| 3 | |
| 4 | ROOT= |
| 5 | |
| 6 | # parse command line params |
| 7 | action= |
| 8 | while [ $# != 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 |
| 59 | done |
| 60 | if [ "$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 |
| 64 | fi |
| 65 | |
| 66 | for 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 |
| 87 | if [ -z `echo $service | sed 's/^.\+@\(.*\)\.[^.]\+/\1/'` ]; then |
| 88 | instance_specified=false |
| 89 | else |
| 90 | instance_specified=true |
| 91 | fi |
| 92 | fi |
| 93 | |
| 94 | # find service file |
| 95 | for p in $ROOT/etc/systemd/system \ |
| 96 | $ROOT/lib/systemd/system \ |
| 97 | $ROOT/usr/lib/systemd/system; do |
| 98 | if [ -e $p/$service_base_file ]; then |
| 99 | service_file=$p/$service_base_file |
| 100 | service_file=${service_file##$ROOT} |
| 101 | fi |
| 102 | done |
| 103 | if [ -z "$service_file" ]; then |
| 104 | echo "'$service_base_file' couldn't be found; exiting with error" |
| 105 | exit 1 |
| 106 | fi |
| 107 | echo "Found $service in $service_file" |
| 108 | |
| 109 | # If any new unit types are added to systemd they should be added |
| 110 | # to this regular expression. |
| 111 | unit_types_re='\.\(service\|socket\|device\|mount\|automount\|swap\|target\|path\|timer\|snapshot\)$' |
| 112 | if [ "$action" = "preset" ]; then |
| 113 | action=`egrep -sh $service $ROOT/etc/systemd/user-preset/*.preset | cut -f1 -d' '` |
| 114 | if [ -z "$action" ]; then |
| 115 | globalpreset=`egrep -sh '\*' $ROOT/etc/systemd/user-preset/*.preset | cut -f1 -d' '` |
| 116 | if [ -n "$globalpreset" ]; then |
| 117 | action="$globalpreset" |
| 118 | else |
| 119 | action="enable" |
| 120 | fi |
| 121 | fi |
| 122 | fi |
| 123 | # create the required symbolic links |
| 124 | wanted_by=$(sed '/^WantedBy[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \ |
| 125 | | tr ',' '\n' \ |
| 126 | | grep "$unit_types_re") |
| 127 | |
| 128 | for r in $wanted_by; do |
| 129 | echo "WantedBy=$r found in $service" |
| 130 | if [ "$action" = "enable" ]; then |
| 131 | enable_service=$service |
| 132 | if [ "$service_template" = true -a "$instance_specified" = false ]; then |
| 133 | default_instance=$(sed '/^DefaultInstance[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file") |
| 134 | if [ -z $default_instance ]; then |
| 135 | echo "Template unit without instance or DefaultInstance directive, nothing to enable" |
| 136 | continue |
| 137 | else |
| 138 | echo "Found DefaultInstance $default_instance, enabling it" |
| 139 | enable_service=$(echo $service | sed "s/@/@$default_instance/") |
| 140 | fi |
| 141 | fi |
| 142 | mkdir -p $ROOT/etc/systemd/system/$r.wants |
| 143 | ln -s $service_file $ROOT/etc/systemd/system/$r.wants/$enable_service |
| 144 | echo "Enabled $enable_service for $wanted_by." |
| 145 | else |
| 146 | if [ "$service_template" = true -a "$instance_specified" = false ]; then |
| 147 | disable_service="$ROOT/etc/systemd/system/$r.wants/`echo $service | sed 's/@/@*/'`" |
| 148 | else |
| 149 | disable_service="$ROOT/etc/systemd/system/$r.wants/$service" |
| 150 | fi |
| 151 | rm -f $disable_service |
| 152 | [ -d $ROOT/etc/systemd/system/$r.wants ] && rmdir --ignore-fail-on-non-empty -p $ROOT/etc/systemd/system/$r.wants |
| 153 | echo "Disabled ${disable_service##$ROOT/etc/systemd/system/$r.wants/} for $wanted_by." |
| 154 | fi |
| 155 | done |
| 156 | |
| 157 | # create the required symbolic 'Alias' links |
| 158 | alias=$(sed '/^Alias[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \ |
| 159 | | tr ',' '\n' \ |
| 160 | | grep "$unit_types_re") |
| 161 | |
| 162 | for r in $alias; do |
| 163 | if [ "$action" = "enable" ]; then |
| 164 | mkdir -p $ROOT/etc/systemd/system |
| 165 | ln -s $service_file $ROOT/etc/systemd/system/$r |
| 166 | echo "Enabled $service for $alias." |
| 167 | else |
| 168 | rm -f $ROOT/etc/systemd/system/$r |
| 169 | echo "Disabled $service for $alias." |
| 170 | fi |
| 171 | done |
| 172 | |
| 173 | # call us for the other required scripts |
| 174 | also=$(sed '/^Also[[:space:]]*=/s,[^=]*=,,p;d' "$ROOT/$service_file" \ |
| 175 | | tr ',' '\n') |
| 176 | for a in $also; do |
| 177 | echo "Also=$a found in $service" |
| 178 | if [ "$action" = "enable" ]; then |
| 179 | $0 --root=$ROOT enable $a |
| 180 | fi |
| 181 | done |
| 182 | done |