Script could be helpful to those that are using snapshots to update their NetBSD systems. Script originally used 'coprocess' feature of ksh, but then the NetBSD folks changed the releng server from ftp to http. As I'm just too lazy to convert it to Bourne sh script, is still in ksh.
Anyway, I expect opinions and recomendations to improve the script.
Code:
#!/bin/ksh
# getsets.ksh v.0.2
# dalibor Dot gudzic At gmail Dot com
# www.bsdserbia.org
# 11/2009
#
DWNLD_DIR="/home/soxxx/netbsd_sets"
FTP_SITE="http://nyftp.netbsd.org"
FTP_OPTIONS="/usr/bin/ftp -o -"
FTP_RELENG="pub/NetBSD-daily"
TAG="netbsd-5"
VERSION="$(awk -F\' '/BUILDID/ { print $2 }' /etc/release)"
set -A SETS "base.tgz comp.tgz etc.tgz kern-GENERIC.tgz man.tgz misc.tgz tests.tgz \
text.tgz xbase.tgz xcomp.tgz xetc.tgz xfont.tgz xserver.tgz"
f_using() {
cat <<EOF
Script execution: ${0##*/} <getsets | installkernel | installsets | etcupdate | help>
getsets - Downloading of sets from ftp server
installkernel - Extract and install GENERIC kernel
installsets - Installing of sets
etcupdate - Update, merge of files in /etc dirctory
help - Show help on using script
Apart from downloading of sets, to correctly execute the script start it as:
sudo ${0##*/} parameter
EOF
}
f_error_check() {
err=$?
print ""
test "err" -eq "$1" && print "$2" && exit $1
}
if [[ $# -ne 1 ]]
then
print "Error: (only) one parameter needed for execution of script."
f_using
exit 1
fi
case $1 in
getsets)
NEW_VERSION=$(ftp -o - $FTP_SITE/$FTP_RELENG/$TAG/ | \
awk '
{ if ( match ($0, "[0-9]+Z")) {
print substr($0, RSTART, RLENGTH)
}
}' | sort -r | head -1)
if [[ $NEW_VERSION == $VERSION ]]; then
print "Yo, no new sets for update, ending the script."
exit 0
else
VERSION=$NEW_VERSION
print "New version is: $VERSION"
FTP_PATH="$FTP_RELENG/$TAG/$VERSION/i386/binary/sets"
print -n "Checking for presence of 'i386' directory ..."
$FTP_OPTIONS $FTP_SITE/$FTP_RELENG/$TAG/$VERSION | \
awk '/i386/ {yeah_baby=1}
END{ if (yeah_baby) {
print " present at the class, continue."
} else {
print " not present. Unexcused absence."
exit 33;
}
}'
f_error_check 33 "Aborting script."
print "Checking the entries ..."
$FTP_OPTIONS $FTP_SITE/$FTP_PATH/ | \
awk -v S="$SETS" '
BEGIN {
split(S, sets_arr)
}
{
for (i in sets_arr) {
if ($0 ~ sets_arr[i]) {
found_arr[sets_arr[i]]++
}
}
}
END {
for (j in sets_arr) {
if (sets_arr[j] in found_arr) {
print "Set " sets_arr[j] " is on the server."
} else {
print "Set " sets_arr[j] " was not found on the server."
n++
}
} if(n>0)
exit 44;
}'
f_error_check 44 "Some sets were not found. Aborting script."
if [[ $(pwd) != $DWNLD_DIR ]]; then
print "Moving to $DWNLD_DIR"
cd $DWNLD_DIR
f_error_check 1 "Error when moving to $DWNLD_DIR."
fi
for SET in ${SETS[@]}; do
print "Downloading set ==> $SET..."
ftp $FTP_SITE/$FTP_PATH/$SET
f_error_check 1 "Error while downloading set $SET."
done
fi
;;
installkernel)
if [ -f $DWNLD_DIR/kern-GENERIC.tgz ]; then
print "We backup old kernel as netbsd.old ..."
/bin/mv /netbsd /netbsd.old
f_error_check 1 "Error while backuping the kernel."
print "Extracting GENERIC kernel to / ..."
/usr/bin/progress -zf $DWNLD_DIR/kern-GENERIC.tgz /bin/tar -C / -xpf -
f_error_check 1 "Error while extracting the kernel."
print ""
print -n "New kernel extracted, reboot to boot the new kernel ( yes/no ): "
while read ANSWER; do
case $ANSWER in
[Yy]|[Yy][Ee][Ss])
sync; sync; shutdown -r now ;;
[Nn]|[Nn][Oo])
print "New kernel will boot on next reboot."
exit ;;
*)
print "Answer with [y]es or [n]o"
continue ;;
esac
done
else
print "kern-GENERIC doesn’t exist" && exit 1
fi
;;
installsets)
print "Checking the validity of archives:"; print ""
for SET in ${SETS[@]}; do
[[ $SET == "kern-GENERIC.tgz" ]] && continue
print -n "Testing archive ==> $SET ..."
/bin/tar -tzvf $DWNLD_DIR/$SET >/dev/null 2>&1 && print " this one is fine."
f_error_check 1 " damn, something smells bad in set $SET. Aborting the script"
done
print
print "All archives (hopefully) valid. :-) We continue ..."
for SET in ${SETS[@]}; do
[[ ($SET == "etc.tgz") || ($SET == "xetc.tgz") || ($SET == "kern-GENERIC.tgz") ]] && continue
print "Extracting set ==> $SET..."
/usr/bin/progress -zf $DWNLD_DIR/$SET /rescue/tar -C / -xpf -
f_error_check 1 "Error while extracting set $SET."
done
;;
etcupdate)
/usr/sbin/etcupdate -s $DWNLD_DIR/etc.tgz -s $DWNLD_DIR/xetc.tgz
;;
help)
f_using
;;
*)
print ""
print "Error: unknown parameter $1"
print ""
f_using
exit 1
;;
esac