PDA

View Full Version : Need a few pointers for my fedora 7 shellscript



leadgolem
1st June 2007, 09:28 PM
While I'm waiting for my fedora 7 disk image to download I've been writing a shellscript to set it up the way I want it.

I need clarification on uncompressing a .tar.bz2 file in the script. What I'm going to do, is backup my home directory to a .tar.bz2 file. I then want to use the script to uncompress it in fc7, replacing any files with identical names. The file will be on another mounted HD. I think that this will do it.
tar -xzvf /mnt/storagefat32/username.tar.bz2 /home
chmod -R a=rwx /home/usernameThe script has to be run as root, hence the chmod. Do you guys think this will work?

Also, if anyone knows how to turn off services for runlevel 5 in the command line, that would be much appreciated.

Here's the rest of the script as it stands. Please let me know if I screwed up anywhere.
#!/bin/sh
#You need to become root and then run this script.
#./setupfedora7

E_NOTROOT=67

if [ "$UID" -ne "0" ]
then
echo "Hello, $USER. You must be root to run this script."
echo "Please enter your root password and rerun this script."
su
exit $E_NOTROOT
fi

#Remove a few unnecessary programs.
echo 'Remove a few things'
yum -y remove totem gaim compiz gnome-games
#Updates first
echo 'Starting updates, this may take a long time.'
yum -y update
#Install a boat load of tools.
echo 'Starting tools installation.'
yum -y install rpm-build cabextract numlockx kbd k3b k3b-extras-nonfree k3b-extras p7zip p7zip-plugins
#Finish setting numlock to default.
echo 'Setting numlock to default in command line.'
/bin/cp ./modular/resources/rc.local /etc
#Install Graphic Programs
echo 'Installing graphics programs'
yum -y install blender inkscape
#Install office
echo 'Installing extra office programs'
yum -y install tellico abiword
#Install sound stuff
echo 'Installing sound tools'
yum -y install xmms xmms-libs xmms-adplug xmms-cdread xmms-flac
#Install Games
echo 'Installing Games, this may take a long time'
yum -y install freeciv gweled lincity-ng lincity-ng-data lucidlife nethack-vultures wesnoth wesnoth-tools wesnoth-server viruskiller scorched3d wormux warzone2100 warzone2100-data dosbox wine wine-core wine-esd wine-jack wine-cms wine-tools wine-ldap wine-capi wine-twain wine-nas freedroidrpg
#Livna installs
echo 'Installing livna repository'
rpm -ivh http://rpm.livna.org/livna-release-7.rpm
echo 'Attempting installations requiring livna repository'
yum -y --enablerepo=livna install kmod-nvidia kmod-ntfs audacity-nonfree avidemux xmms-mp3 dvdstyler totem-xine gstreamer-plugins-ugly mplayerplug-in libdvdcss libdvdnav unrar
#Finish setting up unrar.
echo 'Finishing unrar setup.'
ln -s /usr/bin/unrar /usr/bin/rar
echo 'Complete'
#localstuff
echo 'Installing local packages'
yum -y --enablerepo=livna localinstall dvd95-1.2p0-fc6.i386.rpm gnormalize-0.52-1.noarch.rpm XnView-static-fc4.i386.rpm yafray-0.0.7-2.i386.rpm
echo 'Complete'
#Install swiftfox
./install-swiftfox.sh
#Replace swiftfox icons
echo 'Replacing Swiftfox Icons'
/bin/cp ./graphics/*.* /opt/swiftfox/icons
echo 'Complete'
#Install Desktop Entries
echo 'Installing Menu Items'
cp ./desktops/*.desktop /usr/share/applications
echo 'Complete'
#fstab
echo 'Configuring fstab'
mkdir /mnt/storagefat32
mkdir /mnt/storage2fat32
mkdir /mnt/storate3fat32
mkdir /mnt/windows
/bin/cp /etc/fstab /etc/fstab.bak
/bin/cp fstab /etc
echo 'Configuration Complete'
echo 'Enabling additional hardrives'
mount -a
echo 'Hardrives enabled'
exit

The_Jaymz
1st June 2007, 09:41 PM
Looks good. I think you have a typo in your #fstab section though.
#fstab
echo 'Configuring fstab'
mkdir /mnt/storagefat32
mkdir /mnt/storage2fat32
mkdir /mnt/storate3fat32 <----
mkdir /mnt/windows


And I thought to use bz2 with tar you had to use the j option instead of z.

The_Jaymz
1st June 2007, 09:44 PM
You might use this too for the "make sure it's run as root part."

if [ ! $( id -u ) -eq 0 ]; then
echo "Please enter root password: "
exec su -c "${0} ${CMDLN_ARGS}"
exit ${?}
fi

The_Jaymz
1st June 2007, 09:57 PM
Turn off services for runlevel 5:

chkconfig --level 5 yum-updatesd on --------------- or off depending on your needs.

lmo
1st June 2007, 09:58 PM
I thought tar -p (permissions) didn't need chmod.

tar -xjpvf

-j .bz2
-z .gz

sej7278
1st June 2007, 10:04 PM
chmod -R a=rwx /home/username

NOT GOOD.

surely you just want to do a "chown -R username", just to change the ownership to that user, not allow any user to write to your home directory?!

and putting absolute paths might be better, rather than from the current directory ./blah

it's a good idea though, and something i keep meaning to get around to, i've used jumpstart on solaris, kickstart is the redhat alternative, but i guess a shell script will do.

oh and the runlevel 5 thing is "chkconfig --level 5 apache off"

and yeah, it's "tar -jxvf" for bzip2

leadgolem
1st June 2007, 10:29 PM
sej7278
I'm using relative paths because this will be run from my pen drive, which will have all the other files I need on it. You are correct, I did just want to give myself ownership of the files. However, I simply didn't know the chown command existed. I am the only user on this system, but still VERY good info.

Imo
Thank you for the tip.

The_Jaymz
Good catch on the type. Keep in mind that most of the scripts I've created have been 3 lines, to launch wine apps with my prefered options. Will this allow me to enter my root password and then continue the script, if I wasn't running it as root before?
if [ ! $( id -u ) -eq 0 ]; then
echo "Please enter root password: "
exec su -c "${0} ${CMDLN_ARGS}"
exit ${?}
fiThank you everybody, I think I will be able to finish this script now. :)

wdgiles
1st June 2007, 11:53 PM
Will you consider posting / editing the first one with the completed version? I was just gathering all the files to build the same, but you beat me to it.

leadgolem
2nd June 2007, 01:09 AM
Sure, but it will be specific to my setup and preferences.

The_Jaymz
2nd June 2007, 02:13 AM
sej7278
I'm using relative paths because this will be run from my pen drive, which will have all the other files I need on it. You are correct, I did just want to give myself ownership of the files. However, I simply didn't know the chown command existed. I am the only user on this system, but still VERY good info.

Imo
Thank you for the tip.

The_Jaymz
Good catch on the type. Keep in mind that most of the scripts I've created have been 3 lines, to launch wine apps with my prefered options. Will this allow me to enter my root password and then continue the script, if I wasn't running it as root before?Thank you everybody, I think I will be able to finish this script now. :)



Yes, that will ask for the root password and then continue to run if it's provided; exit if not.

leadgolem
2nd June 2007, 02:54 AM
Thank you! I've been looking for something like this for awhile.

leadgolem
2nd June 2007, 03:13 AM
I'll post the completed script tomorrow. I need to move 60gb of data out of my home folder before I make my archive, and I think I'm going to need more then one archive. Besides, I've been up for 24 hours.

v00d00
2nd June 2007, 03:30 AM
This is what mine looks like, but im in the middle of redoing it, to add more stuff, but it gives you an idea. Also check out the link at the bottom.



#!/bin/bash
path=/home/zulu/backups

cd $path

# Add extra repos
rpm -ivh livna-release-7.rpm
rpm -ivh freshrpms-release-1.1-1.fc.noarch.rpm

# Install extra software

# Desktop Stuff
yum -y install fluxbox xscreensaver gedit gkrellm2 lm_sensors xarchiver gcalctool gqview

# Games
yum -y install lbreakout2

# Multimedia
yum -y --disablerepo=fresh* mplayer-gui
yum -y install k3b mplayerplug-in mplayer-skins vlc xmms xmms-mp3

# Network
yum -y install gftp gaim gnome-nettool seamonkey

# Office
yum -y install abiword gnumeric koffice-kivio koffice-karbon koffice-krita koffice-kchart koffice-kformula koffice-kugar

# Security
yum -y install aide chkrootkit tor privoxy scrub

# Development
yum install gcc kernel-devel gdb strace gcc-c++ gcc-gfortran

# Copy files into place
cp $path/themes/* /usr/share/gkrellm2/themes/
cp $path/etc/cron.hourly/wipe-bashhist /etc/cron.hourly/
cp $path/backgrounds/*.jpg /usr/share/backgrounds/images/
cp $path/etc/hosts /etc/hosts
cp $path/etc/init.d/encrypted_swap /etc/init.d/encrypted_swap
cat $path/etc/rc.local > /etc/rc.local
cat $path/etc/securetty > /etc/securetty

# Stop annoying services that shouldnt be running and start ones that should
chkconfig --level 2345 avahi-daemon off
service avahidaemon stop
chkconfig --level 2345 cpuspeed off
service cpuspeed stop
chkconfig --level 2345 cups off
service cups stop
chkconfig --level 2345 dhcdbd off
service dhcdbd stop
chkconfig --level 2345 dnsmasq off
chkconfig --level 2345 exim off
service exim stop
chkconfig --level 2345 lm_sensors on
chkconfig --level 2345 nmb off
service nmb stop
chkconfig --level 2345 nscd off
chkconfig --level 2345 pcscd off
chkconfig --level 2345 smolt off
chkconfig --level 2345 sshd off
service sshd stop
chkconfig --level 2345 tor on
service tor start
chkconfig --level 2345 privoxy on
service privoxy start
chkconfig --level 2345 xfs off
service xfs stop
chkconfig --level 2345 yum-updatesd on
service yum start
chkconfig --level 2345 portmap off
service portmap stop
chkconfig --level 2345 rpcgssd off
service rpcgssd stop
chkconfig --level 2345 rpcidmapd off
service rpcidmapd stop
chkconfig --level 2345 rpcsvcgssd off
service rpcsvcgssd stop
chkconfig --level 2345 nfslock off
service nfslock stop
chkconfig --level 2345 nfs off
service nfs stop
service smb stop

# Update everything
yum update

# Make a few choice files immutable
chattr +i /etc/securetty
chattr +i /etc/hosts
chattr +i /etc/shadow
chattr +i /etc/passwd
chattr +i /etc/at.allow
chattr +i /etc/cron.allow
chattr +i /etc/exports
chattr +i /etc/hosts.equiv
chattr +i /etc/services
chattr +i /etc/suauth
chattr +i /etc/inittab
chattr +i /usr/sbin/chkrootkit
chattr +i /usr/bin/chkrootkit

# Copy the binary files to a directory, tar/gzip them, scp to remote host
#mkdir /root/1/bin
#cp /bin/* /root/1/bin
#mkdir /root/1/sbin
#cp /sbin /root/1/sbin
#mkdir /root/1/usr
#mkdir /root/1/usr/bin
#cp /usr/bin /root/1/usr/bin
#mkdir /root/1/usr/sbin
#cp /usr/sbin /root/1/usr/sbin
#tar cvzf /root/apps.tgz /root/1
#rm -rf /root/1
#scp apps.tgz yankee@backupmachine:apps.tgz


http://www.cochiselinux.org/files/system-hardening-10.2.txt

leadgolem
2nd June 2007, 01:38 PM
Ok, here is the completed script.
#!/bin/sh
#You need to become root and then run this script.
#./setupfedora7
if [ ! $( id -u ) -eq 0 ]; then
echo "Please enter root password: "
exec su -c "${0} ${CMDLN_ARGS}"
exit ${?}
fi

#Remove a few unnecessary programs.
echo 'Remove a few things'
yum -y remove totem gaim compiz gnome-games

#Updates first
echo 'Starting updates, this may take a long time.'
yum -y update

#Install a boat load of tools.
echo 'Starting tools installation.'
yum -y install rpm-build cabextract numlockx kbd k3b k3b-extras p7zip p7zip-plugins transmission SDL_image perl-SDL SDL_mixer SDL_ttf SDL SDL_gfx ksensors
#Finish setting numlock to default.
echo 'Setting numlock to default in command line.'
/bin/cp ./rc.local /etc
#Install Graphics Programs
echo 'Installing graphics programs'
yum -y install blender inkscape

#Install office
echo 'Installing extra office programs'
yum -y install tellico abiword

#Install sound stuff
echo 'Installing sound tools'
yum -y install xmms xmms-libs xmms-adplug xmms-cdread xmms-flac

#Install Games
echo 'Installing Games, this may take a long time'
yum -y install freeciv gweled lincity-ng lincity-ng-data lucidlife nethack-vultures wesnoth wesnoth-tools wesnoth-server viruskiller scorched3d wormux warzone2100 warzone2100-data dosbox wine wine-core wine-esd wine-jack wine-cms wine-tools wine-ldap wine-capi wine-twain wine-nas freedroidrpg widelands

#Livna installs
echo 'Installing livna repository'
rpm -ivh http://rpm.livna.org/livna-release-7.rpm

echo 'Attempting installations requiring livna repository'
yum -y --enablerepo=livna install kmod-nvidia kmod-ntfs audacity-nonfree avidemux xmms-mp3 dvdstyler totem-xine gstreamer-plugins-ugly mplayerplug-in libdvdcss libdvdnav unrar xine-lib-extras-nonfree xine-lib-extras
#Finish setting up unrar.
echo 'Finishing unrar setup.'
ln -s /usr/bin/unrar /usr/bin/rar
echo 'Unrar setup complete'
#localstuff
echo 'Installing local packages'
yum -y --enablerepo=livna localinstall dvd95-1.2p0-fc6.i386.rpm gnormalize-0.52-1.noarch.rpm XnView-static-fc4.i386.rpm yafray-0.0.9-2-exr.i386.rpm k3b-extras-nonfree
echo 'Local packages installed'

#Install Desktop Entries
echo 'Installing Menu Items'
cp ./desktops/*.desktop /usr/share/applications
echo 'Menu items installed'
#fstab
echo 'Configuring fstab'
mkdir /mnt/storagefat32
mkdir /mnt/storage2fat32
mkdir /mnt/storage3fat32
mkdir /mnt/windows
chmod -R a=rwx /mnt
/bin/cp /etc/fstab /etc/fstab.bak
/bin/cp fstab /etc
echo 'fstab configuration complete'
echo 'Enabling additional hardrives'
mount -a
echo 'Hardrives enabled'
#Restore Home Directory
echo 'Restoring home directory, this will take an extremely long time.'
tar -jxpvf /mnt/storage2fat32/$USERNAME.tar.bz2 /home
echo 'Main Configuration files restored'
echo 'Restoring Blender meshes'
tar -jxpvf /mnt/storagefat32/blender.tar.bz2 /home/$USERNAME
echo 'Blender meshes restored'
echo 'Restoring Dosbox files'
tar -jxpvf /mnt/storagefat32/dos.tar.bz2 /home/$USERNAME
echo 'Dosbox files restored'
echo 'Restoring Icons folder'
tar -jxpvf /mnt/storagefat32/icons.tar.bz2 /home/$USERNAME
echo 'Icons folder restored'
echo 'Restoring Rockbox files'
tar -jxpvf /mnt/storagefat32/rockbox.tar.bz2 /home/$USERNAME
echo 'Rockbox files restored'
echo 'Restoring Dvdstyler templates'
tar -jxpvf /mnt/storagefat32/dvdstylertemplates.tar.bz2 /home/$USERNAME
echo 'Dvdstyler templates restored'
echo 'Restoring Never Winter Nights'
tar -jxpvf /mnt/storage2fat32/nwn.tar.bz2 /home/$USERNAME
echo 'Never Winter Nights restored'
echo 'Restoring script launchers hidden folder'
tar -jxpvf /mnt/storagefat32/scriptlaunchers.tar.bz2 /home/$USERNAME
echo 'Script launchers hidden folder restored'
echo 'Restoring wine program files'
tar -jxpvf /mnt/storage2fat32/wineprogramfiles.tar.bz2 /home/$USERNAME/.wine/drive_c
echo 'Wine program files restored'

#Fix file ownership
echo 'Transfering file ownership'
chown -R $USERNAME /home/$USERNAME
echo 'File ownership transfered'
#Install swiftfox
echo 'Installing swiftfox'
./install-swiftfox.sh
#Replace swiftfox icons
echo 'Replacing Swiftfox Icons'
/bin/cp ./graphics/*.* /opt/swiftfox/icons
echo 'Swiftfox icons replaced'

#Configure services
echo 'Configuring services'
./services

#Restart the system
echo 'Restarting Computer'
reboot
exit

Here is the services script.
#!/bin/sh
# Stop annoying services that shouldnt be running
chkconfig --level 2345 atd off
chkconfig --level 2345 apmd off
chkconfig --level 2345 ahavi-daemon off
chkconfig --level 2345 avavi-dnsconfd off
chkconfig --level 2345 bluetooh off
chkconfig --level 2345 btseed off
chkconfig --level 2345 bttrack off
chkconfig --level 2345 cpuspeed off
chkconfig --level 2345 cups off
chkconfig --level 2345 cups-config-daemon off
chkconfig --level 2345 dc_client off
chkconfig --level 2345 dc_server off
chkconfig --level 2345 diskdump off
chkconfig --level 2345 firstboot off
chkconfig --level 2345 gpm off
chkconfig --level 2345 hplip off
chkconfig --level 2345 httpd off
chkconfig --level 2345 isdn off
chkconfig --level 2345 kudzu off
chkconfig --level 2345 lirc off
chkconfig --level 2345 mdnsresponder off
chkconfig --level 2345 mysqld off
chkconfig --level 2345 named off
chkconfig --level 2345 netdump off
chkconfig --level 2345 netplugd off
chkconfig --level 2345 nifd off
chkconfig --level 2345 nscd off
chkconfig --level 2345 ntpd off
chkconfig --level 2345 nfs off
chkconfig --level 2345 nfslock off
chkconfig --level 2345 portmap off
chkconfig --level 2345 pcmcia off
chkconfig --level 2345 rpcgssd off
chkconfig --level 2345 rpcidmapd off
chkconfig --level 2345 rpcsvcgssd off
chkconfig --level 2345 saslauthd off
chkconfig --level 2345 sendmail off
chkconfig --level 2345 smb off
chkconfig --level 2345 snmpd off
chkconfig --level 2345 snmptrapd off
chkconfig --level 2345 sshd off
chkconfig --level 2345 wpa_supplicant off
chkconfig --level 2345 spamassasin off
chkconfig --level 2345 yum-updatesd off
echo 'Services configured'
exit
Here is the modified version of the swiftfox installer script.
#!/bin/sh
#
# This script installs Swiftfox

# Download Swiftfox
if [ ! $( id -u ) -eq 0 ]; then
echo "Please enter root password: "
exec su -c "${0} ${CMDLN_ARGS}"
exit ${?}
fi
echo " "

wget http://getswiftfox.com/builds/releases/2.0.0.3/swiftfox-2.0.0.3-athlon-xp.tar.bz2

# Install Swiftfox
echo " "
echo "Installing Swiftfox..."

# If there is an existing swiftfox install here let's move it out of the way
/bin/mv /opt/swiftfox /opt/swiftfox.old

# Extract the build
tar -jxf swiftfox-2.0.0.3-athlon-xp.tar.bz2 /opt

# Search for plugins
echo " "
echo "Searching for existing plugins, errors can be safely ignored..."
/bin/cp /opt/swiftfox.old/plugins/* /opt/swiftfox/plugins
/bin/cp /usr/lib/firefox/plugins/* /opt/swiftfox/plugins
/bin/cp /usr/lib/mozilla/plugins/* /opt/swiftfox/plugins
/bin/cp /usr/lib/mozilla-firefox/plugins/* /opt/swiftfox/plugins
/bin/cp /usr/lib/browser-plugins/* /opt/swiftfox/plugins

# Make sure root owns the Swiftfox directory
chown -hR root:root /opt/swiftfox

# Add link in /usr/bin
ln -s /opt/swiftfox/swiftfox /usr/bin/swiftfox

# Add Swiftfox to menu
wget http://getswiftfox.com/builds/installer/Swiftfox.desktop
/bin/mv Swiftfox.desktop /usr/share/applications

# Remove the downloaded tarball
/bin/rm swiftfox-2.0.0.3-athlon-xp.tar.bz2

echo " "
echo "Swiftfox 2.0.0.3 has been installed. Happy Surfing!"
echo " "
exit

Questions, comments, errors, let me know.

The_Jaymz
2nd June 2007, 05:13 PM
I saw that you're installing the nonfree plugins for k3b pretty early in your script. Are they available in the normal repo? If you have to get them from livna, you'll need to install livna first.

try this
rpm -ivh http://rpm.livna.org/fedora/7/i386/livna-release-7-2.noarch.rpm

leadgolem
2nd June 2007, 06:03 PM
You are correct. I'll move it to livna installs.

leadgolem
2nd June 2007, 10:18 PM
Don't use this as the basis for any of your personal scripts, it doesn't work. The swiftfox script is also broken. Specifying a path after the tar command tells it what path inside the compressed file to extract. Not where to extract it.

wdgiles
2nd June 2007, 10:50 PM
I didn't plan to copy it verbatim, but it did provide some ideas I had not included in mine. Thanks for the help.

leadgolem
3rd June 2007, 02:38 PM
Sure, you may want to add a line for yum to clean itself after each main install/update. Apparently yum no longer automatically does this.