Hi all. Because it is school holidays in New Zealand I decided to create a script that would allow a user to upgrade his openSUSE from an old version to a new version. e.g. from 11.2 to 11.3. It does all the work for you. e.g. disabling old repos, adding the new repos and finally upgrading with a bit of error checking in the script. I tried it on a virtual machine and it successfully upgraded openSUSE 11.2 to openSUSE 11.3
Usage:
for example to upgrade openSUSE to 11.3:
./suseupdater 11.3
And it will update openSUSE to 11.3
Here is source code:
#!/bin/bash
#SUSEUpdater by Andrew Hill.
updaterepo="http://download.opensuse.org"
if "$1" == "" ]; then
echo "ERROR: You have to specify the version you want to upgrade to"
exit 1
fi
oldrelease=`cat /etc/SuSE-release | grep VERSION | awk '{print $3}'`
if "$1" == "$oldrelease" ]; then
echo "ERROR: You are trying to upgrade to the version already installed"
exit 2
fi
getuser=`echo $UID`
if "$getuser" != "0" ]; then
echo "ERROR: You are not root. Please rerun the updater as root"
exit 3
fi
echo -n "Welcome to the SUSE updater. Do you want to upgrade openSUSE $oldrelease to openSUSE $1? (y/n): "
read userinput1
if "$userinput1" == "n" ]; then
exit 0
fi
repourl[1]="/distribution/$1/repo/oss/"
repourl[2]="/distribution/$1/repo/non-oss/"
repourl[3]="/update/$1/"
repoalias[1]="repo-$1-oss"
repoalias[2]="repo-$1-non-oss"
repoalias[3]="repo-$1-update"
reponame[1]="openSUSE-$1 OSS"
reponame[2]="openSUSE-$1 Non-OSS"
reponame[3]="openSUSE-$1 Updates"
repoerror="0"
echo "Making sure the repo's exist. Please wait..."
for i in "${repourl@]}"
do
wget -q --spider "$updaterepo""$i"
if "$?" != 0 ]; then
echo "ERROR: Repo ""$updaterepo""$i"" does not exist"
repoerror="1"
fi
done
if "$repoerror" == "1" ]; then
echo "ERROR: One or more of the repo's do not exist. Exiting. Nothing Changed"
exit 4
fi
echo "DONE"
echo "Disabling all previous repos"
zypper mr --all --disable
echo "DONE"
echo "Adding the new repos"
repocount=`printf "%s
" "${repourl@]}" | wc -l`
for i in `seq 1 $repocount`
do
zypper ar --name "${reponame$i]}" "$updaterepo""${repourl$i]}" "${repoalias$i]}"
zypper mr -r "${repoalias$i]}"
done
echo "DONE"
echo "Refreshing repos"
zypper --no-gpg-checks --non-interactive ref
echo "DONE"
echo "Doing distribution upgrade"
zypper --non-interactive dup -l
echo "DONE"
echo "Please reboot to finish update"
This script took me two hours to make. Enjoy and please feel free to suggest any improvements to the script rotfl!
Sorry no commenting in script. I will get around to that soon
#!/bin/bash
#SUSEUpdater by Andrew Hill.
#Date created: 29/10/10
#Version: 0.1
#Description: upgrade to a new version of openSUSE easily without having to manually modify repo's etc
#Options: Version of openSUSE to upgrade to $1
#The repo to download the updated openSUSE from
updaterepo="http://download.opensuse.org"
#Check to see if user has specified version to upgrade to
if "$1" == "" ]; then
#They haven't specified version
echo "ERROR: You have to specify the version you want to upgrade to"
exit 1
fi
#Get the current release installed
oldrelease=`cat /etc/SuSE-release | grep VERSION | awk '{print $3}'`
#Check to see if its the same version already installed
if "$1" == "$oldrelease" ]; then
#Same version is already installed
echo "ERROR: You are trying to upgrade to the version already installed"
exit 2
fi
#Get UID of user
getuser=`echo $UID`
#Make sure that user is root
if "$getuser" != "0" ]; then
#They are not root
echo "ERROR: You are not root. Please rerun the updater as root"
exit 3
fi
#Welcome the user and make sure they want to continue
echo -n "Welcome to the SUSE updater. Do you want to upgrade openSUSE $oldrelease to openSUSE $1? (y/n): "
#Get user input
read userinput1
if "$userinput1" == "n" ]; then
#They don't wan't to continue
exit 0
fi
#Arrays to define repo name and url etc. You can add extra repos if you want as long if its on the updaterepo server
repourl[1]="/distribution/$1/repo/oss/"
repourl[2]="/distribution/$1/repo/non-oss/"
repourl[3]="/update/$1/"
repoalias[1]="repo-$1-oss"
repoalias[2]="repo-$1-non-oss"
repoalias[3]="repo-$1-update"
reponame[1]="openSUSE-$1 OSS"
reponame[2]="openSUSE-$1 Non-OSS"
reponame[3]="openSUSE-$1 Updates"
repoerror="0"
#Make sure the repos exist
echo "Making sure the repo's exist. Please wait..."
for i in "${repourl@]}"
do
#Check repo exist with wget
wget -q --spider "$updaterepo""$i"
if "$?" != 0 ]; then
#Repo does not exist
echo "ERROR: Repo ""$updaterepo""$i"" does not exist"
repoerror="1"
fi
done
if "$repoerror" == "1" ]; then
#Exit because one or repos do not exist
echo "ERROR: One or more of the repo's do not exist. Exiting. Nothing Changed"
exit 4
fi
echo "DONE"
echo "Disabling all previous repos"
#Disable all previous repos
zypper mr --all --disable
echo "DONE"
echo "Adding the new repos"
#Add the new repos for new version of openSUSE
repocount=`printf "%s
" "${repourl@]}" | wc -l`
for i in `seq 1 $repocount`
do
zypper ar --name "${reponame$i]}" "$updaterepo""${repourl$i]}" "${repoalias$i]}"
zypper mr -r "${repoalias$i]}"
done
echo "DONE"
echo "Refreshing repos"
#Refresh the repos
zypper --no-gpg-checks --non-interactive ref
echo "DONE"
echo "Doing distribution upgrade"
#Finally to the upgrade
zypper --non-interactive dup -l
echo "DONE"
echo "Please reboot to finish update"
Also I forgot to add this - save this script as suseupdater. Then do:
chmod +x suseupdater
Then you can run it. e.g. upgrade to openSUSE 11.3:
Just a remark that will not influence the outcome of your script.
#Get UID of user
getuser=`echo $UID`
There is no need to echo what you allready have
getuser=$UID
gives the same result without the (I admit minimal) overhead of calling echo.
But the whole statement is not needed. Why don’t you use* $UID* instead of *$getuser *in the code that follows? The variable UID is allready there to be used.
BTW, instead of the error you could try to play a bit with something like:
Ok I have made modifications with Henk suggestions. Here is the new one:
#!/bin/bash
#SUSEUpdater by Andrew Hill.
#Date created: 29/10/10
#Version: 0.3
#Description: upgrade to a new version of openSUSE easily without having to manually modify repo's etc
#Options: Version of openSUSE to upgrade to $1
#The repo to download the updated openSUSE from
updaterepo="http://download.opensuse.org"
#Check to see if user has specified version to upgrade to
if "$1" == "" ]; then
#They haven't specified version
echo "ERROR: You have to specify the version you want to upgrade to"
exit 1
fi
#Get the current release installed
oldrelease=`cat /etc/SuSE-release | grep VERSION | awk '{print $3}'`
#Check to see if its the same version already installed
if "$1" == "$oldrelease" ]; then
#Same version is already installed
echo "ERROR: You are trying to upgrade to the version already installed"
exit 2
fi
#Make sure that user is root
if "$UID" != "0" ]; then
#They are not root
echo "ERROR: You are not root. Please enter your root password to continue"
su -c $0 $1
fi
#Welcome the user and make sure they want to continue
echo -n "Welcome to the SUSE updater. Do you want to upgrade openSUSE $oldrelease to openSUSE $1? (y/n): "
#Get user input
read userinput1
if "$userinput1" == "n" ]; then
#They don't wan't to continue
exit 0
fi
#Arrays to define repo name and url etc. You can add extra repos if you want as long if its on the updaterepo server
repourl[1]="/distribution/$1/repo/oss/"
repourl[2]="/distribution/$1/repo/non-oss/"
repourl[3]="/update/$1/"
repoalias[1]="repo-$1-oss"
repoalias[2]="repo-$1-non-oss"
repoalias[3]="repo-$1-update"
reponame[1]="openSUSE-$1 OSS"
reponame[2]="openSUSE-$1 Non-OSS"
reponame[3]="openSUSE-$1 Updates"
repoerror="0"
#Make sure the repos exist
echo "Making sure the repo's exist. Please wait..."
for i in "${repourl@]}"
do
#Check repo exist with wget
wget -q --spider "$updaterepo""$i"
if "$?" != 0 ]; then
#Repo does not exist
echo "ERROR: Repo ""$updaterepo""$i"" does not exist"
repoerror="1"
fi
done
if "$repoerror" == "1" ]; then
#Exit because one or repos do not exist
echo "ERROR: One or more of the repo's do not exist. Exiting. Nothing Changed"
exit 4
fi
echo "DONE"
echo "Disabling all previous repos"
#Disable all previous repos
zypper mr --all --disable
echo "DONE"
echo "Adding the new repos"
#Add the new repos for new version of openSUSE
repocount=`printf "%s
" "${repourl@]}" | wc -l`
for i in `seq 1 $repocount`
do
zypper ar --name "${reponame$i]}" "$updaterepo""${repourl$i]}" "${repoalias$i]}"
zypper mr -r "${repoalias$i]}"
done
echo "DONE"
echo "Refreshing repos"
#Refresh the repos
zypper --no-gpg-checks --non-interactive ref
echo "DONE"
echo "Doing distribution upgrade"
#Finally to the upgrade
zypper --non-interactive dup -l
echo "DONE"
echo "Please reboot to finish update"
The whole seems to be a root action. Thus you should NOT stay in the current directory where some end-user happens to be when (s)he calls your script. Also other left-overs (think of aliases) can be a security problem. You should
. use* su - *for several security reasons;
. check in your script either where you are, or better, cd (with an absolute path) to the directory where you want to be, alternative: do not use the current directory explicit or implicit, but use only absolute pathes in all your statements (but are you sure the tools you call are working in the same secure way).
Writining a script that you propagate to other users (yourself amongst them) that does not create havoc in circumstances you may not preview is not easy. One should take a lot of precautions even before one adds the statement: “This script comes without warranty so if it stuffs up your computer then its not my fault” (which is a sensible thing to do after one reads and rereads ones script thouroughly say a week after the first time one thinks it is ready).
OK I have changed script to use “su -” to go into root. It will get the working directory plus the name of the script.
Here is version 0.4
#!/bin/bash
#SUSEUpdater by Andrew Hill.
#Date created: 29/10/10
#Version: 0.4
#Description: upgrade to a new version of openSUSE easily without having to manually modify repo's etc
#Options: Version of openSUSE to upgrade to $1
#The repo to download the updated openSUSE from
updaterepo="http://download.opensuse.org"
#Check to see if user has specified version to upgrade to
if "$1" == "" ]; then
#They haven't specified version
echo "ERROR: You have to specify the version you want to upgrade to"
exit 1
fi
#Get the current release installed
oldrelease=`cat /etc/SuSE-release | grep VERSION | awk '{print $3}'`
#Check to see if its the same version already installed
if "$1" == "$oldrelease" ]; then
#Same version is already installed
echo "ERROR: You are trying to upgrade to the version already installed"
exit 2
fi
#Make sure that user is root
if "$UID" != "0" ]; then
#They are not root
echo "ERROR: You are not root. Please enter your root password to continue"
path=`pwd`/${0##*/}
su - -c $path $1
fi
#Welcome the user and make sure they want to continue
echo -n "Welcome to the SUSE updater. Do you want to upgrade openSUSE $oldrelease to openSUSE $1? (y/n): "
#Get user input
read userinput1
if "$userinput1" == "n" ]; then
#They don't wan't to continue
exit 0
fi
#Arrays to define repo name and url etc. You can add extra repos if you want as long if its on the updaterepo server
repourl[1]="/distribution/$1/repo/oss/"
repourl[2]="/distribution/$1/repo/non-oss/"
repourl[3]="/update/$1/"
repoalias[1]="repo-$1-oss"
repoalias[2]="repo-$1-non-oss"
repoalias[3]="repo-$1-update"
reponame[1]="openSUSE-$1 OSS"
reponame[2]="openSUSE-$1 Non-OSS"
reponame[3]="openSUSE-$1 Updates"
repoerror="0"
#Make sure the repos exist
echo "Making sure the repo's exist. Please wait..."
for i in "${repourl@]}"
do
#Check repo exist with wget
wget -q --spider "$updaterepo""$i"
if "$?" != 0 ]; then
#Repo does not exist
echo "ERROR: Repo ""$updaterepo""$i"" does not exist"
repoerror="1"
fi
done
if "$repoerror" == "1" ]; then
#Exit because one or repos do not exist
echo "ERROR: One or more of the repo's do not exist. Exiting. Nothing Changed"
exit 4
fi
echo "DONE"
echo "Disabling all previous repos"
#Disable all previous repos
zypper mr --all --disable
echo "DONE"
echo "Adding the new repos"
#Add the new repos for new version of openSUSE
repocount=`printf "%s
" "${repourl@]}" | wc -l`
for i in `seq 1 $repocount`
do
zypper ar --name "${reponame$i]}" "$updaterepo""${repourl$i]}" "${repoalias$i]}"
zypper mr -r "${repoalias$i]}"
done
echo "DONE"
echo "Refreshing repos"
#Refresh the repos
zypper --no-gpg-checks --non-interactive ref
echo "DONE"
echo "Doing distribution upgrade"
#Finally to the upgrade
zypper --non-interactive dup -l
echo "DONE"
echo "Please reboot to finish update"
#!/bin/bash
#SUSEUpdater by Andrew Hill.
#Date created: 29/10/10
#Version: 0.5
#Description: upgrade to a new version of openSUSE easily without having to manually modify repo's etc
#Options: Version of openSUSE to upgrade to $1
#The repo to download the updated openSUSE from
updaterepo="http://download.opensuse.org"
#Check to see if user has specified version to upgrade to
if "$1" == "" ]; then
#They haven't specified version
echo "ERROR: You have to specify the version you want to upgrade to"
exit 1
fi
#Get the current release installed
oldrelease=`cat /etc/SuSE-release | grep VERSION | awk '{print $3}'`
#Check to see if its the same version already installed
if "$1" == "$oldrelease" ]; then
#Same version is already installed
echo "ERROR: You are trying to upgrade to the version already installed"
exit 2
fi
#Make sure that user is root
if "$UID" != "0" ]; then
#They are not root
echo "ERROR: You are not root. Please enter your root password to continue"
path=`pwd`/${0##*/}
su root - -c "$path $1"
exit
fi
#Welcome the user and make sure they want to continue
echo -n "Welcome to the SUSE updater. Do you want to upgrade openSUSE $oldrelease to openSUSE $1? (y/n): "
#Get user input
read userinput1
if "$userinput1" == "n" ]; then
#They don't wan't to continue
exit 0
fi
#Arrays to define repo name and url etc. You can add extra repos if you want as long if its on the updaterepo server
repourl[1]="/distribution/$1/repo/oss/"
repourl[2]="/distribution/$1/repo/non-oss/"
repourl[3]="/update/$1/"
repoalias[1]="repo-$1-oss"
repoalias[2]="repo-$1-non-oss"
repoalias[3]="repo-$1-update"
reponame[1]="openSUSE-$1 OSS"
reponame[2]="openSUSE-$1 Non-OSS"
reponame[3]="openSUSE-$1 Updates"
repoerror="0"
#Make sure the repos exist
echo "Making sure the repo's exist. Please wait..."
for i in "${repourl@]}"
do
#Check repo exist with wget
wget -q --spider "$updaterepo""$i"
if "$?" != 0 ]; then
#Repo does not exist
echo "ERROR: Repo ""$updaterepo""$i"" does not exist"
repoerror="1"
fi
done
if "$repoerror" == "1" ]; then
#Exit because one or repos do not exist
echo "ERROR: One or more of the repo's do not exist. Exiting. Nothing Changed"
exit 4
fi
echo "DONE"
echo "Disabling all previous repos"
#Disable all previous repos
zypper mr --all --disable
echo "DONE"
echo "Adding the new repos"
#Add the new repos for new version of openSUSE
repocount=`printf "%s
" "${repourl@]}" | wc -l`
for i in `seq 1 $repocount`
do
zypper ar --name "${reponame$i]}" "$updaterepo""${repourl$i]}" "${repoalias$i]}"
zypper mr -r "${repoalias$i]}"
done
echo "DONE"
echo "Refreshing repos"
#Refresh the repos
zypper --no-gpg-checks --non-interactive ref
echo "DONE"
echo "Doing distribution upgrade"
#Finally to the upgrade
zypper --non-interactive dup -l
echo "DONE"
echo "Please reboot to finish update"