7232c0cca53417bb35c1b2526354045dccc4fd08
[pti/rtp.git] / meta-stx / classes / stx-anaconda-image.bbclass
1 #
2 ## Copyright (C) 2019 Wind River Systems, Inc.
3 #
4 #  Licensed under the Apache License, Version 2.0 (the "License");
5 #  you may not use this file except in compliance with the License.
6 #  You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #  Unless required by applicable law or agreed to in writing, software
11 #  distributed under the License is distributed on an "AS IS" BASIS,
12 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #  See the License for the specific language governing permissions and
14 #  limitations under the License.
15
16 RPM_POSTPROCESS_COMMANDS_append = "wrl_installer;"
17 do_rootfs[vardeps] += "INSTALLER_TARGET_BUILD INSTALLER_TARGET_IMAGE"
18
19 # Fix system-shutdown hang at ratelimiting
20 APPEND_append = " printk.devkmsg=on"
21
22 INSTPRODUCT ?= "${DISTRO_NAME}"
23 INSTVER     ?= "${DISTRO_VERSION}"
24 INSTBUGURL  ?= "http://www.windriver.com/"
25
26 # NOTE: Please update anaconda-init when you change INSTALLER_CONFDIR, use "="
27 #       but not "?=" since this is not configurable.
28 INSTALLER_CONFDIR = "${IMAGE_ROOTFS}/installer-config"
29 KICKSTART_FILE ??= ""
30 KICKSTART_FILE_EXTRA ??= ""
31 WRL_INSTALLER_CONF ?= ""
32 REPO_EXCLUDE_PKG ?= ""
33
34 build_iso_prepend() {
35         install -d ${ISODIR}
36         ln -snf /.discinfo ${ISODIR}/.discinfo
37         ln -snf /.buildstamp ${ISODIR}/.buildstamp
38         ln -snf /Packages ${ISODIR}/Packages
39 }
40
41 build_iso_append() {
42         implantisomd5 ${IMGDEPLOYDIR}/${IMAGE_NAME}.iso
43 }
44
45 # Check WRL_INSTALLER_CONF and copy it to
46 # ${IMAGE_ROOTFS}/.buildstamp.$prj_name when exists
47 wrl_installer_copy_buildstamp() {
48     prj_name=$1
49     buildstamp=$2
50     if [ -f $buildstamp ]; then
51         bbnote "Using $buildstamp as the buildstamp"
52         cp $buildstamp ${IMAGE_ROOTFS}/.buildstamp.$prj_name
53     else
54         bbfatal "Can't find WRL_INSTALLER_CONF: $buildstamp"
55     fi
56 }
57
58 # Hardlink when possible, otherwise copy.
59 # $1: src
60 # $2: target
61 wrl_installer_hardlinktree() {
62     src_dev="`stat -c %d $1`"
63     if [ -e "$2" ]; then
64         tgt_dev="`stat -c %d $2`"
65     else
66         tgt_dev="`stat -c %d $(dirname $2)`"
67     fi
68     hdlink=""
69     if [ "$src_dev" = "$tgt_dev" ]; then
70         hdlink="--link"
71     fi
72     cp -rvf $hdlink $1 $2
73 }
74
75 wrl_installer_copy_local_repos() {
76     deploy_dir_rpm=$1
77
78     if [ -d "$deploy_dir_rpm" ]; then
79         echo "Copy rpms from target build to installer image."
80         mkdir -p ${IMAGE_ROOTFS}/Packages.$prj_name
81
82         : > ${IMAGE_ROOTFS}/Packages.$prj_name/.treeinfo
83         echo "[general]" >> ${IMAGE_ROOTFS}/Packages.$prj_name/.treeinfo
84         echo "version = ${DISTRO_VERSION}" >> ${IMAGE_ROOTFS}/Packages.$prj_name/.treeinfo
85
86         # Determine the max channel priority
87         channel_priority=5
88         for pt in $installer_target_archs ; do
89             channel_priority=$(expr $channel_priority + 5)
90         done
91
92         : > ${IMAGE_ROOTFS}/Packages.$prj_name/.feedpriority
93         for arch in $installer_target_archs; do
94             if [ -d "$deploy_dir_rpm/"$arch -a ! -d "${IMAGE_ROOTFS}/Packages.$prj_name/"$arch ]; then
95                 channel_priority=$(expr $channel_priority - 5)
96                 echo "$channel_priority $arch" >> ${IMAGE_ROOTFS}/Packages.$prj_name/.feedpriority
97                 wrl_installer_hardlinktree "$deploy_dir_rpm/"$arch "${IMAGE_ROOTFS}/Packages.$prj_name/."
98             fi
99         done
100
101         for pkg in ${REPO_EXCLUDE_PKG}; do
102             rm -rf ${IMAGE_ROOTFS}/Packages.$prj_name/${pkg}
103         done
104
105         createrepo_c --update -q ${IMAGE_ROOTFS}/Packages.$prj_name/
106     fi
107 }
108
109 # Update .buildstamp and copy rpm packages to IMAGE_ROOTFS
110 wrl_installer_copy_pkgs() {
111
112     target_build="$1"
113     target_image="$2"
114     prj_name="$3"
115     if [ -n "$4" ]; then
116         installer_conf="$4"
117     else
118         installer_conf=""
119     fi
120
121     common_grep="-e '^ALL_MULTILIB_PACKAGE_ARCHS=.*' \
122             -e '^MULTILIB_VARIANTS=.*' -e '^PACKAGE_ARCHS=.*'\
123             -e '^PACKAGE_ARCH=.*' -e '^PACKAGE_INSTALL_ATTEMPTONLY=.*' \
124             -e '^DISTRO=.*' -e '^DISTRO_NAME=.*' -e '^DISTRO_VERSION=.*' \
125             "
126
127     if [ -f "$installer_conf" ]; then
128         eval "grep -e \"^PACKAGE_INSTALL=.*\" $common_grep $installer_conf \
129             | sed -e 's/=/=\"/' -e 's/$/\"/' > ${BB_LOGFILE}.distro_vals"
130
131         eval "cat $target_build/installersupport_$target_image | \
132             grep -e '^WORKDIR=.*' >> ${BB_LOGFILE}.distro_vals"
133
134         eval `cat ${BB_LOGFILE}.distro_vals`
135         if [ $? -ne 0 ]; then
136             bbfatal "Something is wrong in $installer_conf, please correct it"
137         fi
138         if [ -z "$PACKAGE_ARCHS" -o -z "$PACKAGE_INSTALL" ]; then
139             bbfatal "PACKAGE_ARCHS or PACKAGE_INSTALL is null, please check $installer_conf"
140         fi
141     else
142         eval "cat $target_build/installersupport_$target_image | \
143             grep $common_grep -e '^PN=.*' -e '^SUMMARY=.*' -e '^WORKDIR=.*'\
144             -e '^DESCRIPTION=.*' -e '^export PACKAGE_INSTALL=.*' > ${BB_LOGFILE}.distro_vals"
145
146         eval `cat ${BB_LOGFILE}.distro_vals`
147     fi
148
149     export installer_default_arch="$PACKAGE_ARCH"
150     # Reverse it for priority
151     export installer_default_archs="`for arch in $PACKAGE_ARCHS; do echo $arch; done | tac | tr - _`"
152     installer_target_archs="$installer_default_archs"
153     if [ -n "$MULTILIB_VARIANTS" ]; then
154         export MULTILIB_VARIANTS
155         mlarchs_reversed="`for mlarch in $ALL_MULTILIB_PACKAGE_ARCHS; do echo $mlarch; \
156             done | tac | tr - _`"
157         for arch in $mlarchs_reversed; do
158             if [ "$arch" != "noarch" -a "$arch" != "all" -a "$arch" != "any" ]; then
159                 installer_target_archs="$installer_target_archs $arch"
160             fi
161         done
162     fi
163     export installer_target_archs
164
165     # Save the vars to .buildstamp when no installer_conf
166     if [ ! -f "$installer_conf" ]; then
167         cat >> ${IMAGE_ROOTFS}/.buildstamp.$prj_name <<_EOF
168 DISTRO=$DISTRO
169 DISTRO_NAME=$DISTRO_NAME
170 DISTRO_VERSION=$DISTRO_VERSION
171
172 [Rootfs]
173 LIST=$PN
174
175 [$PN]
176 SUMMARY=$SUMMARY
177 DESCRIPTION=$DESCRIPTION
178
179 PACKAGE_INSTALL=$PACKAGE_INSTALL
180 PACKAGE_INSTALL_ATTEMPTONLY=$PACKAGE_INSTALL_ATTEMPTONLY
181 ALL_MULTILIB_PACKAGE_ARCHS=$ALL_MULTILIB_PACKAGE_ARCHS
182 MULTILIB_VARIANTS=$MULTILIB_VARIANTS
183 PACKAGE_ARCHS=$PACKAGE_ARCHS
184 PACKAGE_ARCH=$PACKAGE_ARCH
185 IMAGE_LINGUAS=${IMAGE_LINGUAS}
186 _EOF
187     fi
188
189     if [ -d "$WORKDIR/oe-rootfs-repo/rpm" ]; then
190         # Copy local repos while the image is not initramfs
191         bpn=${BPN}
192         if [ "${bpn##*initramfs}" = "${bpn%%initramfs*}" ]; then
193             wrl_installer_copy_local_repos $WORKDIR/oe-rootfs-repo/rpm
194         fi
195         echo "$DISTRO::$prj_name::$DISTRO_NAME::$DISTRO_VERSION" >> ${IMAGE_ROOTFS}/.target_build_list
196     fi
197 }
198
199 wrl_installer_get_count() {
200     sum=0
201     for i in $*; do
202         sum=$(expr $sum + 1)
203     done
204     echo $sum
205 }
206
207 wrl_installer[vardepsexclude] = "DATETIME"
208 wrl_installer() {
209     cat >${IMAGE_ROOTFS}/.discinfo <<_EOF
210 ${DATETIME}
211 ${DISTRO_NAME} ${DISTRO_VERSION}
212 ${TARGET_ARCH}
213 _EOF
214
215     : > ${IMAGE_ROOTFS}/.target_build_list
216     counter=0
217     targetimage_counter=0
218     for target_build in ${INSTALLER_TARGET_BUILD}; do
219         target_build="`readlink -f $target_build`"
220         echo "Installer Target Build: $target_build"
221         counter=$(expr $counter + 1)
222         prj_name="`echo $target_build | sed -e 's#/ *$##g' -e 's#.*/##'`"
223         prj_name="$prj_name-$counter"
224
225             # Generate .buildstamp
226             if [ -n "${WRL_INSTALLER_CONF}" ]; then
227                 installer_conf="`echo ${WRL_INSTALLER_CONF} | awk '{print $'"$counter"'}'`"
228                 wrl_installer_copy_buildstamp $prj_name $installer_conf
229             else
230                 cat >${IMAGE_ROOTFS}/.buildstamp.$prj_name <<_EOF
231 [Main]
232 Product=${INSTPRODUCT}
233 Version=${INSTVER}
234 BugURL=${INSTBUGURL}
235 IsFinal=True
236 UUID=${DATETIME}.${TARGET_ARCH}
237 _EOF
238             fi
239
240             if [ -f "$target_build" ]; then
241                 filename=$(basename "$target_build")
242                 extension="${filename##*.}"
243                 bpn=${BPN}
244                 # Do not copy image for initramfs
245                 if [ "${bpn##*initramfs}" != "${bpn%%initramfs*}" ]; then
246                     continue
247                 elif [ "x$extension" = "xext2" -o "x$extension" = "xext3" -o "x$extension" = "xext4" ]; then
248                     echo "Image based target install selected."
249                     mkdir -p "${IMAGE_ROOTFS}/LiveOS.$prj_name"
250                     wrl_installer_hardlinktree "$target_build" "${IMAGE_ROOTFS}/LiveOS.$prj_name/rootfs.img"
251                     echo "::$prj_name::" >> ${IMAGE_ROOTFS}/.target_build_list
252                 else
253                     bberror "Unsupported image: $target_build."
254                     bberror "The image must be ext2, ext3 or ext4"
255                     exit 1
256                 fi
257             elif [ -d "$target_build" ]; then
258                 targetimage_counter=$(expr $targetimage_counter + 1)
259                 target_image="`echo ${INSTALLER_TARGET_IMAGE} | awk '{print $'"$targetimage_counter"'}'`"
260                 echo "Target Image: $target_image"
261                 wrl_installer_copy_pkgs $target_build $target_image $prj_name $installer_conf
262             else
263                 bberror "Invalid configuration of INSTALLER_TARGET_BUILD: $target_build."
264                 bberror "It must either point to an image (ext2, ext3 or ext4) or to the root of another build directory"
265                 exit 1
266             fi
267
268             ks_cfg="${INSTALLER_CONFDIR}/ks.cfg.$prj_name"
269             if [ -n "${KICKSTART_FILE}" ]; then
270                 ks_file="`echo ${KICKSTART_FILE} | awk '{print $'"$counter"'}'`"
271                 bbnote "Copying kickstart file $ks_file to $ks_cfg ..."
272                 mkdir -p ${INSTALLER_CONFDIR}
273                 cp $ks_file $ks_cfg
274             fi
275             if [ -n "${KICKSTART_FILE_EXTRA}" ]; then
276                 for ks_file in ${KICKSTART_FILE_EXTRA}; do
277                     if [ -f $ks_file ]; then
278                         cp $ks_file ${INSTALLER_CONFDIR}
279                     else
280                         bbwarn "The kickstart file $ks_file in KICKSTART_FILE_EXTRA doesn't exist!"
281                     fi
282                 done
283             fi
284     done
285
286     # Setup the symlink if only one target build dir.
287     if [ "$counter" = "1" ]; then
288         prj_name="`awk -F:: '{print $2}' ${IMAGE_ROOTFS}/.target_build_list`"
289         entries=".buildstamp LiveOS Packages installer-config/ks.cfg"
290         for i in $entries; do
291             if [ -e ${IMAGE_ROOTFS}/$i.$prj_name ]; then
292                 ln -sf `basename $i.$prj_name` ${IMAGE_ROOTFS}/$i
293             fi
294         done
295     fi
296 }
297
298 python __anonymous() {
299     if "selinux" in d.getVar("DISTRO_FEATURES", True).split():
300         raise bb.parse.SkipPackage("Unable to build the installer when selinux is enabled.")
301
302     if bb.data.inherits_class('image', d):
303         if d.getVar("DISTRO", True) != "anaconda":
304             raise bb.parse.SkipPackage("Set DISTRO = 'anaconda' in local.conf")
305
306         target_builds = d.getVar('INSTALLER_TARGET_BUILD', True)
307         if not target_builds:
308             errmsg = "No INSTALLER_TARGET_BUILD is found,\n"
309             errmsg += "set INSTALLER_TARGET_BUILD = '<target-build-topdir>' and\n"
310             errmsg += "INSTALLER_TARGET_IMAGE = '<target-image-pn>' to do RPMs\n"
311             errmsg += "install, or\n"
312             errmsg += "set INSTALLER_TARGET_BUILD = '<target-build-image>' to do\n"
313             errmsg += "image copy install"
314             raise bb.parse.SkipPackage(errmsg)
315
316         count = 0
317         for target_build in target_builds.split():
318             if not os.path.exists(target_build):
319                 raise bb.parse.SkipPackage("The %s of INSTALLER_TARGET_BUILD does not exist" % target_build)
320
321             if os.path.isdir(target_build):
322                 count += 1
323
324         # While do package management install
325         if count > 0:
326             target_images = d.getVar('INSTALLER_TARGET_IMAGE', True)
327             if not target_images:
328                 errmsg = "The INSTALLER_TARGET_BUILD is a dir, but not found INSTALLER_TARGET_IMAGE,\n"
329                 errmsg += "set INSTALLER_TARGET_IMAGE = '<target-image-pn>' to do RPMs install"
330                 raise bb.parse.SkipPackage(errmsg)
331
332             elif count != len(target_images.split()):
333                 errmsg = "The INSTALLER_TARGET_BUILD has %s build dirs: %s\n" % (count, target_builds)
334                 errmsg += "But INSTALLER_TARGET_IMAGE has %s build images: %s\n" % (len(target_images.split()), target_images)
335                 raise bb.parse.SkipPackage(errmsg)
336
337         # The count of INSTALLER_TARGET_BUILD and WRL_INSTALLER_CONF must match when set.
338         wrlinstaller_confs = d.getVar('WRL_INSTALLER_CONF', True)
339         if wrlinstaller_confs:
340             if len(wrlinstaller_confs.split()) != len(target_builds.split()):
341                 raise bb.parse.SkipPackage("The count of INSTALLER_TARGET_BUILD and WRL_INSTALLER_CONF not match!")
342             for wrlinstaller_conf in wrlinstaller_confs.split():
343                 if not os.path.exists(wrlinstaller_conf):
344                     raise bb.parse.SkipPackage("The installer conf %s in WRL_INSTALLER_CONF doesn't exist!" % wrlinstaller_conf)
345
346         # The count of INSTALLER_TARGET_BUILD and KICKSTART_FILE must match when set.
347         kickstart_files = d.getVar('KICKSTART_FILE', True)
348         if kickstart_files:
349             if len(kickstart_files.split()) != len(target_builds.split()):
350                 raise bb.parse.SkipPackage("The count of INSTALLER_TARGET_BUILD and KICKSTART_FILE not match!")
351             for kickstart_file in kickstart_files.split():
352                 if not os.path.exists(kickstart_file):
353                     raise bb.parse.SkipPackage("The kickstart file %s in KICKSTART_FILE doesn't exist!" % kickstart_file)
354
355 }
356
357 python build_syslinux_cfg () {
358     import copy
359     import sys
360
361     workdir = d.getVar('WORKDIR')
362     if not workdir:
363         bb.error("WORKDIR not defined, unable to package")
364         return
365
366     cfile = d.getVar('SYSLINUX_CFG')
367     if not cfile:
368         bb.fatal('Unable to read SYSLINUX_CFG')
369 }