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