#! /bin/sh # Script to grab a given kernel version (stolen from Virtual Anton). # Posted by Rusty Russell. # Updates by Rusty Russell and Randy.Dunlap. # Update 2006-10-26: # when an -mm 2.6.n-mmX patch is downloaded, # it is renamed to patch-2.6.n-mmX, # and then that file is applied as a patch. # 2008-05-15: add -next kernel patches; barf() { echo "$@" >&2 exit 1 } pr_exists() { echo "$@ already exists" >&2 exit 0 } make_exec() { wd=`/bin/pwd` ###echo "INFO: CWD=$wd" dir=$1 ###echo "INFO: make_exec: dir=$dir" chmod +x $dir/scripts/*.sh $dir/scripts/*.pl } # Takes destination directory, and filename, tries each mirror. get_file() { get_file_dir=$1 get_file_name=$2 cwd=`/bin/pwd` ###echo "INFO: get_file: dir=$1; name=$2; CWD=$cwd" >&2 # Patch there already? if [ -f "$get_file_dir"/"$(basename "$get_file_name")" ]; then echo " Already have patch: [$get_file_dir/$(basename "$get_file_name")]" >&2 return 0 fi for get_file_server in $MIRRORS; do echo Trying to get file from: $get_file_server... case $get_file_server in rsync://*) get_file_s=`echo $get_file_server | cut -c9-`; rsync $get_file_s/$get_file_name $get_file_dir && return 0 ;; http://*) get_file_s="$get_file_server"; ###echo "INFO: cd to: get_file_dir=$get_file_dir" cd $get_file_dir ###echo "INFO: cd back to: $cwd" curl -O $get_file_s/$get_file_name && cd $cwd && return 0 # Leaves a zero-len file on failure 8( ###echo "INFO: cd back to: $cwd" cd $cwd rm -f $get_file_dir/`basename $get_file_name` ;; local:*) get_file_s=`echo $get_file_server | cut -c9-`; scp -p $get_file_server/$get_file_name $get_file_dir && return 0 ;; esac done return 1 } # Given vanity kernel name, major version, & previous/base version, # produce pathname vanity_source() { ###echo "INFO: vanity_source: series:$1; major:$2; base:$3;" >&2 case "$1" in *-ac*) echo people/alan/linux-$2/$3/patch-$1.bz2;; *-dj*) echo people/davej/patches/$2/$3/patch-$1.bz2;; *-mm*) echo people/akpm/patches/$2/$3/$1/$1.bz2;; *next-*) echo people/sfr/linux-next/patch-$3-next-$1.bz2;; *) barf "Unknown kernel series $1";; esac } # Given base name, new name and patch, do patch application. apply_patch() { base=$1 ver=$2 pfile=$3 ###echo "INFO: apply_patch: level: $level, base: $base; ver: $ver; pfile: $pfile" >&2 cwd=`/bin/pwd` ###echo "INFO: apply_patch: KDIR=$KDIR, CWD=$cwd" rm -rf $KDIR/.linux-$ver cp -al $KDIR/linux-$base $KDIR/.linux-$ver || barf "Couldn't clone $base dir" bunzip2 -t $pfile || barf "Corrupt patch $pfile" bzcat $pfile | patch -bs -p1 -d $KDIR/.linux-$ver || barf "Problem patching new kernel $ver" mv $KDIR/.linux-$ver $KDIR/linux-$ver ###echo "INFO: made new $KDIR/linux-$ver" >&2 if [ $level -eq 0 ]; then echo "rename /linux-$VERSION/ as needed....." >&2 fi ###echo "INFO: ready to rm $KDIR/linux-$base" >&2 rm -rf $KDIR/linux-$base } # main: # keep up with recursion level level=0 if [ -r /etc/crucible.cfg ] && [ "$HOME" == "/root" ]; then source /etc/crucible.cfg RCFILE="$ETC_DIR/grab-kernel.rc" else RCFILE="$HOME/.grabkernelrc" fi # set empty param strings PATCHDIR="" MIRRORS="" DEFPATCHDIR=/usr/src/kernel-archive DEFMIRRORS="http://www.kernel.org/pub/linux/kernel" # get base defaults from $RCFILE # Note: using '~' instead of $HOME fails; if [ -r $RCFILE ]; then #echo "debug: sourcing RCFILE=$RCFILE" source $RCFILE else echo "Warning: no RCFILE=$RCFILE" fi if [ "$PATCHDIR" == "" ]; then PATCHDIR=$DEFPATCHDIR fi #echo "debug: using PATCHDIR=$PATCHDIR" if [ "$MIRRORS" == "" ]; then # MIRRORS="rsync://rsync.kernel.org:/pub/linux/kernel http://www.kernel.org/pub/linux/kernel rsync://master.kernel.org:/pub/linux/kernel" # MIRRORS="rsync.planetmirror.com::ftp.kernel.org/pub/linux/kernel master.kernel.org:/pub/linux/kernel" MIRRORS=$DEFMIRRORS fi #echo "debug: using MIRRORS=$MIRRORS" if [ $# -eq 3 ]; then level=$3 ###echo "INFO: level is: $level" >&2 else if [ $# -ne 2 ]; then echo Usage: $0 version dest_kernel_dir >&2 echo " with other parameters in $RCFILE" >&2 exit 1 fi fi VERSION=$1 KDIR=$2 if [ -d $KDIR/linux-$VERSION ]; then pr_exists "$KDIR/linux-$VERSION" ; fi MAJOR=`echo $VERSION | cut -d. -f1-2` echo Grabbing $VERSION case "$VERSION" in # Bitkeeper snapshot *-bk*) if [ -d $KDIR/linux-$VERSION ]; then pr_exists "$KDIR/linux-$VERSION" ; fi PREVIOUS=`echo "$VERSION" | sed 's/-bk.*//'` $0 "$PREVIOUS" $KDIR $((level + 1)) || barf "Couldn't get $PREVIOUS to build $VERSION" # Grab patch get_file $PATCHDIR v$MAJOR/snapshots/patch-$VERSION.bz2 || barf "Couldn't get v$MAJOR/snapshots/patch-$VERSION.bz2" apply_patch $PREVIOUS $VERSION $PATCHDIR/patch-$VERSION.bz2 ;; # git snapshot *-git*) if [ -d $KDIR/linux-$VERSION ]; then pr_exists "$KDIR/linux-$VERSION" ; fi PREVIOUS=`echo "$VERSION" | sed 's/-git.*//'` $0 "$PREVIOUS" $KDIR $((level + 1)) || barf "Couldn't get $PREVIOUS to build $VERSION" # Grab patch get_file $PATCHDIR v$MAJOR/snapshots/patch-$VERSION.bz2 || barf "Couldn't get v$MAJOR/snapshots/patch-$VERSION.bz2" apply_patch $PREVIOUS $VERSION $PATCHDIR/patch-$VERSION.bz2 ;; # Various vanity patches *-mm[0-9]*|*-ac[0-9]*|*-dj[0-9]*) # First make sure we have base tree BASE_KERNEL=`echo "$VERSION" | sed -e 's/-[a-z][a-z][0-9]*$//'` $0 "$BASE_KERNEL" $KDIR $((level + 1)) || barf "Couldn't get $BASE_KERNEL to build $VERSION" VANITY=`vanity_source $VERSION $MAJOR $BASE_KERNEL` get_file $PATCHDIR $VANITY || barf "Couldn't fetch $VANITY" # rename 2.6.*-mm-* to patch-2.6.*-mm-* if [ -e $PATCHDIR/$VERSION.bz2 ]; then mv $PATCHDIR/$VERSION.bz2 $PATCHDIR/patch-$VERSION.bz2 fi ##apply_patch $BASE_KERNEL $VERSION $PATCHDIR/`basename $VANITY` apply_patch $BASE_KERNEL $VERSION $PATCHDIR/patch-$VERSION.bz2 ;; *next-*) # First make sure we have base tree echo "## -next- target VERSION=$VERSION ##" BASE_KERNEL=`echo "$VERSION" | sed -e 's/-[a-z][a-z][0-9]*$//'` echo "## -next- BASE_KERNEL=$BASE_KERNEL ##" ##$0 "$BASE_KERNEL" $KDIR $((level + 1)) || ##barf "Couldn't get $BASE_KERNEL to build $VERSION" VANITY=`vanity_source $VERSION $MAJOR $BASE_KERNEL` echo "## -next- VANITY=$VANITY ##" ##get_file $PATCHDIR $VANITY || barf "Couldn't fetch $VANITY" ####apply_patch $BASE_KERNEL $VERSION $PATCHDIR/`basename $VANITY` ##apply_patch $BASE_KERNEL $VERSION $PATCHDIR/patch-$VERSION.bz2 ;; # *-pre* or *-rc*: This refers to the Linus or Marcelo pre-patches. *-pre*|*-rc*) # Linus or Marcelo pre-patch. if [ -d $KDIR/linux-$VERSION ]; then pr_exists "$KDIR/linux-$VERSION" ; fi PREVIOUS=`echo "$VERSION" | sed 's/-\(pre\|rc\).*//'` LAST_VER=`echo "$PREVIOUS" | cut -d. -f3` PREVIOUS=`echo "$PREVIOUS" | cut -d. -f1,2`.`expr $LAST_VER - 1 || true` $0 "$PREVIOUS" $KDIR $((level + 1)) || barf "Couldn't get $PREVIOUS to build $VERSION" # Grab patch get_file $PATCHDIR v$MAJOR/testing/patch-$VERSION.bz2 || get_file $PATCHDIR v$MAJOR/testing/old/patch-$VERSION.bz2 || barf "Couldn't get v$MAJOR/testing/patch-$VERSION.bz2" apply_patch $PREVIOUS $VERSION $PATCHDIR/patch-$VERSION.bz2 ;; # Linus 2.6.0 test patch. 2.6.0-test*) VER=`echo "$VERSION" | sed 's/.*test//'` PREVIOUS=2.6.0-test`expr $VER - 1 || true` if [ $PREVIOUS = 2.6.0-test0 ]; then PREVIOUS=2.5.75 fi $0 "$PREVIOUS" $KDIR $((level + 1)) || barf "Couldn't get $PREVIOUS to build $VERSION" # Grab patch get_file $PATCHDIR v$MAJOR/patch-$VERSION.bz2 || get_file $PATCHDIR v$MAJOR/old/patch-$VERSION.bz2 || barf "Couldn't get v$MAJOR/patch-$VERSION.bz2" apply_patch $PREVIOUS $VERSION $PATCHDIR/patch-$VERSION.bz2 ;; 2.6.0) PREVIOUS=2.6.0-test11 $0 "$PREVIOUS" $KDIR $((level + 1)) || barf "Couldn't get $PREVIOUS to build $VERSION" # Grab patch get_file $PATCHDIR v$MAJOR/patch-$VERSION.bz2 || get_file $PATCHDIR v$MAJOR/old/patch-$VERSION.bz2 || barf "Couldn't get v$MAJOR/patch-$VERSION.bz2" apply_patch $PREVIOUS $VERSION $PATCHDIR/patch-$VERSION.bz2 ;; # *.0: This refers to the base of the Linus kernels, fetched whole. *.0) # Linus' root of all trees. get_file $PATCHDIR v$MAJOR/linux-$VERSION.tar.bz2 || barf "Couldn't get v$MAJOR/linux-$VERSION.tar.bz2" bzcat $PATCHDIR/linux-$VERSION.tar.bz2 | (cd $KDIR && tar xf -) || barf "Problem unpacking linux-$VERSION.tar.bz2" mv $KDIR/linux $KDIR/linux-$VERSION ;; # [1-9].*.*: This refers to the any other Linus kernel. [1-9].*.*[0-9]) # Linus standard ? if [ -d $KDIR/linux-$VERSION ]; then pr_exists "$KDIR/linux-$VERSION" ; fi LAST_VER=`echo "$VERSION" | cut -d. -f3` PREVIOUS=`echo "$VERSION" | cut -d. -f1,2`.`expr $LAST_VER - 1 || true` ###$0 "$PREVIOUS" $KDIR $((level + 1)) || ### barf "Couldn't get $PREVIOUS to build $VERSION" # Grab tarball get_file $PATCHDIR v$MAJOR/linux-$VERSION.tar.bz2 || barf "Couldn't get v$MAJOR/linux-$VERSION.tar.bz2" ###trap "rm -rf $KDIR/linux-$VERSION" 0 ###apply_patch $PREVIOUS $VERSION $PATCHDIR/linux-$VERSION.tar.bz2 ###echo "INFO: cd to: KDIR=$KDIR" cd $KDIR && echo "untar in $KDIR....." tar xjf $PATCHDIR/linux-$VERSION.tar.bz2 || barf "Problem with kernel tarball ($VERSION)" ###echo "INFO: level: $level" >&2 if [ $level -eq 0 ]; then echo "rename /linux-$VERSION/ as needed....." >&2 fi ;; *) barf "Unknown kernel version $VERSION" ;; esac make_exec linux-$VERSION # sample ~/.grabkernelrc file (remove '#' to activate a line): #PATCHDIR=~/kernel-patches #MIRRORS="http://www.kernel.org/pub/linux/kernel"