Tuesday, July 19, 2011

new script: getlatestinstall

I play alot around with kvm, thus i also need quite often the install-cds for gentoo. Unfortunately i never use the latest install-cds because i'm to lazy to download the media first. It's not a big problem, but i wanted to have a nice solution to have always the latest install-cds. Now i wrote a small script which does the downloads for me.

The Features are:
  • check if the passed directories exist and if they are writeable
  • download the latest iso images from gentoo.distfiles.com + checksum files
  • check the md5sum of the iso-image, if failed delete the downloaded files, and try again (3 times)
  • if passed, create a link to the iso-image
  • works for amd64,x86,alpha and ia64
I think it's quite useful and i put it into my cronjobs to run it every sunday. Maybe someone else can use it too.

There is also a small discription included if you run the script without arguments. Don't forget to change the ${DOWNLOADDIR} and ${LINKSDIR} vars. Suggestions, bugs and improvements are welcome ;)

Download: Link

#!/bin/sh
# Datei: getlatestinstall
# Autor: Michael Mair-Keimberger (m DOT mairkeimberger AT gmail DOT com)

# Copyright (C) 2011  Michael Mair-Keimberger
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# Discription:
# A script which automaticly download the latest
# gentoo install cds for x86,amd64,alpha,ia64
# checks the md5sum and make symbolic links to the iso

# programs to use
MD5SUM=/usr/bin/md5sum
WGET=/usr/bin/wget
LN=/bin/ln
RM=/bin/rm
CAT=/bin/cat
TAIL=/bin/tail
MKDIR=/bin/mkdir
PING=/bin/ping

# make sure both directory are writeable for the user
# who execute this script
LINKSDIR="/home/${USER}"
DOWNLOADDIR="/home/${USER}"

usage(){
  echo "$0 arch (downloaddir) (linksdir)"
  echo "arch  > select between amd64,x86,alpha or ia64"
  echo "downloaddir > directory where the iso will be stored (optional)"
  echo "linksdir > directory where the links will be stored (optional)"
  echo "A script to download the latest minimal cd for gentoo"
  exit 1
}

cfgchecks(){
  # check if the directories exists and if there are writeable
  for checkdir in ${DOWNLOADDIR} ${LINKSDIR}; do
    if [ -d "${checkdir}" ]; then
      if ! [ -w "${checkdir}" ]; then
 echo "Directory \"${checkdir}\" is not writeable!"
 return 1
      fi
    else
      echo "Directory \"${checkdir}\" dosen't exists"
      return 1
    fi
  done

  # create arch dirctory if it dosen't exists
  if ! [ -d "${DOWNLOADDIR}/gentoo-${1}/" ]; then
    ${MKDIR} ${DOWNLOADDIR}/gentoo-${1}/ > /dev/null 2>&1
  fi

  # check if there is internet connection
  if ! (${PING} -c1 -q distfiles.gentoo.org > /dev/null 2>&1); then 
    echo "No internet connection"
    return 1
  fi
}

md5sumcheck(){
  cd ${DOWNLOADDIR}/gentoo-${1}/

  if [ `${MD5SUM} -c --status ${DOWNLOADDIR}/gentoo-${1}/*.DIGESTS` ]; then
    ${RM} ${DOWNLOADDIR}/gentoo-${1}/*
    echo "md5sum check failed"
    return 1
  else
    # -f because if a link already exists it has to be forced
    ${LN} -s -f ${DOWNLOADDIR}/gentoo-${1}/*.iso ${LINKSDIR}/gentoo_${1}
  fi
}

download(){
  # download the txt file which points to the latest minimal cd
  ${WGET} -q -P/tmp/ http://distfiles.gentoo.org/releases/${1}/autobuilds/latest-install-${1}-minimal.txt
  LATEST_ISO=`${CAT} /tmp/latest-install-${1}-minimal.txt | ${TAIL} -n 1`
  ${RM} /tmp/latest-install-${1}-minimal.txt > /dev/null 2>&1
  ${RM} ${DOWNLOADDIR}/gentoo-${1}/* > /dev/null 2>&1

  # download the latest iso with the checksum files
  ${WGET} -q -P${DOWNLOADDIR}/gentoo-${1}/ http://distfiles.gentoo.org/releases/${1}/autobuilds/${LATEST_ISO} \
    http://distfiles.gentoo.org/releases/${1}/autobuilds/${LATEST_ISO}.CONTENTS \
    http://distfiles.gentoo.org/releases/${1}/autobuilds/${LATEST_ISO}.DIGESTS \
    http://distfiles.gentoo.org/releases/${1}/autobuilds/${LATEST_ISO}.DIGESTS.asc
}

main() {
  if cfgchecks $1; then
    for retry in {1..3}; do
      download $1
      if md5sumcheck $1; then
 break;
      fi
    done
  fi
}


if [ $# -lt 1 ]; then
  usage;
else
  # change DOWNLOADDIR and LINKSDIR to user settings
  if [ "${2}" ]; then
    DOWNLOADDIR=${2}
  fi

  if [ "${3}" ]; then
    LINKSDIR=${3}
  fi
  main $1
fi

No comments:

Post a Comment