image: ext4: allow creation of a journaling filesystem
[openwrt/staging/chunkeey.git] / include / image.mk
1 #
2 # Copyright (C) 2006-2010 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 override TARGET_BUILD=
9 include $(INCLUDE_DIR)/prereq.mk
10 include $(INCLUDE_DIR)/kernel.mk
11 include $(INCLUDE_DIR)/host.mk
12
13 .NOTPARALLEL:
14 override MAKEFLAGS=
15 override MAKE:=$(SUBMAKE)
16 KDIR=$(KERNEL_BUILD_DIR)
17
18 IMG_PREFIX:=openwrt-$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
19
20 MKFS_DEVTABLE_OPT := -D $(INCLUDE_DIR)/device_table.txt
21
22 ifneq ($(CONFIG_BIG_ENDIAN),)
23 JFFS2OPTS := --big-endian --squash-uids -v
24 else
25 JFFS2OPTS := --little-endian --squash-uids -v
26 endif
27
28 ifeq ($(CONFIG_JFFS2_RTIME),y)
29 JFFS2OPTS += -X rtime
30 endif
31 ifeq ($(CONFIG_JFFS2_ZLIB),y)
32 JFFS2OPTS += -X zlib
33 endif
34 ifeq ($(CONFIG_JFFS2_LZMA),y)
35 JFFS2OPTS += -X lzma --compression-mode=size
36 endif
37 ifneq ($(CONFIG_JFFS2_RTIME),y)
38 JFFS2OPTS += -x rtime
39 endif
40 ifneq ($(CONFIG_JFFS2_ZLIB),y)
41 JFFS2OPTS += -x zlib
42 endif
43 ifneq ($(CONFIG_JFFS2_LZMA),y)
44 JFFS2OPTS += -x lzma
45 endif
46
47 JFFS2OPTS += $(MKFS_DEVTABLE_OPT)
48
49 SQUASHFS_BLOCKSIZE := 256k
50 SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
51 SQUASHFSOPT += -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1'
52 SQUASHFSCOMP := gzip
53 LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
54 ifeq ($(CONFIG_SQUASHFS_LZMA),y)
55 SQUASHFSCOMP := lzma $(LZMA_XZ_OPTIONS)
56 endif
57 ifeq ($(CONFIG_SQUASHFS_XZ),y)
58 ifneq ($(filter arm x86 powerpc sparc,$(LINUX_KARCH)),)
59 BCJ_FILTER:=-Xbcj $(LINUX_KARCH)
60 endif
61 SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS) $(BCJ_FILTER)
62 endif
63
64 JFFS2_BLOCKSIZE ?= 64k 128k
65
66 define add_jffs2_mark
67 echo -ne '\xde\xad\xc0\xde' >> $(1)
68 endef
69
70 define toupper
71 $(shell echo $(1) | tr '[:lower:]' '[:upper:]')
72 endef
73
74 # pad to 4k, 8k, 16k, 64k, 128k, 256k and add jffs2 end-of-filesystem mark
75 define prepare_generic_squashfs
76 $(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
77 endef
78
79 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
80 define Image/BuildKernel/Initramfs
81 cp $(KDIR)/vmlinux-initramfs.elf $(BIN_DIR)/$(IMG_PREFIX)-vmlinux-initramfs.elf
82 $(call Image/Build/Initramfs)
83 endef
84 else
85 define Image/BuildKernel/Initramfs
86 endef
87 endif
88
89 define Image/BuildKernel/MkuImage
90 mkimage -A $(ARCH) -O linux -T kernel -C $(1) -a $(2) -e $(3) \
91 -n '$(call toupper,$(ARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $(4) $(5)
92 endef
93
94 define Image/BuildKernel/MkFIT
95 $(TOPDIR)/scripts/mkits.sh \
96 -D $(1) -o $(KDIR)/fit-$(1).its -k $(2) -d $(3) -C $(4) -a $(5) -e $(6) \
97 -A $(ARCH) -v $(LINUX_VERSION)
98 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $(KDIR)/fit-$(1).its $(KDIR)/fit-$(1)$(7).itb
99 endef
100
101 define Image/mkfs/jffs2/sub
102 # FIXME: removing this line will cause strange behaviour in the foreach loop below
103 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) --pad -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2) -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
104 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2)-raw -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
105 $(call add_jffs2_mark,$(KDIR)/root.jffs2-$(2))
106 $(call Image/Build,jffs2-$(2))
107 endef
108
109 ifneq ($(CONFIG_TARGET_ROOTFS_JFFS2),)
110 define Image/mkfs/jffs2
111 $(foreach SZ,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/sub,$(SZ),$(SZ),$(JFFS2OPTS)))
112 endef
113 endif
114
115 ifneq ($(CONFIG_TARGET_ROOTFS_JFFS2_NAND),)
116 define Image/mkfs/jffs2_nand
117 $(foreach SZ,$(NAND_BLOCKSIZE), $(call Image/mkfs/jffs2/sub, \
118 $(word 2,$(subst :, ,$(SZ))),nand-$(subst :,-,$(SZ)), \
119 $(JFFS2OPTS) --no-cleanmarkers --pagesize=$(word 1,$(subst :, ,$(SZ)))) \
120 )
121 endef
122 endif
123
124 ifneq ($(CONFIG_TARGET_ROOTFS_SQUASHFS),)
125 define Image/mkfs/squashfs
126 @mkdir -p $(TARGET_DIR)/overlay
127 $(STAGING_DIR_HOST)/bin/mksquashfs4 $(TARGET_DIR) $(KDIR)/root.squashfs -nopad -noappend -root-owned -comp $(SQUASHFSCOMP) $(SQUASHFSOPT) -processors $(if $(CONFIG_PKG_BUILD_JOBS),$(CONFIG_PKG_BUILD_JOBS),1)
128 $(call Image/Build,squashfs)
129 endef
130 endif
131
132 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
133 define Image/mkfs/ubifs/generate
134 $(CP) ./ubinize$(1).cfg $(KDIR)
135 ( cd $(KDIR); \
136 $(STAGING_DIR_HOST)/bin/ubinize \
137 $(if $($(PROFILE)_UBI_OPTS), \
138 $(shell echo $($(PROFILE)_UBI_OPTS)), \
139 $(shell echo $(UBI_OPTS)) \
140 ) \
141 -o $(KDIR)/root$(1).ubi \
142 ubinize$(1).cfg \
143 )
144 endef
145
146 define Image/mkfs/ubifs
147
148 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
149 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
150 $(if $($(PROFILE)_UBIFS_OPTS), \
151 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
152 $(shell echo $(UBIFS_OPTS)) \
153 ) \
154 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
155 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
156 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
157 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
158 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
159 --squash-uids \
160 -o $(KDIR)/root.ubifs \
161 -d $(TARGET_DIR)
162 endif
163 $(call Image/Build,ubifs)
164
165 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
166 $(call Image/mkfs/ubifs/generate,)
167 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
168 endif
169 $(call Image/Build,ubi)
170 endef
171 endif
172
173 ifneq ($(CONFIG_TARGET_ROOTFS_CPIOGZ),)
174 define Image/mkfs/cpiogz
175 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
176 endef
177 endif
178
179 ifneq ($(CONFIG_TARGET_ROOTFS_TARGZ),)
180 define Image/mkfs/targz
181 # Preserve permissions (-p) when building as non-root user
182 $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE),-$(PROFILE))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
183 endef
184 endif
185
186 ifneq ($(CONFIG_TARGET_ROOTFS_EXT4FS),)
187 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024/$(CONFIG_TARGET_EXT4_BLOCKSIZE))))
188
189 define Image/mkfs/ext4
190 # generate an ext2 fs
191 $(STAGING_DIR_HOST)/bin/genext2fs -U -B $(CONFIG_TARGET_EXT4_BLOCKSIZE) -b $(E2SIZE) -N $(CONFIG_TARGET_ROOTFS_MAXINODE) -d $(TARGET_DIR)/ $(KDIR)/root.ext4 -m $(CONFIG_TARGET_ROOTFS_RESERVED_PCT) $(MKFS_DEVTABLE_OPT)
192 # convert it to ext4
193 $(STAGING_DIR_HOST)/bin/tune2fs $(if $(CONFIG_TARGET_EXT4_JOURNAL),-j) -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
194 # fix it up
195 $(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
196 $(call Image/Build,ext4)
197 endef
198 endif
199
200 ifneq ($(CONFIG_TARGET_ROOTFS_ISO),)
201 define Image/mkfs/iso
202 $(call Image/Build,iso)
203 endef
204 endif
205
206
207 define Image/mkfs/prepare/default
208 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
209 - $(FIND) $(TARGET_DIR) -type f -not -perm +0100 -not -name 'ssh_host*' -not -name 'shadow' -print0 | $(XARGS) -0 chmod u+rw,g+r,o+r
210 - $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
211 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
212 $(INSTALL_DIR) $(TARGET_DIR)/tmp
213 chmod 1777 $(TARGET_DIR)/tmp
214 endef
215
216 define Image/mkfs/prepare
217 $(call Image/mkfs/prepare/default)
218 endef
219
220
221 define Image/Checksum
222 ( cd ${BIN_DIR} ; \
223 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs \
224 md5sum --binary > md5sums \
225 )
226 endef
227
228
229 define BuildImage
230
231 download:
232 prepare:
233
234 ifeq ($(IB),)
235 compile: compile-targets FORCE
236 $(call Build/Compile)
237 else
238 compile:
239 endif
240
241 ifeq ($(IB),)
242 install: compile install-targets FORCE
243 $(call Image/Prepare)
244 $(call Image/mkfs/prepare)
245 $(call Image/BuildKernel)
246 $(call Image/BuildKernel/Initramfs)
247 $(call Image/InstallKernel)
248 $(call Image/mkfs/cpiogz)
249 $(call Image/mkfs/targz)
250 $(call Image/mkfs/ext4)
251 $(call Image/mkfs/iso)
252 $(call Image/mkfs/jffs2)
253 $(call Image/mkfs/jffs2_nand)
254 $(call Image/mkfs/squashfs)
255 $(call Image/mkfs/ubifs)
256 $(call Image/Checksum)
257 else
258 install: compile install-targets
259 $(call Image/BuildKernel)
260 $(call Image/BuildKernel/Initramfs)
261 $(call Image/InstallKernel)
262 $(call Image/mkfs/cpiogz)
263 $(call Image/mkfs/targz)
264 $(call Image/mkfs/ext4)
265 $(call Image/mkfs/iso)
266 $(call Image/mkfs/jffs2)
267 $(call Image/mkfs/jffs2_nand)
268 $(call Image/mkfs/squashfs)
269 $(call Image/mkfs/ubifs)
270 $(call Image/Checksum)
271 endif
272
273 ifeq ($(IB),)
274 clean: clean-targets
275 $(call Build/Clean)
276 else
277 clean:
278 endif
279
280 compile-targets:
281 install-targets:
282 clean-targets:
283
284 endef