Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

#!/bin/bash
################################################################################
# This file is to be run by root
# It is used to configure a new VM on first boot
#
# Source is controlled in the VIXcentos7LTS_VMcfg package
#
# This script is called from
#    /etc/gdm/Init/:0
#
################################################################################

# Log all output 
mkdir -p /root/.vix
exec 1>/root/.vix/vix.log 2>&1
echo Starting $*

mkdir -p /root/Desktop
HelpFile="/root/Desktop/configureVM Help.txt"
function createHelpText {
cat > "$HelpFile" <<HelpDoc
Initial Machine Setup

This process will setup the VM.
It will be performed once, but can be manually rerun via $0
This text file can be found at: "$HelpFile"

Use Information:
Root Password: maple01

Local User: vix
  Password: maple01

Machine Name:
This should be globally unique so as to avoid problems with IP address
assignment. The recommended name is based on:
  1) Your Desktop machine Name. ie AUPERAWS123
  2) The number of VMs that you already have created
ie: AUPERAWS123VM01

Configuration:
    Automount home drivers
    Intended for a Virtual Machine based in Perth attached to the Perth Unix
    home drives. Use outside of Perth may suffer performance delays

    Local Home
    Intended for a VM not based in Perth, but aware of the Perth VIX NIS. The
    machine will have a local home drive, but will use the Perth VIX NIS.

    A local home directory will be configured. The user will authenicate against
    the NIS, but use the local home drive.

    Stand Alone:
    Intended for a Virtual Machine that will not be attached to the Perth file
    system. This type of machine will require that the user set up a
    non-root user.

Package Server:
    This is the name of the build system package server. It will be used to:
        1) Provide the location of JATS
        2) Provide dpkg_archive
    Example: auperaarc01 [Perth Based Development]
             auperaarc02 [Pulse]
    It is expected that dpkg_archive will be found at:
        /net/PackageServerName/export/devl/dpkg_archive

Additional Notes:
    * Kedevelop has been installed. This is a GUI interface to GDB

HelpDoc
}

#
#   Examine user commandline.
#   Expect one of
#       -install    - Used by installer
#       -remove     - Used by installer
#       -auto       - Invoked by startup at boot
#                   - User invocation
#
startupMode=No
for ii in $*; do
    [ "$ii" = "-auto" ] && startupMode=
    [ "$ii" = "-install" ] && startupMode=install
    [ "$ii" = "-remove" ] && startupMode=remove
done

#
#   Self install and removal
#   Normally run from the installer
#
GdmBannerFile=/etc/dconf/db/gdm.d/01-banner-message
kdeScriptCfg=/.kde/share/config/configureVM
if [ "$startupMode" = "install" ] ; then
    ln -s $0 /root/Desktop/configureVM
    rm -f /root/.vix/config
    createHelpText
    rm -f $kdeScriptCfg
    exit 0
elif [ "$startupMode" = "remove" ] ; then
    rm -f /root/Desktop/configureVM*
    rm -rf /root/.vix
    rm -f $kdeScriptCfg
    rm -f $GdmBannerFile
    exit 0
fi

#
#   Non Manual: Only run once
#
if [ -z "$startupMode" -a -f /root/.vix/config ]; then
  #
  # Non Manual: After first time
  #
  # Display the OS Type
  #             Machine Name
  #             IP Address
  #
  # Allows users to determine the IP address to:
  #     Simplify ssh access
  #     Show when it does not have an IP address
  #
  # Note: Under Centos we can't pop up a dialog at this point as it will stall the logon
  #       process and prevent users from accessing the box via ssh
  #
  #       Under Centos we update the Greeter banner instead
  #
  IP=$(/sbin/ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{ print $2 }' )
  OS=$(hostnamectl | grep Operating | sed -e 's/.*: //')
  KERN=$(hostnamectl | grep Kernel| sed -e 's/.*: //')
  TEXT="Host Name: $(hostname)\\nOS: $OS\\nKernel: $KERN\\nIP Address: $IP"

  cat << EOF > $GdmBannerFile
[org/gnome/login-screen]
banner-message-enable=true
disable-user-list=true
banner-message-text='$TEXT'
EOF
  dconf update

#  text="
#OS Version: ${OS}
#Kernel: ${KERN}
#HostName: $(hostname)
#IP address: $ipaddress
#
#Continue to login"
#  # May create /.kde/share/config/configureVM ($kdeScriptCfg)
#  kdialog --msgbox "$text" --dontagain configureVM:noipaddressmsg
  exit
fi  
rm -f $kdeScriptCfg

#
#   Running full script
#   Perform full logging to another log file
mkdir -p /root/.vix
exec 1>/root/.vix/vix_full.log 2>&1
echo Starting $*
env
set -x

#
#   Create the Help Text
#   Place link to this script on the Desktop
#
[ -e /root/Desktop/configureVM ] || /root/Desktop/configureVM
createHelpText

#
# Extract information from the user
#
#
TITLE='First Time VM Configuration'

#
#   Display the Help Text
#
kdialog --title "$TITLE" --textbox "${HelpFile}" 800 800

#
#   Set defaults, before overiding with users last values
#
mname=$(cat /etc/hostname)
mode='Automount home drives'
pkgServer='auperaarc02'
[ -f /root/.vix/config.data ] && source /root/.vix/config.data

ok=false
prompt="Unique machine name\
        <pre>Suggested format: AUPERAWSxxxVMnn\
        <br>Where:\
        <br>   AUPERA - Site prefix\
        <br>   WSxxx  - Your Workstation ID\
        <br>   VM     - Indicated a VM\
        <br>   nn     - VM Instance\
        </pre>Machine Name:"
while ! $ok ; do
    [ -z "$mname" ] && mname=AUPERAWSxxxVMnn
    mname=$(kdialog --title "$TITLE" --inputbox "$prompt" "$mname")
    [[ $mname =~ ^[a-zA-Z][a-zA-Z0-9-]+$ ]] && ok=true
    [[ $mname =~ AUPERAWSx ]] && ok=false
    [[ $mname =~ VMnn ]] && ok=false
done

ok=false
prompt="<pre>Stand Alone VM:        No  NIS, Setup local user\
         <br>Local Home:            Use NIS, Setup local home\
         <br>Automount home drives: Use NIS, Mount Unix Home Drive\
        </pre>Select configuration:"
while ! $ok ; do
    umode=$(kdialog --title "$TITLE" --combobox "$prompt" \
            'Stand Alone VM' \
            'Local Home' \
            'Automount home drives' \
            --default "$mode"  )
    [ -n "$umode" ] && ok=true
done
mode="$umode"

ok=false
utext=""
prompt="The name of the build system package server\
        <br>Package Server:"
while ! $ok ; do
    [ -z "$utext" ] && utext="$pkgServer"
    utext=$(kdialog --title "$TITLE" --inputbox "$prompt" "$utext")
    [[ "$utext" =~ ^[a-zA-Z][a-zA-Z0-9_-]+$ ]] && ok=true
done
pkgServer="$utext"

erase=1
if [ -n "$startupMode" ]; then
    kdialog --title "$TITLE" --yesno \
        "This utility is being manually run.<br>\
        Do you want to force the script to be run on the next reboot"
    erase=$?
fi

#
#  Save config for next time
#
echo $mname
echo $mode
echo $pkgServer
mkdir -p /root/.vix
cat > /root/.vix/config.data <<hereData
mname="$mname"
mode="$mode"
pkgServer="$pkgServer"
hereData

################################################################################
#
#   Perform the initialisation
#
echo "--------------------------------"
echo "Data collected. Configure system"

# Extend the path
PATH="/sbin:$PATH"

# Machine-specific, so remove in case this system is going to be
# cloned.  These will be regenerated on the first boot.
if [ -z "$startupMode" ] ; then
    rm -f /etc/udev/rules.d/70-persistent-cd.rules
    rm -f /etc/udev/rules.d/70-persistent-net.rules

    # Potentially sensitive.
    rm -f /root/.ssh/known_hosts
    rm -f /root/.svn
fi

#
#   Set new machine name
#
echo $mname > /etc/hostname
hostname -b -F /etc/hostname

#
#   Set the required Mode
#
autoHome=false
perthNis=false
addUser=false

if [ "$mode" = "Stand Alone VM" ] ; then
    autoHome=false
    perthNis=false
    addUser=true

elif [ "$mode" = "Local Home" ] ; then
    autoHome=false
    perthNis=true

else
    autoHome=true
    perthNis=true
fi

#
#   Configure NIS
#
echo "Configure NIS"
if $perthNis; then
    echo "Enable yp binding"
    rm -f /usr/lib/systemd/system/ypbind.override
    for ii in passwd shadow group ; do
        sed -i /etc/$ii -e "/^+/d"
        echo >> /etc/$ii '+'
    done
    systemctl enable ypbind
    systemctl start ypbind
else
    echo "Disable yp binding"
    for ii in passwd shadow group; do
        sed -i /etc/$ii -e "/^+/d"
    done
    systemctl disable ypbind
fi

#
#   Configure auto mounting of Home Drives
#
echo "Configure Automounter"
VIXAUTOHOME=/etc/auto.master.d/vix.home.autofs
if $autoHome; then
    echo "Setup automount of /home"
    if [ -f ${VIXAUTOHOME}.disabled ] ; then
        mv ${VIXAUTOHOME}.disabled ${VIXAUTOHOME}
    fi
else
    echo "Stop automount of /home"
    if [ -f ${VIXAUTOHOME} ] ; then
        mv ${VIXAUTOHOME} ${VIXAUTOHOME}.disabled
    fi
fi

#
#   Stop auto mounter so that it will not interfere with the
#   creation of user home directories
#   
#   Note: This does not appear to stop the automounter
#         May not be an issue as we are about to reboot
systemctl stop autofs 

#
#   Create local user if need be
#   Note: There is no need to create a local home directory for local users
#         as Ubuntu appears to do this on the fly.
#
echo "Configure Local User"
if $addUser; then
    /usr/bin/python /usr/share/system-config-users/system-config-users.py
fi

#
#   Configure the package server
#
echo "Configure Package Server"
sed -i /etc/profile.d/jats.sh -e "s~^\(GBE_DPKG=/net\)/\([^/]*\)/\(.*\)~\1/$pkgServer/\3~"

#
#   Flag - configuration done
#   This should stop the auto script from running again
#
if [ -z "$startupMode" ]; then
    echo "Flag - don't run again"
    mkdir -p /root/.vix
    touch /root/.vix/config
fi
if [ "$erase" -eq "0" ] ; then
    echo "Erase config details. Force script to run on next reboot"
    rm -f /root/.vix/config
fi

#
#   Reboot the system
#   If manually invoked then give the user the option of aborting the boot
#
ktype=yesno
[ -z "$startupMode" ] && ktype=msgbox
kdialog --title "$TITLE" --$ktype "The system will now reboot in order for the changes to take effect"
if [ $? -ne 1 ]; then
    echo "Reboot the system"
    reboot --no-wall -f
    echo "Reboot failed. Command returned"
fi
echo "Script complete"