correctly remove USE=build
[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 [ -n "${clst_linuxrc}" ]
54         then
55                 GK_ARGS="${GK_ARGS} --linuxrc=/tmp/linuxrc"
56         fi
57
58         if [ -n "${clst_busybox_config}" ]
59         then
60                 GK_ARGS="${GK_ARGS} --busybox-config=/tmp/busy-config"
61         fi
62
63         if [ "${clst_target}" == "netboot2" ]
64         then
65                 GK_ARGS="${GK_ARGS} --netboot"
66
67                 if [ -n "${clst_merge_path}" ]
68                 then
69                         GK_ARGS="${GK_ARGS} --initramfs-overlay=\"${clst_merge_path}\""
70                 fi
71         fi
72 }
73
74 genkernel_compile(){
75         eval "clst_initramfs_overlay=\$clst_boot_kernel_${filtered_kname}_initramfs_overlay"
76         eval "clst_kernel_merge=\$clst_boot_kernel_${filtered_kname}_packages"
77
78         setup_gk_args
79         #echo "The GK_ARGS are"
80         #echo ${GK_ARGS}
81         export clst_kernel_merge
82         export clst_initramfs_overlay
83         # Build our list of kernel packages
84         case ${clst_livecd_type} in
85                 gentoo-release-live*)
86                         if [ -n "${clst_kernel_merge}" ]
87                         then
88                                 mkdir -p /usr/livecd
89                                 echo "${clst_kernel_merge}" > /usr/livecd/kernelpkgs.txt
90                         fi
91                 ;;
92         esac
93         # Build with genkernel using the set options
94         # callback is put here to avoid escaping issues
95         gk_callback_opts="-qN"
96         PKGDIR=${PKGDIR}
97         if [ -n "${clst_KERNCACHE}" ]
98         then
99                 gk_callback_opts="${gk_callback_opts} -kb"
100         fi
101         if [ -n "${clst_FETCH}" ]
102         then
103                 gk_callback_opts="${gk_callback_opts} -f"
104         fi
105         if [ "${clst_kernel_merge}" != "" ]
106         then
107                 genkernel --callback="emerge ${gk_callback_opts} ${clst_kernel_merge}" \
108                         ${GK_ARGS} || exit 1
109         else
110                 genkernel ${GK_ARGS} || exit 1
111         fi
112         if [ -e /var/tmp/${clst_kname}.config ]
113         then
114                 md5sum /var/tmp/${clst_kname}.config | awk '{print $1}' > \
115                         /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.CONFIG
116         fi
117 }
118
119 build_kernel() {
120         genkernel_compile
121 }
122
123 [ -n "${clst_ENVSCRIPT}" ] && source /tmp/envscript
124 export CONFIG_PROTECT="-*"
125
126 # Set the timezone for the kernel build
127 rm /etc/localtime
128 cp -f /usr/share/zoneinfo/UTC /etc/localtime
129
130 filtered_kname=${clst_kname/-/_}
131 filtered_kname=${clst_kname/\//_}
132 filtered_kname=${filtered_kname/\./_}
133
134 eval "clst_kernel_use=\$clst_boot_kernel_${filtered_kname}_use"
135 eval "clst_kernel_gk_kernargs=\$clst_boot_kernel_${filtered_kname}_gk_kernargs"
136 eval "clst_ksource=\$clst_boot_kernel_${filtered_kname}_sources"
137
138 if [ -z "${clst_ksource}" ]
139 then
140         clst_ksource="virtual/linux-sources"
141 fi
142
143 # Don't use pkgcache here, as the kernel source may get emerged with different
144 # USE variables (and thus different patches enabled/disabled.) Also, there's no
145 # real benefit in using the pkgcache for kernel source ebuilds.
146
147 USE_MATCH=0
148 if [ -e /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.USE ]
149 then
150         STR1=$(for i in `cat /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.USE`; do echo $i; done|sort)
151         STR2=$(for i in ${clst_kernel_use}; do echo $i; done|sort)
152         if [ "${STR1}" = "${STR2}" ]
153         then
154                 #echo "USE Flags match"
155                 USE_MATCH=1
156         else
157                 if [ -n "${clst_KERNCACHE}" ]
158                 then
159                 [ -d /tmp/kerncache/${clst_kname}/ebuilds ] && \
160                         rm -r /tmp/kerncache/${clst_kname}/ebuilds
161                 [ -e /tmp/kerncache/${clst_kname}/usr/src/linux/.config ] && \
162                         rm /tmp/kerncache/${clst_kname}/usr/src/linux/.config
163                 fi
164         fi
165 fi
166
167 EXTRAVERSION_MATCH=0
168 if [ -e /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.EXTRAVERSION ]
169 then
170         STR1=`cat /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.EXTRAVERSION`
171         STR2=${clst_kextraversion}
172         if [ "${STR1}" = "${STR2}" ]
173         then
174                 if [ -n "${clst_KERNCACHE}" ]
175                 then
176                         #echo "EXTRAVERSION match"
177                         EXTRAVERSION_MATCH=1
178                 fi
179         fi
180 fi
181
182 CONFIG_MATCH=0
183 if [ -n "${clst_KERNCACHE}" -a \
184      -e /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.CONFIG ]
185 then
186         if [ ! -e /var/tmp/${clst_kname}.config ]
187         then
188                 CONFIG_MATCH=1
189         else
190                 STR1=`cat /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.CONFIG`
191                 STR2=`md5sum /var/tmp/${clst_kname}.config|awk '{print $1}'`
192                 if [ "${STR1}" = "${STR2}" ]
193                 then
194                         CONFIG_MATCH=1
195                 fi
196         fi
197 fi
198
199 [ -e /etc/portage/make.conf ] && \
200         echo "USE=\"\${USE} ${clst_kernel_use} \"" >> /etc/portage/make.conf
201
202 if [ -n "${clst_KERNCACHE}" ]
203 then
204         mkdir -p /tmp/kerncache/${clst_kname}
205         clst_root_path=/tmp/kerncache/${clst_kname} PKGDIR=${PKGDIR} clst_myemergeopts="--quiet --nodeps --update --newuse" run_merge "${clst_ksource}" || exit 1
206         KERNELVERSION=`portageq best_visible / "${clst_ksource}"`
207         if [ ! -e /etc/portage/profile/package.provided ]
208         then
209                 mkdir -p /etc/portage/profile
210                 echo "${KERNELVERSION}" > /etc/portage/profile/package.provided
211         else
212                 if ( ! grep -q "^${KERNELVERSION}\$"  /etc/portage/profile/package.provided )
213                 then
214                         echo "${KERNELVERSION}" >> /etc/portage/profile/package.provided
215                 fi
216         fi
217         [ -L /usr/src/linux ] && rm -f /usr/src/linux
218         ln -s /tmp/kerncache/${clst_kname}/usr/src/linux /usr/src/linux
219 else
220         [ -L /usr/src/linux ] && rm -f /usr/src/linux
221         run_merge "${clst_ksource}" || exit 1
222 fi
223 make_destpath
224
225 # If catalyst has set to a empty string, extraversion wasn't specified so we
226 # skip this part
227 if [ "${EXTRAVERSION_MATCH}" = "0" ]
228 then
229         if [ ! "${clst_kextraversion}" = "" ]
230         then
231                 echo "Setting extraversion to ${clst_kextraversion}"
232                 sed -i -e "s:EXTRAVERSION \(=.*\):EXTRAVERSION \1-${clst_kextraversion}:" /usr/src/linux/Makefile
233                 echo ${clst_kextraversion} > /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.EXTRAVERSION
234         else
235                 touch /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.EXTRAVERSION
236         fi
237 fi
238
239 build_kernel
240 sed -i "/USE=\"\${USE} ${clst_kernel_use} \"/d" /etc/portage/make.conf
241 # grep out the kernel version so that we can do our modules magic
242 VER=`grep ^VERSION\ \= /usr/src/linux/Makefile | awk '{ print $3 };'`
243 PAT=`grep ^PATCHLEVEL\ \= /usr/src/linux/Makefile | awk '{ print $3 };'`
244 SUB=`grep ^SUBLEVEL\ \= /usr/src/linux/Makefile | awk '{ print $3 };'`
245 EXV=`grep ^EXTRAVERSION\ \= /usr/src/linux/Makefile | sed -e "s/EXTRAVERSION =//" -e "s/ //g"`
246 clst_fudgeuname=${VER}.${PAT}.${SUB}${EXV}
247
248 /sbin/update-modules --assume-kernel=${clst_fudgeuname}
249
250 unset USE
251 echo ${clst_kernel_use} > /tmp/kerncache/${clst_kname}/${clst_kname}-${clst_version_stamp}.USE
252
253
254 if [ -n "${clst_KERNCACHE}" ]
255 then
256         if [ -e /etc/portage/profile/package.provided ]
257         then
258                 sed -i "/^$(echo "${KERNELVERSION}" | sed -e 's|/|\\/|g')\$/d" /etc/portage/profile/package.provided
259         fi
260 fi