Rev 165 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed
#!/bin/bash## Fix things during bootup - run last# called at the end of /etc/rc.d/rc.local## Urs Beyerle#### -----------------------------------------------------------### definitions### -----------------------------------------------------------# dir of mounted live system: /$MOUNTDIR/liveMOUNTDIR=livecd# source functions. /$MOUNTDIR/live/liblinuxlive. /etc/init.d/functions# defaults for xorg.confXORG_CONF=/etc/X11/xorg.confHSYNC_DEFAULT="30.0-80.0"VSYNC_DEFAULT="50.0-90.0"SCREEN_DEFAULT="1400x1050"MODES_DEFAULT='"2048x1536" "1920x1200" "1600x1200" "1400x1050" "1280x1024" "1280x960" "1280x800" "1152x864" "1152x768" "1024x768" "800x600" "640x480"'### -----------------------------------------------------------### 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 passwordNOPASSWD=$( 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 )# failsafe X serverSIMPLEX=$( cmdline_parameter simplex )# NVIDIA driverNVIDIA=$( cmdline_parameter nvidia )[ "$XDRIVER" = "nvidia" ] && NVIDIA=nvidia# 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"fi# should we read only mount all found devices?ROMOUNT=$( cmdline_parameter romount )[ $ROMOUNT ] && MOUNTOPT="$MOUNTOPT,ro"# folder where we find data which was previously stored# (can be on an usbstick or harddisk partition)SAVEFOLDER=SL_LIVECD# 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 update fstab?NOFSTAB=$( cmdline_parameter nofstab )# kde/gnome as desktopGNOME=$( cmdline_parameter gnome )KDE=$( cmdline_parameter kde )# do no fixes (useful for debugging)NOFIX=$( cmdline_parameter nofix )# do not start networkNONET=$( cmdline_parameter nonet )### -----------------------------------------------------------### Functions### -----------------------------------------------------------reload_soundmodule() {### unload/load sound module for snd-card-X (timing problem?)if [ ! $NOFIX ]; thenrpm -q alsa-lib | grep el5 >/dev/nullif [ "$?" = "0" ]; then# disable kernel messagesecho "0" > /proc/sys/kernel/printksound_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>&1modprobe $module >/dev/null 2>&1fidone# enable kernel messagesecho "6" > /proc/sys/kernel/printkfifi}### -----------------------------------------------------------### Improved hardware detection### -----------------------------------------------------------### Improved graphic card detectionif [ ! $SIMPLEX ] && [ ! $NOFIX ]; then### Intel Mobile 915GMlspci | grep -q VGA.*Intel.*Mobile.*915GMif [ "$?" = "0" ]; thenI915GM=i915gm[ $XDRIVER ] || XDRIVER=i810fi### Intel Mobile 855GMlspci | grep -q VGA.*Intel.*855GMif [ "$?" = "0" ]; thenI855GM=i855gmfifi### -----------------------------------------------------------### Main### -----------------------------------------------------------### try to load fuse kernel modulemodprobe fuse 2>/dev/null### reload sound module (first time)reload_soundmodule### 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=$( basename $DEVICE )mkdir -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 in fstabif [ "$?" = "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/nullfidone# 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### check for xorg.conf in CONFIG_FOLDERif [ -r $CONFIG_FOLDER/xorg.conf ]; thencp -af $CONFIG_FOLDER/xorg.conf $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### configure Xserverif [ -x /usr/bin/system-config-display ]; thenecho "Configure Xserver:"if [ ! $HAVE_XORG_CONF ]; thenif [ ! $SIMPLEX ]; then# default values[ ! $HSYNC ] && HSYNC="$HSYNC_DEFAULT"[ ! $VSYNC ] && VSYNC="$VSYNC_DEFAULT"[ ! $SCREEN ] && SCREEN="$SCREEN_DEFAULT"# run system-config-displaysystem-config-display \--reconfig \-v \--set-resolution=$SCREEN \--set-hsync=$HSYNC \--set-vsync=$VSYNC \$video_driver | grep Trying# system-config-display in SL5 does not work nicely :-(# e.g. No Monitor Section# add Monitor Section and add Monitor0 to Screen Sectiongrep -q Monitor $XORG_CONFif [ "$?" != "0" ]; thenecho "Section \"Monitor\"" >> $XORG_CONFecho -e "\tIdentifier \"Monitor0\"" >> $XORG_CONFecho -e "\tHorizSync $HSYNC" >> $XORG_CONFecho -e "\tVertRefresh $VSYNC" >> $XORG_CONFecho -e "\tOption \"dpms\"" >> $XORG_CONFecho -e "EndSection" >> $XORG_CONFsed -i "s|Device \"Videocard0\"|Device \"Videocard0\"\n\tMonitor \"Monitor0\"|" $XORG_CONFfi# add Modes for $SCREEN, if not yet theregrep -q Modes $XORG_CONFif [ "$?" != "0" ]; thenmodeline=$( echo ${MODES_DEFAULT} | sed "s|.*\"$SCREEN|\"$SCREEN|" )if [ "$modeline" ]; thensed -i "s|\tDepth.*|\tDepth 24\n\t\tModes $modeline|" $XORG_CONFfifi# simple Xserver configuration.elseecho "Use simple Xserver configuration."system-config-display --noui --reconfig -v | grep Tryingfififi### enable nvidia driverif [ $NVIDIA ] && [ ! $NONVIDIA ]; then# serach for Nvidia Video Card/sbin/lspci | grep VGA | grep -i -q nvidiaif [ "$?" = "0" ]; then# nvidia-x11-drv installed?which nvidia-xconfig >/dev/null 2>&1if [ "$?" = "0" ]; thenecho "Enable nvidia driver."# set driver to nv in case it's only vesased -i 's|"vesa"|"nv"|' /etc/X11/xorg.conf# configure for nvidia drivernvidia-xconfig >/dev/null 2>&1nvidia-config-display enableelseecho "Nvidia driver not found."fififi### 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 $XORG_CONFfi# Set BoardName for Intel 915GMif [ $I915GM ]; thensed -i 's/BoardName.*VESA driver.*/BoardName "Intel 915"/' $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 (only for PSI)if [ $PSI ] && [ $AFS ]; thenecho "Update AFS users..."[ -x /afs/psi.ch/sys/common/update_user.pl ] && /afs/psi.ch/sys/common/update_user.pl >/dev/null 2>&1 &if [ -d /usr/pack ]; thenecho "Update SEPP links..."[ -x /afs/psi.ch/sys/common/update_sepp.pl ] && /afs/psi.ch/sys/common/update_sepp.pl >/dev/null 2>&1 &fiechofi### create local user, if "nolocal" is not setif [ ! $NOLOCAL ]; thenuser=$LOCALUSER# execute useradd/groupadd twice, to make it work (don't know why)if [ $PSI ] && [ ! $NOADMIN ]; thengroupdel $user 2>/dev/nullgroupadd $user 2>/dev/nullgroupdel $user 2>/dev/nullgroupadd $useruserdel -r $user 2>/dev/nulluseradd -G $LocalAdminGroup $user 2>/dev/nulluserdel -r $user 2>/dev/null# add user to fuse group ?grep -q "^fuse:" /etc/groupif [ "$?" = "0" ]; thenuseradd -g $user -G ${LocalAdminGroup},fuse $userelseuseradd -g $user -G ${LocalAdminGroup} $userfielseuserdel -r $user 2>/dev/nulluseradd $user 2>/dev/nulluserdel -r $user 2>/dev/nullgrep -q "^fuse:" /etc/group# add user to fuse group ?if [ "$?" = "0" ]; thenuseradd -G fuse $userelseuseradd $userfififi### 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### autostart kmix and krandrtray in KDEif [ ! $NOLOCAL ]; thenmkdir -p /home/${LOCALUSER}/.kde/Autostartcp -a /usr/share/applications/kde/krandrtray.desktop /home/${LOCALUSER}/.kde/Autostart 2>/dev/nullcp -a /usr/share/applications/kde/kmix.desktop /home/${LOCALUSER}/.kde/Autostart 2>/dev/nullchown -R ${LOCALUSER}.${LOCALUSER} /home/${LOCALUSER}/.kde/Autostartfi### first set empty passwordif [ ! $NOROOT ]; thensed -i "s|^root:.*|root::12345:0:99999:1:::|" /etc/shadowfiif [ ! $NOLOCAL]; thensed -i "s|^$LOCALUSER:.*|$LOCALUSER::12345:0:99999:1:::|" /etc/shadowfi### 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' "fiechoecho "or hit return for empty password."if [ ! $NOLOCAL ]; thenecho "Login as local user '$user' with this password."fiecho# ask for password, if not yet given by $PASSWDif [ ! $PASSWD ]; thenecho -n "Password: "read -s PASSWDechofi# set password - or - set empty passwordif [ ! $NOROOT ] && [ "$PASSWD" ]; thenecho $PASSWD | passwd --stdin root >/dev/nullfiif [ ! $NOLOCAL] && [ "$PASSWD" ]; thenecho $PASSWD | passwd --stdin $user >/dev/nullfiechofifi### 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 is 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/psififor 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.confgrep -q ServerName /etc/cups/client.conf || echo "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>&1### set keyboard for rdesktopif [ -e $XORG_CONF ]; thengrep -q de_CH $XORG_CONF && RDESKTOP_OPT="-k de-ch"grep -q fr_CH $XORG_CONF && RDESKTOP_OPT="-k fr-ch"grep -q jp106 $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### 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### reload sound module (second time)reload_soundmodule### bring up all network interfaces (useful to enable WLAN devices)if [ ! $NONET ]; thendevices=$( ls /etc/sysconfig/network-scripts/ifcfg-* \| sed "s|/etc/sysconfig/network-scripts/ifcfg-||" | grep -v lo )for device in $devices; doifconfig $device up > /dev/null 2>&1 &donefi