Subversion Repositories DevTools

Rev

Rev 6823 | 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
#
6825 dpurdie 92
kdeScriptCfg="/.kde/share/config/configureVM /var/lib/lightdm/.kde/share/config/configureVM"
6821 dpurdie 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)
6825 dpurdie 128
  # Or "/var/lib/lightdm/.kde/share/config/configureVM"
6821 dpurdie 129
  kdialog --msgbox "$text" --dontagain configureVM:noipaddressmsg
130
  exit
131
fi  
132
rm -f $kdeScriptCfg
133
 
134
#
135
#   Running full script
136
#   Perform full logging to another log file
137
mkdir -p /root/.vix
138
exec 1>/root/.vix/vix_full.log 2>&1
139
echo Starting $*
140
env
141
set -x
142
 
143
#
144
#   Create the Help Text
145
#   Place link to this script on the Desktop
146
#
147
[ -e /root/Desktop/configureVM ] || /root/Desktop/configureVM
148
createHelpText
149
 
150
#
151
# Extract information from the user
152
#
153
#
154
TITLE='First Time VM Configuration'
155
 
156
#
157
#   Display the Help Text
158
#
159
kdialog --title "$TITLE" --textbox "${HelpFile}" 800 800
160
 
161
#
162
#   Set defaults, before overiding with users last values
163
#
164
mname=$(cat /etc/hostname)
165
mode='Automount home drives'
6825 dpurdie 166
pkgServer='auperaarc01'
6821 dpurdie 167
[ -f /root/.vix/config.data ] && source /root/.vix/config.data
168
 
169
ok=false
170
prompt="Unique machine name\
171
        <pre>Suggested format: AUPERAWSxxxVMnn\
172
        <br>Where:\
173
        <br>   AUPERA - Site prefix\
174
        <br>   WSxxx  - Your Workstation ID\
175
        <br>   VM     - Indicated a VM\
176
        <br>   nn     - VM Instance\
177
        </pre>Machine Name:"
178
while ! $ok ; do
179
    [ -z "$mname" ] && mname=AUPERAWSxxxVMnn
180
    mname=$(kdialog --title "$TITLE" --inputbox "$prompt" "$mname")
181
    [[ $mname =~ ^[a-zA-Z][a-zA-Z0-9-]+$ ]] && ok=true
182
    [[ $mname =~ AUPERAWSx ]] && ok=false
183
    [[ $mname =~ VMnn ]] && ok=false
184
done
185
 
186
ok=false
187
prompt="<pre>Stand Alone VM:        No  NIS, Setup local user\
188
         <br>Local Home:            Use NIS, Setup local home\
189
         <br>Automount home drives: Use NIS, Mount Unix Home Drive\
190
        </pre>Select configuration:"
191
while ! $ok ; do
192
    umode=$(kdialog --title "$TITLE" --combobox "$prompt" \
193
            'Stand Alone VM' \
194
            'Local Home' \
195
            'Automount home drives' \
196
            --default "$mode"  )
197
    [ -n "$umode" ] && ok=true
198
done
199
mode="$umode"
200
 
201
ok=false
202
utext=""
203
prompt="The name of the build system package server\
204
        <br>Package Server:"
205
while ! $ok ; do
206
    [ -z "$utext" ] && utext="$pkgServer"
207
    utext=$(kdialog --title "$TITLE" --inputbox "$prompt" "$utext")
208
    [[ "$utext" =~ ^[a-zA-Z][a-zA-Z0-9_-]+$ ]] && ok=true
209
done
210
pkgServer="$utext"
211
 
212
erase=1
213
if [ -n "$startupMode" ]; then
214
    kdialog --title "$TITLE" --yesno \
215
        "This utility is being manually run.<br>\
216
        Do you want to force the script to be run on the next reboot"
217
    erase=$?
218
fi
219
 
220
#
221
#  Save config for next time
222
#
223
echo $mname
224
echo $mode
225
echo $pkgServer
226
mkdir -p /root/.vix
227
cat > /root/.vix/config.data <<hereData
228
mname="$mname"
229
mode="$mode"
230
pkgServer="$pkgServer"
231
hereData
232
 
6823 dpurdie 233
#
234
#   Set the required Mode
235
#
236
autoHome=false
237
perthNis=false
238
addUser=false
239
reboot=false
240
 
241
if [ "$mode" = "Stand Alone VM" ] ; then
242
    autoHome=false
243
    perthNis=false
244
    addUser=true
245
 
246
elif [ "$mode" = "Local Home" ] ; then
247
    autoHome=false
248
    perthNis=true
249
 
250
else
251
    autoHome=true
252
    perthNis=true
253
fi
254
 
255
#
256
#   Create local user if need be
257
#   Note: There is no need to create a local home directory for local users
258
#         as Ubuntu appears to do this on the fly.
259
#
260
echo "Configure Local User"
261
if $addUser; then
262
    kuser "--title=Configure local user" "--caption=Don't forget the passwd"
263
fi
264
 
265
#
266
#   Reboot the system
267
#   If manually invoked then give the user the option of aborting the boot
268
#
269
ktype=yesno
270
[ -z "$startupMode" ] && ktype=msgbox
271
kdialog --title "$TITLE" --$ktype "The system will now reboot in order for the changes to take effect"
272
if [ $? -ne 1 ]; then
273
    reboot=true
274
fi
275
 
276
 
6821 dpurdie 277
################################################################################
278
#
279
#   Perform the initialisation
280
#
281
echo "--------------------------------"
282
echo "Data collected. Configure system"
283
 
284
# Extend the path
285
PATH="/sbin:$PATH"
286
 
287
# Machine-specific, so remove in case this system is going to be
288
# cloned.  These will be regenerated on the first boot.
289
if [ -z "$startupMode" ] ; then
290
    rm -f /etc/udev/rules.d/70-persistent-cd.rules
291
    rm -f /etc/udev/rules.d/70-persistent-net.rules
292
 
293
    # Potentially sensitive.
294
    rm -f /root/.ssh/known_hosts
295
    rm -f /root/.svn
296
    if [ -f /etc/apt/apt.conf ] ; then
297
        sed -i /etc/apt/apt.conf -e 's~http://.*@proxy~http://username:password@proxy~'
298
    fi
299
fi
300
 
301
#
6825 dpurdie 302
#   Save the VM image name
303
#   Should be held in the hostname
304
#   Save it on first use
305
#
306
if [ ! -f /root/.vix/vix-image-name ] ; then
307
    cp /etc/hostname /root/.vix/vix-image-name
308
fi
309
 
310
#
6821 dpurdie 311
#   Set new machine name
312
#
313
echo $mname > /etc/hostname
314
sed -i /etc/hosts -e "s~^127\.0\.1\.1.*~127.0.1.1\t$mname~"
315
hostname -b -F /etc/hostname
316
 
317
#
318
#   Configure NIS
319
#
320
echo "Configure NIS"
321
if $perthNis; then
322
    echo "Enable yp binding"
323
    rm -f /etc/init/ypbind.override
324
    [ -f /etc/defaultdomain.saved ] && mv /etc/defaultdomain.saved /etc/defaultdomain
325
    for ii in passwd shadow group ; do
326
        sed -i /etc/$ii -e "/^+/d"
327
        echo >> /etc/$ii '+'
328
    done
329
    systemctl start ypbind
330
else
331
    echo "Disable yp binding"
332
    [ -f /etc/defaultdomain ] && mv /etc/defaultdomain /etc/defaultdomain.saved
333
    for ii in passwd shadow group; do
334
        sed -i /etc/$ii -e "/^+/d"
335
    done
336
fi
337
 
338
#
339
#   Configure auto mounting of Home Drives
340
#
341
echo "Configure Automounter"
342
VIXAUTOHOME=/etc/auto.master.d/vix.home.autofs
343
if $autoHome; then
344
    echo "Setup automount of /home"
345
    if [ -f ${VIXAUTOHOME}.disabled ] ; then
346
        mv ${VIXAUTOHOME}.disabled ${VIXAUTOHOME}
347
    fi
348
else
349
    echo "Stop automount of /home"
350
    if [ -f ${VIXAUTOHOME} ] ; then
351
        mv ${VIXAUTOHOME} ${VIXAUTOHOME}.disabled
352
    fi
353
fi
354
 
355
#
356
#   Stop auto mounter so that it will not interfere with the
357
#   creation of user home directories
358
#   
359
#   Note: This does not appear to stop the automounter
360
#         May not be an issue as we are about to reboot
361
systemctl stop autofs 
362
 
363
#
364
#   Configure the package server
6825 dpurdie 365
#   Support old - /etc/profile.d
366
#   and New     - /etc/dpkg_archive
6821 dpurdie 367
#
368
echo "Configure Package Server"
369
sed -i /etc/profile.d/jats.sh -e "s~^\(GBE_DPKG=/net\)/\([^/]*\)/\(.*\)~\1/$pkgServer/\3~"
6825 dpurdie 370
echo "/net/$pkgServer/export/devl/dpkg_archive" > /etc/dpkg_archive
6821 dpurdie 371
 
372
#
373
#   Flag - configuration done
374
#   This should stop the auto script from running again
375
#
376
if [ -z "$startupMode" ]; then
377
    echo "Flag - don't run again"
378
    mkdir -p /root/.vix
379
    touch /root/.vix/config
380
fi
381
if [ "$erase" -eq "0" ] ; then
382
    echo "Erase config details. Force script to run on next reboot"
383
    rm -f /root/.vix/config
384
fi
385
 
386
#
387
#   Reboot the system
388
#   If manually invoked then give the user the option of aborting the boot
389
#
6823 dpurdie 390
if $reboot ; then
6821 dpurdie 391
    echo "Reboot the system"
392
    reboot
393
    echo "Reboot failed. Command returned"
394
fi
395
echo "Script complete"
396