Subversion Repositories DevTools

Rev

Rev 6821 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6821 dpurdie 1
#!/bin/bash
2
################################################################################
3
# This file is to be run by root
4
# It is used to configure a new VM on first boot
5
#
6
# Source is controlled in the VIXubuntu1604LTS_VMcfg package
7
#
8
# This script is called from
9
#   /usr/share/lightdm/lightdm.conf.d/50-vix-hook.conf
10
#
11
################################################################################
12
 
13
# Log all output 
14
mkdir -p /root/.vix
15
exec 1>/root/.vix/vix.log 2>&1
16
echo Starting $*
17
 
18
mkdir -p /root/Desktop
19
HelpFile="/root/Desktop/configureVM Help.txt"
20
function createHelpText {
21
cat > "$HelpFile" <<HelpDoc
22
Initial Machine Setup
23
 
24
This process will setup the VM.
25
It will be performed once, but can be manually rerun via $0
26
This text file can be found at: "$HelpFile"
27
 
28
Use Information:
29
Root Password: maple01
30
 
31
Local User: Ubuntu
32
  Password: maple01
33
 
34
Machine Name:
35
This should be globally unique so as to avoid problems with IP address
36
assignment. The recommended name is based on:
37
  1) Your Desktop machine Name. ie AUPERAWS123
38
  2) The number of VMs that you already have created
39
ie: AUPERAWS123VM01
40
 
41
Configuration:
42
    Automount home drivers
43
    Intended for a Virtual Machine based in Perth attached to the Perth Unix
44
    home drives. Use outside of Perth may suffer performance delays
45
 
46
    Local Home
47
    Intended for a VM not based in Perth, but aware of the Perth VIX NIS. The
48
    machine will have a local home drive, but will use the Perth VIX NIS.
49
 
50
    A local home directory will be configured. The user will authenicate against
51
    the NIS, but use the local home drive.
52
 
53
    Stand Alone:
54
    Intended for a Virtual Machine that will not be attached to the Perth file
55
    system. This type of machine will require that the user set up a
56
    non-root user.
57
 
58
Package Server:
59
    This is the name of the build system package server. It will be used to:
60
        1) Provide the location of JATS
61
        2) Provide dpkg_archive
62
    Example: auperaarc01 [Perth Based Development]
63
             auperaarc02 [Pulse]
64
    It is expected that dpkg_archive will be found at:
65
        /net/PackageServerName/export/devl/dpkg_archive
66
 
67
Additional Notes:
68
    * Kdbg has been installed. This is a GUI interface to GDB
69
 
70
HelpDoc
71
}
72
 
73
#
74
#   Examine user commandline.
75
#   Expect one of
76
#       -install    - Used by installer
77
#       -remove     - Used by installer
78
#       -auto       - Invoked by startup at boot
79
#                   - User invocation
80
#
81
startupMode=No
82
for ii in $*; do
83
    [ "$ii" = "-auto" ] && startupMode=
84
    [ "$ii" = "-install" ] && startupMode=install
85
    [ "$ii" = "-remove" ] && startupMode=remove
86
done
87
 
88
#
89
#   Self install and removal
90
#   Normally run from the installer
91
#
92
kdeScriptCfg=/.kde/share/config/configureVM
93
if [ "$startupMode" = "install" ] ; then
94
    ln -s $0 /root/Desktop/configureVM
95
    rm -f /root/.vix/config
96
    createHelpText
97
    rm -f $kdeScriptCfg
98
    exit 0
99
elif [ "$startupMode" = "remove" ] ; then
100
    rm -f /root/Desktop/configureVM*
101
    rm -rf /root/.vix
102
    rm -f $kdeScriptCfg
103
    exit 0
104
fi
105
 
106
#
107
#   Non Manual: Only run once
108
#
109
if [ -z "$startupMode" -a -f /root/.vix/config ]; then
110
  #
111
  # Non Manual: After first time
112
  #
113
  # Display the OS Type
114
  #             Machine Name
115
  #             IP Address
116
  #
117
  # Allows users to determine the IP address to:
118
  #     Simplify ssh access
119
  #     Show when it does not have an IP address
120
  ipaddress=$(/sbin/ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{ print $2 }' | awk -F: '{ print $2 }')
121
  text="
122
OS Version: $(lsb_release -sd)
123
HostName: $(hostname)
124
IP address: $ipaddress
125
 
126
Continue to login"
127
  # May create /.kde/share/config/configureVM ($kdeScriptCfg)
128
  kdialog --msgbox "$text" --dontagain configureVM:noipaddressmsg
129
  exit
130
fi  
131
rm -f $kdeScriptCfg
132
 
133
#
134
#   Running full script
135
#   Perform full logging to another log file
136
mkdir -p /root/.vix
137
exec 1>/root/.vix/vix_full.log 2>&1
138
echo Starting $*
139
env
140
set -x
141
 
142
#
143
#   Create the Help Text
144
#   Place link to this script on the Desktop
145
#
146
[ -e /root/Desktop/configureVM ] || /root/Desktop/configureVM
147
createHelpText
148
 
149
#
150
# Extract information from the user
151
#
152
#
153
TITLE='First Time VM Configuration'
154
 
155
#
156
#   Display the Help Text
157
#
158
kdialog --title "$TITLE" --textbox "${HelpFile}" 800 800
159
 
160
#
161
#   Set defaults, before overiding with users last values
162
#
163
mname=$(cat /etc/hostname)
164
mode='Automount home drives'
165
pkgServer='auperaarc02'
166
[ -f /root/.vix/config.data ] && source /root/.vix/config.data
167
 
168
ok=false
169
prompt="Unique machine name\
170
        <pre>Suggested format: AUPERAWSxxxVMnn\
171
        <br>Where:\
172
        <br>   AUPERA - Site prefix\
173
        <br>   WSxxx  - Your Workstation ID\
174
        <br>   VM     - Indicated a VM\
175
        <br>   nn     - VM Instance\
176
        </pre>Machine Name:"
177
while ! $ok ; do
178
    [ -z "$mname" ] && mname=AUPERAWSxxxVMnn
179
    mname=$(kdialog --title "$TITLE" --inputbox "$prompt" "$mname")
180
    [[ $mname =~ ^[a-zA-Z][a-zA-Z0-9-]+$ ]] && ok=true
181
    [[ $mname =~ AUPERAWSx ]] && ok=false
182
    [[ $mname =~ VMnn ]] && ok=false
183
done
184
 
185
ok=false
186
prompt="<pre>Stand Alone VM:        No  NIS, Setup local user\
187
         <br>Local Home:            Use NIS, Setup local home\
188
         <br>Automount home drives: Use NIS, Mount Unix Home Drive\
189
        </pre>Select configuration:"
190
while ! $ok ; do
191
    umode=$(kdialog --title "$TITLE" --combobox "$prompt" \
192
            'Stand Alone VM' \
193
            'Local Home' \
194
            'Automount home drives' \
195
            --default "$mode"  )
196
    [ -n "$umode" ] && ok=true
197
done
198
mode="$umode"
199
 
200
ok=false
201
utext=""
202
prompt="The name of the build system package server\
203
        <br>Package Server:"
204
while ! $ok ; do
205
    [ -z "$utext" ] && utext="$pkgServer"
206
    utext=$(kdialog --title "$TITLE" --inputbox "$prompt" "$utext")
207
    [[ "$utext" =~ ^[a-zA-Z][a-zA-Z0-9_-]+$ ]] && ok=true
208
done
209
pkgServer="$utext"
210
 
211
erase=1
212
if [ -n "$startupMode" ]; then
213
    kdialog --title "$TITLE" --yesno \
214
        "This utility is being manually run.<br>\
215
        Do you want to force the script to be run on the next reboot"
216
    erase=$?
217
fi
218
 
219
#
220
#  Save config for next time
221
#
222
echo $mname
223
echo $mode
224
echo $pkgServer
225
mkdir -p /root/.vix
226
cat > /root/.vix/config.data <<hereData
227
mname="$mname"
228
mode="$mode"
229
pkgServer="$pkgServer"
230
hereData
231
 
6823 dpurdie 232
#
233
#   Set the required Mode
234
#
235
autoHome=false
236
perthNis=false
237
addUser=false
238
reboot=false
239
 
240
if [ "$mode" = "Stand Alone VM" ] ; then
241
    autoHome=false
242
    perthNis=false
243
    addUser=true
244
 
245
elif [ "$mode" = "Local Home" ] ; then
246
    autoHome=false
247
    perthNis=true
248
 
249
else
250
    autoHome=true
251
    perthNis=true
252
fi
253
 
254
#
255
#   Create local user if need be
256
#   Note: There is no need to create a local home directory for local users
257
#         as Ubuntu appears to do this on the fly.
258
#
259
echo "Configure Local User"
260
if $addUser; then
261
    kuser "--title=Configure local user" "--caption=Don't forget the passwd"
262
fi
263
 
264
#
265
#   Reboot the system
266
#   If manually invoked then give the user the option of aborting the boot
267
#
268
ktype=yesno
269
[ -z "$startupMode" ] && ktype=msgbox
270
kdialog --title "$TITLE" --$ktype "The system will now reboot in order for the changes to take effect"
271
if [ $? -ne 1 ]; then
272
    reboot=true
273
fi
274
 
275
 
6821 dpurdie 276
################################################################################
277
#
278
#   Perform the initialisation
279
#
280
echo "--------------------------------"
281
echo "Data collected. Configure system"
282
 
283
# Extend the path
284
PATH="/sbin:$PATH"
285
 
286
# Machine-specific, so remove in case this system is going to be
287
# cloned.  These will be regenerated on the first boot.
288
if [ -z "$startupMode" ] ; then
289
    rm -f /etc/udev/rules.d/70-persistent-cd.rules
290
    rm -f /etc/udev/rules.d/70-persistent-net.rules
291
 
292
    # Potentially sensitive.
293
    rm -f /root/.ssh/known_hosts
294
    rm -f /root/.svn
295
    if [ -f /etc/apt/apt.conf ] ; then
296
        sed -i /etc/apt/apt.conf -e 's~http://.*@proxy~http://username:password@proxy~'
297
    fi
298
fi
299
 
300
#
301
#   Set new machine name
302
#
303
echo $mname > /etc/hostname
304
sed -i /etc/hosts -e "s~^127\.0\.1\.1.*~127.0.1.1\t$mname~"
305
hostname -b -F /etc/hostname
306
 
307
#
308
#   Configure NIS
309
#
310
echo "Configure NIS"
311
if $perthNis; then
312
    echo "Enable yp binding"
313
    rm -f /etc/init/ypbind.override
314
    [ -f /etc/defaultdomain.saved ] && mv /etc/defaultdomain.saved /etc/defaultdomain
315
    for ii in passwd shadow group ; do
316
        sed -i /etc/$ii -e "/^+/d"
317
        echo >> /etc/$ii '+'
318
    done
319
    systemctl start ypbind
320
else
321
    echo "Disable yp binding"
322
    [ -f /etc/defaultdomain ] && mv /etc/defaultdomain /etc/defaultdomain.saved
323
    for ii in passwd shadow group; do
324
        sed -i /etc/$ii -e "/^+/d"
325
    done
326
fi
327
 
328
#
329
#   Configure auto mounting of Home Drives
330
#
331
echo "Configure Automounter"
332
VIXAUTOHOME=/etc/auto.master.d/vix.home.autofs
333
if $autoHome; then
334
    echo "Setup automount of /home"
335
    if [ -f ${VIXAUTOHOME}.disabled ] ; then
336
        mv ${VIXAUTOHOME}.disabled ${VIXAUTOHOME}
337
    fi
338
else
339
    echo "Stop automount of /home"
340
    if [ -f ${VIXAUTOHOME} ] ; then
341
        mv ${VIXAUTOHOME} ${VIXAUTOHOME}.disabled
342
    fi
343
fi
344
 
345
#
346
#   Stop auto mounter so that it will not interfere with the
347
#   creation of user home directories
348
#   
349
#   Note: This does not appear to stop the automounter
350
#         May not be an issue as we are about to reboot
351
systemctl stop autofs 
352
 
353
#
354
#   Configure the package server
355
#
356
echo "Configure Package Server"
357
sed -i /etc/profile.d/jats.sh -e "s~^\(GBE_DPKG=/net\)/\([^/]*\)/\(.*\)~\1/$pkgServer/\3~"
358
 
359
#
360
#   Flag - configuration done
361
#   This should stop the auto script from running again
362
#
363
if [ -z "$startupMode" ]; then
364
    echo "Flag - don't run again"
365
    mkdir -p /root/.vix
366
    touch /root/.vix/config
367
fi
368
if [ "$erase" -eq "0" ] ; then
369
    echo "Erase config details. Force script to run on next reboot"
370
    rm -f /root/.vix/config
371
fi
372
 
373
#
374
#   Reboot the system
375
#   If manually invoked then give the user the option of aborting the boot
376
#
6823 dpurdie 377
if $reboot ; then
6821 dpurdie 378
    echo "Reboot the system"
379
    reboot
380
    echo "Reboot failed. Command returned"
381
fi
382
echo "Script complete"
383