Work around "udev >=208-6 without systemd" bug with loopback device, causing serious...
[grml-udev-config.git] / debian / grml-udev-config.grml-udev.init
1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          grml-udev
4 # Required-Start:    mountkernfs
5 # Required-Stop:
6 # Should-Start:
7 # Should-Stop:
8 # Default-Start:     S
9 # Default-Stop:
10 # Short-Description: wrapper around udev to support bootoptions noudev and blacklist
11 ### END INIT INFO
12
13 . /lib/lsb/init-functions
14
15 # shell version of /usr/bin/tty
16 my_tty() {
17   [ -x /bin/readlink ] || return 0
18   [ -e /proc/self/fd/0 ] || return 0
19   readlink --silent /proc/self/fd/0 || true
20 }
21
22 # are we running within init (non_interactive) or within shell (interactive)?
23 check_for_non_interactive() {
24   if [ "$RUNLEVEL" = "S" -a "$PREVLEVEL" = "N" ]; then
25     return
26   fi
27
28   TTY=$(my_tty)
29   if [ -z "$TTY" -o "$TTY" = "/dev/console" -o "$TTY" = "/dev/null" ]; then
30     return
31   fi
32
33   return 1
34 }
35
36 # start init script through official Debian way
37 udev_exec() {
38   # avoid syntax error if called without valid parameter:
39   [ -z "$1" ] && exec /etc/init.d/udev "$1"
40
41   exec /etc/init.d/udev "$1"
42 }
43
44 # test whether udev is enabled in init's configuration
45 udev_active() {
46   # file-rc:
47   if [ -r /etc/runlevel.conf ] && egrep -q '^[0-9]+.*-.*-.*\/etc\/init.d\/ydev$' /etc/runlevel.conf ; then
48     return 0
49   fi
50
51   # sysv-rc:
52   if ls /etc/rcS.d/*udev >/dev/null 2>&1 ; then
53     return 0
54   fi
55
56   return 1
57 }
58
59 # if original udev init script is enabled to not execute our script
60 udev_init_check() {
61   if udev_active ; then
62     log_warning_msg "Original udev init script is configured for startup, ignoring request to start $0"
63     return 1
64   fi
65 }
66
67 case "$1" in
68   start)
69         # do not flood console with kernel driver messages
70         # grep -q debug /proc/cmdline || echo 0 > /proc/sys/kernel/printk
71
72         # do not display anything when bootoption *splash is present:
73         # if grep -qe ' splash' -qe ' tsplash' -qe ' textsplash' /proc/cmdline ; then
74         #   exec >/dev/null </dev/null
75         # fi
76
77         # support bootoption blacklist, must be executed *before* udev is present
78         if [ -r /etc/grml/autoconfig.functions ] ; then
79           ( zsh -c '. /etc/grml/autoconfig.functions && config_blacklist || printf "Error when trying to run config_blacklist.\n">&2' )
80         else
81           printf 'Warning: /etc/grml/autoconfig.functions could not be read.\n'>&2
82         fi
83
84         # ugly workaround to get udev >=208-6 without systemd working
85         # without long delay during udev startup :( more details at
86         # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=754987
87         if cat /sys/class/net/lo/operstate 2>/dev/null | grep -q 'down' ; then
88           ip link set dev lo up
89         fi
90
91         # support bootoption noudev and inform user how to skip
92         # execution of udev (being bootoption noudev)
93         if ! grep -q noudev /proc/cmdline ; then
94           check_for_non_interactive && \
95             log_action_msg "If your system hangs now, disable udev with bootoption \"noudev\"" && echo
96         else
97            # - allow execution of initscript through FORCE=1 when booting with noudev
98            if [ -z "$FORCE" ] ; then
99               log_failure_msg "Bootoption \"noudev\" found. Skipping execution of udev init script."
100               if ! check_for_non_interactive ; then
101                  printf "\nIt has been detected that the udev init script\n"
102                  printf "has been run from an interactive shell.\n"
103                  printf "You booted your system using bootoption noudev.\n"
104                  printf "To force startup of udev please run:\n\n"
105                  printf "\tFORCE=1 Start udev\n\n"
106               fi
107               exit 1
108            fi
109         fi
110
111         udev_init_check && udev_exec start
112
113         ;;
114
115   # in any other situation just directly invoke udev:
116   *)
117         udev_init_check && udev_exec "$1"
118         ;;
119 esac
120
121 exit 0
122
123 ##############################################################################