Rev 1 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed
#!/bin/bash## Fix things during bootup - run last## Urs Beyerle, PSI#### -----------------------------------------------------------### definitions### -----------------------------------------------------------# dir of mounted live system: /$MOUNTDIR/liveMOUNTDIR=livecd# source functions. /$MOUNTDIR/live/liblinuxlive. /etc/init.d/functions### -----------------------------------------------------------### Get boot parameters### -----------------------------------------------------------# root on NFS?NFSROOT=$( cmdline_value nfsroot )# NFS ServerNFSSERVER=$( echo $NFSROOT | cut -d":" -f1 )# no local user?NOLOCAL=$( cmdline_parameter nolocal )# no root user?NOROOT=$( cmdline_parameter noroot )# no localadmin?NOADMIN=$( cmdline_parameter noadmin )LocalAdminGroup=localadmin# set passwordPASSWD=$( cmdline_value passwd )[ ! $PASSWD ] && PASSWD=$( cmdline_value pw )# no password = empty password ?NOPASSWD=$( cmdline_parameter nopasswd )# keyboard layout (kb= or keyboard=)KEYBOARD=$( cmdline_value keyboard )KB=$( cmdline_value kb )# PSI setup?PSI=$( cmdline_parameter psi )# cups serverCUPS=$( cmdline_value cups )# no X11NOX=$( cmdline_parameter nox )# failsafe X serverSIMPLEX=$( cmdline_parameter simplex )# NVIDIA driverNVIDIA=$( cmdline_parameter nvidia )if [ "$XDRIVER" = "nvidia" ]; thenNVIDIA=nvidiafi# force NO NVIDIA driver - will overwite NVIDIA optionNONVIDIA=$( cmdline_parameter nonvidia )# VESA driverVESA=$( cmdline_parameter vesa )# force to use a video driver ?XDRIVER=$( cmdline_value xdriver )# Xserver configurationsHSYNC=$( cmdline_value hsync )VSYNC=$( cmdline_value vsync )SCREEN=$( cmdline_value screen )# no SWAPNOSWAP=$( cmdline_parameter noswap )# should we auto mount all found devices?AUTOMOUNT=$( cmdline_parameter automount )if [ $AUTOMOUNT ]; thenMOUNTOPT="auto,users"elseMOUNTOPT="noauto,users,ro"fi# folder where we find data which was previously stored# (normally on a usbstick, but can be partition)if [ $PSI ]; thenSAVEFOLDER=PSI_LIVECDelseSAVEFOLDER=SL_LIVECDfi# local home partition?# (e.g. /dev/hda1, /dev/hda1:/my_home)HOMELOCAL=$( cmdline_value home )# fast booting ?FASTBOOT=$( cmdline_parameter fastboot )# start afs?AFS=$( cmdline_parameter afs )# or force no afs[ $( cmdline_parameter noafs ) ] && AFS=""# turn of sound (volume=0)NOSOUND=$( cmdline_parameter nosound )# local user name?LOCALUSER=$( cmdline_value user )# hostname?HOSTNAME=$( cmdline_value hostname )# do we have defined a configuration directory?CONFIG=$( cmdline_value config )# do not udpate fstab?NOFSTAB=$( cmdline_parameter nofstab )# kde/gnome as desktopGNOME=$( cmdline_parameter gnome )KDE=$( cmdline_parameter kde )### -----------------------------------------------------------### Improved hardware detection### -----------------------------------------------------------### Improved graphic card detection?if [ ! $SIMPLEX ]; then### Intel Mobile 945GMlspci | grep -q VGA.*Intel.*Mobile.*945GMif [ "$?" = "0" ]; thenI945GM=i945gm## not yet supported by i810 driver## [ $XDRIVER ] || XDRIVER=i810I915RESOLUTION=onfi### Intel Mobile 915GMlspci | grep -q VGA.*Intel.*Mobile.*915GMif [ "$?" = "0" ]; thenI915GM=i915gm[ $XDRIVER ] || XDRIVER=i810I855RESOLUTION=onfi### Intel Mobile 855GMlspci | grep -q VGA.*Intel.*855GMif [ "$?" = "0" ]; thenI855GM=i855gmI855RESOLUTION=onfifi### -----------------------------------------------------------### Main### -----------------------------------------------------------### try load fuse kernel modulemodprobe fuse 2>/dev/null### activate SWAP# delete all swap lines in fstabsed -i -e "/ swap /d" /etc/fstab# search for swap partitionfdisk -l > /var/log/fdiskswap_part=$( fdisk -l 2>/dev/null | grep -i "Linux swap" \| grep "^/dev/" | cut -f 1 -d " " | head -1 )# add to /etc/fstab and activate SWAPif [ $swap_part ] && [ ! $NOSWAP ]; thenecho "$swap_part swap swap defaults 0 0" >>/etc/fstab# activate SWAPswapoff -aswapon -a -efi### MAC Address of eth0MAC_ETH0=$( ifconfig | grep eth0 | awk '{print $5}' )### Get CONFIG_FOLDER for this machine from NFS Server (if existing)# and copy it to /$MOUNTDIRif [ $NFSSERVER ] && [ $CONFIG ]; thenmkdir -p /mnt/configecho "Search for configuration files on the server ..."/etc/init.d/portmap start >/dev/nullmount -t nfs $NFSSERVER:$CONFIG /mnt/config 2>/dev/nullif [ "$?" != "0" ]; thenecho "Could not mount $NFSSERVER:$CONFIG."elseif [ -d /mnt/config/$MAC_ETH0 ]; thenCONFIG_FOLDER=/$MOUNTDIR/configmkdir -p $CONFIG_FOLDERcp -a /mnt/config/$MAC_ETH0/* /$CONFIG_FOLDER/ 2>/dev/nullif [ "$?" != "0" ]; thenecho "Could not get $NFSSERVER:$CONFIG/$MAC_ETH0"CONFIG_FOLDER=""elseecho "Found configuration files on the server."fifiumount /mnt/configfi/etc/init.d/portmap stop >/dev/nullfi### search for partitions and update fstab### and look for $SAVEFOLDER (previously stored data)if [ ! $FASTBOOT ] && [ ! $NOFSTAB ]; thenecho "Update /etc/fstab ..."# disable kernel messages during mount/unmountecho 0 > /proc/sys/kernel/printkDEVICES=$( list_partition_devices )for DEVICE in $DEVICES; do# try to mount the device and unmount it.# If OK, we can add it to fstabDEV=$( echo $DEVICE | cut -d"/" -f 3 )if [ $DEV ]; thenmkdir -p /mnt/$DEVmount $DEVICE /mnt/$DEV >/dev/null 2>&1# search for $SAVEFOLDERif [ -d /mnt/$DEV/$SAVEFOLDER ]; thenFOUND_RESTORE_DEV=/dev/$DEVecho "Found folder '$SAVEFOLDER' on $FOUND_RESTORE_DEV"fi# unmount againumount /mnt/$DEV >/dev/null 2>&1# if sucsessful add to fstab, if not already doneif [ "$?" = "0" ]; thengrep -q "^$DEVICE " /etc/fstabif [ "$?" != "0" ]; thenecho -e "$DEVICE \t /mnt/$DEV \t auto \t $MOUNTOPT,suid,dev,exec 0 0" >> /etc/fstabfielsermdir /mnt/$DEV 2>/dev/nullfifidone# enable kernel messages againecho 6 > /proc/sys/kernel/printkfi### if $SAVEFOLDER found, set $CONFIG_FOLDER and mount $FOUND_RESTORE_MNTif [ $FOUND_RESTORE_DEV ]; thenFOUND_RESTORE_MNT=$( grep "^$FOUND_RESTORE_DEV " /etc/fstab | awk '{ print $2 }' )CONFIG_FOLDER=$FOUND_RESTORE_MNT/$SAVEFOLDERecho "Will restore data from $CONFIG_FOLDER"mount $FOUND_RESTORE_MNTfi### source the file "config", if found in $CONFIG_FOLDERif [ -f $CONFIG_FOLDER/config ]; then. $CONFIG_FOLDER/configfi### do we have a shadow/passwd file in $FOUND_FOULDERif [ -r $CONFIG_FOLDER/passwd.tar.gz ] && [ -r $CONFIG_FOLDER/shadow.tar.gz ]; then# we do not need a password for local user and rootNOLOCAL=nolocalNOROOT=norootfi### set local user name and default hostnameif [ $PSI ]; then[ $LOCALUSER ] || LOCALUSER=l_psiDEFAULT_HOSTNAME=psielse[ $LOCALUSER ] || LOCALUSER=sluserDEFAULT_HOSTNAME=slinuxfi### start 855resolution?[ $I855RESOLUTION ] && /etc/init.d/855resolution start 2>/dev/null### start 915resolution?[ $I915RESOLUTION ] && /etc/init.d/915resolution start 2>/dev/null### configure Xserver### check for xorg.conf in CONFIG_FOLDERif [ -r $CONFIG_FOLDER/xorg.conf ]; thencp -af $CONFIG_FOLDER/xorg.conf /etc/X11/xorg.confif [ "$?" = "0" ]; thenecho "Saved xorg.conf file found."XDRIVER=""HAVE_XORG_CONF="yes"fifi### use vesa driver?if [ $VESA ]; thenecho "Force to use VESA driver!"video_driver="--set-driver=vesa"elsevideo_driver=""fi### force an other video driver?if [ $XDRIVER ]; thenecho "Force to use $XDRIVER driver!"video_driver="--set-driver=$XDRIVER"fi### run system-config-displayif [ -x /usr/bin/system-config-display ]; thenecho "Configure Xserver:"if [ ! $HAVE_XORG_CONF ]; thenif [ ! $SIMPLEX ]; then# default values[ ! $HSYNC ] && HSYNC="30.0-80.0"[ ! $VSYNC ] && VSYNC="50.0-90.0"[ ! $SCREEN ] && SCREEN="1280x1024"system-config-display \--reconfig \-v \--set-resolution=$SCREEN \--set-hsync=$HSYNC \--set-vsync=$VSYNC \$video_driver | grep Tryingelseecho "Use simple Xserver configuration."system-config-display --noui --reconfig -v | grep Tryingfififi### enable NVIDIA driver (needs nvidia, nvidia-libs rpms)if [ $NVIDIA ] && [ ! $NONVIDIA ]; then# serach for Nvidia Video Card/sbin/lspci | grep VGA | grep -i -q nvidiaif [ "$?" = "0" ]; then# lib or lib64 ?LIB=lib[ $( arch ) = "x86_64" ] && LIB=lib64# find out installed Nvidia driver versionlibglx=$( ls /usr/X11R6/$LIB/modules/extensions/libglx.so.1.* | head -n 1 )nvidia_version=${libglx##*.so.1.}# enable Nvidia driver (normally done by nvidia-enable - does not work on LiveCD)echo -n "NVIDIA graphic card found. Enable the nvidia driver ${nvidia_version} ... "NVLOG=/var/log/nvidia.log# assuming that the kernel modules are already build# link to Nvidia libsln -sf ../../usr/X11R6/$LIB/libGL.so.1.${nvidia_version} /usr/$LIB/libGL.soln -sf libGL.so.1.${nvidia_version} /usr/X11R6/$LIB/libGL.somv /usr/X11R6/$LIB/modules/extensions/libGLcore.a /usr/X11R6/$LIB/modules/extensions/xxx.libGLcore.a.saved_by_nvidiamv /usr/X11R6/$LIB/modules/extensions/libglx.a /usr/X11R6/$LIB/modules/extensions/xxx.libglx.a.saved_by_nvidialn -sf libglx.so.1.${nvidia_version} /usr/X11R6/$LIB/modules/extensions/libglx.so# reconfigure X/usr/sbin/nvidia-xconfig >> $NVLOG 2>&1if [ $PSI ]; thenecho 'NVIDIA=on' >> /etc/sysconfig/cfenginefiecho "ok."fifi### set special video driver options for Intel Mobile# (external VGA output: Clone)if [ $I915GM ] || [ $I855GM ]; thensed -e "/Section[ \t]*\"Device\"/,/EndSection/{/^[ \t]*Option[ \t]*\"MonitorLayout\"/d/^[ \t]*Option[ \t]*\"Clone/d/EndSection/i\\Option \"MonitorLayout\" \"CRT,LFP\"\\Option \"Clone\" \"1\"\\Option \"CloneRefresh\" \"60\"}" -i /etc/X11/xorg.conffi# Set BoardName for Intel 915GMif [ $I915GM ]; thensed -i 's/BoardName.*VESA driver.*/BoardName "Intel 915"/' /etc/X11/xorg.conffi### parameter KB = KEYBOARDKEYBOARD=$KB### ask user for keyboard layout if no keyboard givenif [ -x /usr/bin/system-config-keyboard ]; thenif [ ! $KEYBOARD ]; thenMORE_LAYOUTS="more..."keytables="U.S...........(us) \Swiss-German..(sg-latin1) \Swiss-French..(fr_CH-latin1) \German........(de-latin1) \Japanese......(jp106) \$MORE_LAYOUTS"PS3="-> "echoecho "Please choose keyboard layout (type 1-6):"select KEYBOARD in $keytables;docase $KEYBOARD in*)# extract keyboard layout string from $KEYBOARDKEYBOARD=${KEYBOARD##*(}KEYBOARD=${KEYBOARD%%)}break;;esacdonefifi### set keyboardif [ -x /usr/bin/system-config-keyboard ]; thenif [ "$KEYBOARD" = "$MORE_LAYOUTS" ]; thensystem-config-keyboard --textelseecho -n "Set keyboard to '$KEYBOARD', please wait ... "system-config-keyboard --noui $KEYBOARD >/dev/null 2>&1echo "done."fiechofi### update AFS users and SEPP links (only for PSI)if [ $PSI ] && [ $AFS ]; thenecho "Update AFS users and SEPP links..."[ -x /afs/psi.ch/sys/common/update_user.pl ] && /afs/psi.ch/sys/common/update_user.pl >/dev/null 2>&1 &[ -x /afs/psi.ch/sys/common/update_sepp.pl ] && /afs/psi.ch/sys/common/update_sepp.pl >/dev/null 2>&1 &echofi### create local user, if "nolocal" is not setif [ ! $NOLOCAL ]; thenuser=$LOCALUSER# execute useradd twice, to make it work (don't know why)if [ $PSI ] && [ ! $NOADMIN ]; thenuserdel -r $user 2>/dev/nulluseradd -G $LocalAdminGroup $user 2>/dev/nulluserdel -r $user 2>/dev/nulluseradd -G $LocalAdminGroup $userelseuserdel -r $user 2>/dev/nulluseradd $user 2>/dev/nulluserdel -r $user 2>/dev/nulluseradd $userfi# only for PSI: change users's group to GID=UID+50000if [ $PSI ]; thenuid=$( id -u $user )old_gid=$( id -g $user )new_gid=$(( $old_gid + 50000 ))# fix /etc/groupsed -i "s/${user}:x:${old_gid}:/${user}:x:${new_gid}:/" /etc/group# fix /etc/passwdsed -i "s/${user}:x:${uid}:${old_gid}:/${user}:x:${uid}:${new_gid}:/" /etc/passwd# fix perm of /home/${user)chgrp -R $user /home/${user}fifi### copy special files for PSI user l_psiif [ $PSI ] && [ ! $NOLOCAL ]; thenfind /usr/share/${LOCALUSER}/ -maxdepth 1 -mindepth 1 2>/dev/null | \while read dir; docp -a $dir /home/${LOCALUSER}/done# set ownerchown -R ${LOCALUSER}.${LOCALUSER} /home/${LOCALUSER}fi### set password for root and local userif [ ! $NOPASSWD ]; thenif [ ! $NOROOT ] || [ ! $NOLOCAL ]; thenecho -n "Set password for "if [ ! $NOROOT ]; thenecho -n "'root' "fiif [ ! $NOROOT ] && [ ! $NOLOCAL ]; thenecho -n "and "fiif [ ! $NOLOCAL ]; thenecho -n "local user '$user' "fiechoif [ ! $NOLOCAL ]; thenecho "Login as local user '$user' with this password."echofi# set password, if not yet given by $PASSWDuntil [ $PASSWD ]; doecho -n "Password: "read -s PASSWDechodoneif [ ! $NOROOT ]; thenecho $PASSWD | passwd --stdin root >/dev/nullfiif [ ! $NOLOCAL ]; thenecho $PASSWD | passwd --stdin $user >/dev/nullfiechofielse# set root and $LOCALUSER to empty passwordsed -i "s|^root:.*|root::12345:0:99999:1:::|" /etc/shadowsed -i "s|^$LOCALUSER:.*|$LOCALUSER::12345:0:99999:1:::|" /etc/shadowfi### try to get hostname from DNS, if not yet definedif [ ! $HOSTNAME ]; thenecho "Try to get hostname from DNS ..."IP=$( /sbin/ip add show dev eth0 2>/dev/null | grep "inet " | cut -d" " -f6 | sed 's|/.*||' )if [ "$IP" = "" ]; thenIP=$( /sbin/ip add show dev eth1 2>/dev/null | grep "inet " | cut -d" " -f6 | sed 's|/.*||' )fiif [ "$IP" != "" ]; thenhost -W 1 $IP >/dev/null 2>&1if [ "$?" = "0" ]; thenHOSTNAME=$( host -W 1 $IP | cut -d" " -f 5 | cut -d"." -f 1 )if [ "$HOSTNAME" = "3(NXDOMAIN)" ]; thenHOSTNAME=$DEFAULT_HOSTNAMEfiif [ "$HOSTNAME" = "no" ]; thenHOSTNAME=$DEFAULT_HOSTNAMEfifififi### define default hostname, if $HOSTNAME still not yet setif [ ! $HOSTNAME ]; thenHOSTNAME=$DEFAULT_HOSTNAMEfi### set hostnameexport HOSTNAME=$HOSTNAMEhostname $HOSTNAMEsed -i "s/HOSTNAME=.*/HOSTNAME=${HOSTNAME}/" /etc/sysconfig/networkif [ $PSI ]; thensed -i "s/hostname=.*/hostname=${HOSTNAME}.psi.ch/" /etc/ssmtp/ssmtp.confsed -i "s/HOSTNAME=.*/HOSTNAME=${HOSTNAME}/" /etc/sysconfig/cfenginefifor iface in eth0 eth1 eth2 eth3; doif [ -e /etc/sysconfig/network-scripts/ifcfg-${iface} ]; thenecho "DHCP_HOSTNAME=${HOSTNAME}" >> /etc/sysconfig/network-scripts/ifcfg-${iface}fidoneecho "Hostname set to: $HOSTNAME"echo### set cups serverif [ $CUPS ]; thensed -i "s|.*ServerName .*|ServerName $CUPS|" /etc/cups/client.conffi### some magic: just access this file in order that it can be sourced laterls -lag /etc/X11/xinit/xinitrc-common >/dev/null 2>&1sleep 1### set keyboard for rdesktopif [ -e /etc/X11/xorg.conf ]; thengrep -q de_CH /etc/X11/xorg.conf && RDESKTOP_OPT="-k de-ch"grep -q fr_CH /etc/X11/xorg.conf && RDESKTOP_OPT="-k fr-ch"grep -q jp106 /etc/X11/xorg.conf && RDESKTOP_OPT="-k ja"fiif [ "$RDESKTOP_OPT" ]; thenecho "alias rdesktop='rdesktop $RDESKTOP_OPT'" > /etc/profile.d/rdesktop.shecho "export RDESKTOP_OPT='$RDESKTOP_OPT'" >> /etc/profile.d/rdesktop.shchmod 755 /etc/profile.d/rdesktop.shfi### run setup script, if found in $CONFIG_FOLDER on $FOUND_RESTORE_MNTif [ -r $CONFIG_FOLDER/setup ]; thenCONFIG_FOLDER=$CONFIG_FOLDER bash $CONFIG_FOLDER/setupfi### umount $FOUND_RESTORE_MNTif [ $FOUND_RESTORE_MNT ]; thenecho "Unmount $FOUND_RESTORE_MNT"umount $FOUND_RESTORE_MNT 2>/dev/nullsleep 3fi### local home partition?if [ $HOMELOCAL ]; thenmv /home /home.oldmkdir -p /mnt/home_par# which partition? which folder?HOMEPAR=$( echo $HOMELOCAL | cut -d":" -f1 )HOMEDIR=$( echo $HOMELOCAL | grep ":" | cut -d":" -f2 )umount $HOMEPAR 2>/dev/null# mount it with option noatime, healthy for USB flash drivesmount -o noatime -rw $HOMEPAR /mnt/home_par 2>/dev/nullif [ "$?" != "0" ]; thenecho "ERROR: Could not mount read/write $HOMEPAR"umount $HOMEPAR >/dev/null 2&>1mv /home.old /homeelseecho "$HOMEPAR mounted on /mnt/home_par"# link to new homeln -s /mnt/home_par/$HOMEDIR /homeif [ ! -d /mnt/home_par/$HOMEDIR ]; thenecho "$HOMELOCAL not found on $HOMEPAR"echo mkdir -p /mnt/home_par/$HOMEDIRif [ "$?" != "0" ]; thenecho "ERROR: Could not create $HOMELOCAL on $HOMEPAR"umount $HOMEPAR >/dev/null 2&>1rm -f /homemv /home.old /homeelseecho "Folder $HOMEDIR created on $HOMEPAR"fifiif [ -d /mnt/home_par/$HOMEDIR ]; thenecho "/home is linked to /mnt/home_par${HOMEDIR}"# copy files from /home.old to /home, which are not yet there/bin/cp -a -i --reply=no /home.old/* /home/rm -rf /home.oldfifisleep 2fi### mount all if AUTOMOUNT setif [ $AUTOMOUNT ]; thenmount -afi### unload/load sound module for snd-card-X (timing problem?)rpm -q alsa-lib | grep el5 >/dev/nullif [ "$?" = "0" ]; thensound_modules=$( grep snd-card- /etc/modprobe.conf | awk ' $1 ~ /alias/ { print $3 }' | tr - _ )for module in $sound_modules; dolsmod | awk '{ print $1 }' | grep $module >/dev/null 2>&1if [ "$?" = "0" ]; thenrmmod $module >/dev/null 2>&1sleep 2modprobe $module >/dev/null 2>&1fidonefi### unmute all mixers and set volumesif [ -x /usr/bin/set-volume ]; thenif [ $NOSOUND ]; then/usr/bin/set-volume 0 > /var/log/set-volume.log 2>&1 &else/usr/bin/set-volume 60 > /var/log/set-volume.log 2>&1 &fifi### set kde or gnome as desktopif [ $KDE ]; thensed -i "/^DESKTOP=.*/d" /etc/sysconfig/desktop 2&>/dev/nullecho "DESKTOP=KDE" >> /etc/sysconfig/desktopfiif [ $GNOME ]; thensed -i "/^DESKTOP=.*/d" /etc/sysconfig/desktop 2&>/dev/nullecho "DESKTOP=GNOME" >> /etc/sysconfig/desktopfi### change to runlevel 3, if NOX (bad hack)if [ $NOX ]; theninit 3fi