Subversion Repositories livecd

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
231 beyerleu 1
#!/bin/bash
2
#
3
################################################################
4
#
5
# Enable or disable auto login to the system
6
#
7
# Urs Beyerle
8
#
9
################################################################
10
 
11
function usage() {
12
 
13
   ## Usage
14
   # ----------------------------------------------------------
15
 
16
   cat <<EOF
17
 
18
Enable or disables auto login
19
 
20
Usage: 
21
 
22
   livecd-autologin [-h] [-u <user>] [-s <session> ] on | off
23
 
24
   on:   enable autologin to system
25
   off:  disable autologin to system
26
 
27
   Optional Options:
28
 
29
   -u <user>:     define user for auto login, default is sluser
30
   -s <session>:  define session startup command, default is icewm-session
31
   -h:            print this screen
32
 
33
   Note: Works only for users with bash shell.
34
 
35
EOF
36
 
37
}
38
 
39
###############################################################
40
### definitions
41
###############################################################
42
 
43
AUTOLOGIN=""
44
ROOTLOGIN=""
45
LOCALUSER_EXISTS=""
46
LOCALUSER="sluser"
47
STARTSESSION="icewm-session"
48
 
49
 
50
###############################################################
51
### Read options from command-line
52
###############################################################
53
 
54
if [ $# -eq 0 ]; then
55
     usage; exit
56
fi
57
 
58
while [ $# -gt 0 ]; do
59
 
60
    case "$1" in
61
       -h)
62
            usage; exit;;
63
       -u)
64
            shift; LOCALUSER=$1; shift; continue;;
65
       -s)
66
            shift; STARTSESSION=$1; shift; continue;;
67
       on)
68
            AUTOLOGIN=on; shift; continue;;
69
       off)
70
            AUTOLOGIN=off; shift; continue;;
71
       *)
72
            usage; exit;;
73
    esac
74
 
75
done
76
 
77
###############################################################
78
### User
79
###############################################################
80
 
81
### test if $LOCALUSER exists
82
id $LOCALUSER >/dev/null 2>&1
83
[ "$?" = "0" ] && LOCALUSER_EXISTS="true"
84
 
85
### home directory of $LOCALUSER
86
[ $LOCALUSER_EXISTS ] && LOCALUSER_HOME=$(grep "^${LOCALUSER}:" /etc/passwd | cut -d":" -f6)
87
 
88
### warn message
89
[ ! $LOCALUSER_EXISTS ] && echo "$LOCALUSER does not exisit. Create user first and run script again."
90
 
91
 
92
###############################################################
93
# Enable autologin 
94
###############################################################
95
if [ $AUTOLOGIN = on ]; then
96
 
97
    echo "Enable auto login for $LOCALUSER ..."
98
 
99
    ### backup /etc/inittab
100
    echo " - Backup /etc/inittab"
101
    if [ ! -f /etc/inittab.no_autologin ]; then
102
	cp -a /etc/inittab /etc/inittab.no_autologin
103
    fi
104
 
105
    ### change init runlevel to 3
106
    echo " - Change init runlevel to 3"
107
    sed -i "s/id\:.\:initdefault/id\:3\:initdefault/" /etc/inittab
108
 
109
    ### autologin at tty6
110
    echo " - Modify /etc/inittab"
111
    line="6:2345:respawn:\/sbin\/mingetty --autologin $LOCALUSER tty6"
112
    sed -i "s/.*respawn.*tty6.*/$line/" /etc/inittab
113
 
114
    if [ $LOCALUSER_EXISTS ]; then
115
 
116
	echo " - Create user files ~/.xinitrc, ~/.xsession"
117
        ### create .xinitrc for LOCALUSER
118
	XINITRC=$LOCALUSER_HOME/.xinitrc
119
	echo "[ -x ~/autostart ] && ~/autostart" > $XINITRC
120
	echo "exec $STARTSESSION" >> $XINITRC
121
	chmod 755 $XINITRC
122
 
123
        ### link .xsession to .xinitrc
124
	ln -sf .xinitrc $LOCALUSER_HOME/.xsession
125
 
126
	echo " - Modify user file ~/.bashrc"
127
        ### run startx  (over .bashrc) at autotlogin of LOCALUSER and root
128
	grep -q "/dev/tty6" $LOCALUSER_HOME/.bashrc >/dev/null 2>&1
129
	if [ "$?" = "1" ]; then
130
	    echo 'if [ "$( tty )" = "/dev/tty6" ]; then startx; fi' >> $LOCALUSER_HOME/.bashrc
131
	fi
132
    fi
133
 
134
    echo "done."
135
 
136
fi
137
 
138
 
139
###############################################################
140
# Disable autologin 
141
###############################################################
142
if [ $AUTOLOGIN = off ]; then
143
 
144
    echo "Disable auto login for $LOCALUSER ..."
145
 
146
    ### restore /etc/inittab
147
    echo " - Restore original /etc/inittab"
148
    if [ -f /etc/inittab.no_autologin ]; then
149
	mv /etc/inittab.no_autologin /etc/inittab
150
    else
151
	# fall back if no original /etc/inittab found
152
	sed -i "s|.*respawn.*tty6.*|6:2345:respawn:/sbin/mingetty tty6|" /etc/inittab
153
    fi
154
 
155
    ### disable auto login for $LOCALUSER and root 
156
 
157
    if [ $LOCALUSER_EXISTS ]; then
158
 
159
	echo " - Delete user files ~/.xinitrc, ~/.xsession"
160
        # remove .xinitrc
161
	[ $LOCALUSER_EXISTS ] && rm -f $LOCALUSER_HOME/.xinitrc
162
 
163
        # remove link .xsession
164
	[ $LOCALUSER_EXISTS ] && rm -f $LOCALUSER_HOME/.xsession
165
 
166
	echo " - Modify user file ~/.bashrc"
167
        # remove startx line from .bashrc
168
	[ $LOCALUSER_EXISTS ] && sed -i "/.*\/dev\/tty6.*then startx.*/d" $LOCALUSER_HOME/.bashrc
169
    fi
170
 
171
    echo "done."
172
 
173
fi
174
 
175
###############################################################
176
# Final note
177
###############################################################
178
 
179
echo
180
echo "Please note, you may have to reboot to make the changes active."
181
echo