Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 323 lines (281 sloc) 6.95 KB
#!/bin/bash
### TODO
## == ## check user wheel/sudo
## == ## Add optionnal packages before running this post install
## == ## Better check group adm/admin and generally group to add
## == ## Check -a option from usermod that may be -A on some linux version
# This script is used for users sync after multi linux install on a single machine
# Determine program name
PRGDIR=`type $0 | awk '{print $3}'`
PRGNAME=`basename $0`
PRGNAME=`basename $0 | awk -F '.' '{print $1}'`
# Get absolute path name of the script
PWD=`pwd`
DIRNAME=`dirname $0| sed 's/^\.//'`
LIB="$PWD/$DIRNAME"
# Define here the default values
# Local group for shared users
LOCALGROUP=local
LOCALGROUPID=10000
# This is the admin group GID
ADMINGROUP=admin
ADMINGID=120
# This is all groups that are assigned by defaults for shared users
ADMGROUPS="adm wheel vboxuser kvm $ADMINGROUP"
# User to sync
# For now just one is supported
USERTOSYNC=
# share data FS name (need to have home directory directly in root of this FS, it will be mounted as /$DATAFS and need $DATAFS as label
DATAFS=data
# Users generic env dir and files
USERENVDIR=/data/env.d
USERBASHRC=$USERENVDIR/bashrc
function usage {
cat << FIN
Create common settings on machine after fresh install
usage : $PRGNAME
-d : run in debug mode
Short explanations :
It needs a device with label data to be mounted as /data
/data/home dir will be linked to local /home
Users will be created with referenced id and standard group called local
admin group will be created and also admin user will receive $ADMGROUPS
FIN
}
function check_user() {
if [ ${USERTOSYNC:=NULL} == "NULL" ] ;
then
__print "ERROR" "USERTOSYNC not defined, exiting"
exit
fi
}
function confirm() {
if [ $1 -ne 0 ];
then
shift
__print "INPUT" "$*"
read __DUM
fi
}
function check_first () {
REZ=$(getent group $ADMINGROUP)
if [ $? -ne 0 ] ;
then
__print "INFO" "$ADMINGROUP group do not exist $ADMINGROUP:$ADMINGID"
else
REZ=$(echo $REZ | cut -d ':' -f 3)
if [ ${REZ:=0} -ne $ADMINGID ];
then
__print "ERROR" "$ADMINGROUP should be $ADMINGID but is $REZ , exiting"
exit 1
fi
fi
}
# Print to std format, ignore output if SILENT env var is set to 1
# First arg is LABEL
# rest is message itself
# printf "%-10s %-10s %-22s %-30s \n" "INFO :" "umounting filesystem" "$i"
__print() {
if [ ${PRGNAME:=NULL} == "NULL" ] ;
then
echo "ERROR __print function exiting"
exit 99
fi
local __label=$1
shift
if [ ${SILENT:=0} -ne 1 ];
then
printf "%-10s %-10s %-22s %-30s \n" "$PRGNAME : " "$__label : " "$*"
#echo $*
fi
}
amiroot() {
CMD=/usr/bin/whoami
if [ ! -x $CMD ];
then
__print "ERROR" "cant get root status"
exit 1
else
__DUM=$(/usr/bin/whoami)
if [ ${__DUM:=NULL} == "root" ];
then
__print "INFO" "You are root, continue"
else
__print "ERROR" "Need to be root to run this"
exit 1
fi
fi
}
is_existing() {
local __SOURCE=$1
shift
local __PARAM=$*
getent $__SOURCE $__PARAM > /dev/null 2>&1
RET=$?
echo $RET
return $RET
}
is_yes() {
local __PARAM
local __RET=1
local __STRING="Do you want to continue"
[[ $# -ne 0 ]] && __STRING="$*"
YES=(Y y )
__print "QUESTION" "$__STRING (${YES[@]})"
read __PARAM
for i in "${YES[@]}" ;
do
if [ "$i" == "$__PARAM" ];
then
#echo "$__PARAM found $i"
__RET=0
break
#else
# echo "$__PARAM not found"
fi
done
#echo $__RET
return $__RET
}
build_datafs() {
local __this
mount | egrep -w /$DATAFS > /dev/null 2>&1
if [ $? -eq 0 ];
then
__this=$(mount | egrep -w /$DATAFS | cut -f 1 -d ' ')
__print "INFO" "/$DATAFS mounted on $__this"
return 1
fi
[[ ! -d /$DATAFS ]] && mkdir /$DATAFS
egrep -w /$DATAFS /etc/fstab > /dev/null 2>&1
__this=$(blkid -L $DATAFS)
if [ $? -ne 0 ];
then
__print "ERROR" "$DATAFS partition not found, create label like e2label partition data and run again"
exit 1
fi
__print "INFO" -n "Adding /$DATAFS (device is $__this) filesystem to fstab ?"
echo "$__this /$DATAFS ext4 defaults 0 2" >> /etc/fstab
mount /$DATAFS
if [ $? -ne 0 ];
then
__print "ERROR" "Error mounting $__this /$DATAFS"
exit 1
fi
}
#Global vars
export SILENT=0
# Getopts
while getopts dh sarg
do
case $sarg in
d) set -x
DEBUG=1 ;;
h) usage
end;;
*) echo "ERROR : $PRGNAME : Bad option or misusage"
usage
end ;;
esac
done
#main
amiroot
#check_user
check_first
build_datafs
if [ $(is_existing group $LOCALGROUP) -ne 0 ] ;
then
__print "INFO" "Creating group local"
groupadd -g $LOCALGROUPID $LOCALGROUP
confirm $? "Error creating $LOCALGROUP groupid $LOCALGROUPID"
else
if [ $(getent group $LOCALGROUP | cut -d ':' -f 3) -ne $LOCALGROUPID ] ;
then
__print "FATAL" "Group $LOCALGROUP is not $LOCALGROUPID"
exit 1
fi
fi
if [ $(is_existing group $ADMINGROUP) -eq 0 ] ;
then
__print "INFO" "$ADMINGROUP:$ADMINGID group is existing"
else
__print "INFO" "creating $ADMINGROUP ($ADMINGID) group"
groupadd -g $ADMINGID $ADMINGROUP
if [ $? -ne 0 ] ;
then
__print "ERROR" "Fail to create admin group $ADMINGROUP:$ADMINGID"
fi
fi
for i in $(ls -d /data/home/*) ;
do
LOCALUID=$(ls -ladn $i | awk '{print $3}')
LOCALGID=$(ls -ladn $i | awk '{print $4}')
USERNAME=$(basename $i)
if [ $(is_existing passwd $USERNAME) -eq 0 -o $(is_existing passwd $LOCALUID) -eq 0 ] ;
then
__print "INFO" "User $USERNAME or UID $LOCALUID already existing, Ignored"
continue
fi
if [ $LOCALGID -ne $LOCALGROUPID ];
then
__print "WARNING" "User $USERNAME group ID is not $LOCALGID, continue ? (^C to quit)"
read DUMMY
fi
__print "INFO" "Creating user $USERNAME"
useradd -u $LOCALUID -g local -d /home/$USERNAME -M -s /bin/bash $USERNAME
if [ $? -ne 0 ] ;
then
__print "ERROR" "Fail to create user $USERNAME:$LOCALUID"
__print "WARNING" "Jumping to next user"
continue
else
__print "WARNING" "Dont forget to update $USERNAME user password"
fi
if [ -h /home/$USERNAME ];
then
STAT=$(stat --format=%F%N /home/$USERNAME)
__print "WARNING" "/home/$USERNAME is existing as $STAT"
else
ln -s /data/home/$USERNAME /home/
fi
is_yes "Add $USERNAME in $ADMINGROUP groups"
if [ $? -eq 0 ] ;
then
#usermod -G $ADMINGROUP $USERNAME
for i in $(echo $ADMGROUPS) ;
do
getent group $i > /dev/nul 2>&1
if [ $? -eq 0 ];
then
__print "INFO" "Add user pierre to $i group"
usermod -a -G $i $USERNAME
else
__print "WARNING" "group $i not found"
fi
done
fi
done
__TEST=$(stat --printf="%G\n" /var/log/messages)
if [ ${__TEST:=NULL} != "$ADMINGROUP" ] ;
then
__print "INFO" "changing messages file group to $ADMINGROUP"
__print "INFO" "for syslog-ng : destination messages { file("/var/log/messages" group("admin") perm(0640)); };"
fi
# update root passwd
__print "INFO" "Updating root .bashrc"
if [ ! /root/.bashrc ];
then
touch /root/.bashrc
fi
grep $USERBASHRC.root /root/.bashrc > /dev/null 2>&1
if [ $? -eq 0 ] ;
then
__print "INFO" "Reference to $USERBASHRC.root already exist"
else
cat << fin >> /root/.bashrc
if [ -f $USERBASHRC.root ] ;
then
. $USERBASHRC.root
fi
fin
fi