add optional 3rd argument to support additional packages, ex;
[bootcd.git] / kvariant.sh
1 #!/bin/bash
2
3 COMMAND=$(basename $0)
4
5 function usage() {
6   echo "$COMMAND variant kernel-rpm"
7   echo "    Allows to create a variant of the bootcd image with a different kernel"
8   echo "    variant will be created under /usr/share/bootcd/<variant>"
9   echo "    with the same structure as the default /usr/share/bootcd/build"
10   echo "    the kernel rpm will also be stored in the variant dir for future reference"
11   echo "e.g. $COMMAND centos5 http://mirror.onelab.eu/centos/5.2/updates/i386/RPMS/kernel-2.6.18-92.1.1.el5.i686.rpm"
12   exit 1
13 }
14
15 function bail () {
16     rm -rf $tmpdir $files
17     exit -1
18 }
19
20 ## locate rpm and store it in variant
21 function getrpm () {
22     kernelrpm_url=$1; shift
23     kernelrpm_local=$1; shift
24     nocolon=$(echo $kernelrpm_url | sed -e s,:,,)
25     if [ "$kernelrpm_url" == "$nocolon" ] ; then
26         echo "Copying $kernelrpm_url in $variant_path"
27         cat $kernelrpm_url > $kernelrpm_local
28     else
29         echo "Fetching $kernelrpm_url in $variant_path"
30         curl -o $kernelrpm_local $kernelrpm_url
31     fi 
32 }
33
34 ## sanity check
35 function checkrpm () {
36     filename=$1
37     if [ -f "$filename" ] ; then
38         if [ $(rpm -qip $filename | wc -l) -eq 1 ] ; then
39             echo "$filename not a valid rpm file"
40             usage
41         fi
42     fi
43 }
44
45 ######################################## let's go
46 set -e
47
48 [[ -z "$@" ]] && usage
49 variant=$1; shift
50 [[ -z "$@" ]] && usage
51 kernelrpm_url=$1; shift
52 if [[ -n "$@" ]] ; then
53     extrarpm_url=$1; shift
54 fi
55 [[ -n "$@" ]] && usage
56
57 basedir=$(cd -P $(dirname $0); pwd)
58 standard_path="$basedir/build"
59 if [ ! -d $standard_path ] ; then
60     echo "Cound not find standard image $standard_path - exiting"
61     exit 1
62 fi
63
64 variant_path="$basedir/$variant"
65 if [ -e $variant_path ] ; then
66     echo "Found $variant_path - please remove first - exiting"
67     exit 1
68 fi
69
70 here=$(pwd)
71 mkdir $variant_path
72 echo "Creating $variant_path from $standard_path"
73 tar -C $standard_path -cf - . | tar -C $variant_path -xf - 
74
75 kernelrpm=$variant_path/$(basename $kernelrpm_url)
76 getrpm $kernelrpm_url $kernelrpm
77 checkrpm $kernelrpm
78
79 if [ -n "$extrarpm_url" ] ; then
80     extrarpm=$variant_path/$(basename $extrarpm_url)
81     getrpm $extrarpm_url $extrarpm
82     checkrpm $extrarpm
83 fi
84
85 isofsdir=$variant_path/isofs
86
87 tmpdir=
88 files=
89
90 tmpdir=$(mktemp -d /var/tmp/bootcd.XXXXXX)
91 trap "bail" ERR INT
92 echo "Updating bootcd image with $kernelrpm"
93 mkdir $tmpdir/bootcd
94 pushd $tmpdir/bootcd
95 echo "Unwrapping bootcd.img in $(pwd)"
96 gzip -d -c $isofsdir/bootcd.img | cpio -diu
97 echo "Cleaning up older kernel"
98 rm -rf boot/*
99 rm -rf lib/modules
100 echo "Replacing with new kernel"
101 rpm2cpio  $kernelrpm | cpio -diu
102 if [ -n $"extrarpm_url" ] ; then
103     echo "Unpacking $extrarpm"
104     rpm2cpio  $extrarpm | cpio -diu
105 fi
106 echo "Running depmod"
107 version=$(cd ./boot && ls vmlinuz* | sed 's,vmlinuz-,,')
108 depmod -b . $version
109 echo "Exposing kernel"
110 cp boot/vmlinuz* ${tmpdir}/kernel
111 echo "Wrapping new bootcd.img"
112 find . | cpio --quiet -c -o | gzip -9 > ${tmpdir}/bootcd.img
113 popd
114
115 #
116 echo -n "Preserving in $isofsdir .."
117 mv ${isofsdir}/kernel ${tmpdir}/kernel.orig
118 echo -n " kernel"
119 mv ${isofsdir}/bootcd.img ${tmpdir}/bootcd.img.orig
120 echo -n " bootcd.img"
121 echo ""
122
123 #
124 echo -n "Populating $isofsdir .."
125 mv ${tmpdir}/kernel ${isofsdir}/kernel
126 echo -n " kernel"
127 mv ${tmpdir}/bootcd.img ${isofsdir}/bootcd.img
128 echo -n " bootcd.img"
129 echo ""
130
131 rm -rf $tmpdir $kernelrpm $extrarpm
132
133 echo "new variant $variant ready"
134 trap - ERR
135 exit 0