- refactored marc's kupdate.sh into a new script kvariant.sh
[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 [[ -n "$@" ]] && usage
53
54 standard_path="/usr/share/bootcd/build"
55 if [ ! -d $standard_path ] ; then
56     echo "Cound not find standard image $standard_path - exiting"
57     exit 1
58 fi
59
60 variant_path=/usr/share/bootcd/$variant
61 if [ -e $variant_path ] ; then
62     echo "Found $variant_path - please remove first - exiting"
63     exit 1
64 fi
65
66 here=$(pwd)
67 mkdir $variant_path
68 echo "Creating $variant_path from $standard_path"
69 tar -C $standard_path -cf - . | tar -C $variant_path -xf - 
70
71 kernelrpm=$variant_path/$(basename $kernelrpm_url)
72 getrpm $kernelrpm_url $kernelrpm
73 checkrpm $kernelrpm
74
75 isofsdir=$variant_path/isofs
76
77 tmpdir=
78 files=
79
80 tmpdir=$(mktemp -d /var/tmp/bootcd.XXXXXX)
81 trap "bail" ERR INT
82 echo "Updating bootcd image with $kernelrpm"
83 mkdir $tmpdir/bootcd
84 pushd $tmpdir/bootcd
85 echo "Unwrapping bootcd.img in $(pwd)"
86 gzip -d -c $isofsdir/bootcd.img | cpio -diu
87 echo "Cleaning up older kernel"
88 rm -rf boot/*
89 rm -rf lib/modules
90 echo "Replacing with new kernel"
91 rpm2cpio  $kernelrpm | cpio -diu
92 echo "Running depmod"
93 version=$(cd ./boot && ls vmlinuz* | sed 's,vmlinuz-,,')
94 depmod -b . $version
95 echo "Exposing kernel"
96 cp boot/vmlinuz* ${tmpdir}/kernel
97 echo "Wrapping new bootcd.img"
98 find . | cpio --quiet -c -o | gzip -9 > ${tmpdir}/bootcd.img
99 popd
100
101 #
102 echo -n "Preserving in $isofsdir .."
103 mv ${isofsdir}/kernel ${tmpdir}/kernel.orig
104 echo -n " kernel"
105 mv ${isofsdir}/bootcd.img ${tmpdir}/bootcd.img.orig
106 echo -n " bootcd.img"
107 echo ""
108
109 #
110 echo -n "Populating $isofsdir .."
111 mv ${tmpdir}/kernel ${isofsdir}/kernel
112 echo -n " kernel"
113 mv ${tmpdir}/bootcd.img ${isofsdir}/bootcd.img
114 echo -n " bootcd.img"
115 echo ""
116
117 rm -rf $tmpdir
118
119 echo "new variant $variant ready"
120 trap - ERR
121 exit 0