Merge branch 'org.openembedded.dev' of ssh+git://git@git.openembedded.net/openembedde...
[openembedded.git] / classes / image.bbclass
1 inherit rootfs_${IMAGE_PKGTYPE}
2 inherit kernel-arch
3
4 LICENSE = "MIT"
5 PACKAGES = ""
6
7 #
8 # udev, devfsd, busybox-mdev (from busybox) or none
9 #
10 IMAGE_DEV_MANAGER ?= "${@base_contains("MACHINE_FEATURES", "kernel26",  "udev","",d)} "
11 #
12 # sysvinit, upstart
13 #
14 IMAGE_INIT_MANAGER ?= "sysvinit sysvinit-pidof"
15 IMAGE_INITSCRIPTS ?= "initscripts"
16 #
17 # tinylogin, getty
18 #
19 IMAGE_LOGIN_MANAGER ?= "tinylogin"
20
21 # set sane default for the SPLASH variable
22 SPLASH ?= ""
23
24 IMAGE_KEEPROOTFS ?= ""
25 IMAGE_KEEPROOTFS[doc] = "Set to non-empty to keep ${IMAGE_ROOTFS} around after image creation."
26
27 IMAGE_BOOT ?= "${IMAGE_INITSCRIPTS} \
28 ${IMAGE_DEV_MANAGER} \
29 ${IMAGE_INIT_MANAGER} \
30 ${IMAGE_LOGIN_MANAGER} "
31
32 RDEPENDS += "${IMAGE_INSTALL} ${IMAGE_BOOT}"
33
34 # "export IMAGE_BASENAME" not supported at this time
35 IMAGE_BASENAME[export] = "1"
36 export PACKAGE_INSTALL ?= "${IMAGE_INSTALL} ${IMAGE_BOOT}"
37
38 # We need to recursively follow RDEPENDS and RRECOMMENDS for images
39 do_rootfs[recrdeptask] += "do_deploy do_populate_staging"
40
41 # Images are generally built explicitly, do not need to be part of world.
42 EXCLUDE_FROM_WORLD = "1"
43
44 USE_DEVFS ?= "0"
45
46 PID = "${@os.getpid()}"
47
48 PACKAGE_ARCH = "${MACHINE_ARCH}"
49
50 do_rootfs[depends] += "makedevs-native:do_populate_staging fakeroot-native:do_populate_staging"
51
52 python () {
53     import bb
54
55     deps = bb.data.getVarFlag('do_rootfs', 'depends', d) or ""
56     for type in (bb.data.getVar('IMAGE_FSTYPES', d, True) or "").split():
57         for dep in ((bb.data.getVar('IMAGE_DEPENDS_%s' % type, d) or "").split() or []):
58             deps += " %s:do_populate_staging" % dep
59     for dep in (bb.data.getVar('EXTRA_IMAGEDEPENDS', d, True) or "").split():
60         deps += " %s:do_populate_staging" % dep
61     bb.data.setVarFlag('do_rootfs', 'depends', deps, d)
62
63     runtime_mapping_rename("PACKAGE_INSTALL", d)
64 }
65
66 #
67 # Get a list of files containing tables of devices to be created.
68 # * IMAGE_DEVICE_TABLE is the old name to an absolute path to a device table file
69 # * IMAGE_DEVICE_TABLES is a new name for a file, or list of files, searched
70 #   for in the BBPATH
71 # If neither are specified then the default name of files/device_table-minimal.txt
72 # is searched for in the BBPATH (same as the old version.)
73 #
74 def get_devtable_list(d):
75     import bb
76     devtable = bb.data.getVar('IMAGE_DEVICE_TABLE', d, 1)
77     if devtable != None:
78         return devtable
79     devtables = bb.data.getVar('IMAGE_DEVICE_TABLES', d, 1)
80     if devtables == None:
81         devtables = 'files/device_table-minimal.txt'
82     return " ".join([ bb.which(bb.data.getVar('BBPATH', d, 1), devtable)
83                       for devtable in devtables.split() ])
84
85 def get_imagecmds(d):
86     import bb
87     cmds = "\n"
88     old_overrides = bb.data.getVar('OVERRIDES', d, 0)
89     for type in bb.data.getVar('IMAGE_FSTYPES', d, True).split():
90         localdata = bb.data.createCopy(d)
91         bb.data.setVar('OVERRIDES', '%s:%s' % (type, old_overrides), localdata)
92         bb.data.update_data(localdata)
93         cmd  = "\t#Code for image type " + type + "\n"
94         cmd += "\t${IMAGE_CMD_" + type + "}\n"
95         cmd += "\tcd ${DEPLOY_DIR_IMAGE}/\n"
96         cmd += "\trm -f ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}." + type + "\n"
97         cmd += "\tln -s ${IMAGE_NAME}.rootfs." + type + " ${DEPLOY_DIR_IMAGE}/${IMAGE_LINK_NAME}." + type + "\n\n"
98         cmds += bb.data.expand(cmd, localdata)
99     return cmds
100
101 IMAGE_POSTPROCESS_COMMAND ?= ""
102 MACHINE_POSTPROCESS_COMMAND ?= ""
103 ROOTFS_POSTPROCESS_COMMAND ?= ""
104
105 # some default locales
106 IMAGE_LINGUAS ?= "de-de fr-fr en-gb"
107
108 LINGUAS_INSTALL = "${@" ".join(map(lambda s: "locale-base-%s" % s, bb.data.getVar('IMAGE_LINGUAS', d, 1).split()))}"
109
110 do_rootfs[nostamp] = "1"
111 do_rootfs[dirs] = "${TOPDIR}"
112 do_build[nostamp] = "1"
113
114 # Must call real_do_rootfs() from inside here, rather than as a separate
115 # task, so that we have a single fakeroot context for the whole process.
116 fakeroot do_rootfs () {
117         set -x
118         rm -rf ${IMAGE_ROOTFS}
119         mkdir -p ${IMAGE_ROOTFS}
120         mkdir -p ${DEPLOY_DIR_IMAGE}
121
122         if [ "${USE_DEVFS}" != "1" ]; then
123                 for devtable in ${@get_devtable_list(d)}; do
124                         makedevs -r ${IMAGE_ROOTFS} -D $devtable
125                 done
126         fi
127
128         rootfs_${IMAGE_PKGTYPE}_do_rootfs
129
130         insert_feed_uris
131
132         ${IMAGE_PREPROCESS_COMMAND}
133
134         ROOTFS_SIZE=`du -ks ${IMAGE_ROOTFS}|awk '{size = ${IMAGE_EXTRA_SPACE} + $1; print (size > ${IMAGE_ROOTFS_SIZE} ? size : ${IMAGE_ROOTFS_SIZE}) }'`
135         ${@get_imagecmds(d)}
136
137         ${IMAGE_POSTPROCESS_COMMAND}
138         
139         ${MACHINE_POSTPROCESS_COMMAND}
140         ${@['rm -rf ${IMAGE_ROOTFS}', ''][bool(d.getVar("IMAGE_KEEPROOTFS", 1))]}
141 }
142
143 do_deploy_to[nostamp] = "1"
144 do_deploy_to () {
145         # A standalone task to deploy built image to the location specified
146         # by DEPLOY_TO variable (likely passed via environment).
147         # Assumes ${IMAGE_FSTYPES} is a single value!
148         cp "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.${IMAGE_FSTYPES}" ${DEPLOY_TO}
149 }
150
151 insert_feed_uris () {
152         
153         echo "Building feeds for [${DISTRO}].."
154
155         for line in ${FEED_URIS}
156         do
157                 # strip leading and trailing spaces/tabs, then split into name and uri
158                 line_clean="`echo "$line"|sed 's/^[ \t]*//;s/[ \t]*$//'`"
159                 feed_name="`echo "$line_clean" | sed -n 's/\(.*\)##\(.*\)/\1/p'`"
160                 feed_uri="`echo "$line_clean" | sed -n 's/\(.*\)##\(.*\)/\2/p'`"
161                 
162                 echo "Added $feed_name feed with URL $feed_uri"
163                 
164                 # insert new feed-sources
165                 echo "src/gz $feed_name $feed_uri" >> ${IMAGE_ROOTFS}/etc/opkg/${feed_name}-feed.conf
166         done
167
168         # Allow to use package deploy directory contents as quick devel-testing
169         # feed. This creates individual feed configs for each arch subdir of those
170         # specified as compatible for the current machine.
171         # NOTE: Development-helper feature, NOT a full-fledged feed.
172         if [ -n "${FEED_DEPLOYDIR_BASE_URI}" ]; then
173             for arch in ${PACKAGE_ARCHS}
174             do
175                 echo "src/gz local-$arch ${FEED_DEPLOYDIR_BASE_URI}/$arch" >> ${IMAGE_ROOTFS}/etc/opkg/local-$arch-feed.conf
176             done
177         fi
178 }
179
180 log_check() {
181         set +x
182         for target in $*
183         do
184                 lf_path="${WORKDIR}/temp/log.do_$target.${PID}"
185                 
186                 echo "log_check: Using $lf_path as logfile"
187                 
188                 if test -e "$lf_path"
189                 then
190                         rootfs_${IMAGE_PKGTYPE}_log_check $target $lf_path
191                 else
192                         echo "Cannot find logfile [$lf_path]"
193                 fi
194                 echo "Logfile is clean"
195         done
196
197         set -x
198 }
199
200 # set '*' as the rootpassword so the images
201 # can decide if they want it or not
202
203 zap_root_password () {
204         sed 's%^root:[^:]*:%root:*:%' < ${IMAGE_ROOTFS}/etc/passwd >${IMAGE_ROOTFS}/etc/passwd.new
205         mv ${IMAGE_ROOTFS}/etc/passwd.new ${IMAGE_ROOTFS}/etc/passwd
206
207
208 create_etc_timestamp() {
209         date +%2m%2d%2H%2M%Y >${IMAGE_ROOTFS}/etc/timestamp
210 }
211
212 # Turn any symbolic /sbin/init link into a file
213 remove_init_link () {
214         if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
215                 LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
216                 rm ${IMAGE_ROOTFS}/sbin/init
217                 cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
218         fi
219 }
220
221 make_zimage_symlink_relative () {
222         if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
223                 (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
224         fi
225 }
226
227 # Make login manager(s) enable automatic login.
228 # Useful for devices where we do not want to log in at all (e.g. phones)
229 set_image_autologin () {
230         sed -i 's%^AUTOLOGIN=\"false"%AUTOLOGIN="true"%g' ${IMAGE_ROOTFS}/etc/sysconfig/gpelogin
231 }
232
233 # Can be use to create /etc/timestamp during image construction to give a reasonably 
234 # sane default time setting
235 rootfs_update_timestamp () {
236         date "+%m%d%H%M%Y" >${IMAGE_ROOTFS}/etc/timestamp
237 }
238
239 # Install locales into image for every entry in IMAGE_LINGUAS
240 install_linguas() {
241 if [ -e ${IMAGE_ROOTFS}/usr/bin/opkg-cl ] ; then
242         OPKG="opkg-cl ${IPKG_ARGS}"
243
244         ${OPKG} update || true
245         ${OPKG} list_installed | awk '{print $1}' |sort | uniq > /tmp/installed-packages
246
247         for i in $(cat /tmp/installed-packages | grep -v locale) ; do
248                 for translation in ${IMAGE_LINGUAS}; do
249                         translation_split=$(echo ${translation} | awk -F '-' '{print $1}')
250                         echo ${i}-locale-${translation}
251                         echo ${i}-locale-${translation_split}
252                 done
253         done | sort | uniq > /tmp/wanted-locale-packages
254
255         ${OPKG} list | awk '{print $1}' |grep locale |sort | uniq > /tmp/available-locale-packages
256
257         cat /tmp/wanted-locale-packages /tmp/available-locale-packages | sort | uniq -d > /tmp/pending-locale-packages
258
259         cat /tmp/pending-locale-packages | xargs ${OPKG} -nodeps install
260         rm -f ${IMAGE_ROOTFS}${libdir}/opkg/lists/*
261
262     for i in ${IMAGE_ROOTFS}${libdir}/opkg/info/*.preinst; do
263         if [ -f $i ] && ! sh $i; then
264             opkg-cl ${IPKG_ARGS} flag unpacked `basename $i .preinst`
265         fi
266     done
267
268     for i in ${IMAGE_ROOTFS}${libdir}/opkg/info/*.postinst; do
269         if [ -f $i ] && ! sh $i configure; then
270             opkg-cl ${IPKG_ARGS} flag unpacked `basename $i .postinst`
271         fi
272     done
273
274 fi
275 }
276
277 # export the zap_root_password, create_etc_timestamp and remote_init_link
278 EXPORT_FUNCTIONS zap_root_password create_etc_timestamp remove_init_link do_rootfs make_zimage_symlink_relative set_image_autologin rootfs_update_timestamp install_linguas
279
280 addtask rootfs before do_build after do_install
281 addtask deploy_to after do_rootfs