]> git.ozlabs.org Git - petitboot/blob - utils/pb-console
utils/pb-console: Trap SIGTERM on boot
[petitboot] / utils / pb-console
1 #!/bin/sh
2 #
3 # Petitboot utility script for running a petitboot UI program
4 # on a console tty.
5 #
6
7 ui=petitboot-nc
8 shell=sh
9 getty=/sbin/getty
10 use_getty=0
11 detach=0
12 pb_config=pb-config
13
14 usage() {
15         cat >&2 <<EOF
16 pb-console [OPTIONS] -- [ARGS]
17 OPTIONS
18      -d, --detach
19              Start in a detached (background) state.
20
21      -g, --getty[=PATH]
22              Start a getty (specified by PATH, otherwise $getty),
23              passing additional ARGS to the getty process
24
25      -s, --shell=PATH
26              Use PATH as the exit-to-shell shell
27
28      -u, --ui=PATH
29              Use PATH as the petitboot UI
30
31      -h, --help
32              Print a help message.
33 EOF
34         exit 1
35 }
36
37 opts=$(getopt --options 'hdg::s:u:' \
38               --long 'help,detach,getty::,shell:,ui:' \
39               -- "$@")
40 [ $? = 0 ] || exit 1
41
42 eval set -- "$opts"
43
44 while :
45 do
46         case "$1" in
47         -d | --detach)
48                 detach=1
49                 shift
50                 ;;
51         -g | --getty)
52                 use_getty=1
53                 getty_arg="$2"
54                 shift 2
55                 ;;
56         -s | --shell)
57                 shell="$2"
58                 shift 2
59                 ;;
60         -u | --ui)
61                 ui="$2"
62                 shift 2
63                 ;;
64         --help | -h)
65                 usage
66                 ;;
67         --)
68                 shift
69                 break
70                 ;;
71         *)
72                 echo "getopt error"
73                 exit 1
74         esac
75 done
76
77 if [ "$use_getty" = 1 ]
78 then
79         if [ -n "$getty_arg" ]
80         then
81                 getty="$getty_arg"
82         fi
83
84         login_arg="-l$0"
85         for ttyarg in "$@"
86         do
87                 # If the getty args include autologin don't override with -l
88                 # and leave calling petitboot-nc to the user's init
89                 if [ "$ttyarg" == "-a" ]
90                 then
91                         login_arg=""
92                 fi
93         done
94
95         if [ "$detach" = 1 ]
96         then
97                 setsid -c $getty $login_arg "$@" &
98                 exit
99         else
100                 exec $getty $login_arg "$@"
101         fi
102 fi
103
104 for f in /etc/environment /etc/locale
105 do
106         if [ -e "$f" ]
107         then
108                 export $(cat "$f")
109         fi
110 done
111
112 # we force local terminals to use the linux termcap definition
113 case "$(tty)" in
114 /dev/tty[0-9]*)
115         export TERM=linux
116         ;;
117 esac
118
119 # we may have been run from udev - ensure we have a sensible PATH
120 if [ -z "$PATH" ]
121 then
122         PATH=/usr/bin:/usr/sbin:/bin:/sbin
123 fi
124 PATH=/var/lib/pb-plugins/bin:$PATH
125 export PATH
126
127 verbose_opt=
128 if $pb_config debug | grep -q enabled
129 then
130         verbose_opt=--verbose
131 fi
132
133 # kernel messages may write over the ncurses ui - change log level to only
134 # show particularly important messages
135 dmesg -n 1
136
137 trap '' SIGINT
138 trap 'reset; echo "SIGTERM received, booting..."; sleep 2' SIGTERM
139
140 while :
141 do
142         $ui $verbose_opt
143         reset
144         $shell
145 done