Run sed on KERNELVERSION to escape the slashes so we can use it in another sed
[catalyst.git] / targets / support / kmerge.sh
1 #!/bin/bash
2
3 source /tmp/chroot-functions.sh
4
5 check_genkernel_version
6
7 install -d /tmp/kerncache
8 PKGDIR=/tmp/kerncache/${clst_kname}/ebuilds
9
10 setup_gk_args() {
11         # default genkernel args
12         GK_ARGS="${clst_gk_mainargs} \
13                          ${clst_kernel_gk_kernargs} \
14                          --cachedir=/tmp/kerncache/${clst_kname}-genkernel_cache-${clst_version_stamp} \
15                          --no-mountboot \
16                          --kerneldir=/usr/src/linux \
17                          --modulespackage=/tmp/kerncache/${clst_kname}-modules-${clst_version_stamp}.tar.bz2 \
18                          --minkernpackage=/tmp/kerncache/${clst_kname}-kernel-initrd-${clst_version_stamp}.tar.bz2 all"
19         # extra genkernel options that we have to test for
20         if [ -n "${clst_KERNCACHE}" ]
21         then
22                 GK_ARGS="${GK_ARGS} --kerncache=/tmp/kerncache/${clst_kname}-kerncache-${clst_version_stamp}.tar.bz2"
23         fi
24         if [ -e /var/tmp/${clst_kname}.config ]
25         then
26                 GK_ARGS="${GK_ARGS} --kernel-config=/var/tmp/${clst_kname}.config"
27         fi
28
29         if [ -n "${clst_splash_theme}" ]
30         then
31                 GK_ARGS="${GK_ARGS} --splash=${clst_splash_theme}"
32                 # Setup case structure for livecd_type
33                 case ${clst_livecd_type} in
34                         gentoo-release-minimal|gentoo-release-universal)
35                                 case ${clst_hostarch} in
36                                         amd64|x86)
37                                                 GK_ARGS="${GK_ARGS} --splash-res=1024x768"
38                                         ;;
39                                 esac
40                         ;;
41                 esac
42         fi
43
44         if [ -d "/tmp/initramfs_overlay/${clst_initramfs_overlay}" ]
45         then
46                 GK_ARGS="${GK_ARGS} --initramfs-overlay=/tmp/initramfs_overlay/${clst_initramfs_overlay}"
47         fi
48         if [ -n "${clst_CCACHE}" ]
49         then
50                 GK_ARGS="${GK_ARGS} --kernel-cc=/usr/lib/ccache/bin/gcc --utils-cc=/usr/lib/ccache/bin/gcc"
51         fi
52         
53         if [ "${clst_devmanager}" == "devfs" ]
54         then
55                 GK_ARGS="${GK_ARGS} --no-udev"
56         fi
57
58         if [ -n "${clst_linuxrc}" ]
59         then
60                 GK_ARGS="${GK_ARGS} --linuxrc=/tmp/linuxrc"
61         fi
62
63     if [ "${clst_target}" == "netboot2" -a -n "${clst_merge_path}" ]
64     then
65         GK_ARGS="${GK_ARGS} --initramfs-overlay=\"${clst_merge_path}\""
66     fi
67 }
68
69 genkernel_compile(){
70         eval "clst_initramfs_overlay=\$clst_boot_kernel_${filtered_kname}_initramfs_overlay"
71         eval "clst_kernel_merge=\$clst_boot_kernel_${filtered_kname}_packages"
72
73         setup_gk_args
74         #echo "The GK_ARGS are"
75         #echo ${GK_ARGS}        
76         export clst_kernel_merge
77         export clst_initramfs_overlay
78         # Build our list of kernel packages
79         if [ "${clst_livecd_type}" = "gentoo-release-livecd" ] \
80         && [ -n "${clst_kernel_merge}" ]
81         then
82                 mkdir -p /usr/livecd
83                 echo "${clst_kernel_merge}" > /usr/livecd/kernelpkgs.txt
84         fi
85         # Build with genkernel using the set options
86         # callback is put here to avoid escaping issues
87         gk_callback_opts="-qN"
88         PKGDIR=${PKGDIR}
89         if [ -n "${clst_KERNCACHE}" ]
90         then
91                 gk_callback_opts="${gk_callback_opts} -kb"
92         fi
93         if [ -n "${clst_FETCH}" ]
94         then
95                 gk_callback_opts="${gk_callback_opts} -f"
96         fi
97         if [ "${clst_kernel_merge}" != "" ]
98         then
99                 genkernel --callback="emerge ${gk_callback_opts} ${clst_kernel_merge}" \
100                         ${GK_ARGS} || exit 1
101         else
102                 genkernel ${GK_ARGS} || exit 1
103         fi
104         md5sum /var/tmp/${clst_kname}.config | awk '{print $1}' > \
105                 /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.CONFIG
106 }
107
108 build_kernel() {
109         genkernel_compile
110 }
111
112 [ -n "${clst_ENVSCRIPT}" ] && source /tmp/envscript
113 export CONFIG_PROTECT="-*"
114
115 # Set the timezone for the kernel build
116 rm /etc/localtime
117 cp -f /usr/share/zoneinfo/UTC /etc/localtime
118
119 filtered_kname=${clst_kname/-/_}
120 filtered_kname=${clst_kname/\//_}
121 filtered_kname=${filtered_kname/\./_}
122
123 eval "clst_kernel_use=\$clst_boot_kernel_${filtered_kname}_use"
124 eval "clst_kernel_gk_kernargs=\$clst_boot_kernel_${filtered_kname}_gk_kernargs"
125 eval "clst_ksource=\$clst_boot_kernel_${filtered_kname}_sources"
126
127 if [ -z "${clst_ksource}" ]
128 then
129         clst_ksource="virtual/linux-sources"
130 fi
131
132 # Don't use pkgcache here, as the kernel source may get emerged with different
133 # USE variables (and thus different patches enabled/disabled.) Also, there's no
134 # real benefit in using the pkgcache for kernel source ebuilds.
135
136 USE_MATCH=0 
137 if [ -e /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.USE ]
138 then
139         STR1=$(for i in `cat /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.USE`; do echo $i; done|sort)
140         STR2=$(for i in ${clst_kernel_use}; do echo $i; done|sort)
141         if [ "${STR1}" = "${STR2}" ]
142         then 
143                 #echo "USE Flags match"
144                 USE_MATCH=1 
145         else
146                 if [ -n "${clst_KERNCACHE}" ]
147                 then
148                 [ -d /tmp/kerncache/${clst_kname}/ebuilds ] && \
149                         rm -r /tmp/kerncache/${clst_kname}/ebuilds
150                 [ -e /tmp/kerncache/${clst_kname}/usr/src/linux/.config ] && \
151                         rm /tmp/kerncache/${clst_kname}/usr/src/linux/.config
152                 fi
153         fi
154 fi
155
156 EXTRAVERSION_MATCH=0
157 if [ -e /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.EXTRAVERSION ]
158 then
159         STR1=`cat /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.EXTRAVERSION`
160         STR2=${clst_kextraversion}
161         if [ "${STR1}" = "${STR2}" ]
162         then 
163                 if [ -n "${clst_KERNCACHE}" ]
164                 then
165                         #echo "EXTRAVERSION match"
166                         EXTRAVERSION_MATCH=1
167                 fi
168         fi
169 fi
170
171 CONFIG_MATCH=0
172 if [ -e /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.CONFIG ]
173 then
174         STR1=`cat /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.CONFIG`
175         STR2=`md5sum /var/tmp/${clst_kname}.config|awk '{print $1}'`
176         if [ "${STR1}" = "${STR2}" ]
177         then 
178                 if [ -n "${clst_KERNCACHE}" ]
179                 then
180                         #echo "CONFIG match"
181                         CONFIG_MATCH=1
182                 fi
183         fi
184 fi
185
186 [ -e /etc/make.conf ] && \
187         echo "USE=\"\${USE} ${clst_kernel_use} build\"" >> /etc/make.conf
188
189 if [ -n "${clst_KERNCACHE}" ]
190 then
191         mkdir -p /tmp/kerncache/${clst_kname}
192         clst_root_path=/tmp/kerncache/${clst_kname} PKGDIR=${PKGDIR} clst_myemergeopts="--quiet --nodeps --update --newuse" run_merge "${clst_ksource}" || exit 1
193         KERNELVERSION=`portageq best_visible / "${clst_ksource}"`
194         if [ ! -e /etc/portage/profile/package.provided ]
195         then
196                 mkdir -p /etc/portage/profile
197                 echo "${KERNELVERSION}" > /etc/portage/profile/package.provided
198         else
199                 if ( ! grep -q "^${KERNELVERSION}\$"  /etc/portage/profile/package.provided ) 
200                 then
201                         echo "${KERNELVERSION}" >> /etc/portage/profile/package.provided
202                 fi
203         fi
204         [ -L /usr/src/linux ] && rm -f /usr/src/linux
205         ln -s /tmp/kerncache/${clst_kname}/usr/src/linux /usr/src/linux
206 else
207         [ -L /usr/src/linux ] && rm -f /usr/src/linux
208         run_merge "${clst_ksource}" || exit 1
209 fi
210 make_destpath
211
212 # If catalyst has set to a empty string, extraversion wasn't specified so we
213 # skip this part
214 if [ "${EXTRAVERSION_MATCH}" = "0" ]
215 then
216         if [ ! "${clst_kextraversion}" = "" ]
217         then
218                 echo "Setting extraversion to ${clst_kextraversion}"
219                 sed -i -e "s:EXTRAVERSION \(=.*\):EXTRAVERSION \1-${clst_kextraversion}:" /usr/src/linux/Makefile
220                 echo ${clst_kextraversion} > /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.EXTRAVERSION
221         else 
222                 touch /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.EXTRAVERSION
223         fi
224 fi
225
226 build_kernel
227 sed -i "/USE=\"\${USE} ${clst_kernel_use} build\"/d" /etc/make.conf
228 # grep out the kernel version so that we can do our modules magic
229 VER=`grep ^VERSION\ \= /usr/src/linux/Makefile | awk '{ print $3 };'`
230 PAT=`grep ^PATCHLEVEL\ \= /usr/src/linux/Makefile | awk '{ print $3 };'`
231 SUB=`grep ^SUBLEVEL\ \= /usr/src/linux/Makefile | awk '{ print $3 };'`
232 EXV=`grep ^EXTRAVERSION\ \= /usr/src/linux/Makefile | sed -e "s/EXTRAVERSION =//" -e "s/ //g"`
233 clst_fudgeuname=${VER}.${PAT}.${SUB}${EXV}
234
235 /sbin/update-modules --assume-kernel=${clst_fudgeuname}
236
237 unset USE
238 echo ${clst_kernel_use} > /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.USE
239
240
241 if [ -n "${clst_KERNCACHE}" ]
242 then
243         if [ -e /etc/portage/profile/package.provided ]
244         then
245                 sed -i "/^$(echo "${KERNELVERSION}" | sed -e 's|/|\\/|g')\$/d" /etc/portage/profile/package.provided
246         fi
247 fi