alpine.mk 8.67 KB
Newer Older
1 2
#!/usr/bin/make -f

3 4 5
PROFILE		?= alpine

-include $(PROFILE).conf.mk
6

7 8 9
BUILD_DATE	:= $(shell date +%y%m%d)
ALPINE_RELEASE	?= $(BUILD_DATE)
ALPINE_NAME	?= alpine-test
10
ALPINE_ARCH	:= x86
11
DESTDIR		?= $(shell pwd)/isotmp.$(PROFILE)
12

13
MKCRAMFS	= mkcramfs
14 15
SUDO		= sudo

16
ISO		?= $(ALPINE_NAME)-$(ALPINE_RELEASE)-$(ALPINE_ARCH).iso
17
ISO_LINK	?= $(ALPINE_NAME).iso
18
ISO_DIR		:= $(DESTDIR)/isofs
19
ISO_PKGDIR	:= $(ISO_DIR)/apks
20

21 22
APKS		?= $(shell sed 's/\#.*//; s/\*/\\*/g' $(PROFILE).packages)

23
APK_OPTS	:= $(addprefix --repository ,$(APK_REPOS)) --keys-dir /etc/apk/keys
24 25

find_apk_ver	= $(shell apk search $(APK_OPTS) $(1) | sort | uniq)
26 27
find_apk_file	= $(addsuffix .apk,$(call find_apk_ver,$(1)))
find_apk	= $(addprefix $(ISO_PKGDIR)/,$(call find_apk_file,$(1)))
28

29 30 31
# get apk does not support wildcards
get_apk         = $(addsuffix .apk,$(shell apk fetch --simulate $(APK_OPTS) $(1) 2>&1 | sed 's:^Downloading :$(ISO_PKGDIR)/:'))

32 33
KERNEL_FLAVOR	?= grsec
KERNEL_PKGNAME	?= linux-$(KERNEL_FLAVOR)
34
KERNEL_NAME	:= $(KERNEL_FLAVOR)
35
KERNEL_APK	:= $(call get_apk,$(KERNEL_PKGNAME))
36

37 38 39
KERNEL		:= $(word 3,$(subst -, ,$(notdir $(KERNEL_APK))))-$(word 2,$(subst -, ,$(notdir $(KERNEL_APK))))

ALPINEBASELAYOUT_APK := $(call find_apk,alpine-baselayout)
40 41 42 43
UCLIBC_APK	:= $(call get_apk,uclibc)
BUSYBOX_APK	:= $(call get_apk,busybox)
APK_TOOLS_APK	:= $(call get_apk,apk-tools)
STRACE_APK	:= $(call get_apk,strace)
44

45 46 47 48
APKS_FILTER	?= | grep -v -- '-dev$$' | grep -v 'sources'

APKS		?= '*'
APK_FILES	:= $(call find_apk,$(APKS))
49

50
all: isofs
51 52 53 54

help:
	@echo "Alpine ISO builder"
	@echo
55
	@echo "Type 'make iso' to build $(ISO)"
56 57 58
	@echo
	@echo "I will use the following sources files:"
	@echo " 1. $(notdir $(KERNEL_APK)) (looks like $(KERNEL))"
59
	@echo " 2. $(notdir $(MOD_APKS))"
60 61 62 63 64 65 66 67 68
	@echo " 3. $(notdir $(ALPINEBASELAYOUT_APK))"
	@echo " 4. $(notdir $(UCLIBC_APK))"
	@echo " 5. $(notdir $(BUSYBOX_APK))"
ifeq ($(APK_BIN),)
	@echo " 6. $(notdir $(APK_TOOLS_APK))"
else
	@echo " 6. $(APK_BIN)"
endif
	@echo
69 70 71
	@echo "ALPINE_NAME:    $(ALPINE_NAME)"
	@echo "ALPINE_RELEASE: $(ALPINE_RELEASE)"
	@echo "KERNEL_FLAVOR:  $(KERNEL_FLAVOR)"
72
	@echo "KERNEL:         $(KERNEL)"
73
	@echo
74

Natanael Copa's avatar
Natanael Copa committed
75 76 77
clean:
	rm -rf $(MODLOOP) $(MODLOOP_DIR) $(MODLOOP_DIRSTAMP) \
		$(INITFS) $(INITFS_DIRSTAMP) $(INITFS_DIR) \
Natanael Copa's avatar
Natanael Copa committed
78
		$(ISO_DIR) $(ISO_REPOS_DIRSTAMP)
79

80

81 82
$(APK_FILES):
	@mkdir -p "$(dir $@)";\
83 84 85 86
	p="$(notdir $(basename $@))";\
	apk fetch $(APK_REPO) -R -v -o "$(dir $@)" $${p%-[0-9]*}
#	apk fetch $(APK_OPTS) -R -v -o "$(dir $@)" \
#		`apk search -q $(APK_OPTS) $(APKS) | sort | uniq`
87

88 89 90
#
# Modloop
#
91
MODLOOP		:= $(ISO_DIR)/boot/$(KERNEL_NAME).cmg
92
MODLOOP_DIR	:= $(DESTDIR)/modloop
93
MODLOOP_KERNELSTAMP := $(DESTDIR)/stamp.modloop.kernel
94
MODLOOP_DIRSTAMP := $(DESTDIR)/stamp.modloop
95 96
MODLOOP_EXTRA	?= $(addsuffix -$(KERNEL_FLAVOR), dahdi-linux iscsitarget xtables-addons)
MODLOOP_PKGS	= $(KERNEL_PKGNAME) $(MODLOOP_EXTRA)
97

98 99
modloop: $(MODLOOP)

100
$(MODLOOP_KERNELSTAMP):
101
	@echo "==> modloop: Unpacking kernel modules";
102 103
	@rm -rf $(MODLOOP_DIR)
	@mkdir -p $(MODLOOP_DIR)/lib/modules/
104 105 106
	@for i in $(MODLOOP_PKGS); do \
		apk fetch $(APK_OPTS) --stdout $$i \
			| tar -C $(MODLOOP_DIR) -xz; \
107
	done
108
	@cp $(MODLOOP_DIR)/usr/share/kernel/$(KERNEL_FLAVOR)/kernel.release $@
109 110 111 112

MODLOOP_KERNEL_RELEASE = $(shell cat $(MODLOOP_KERNELSTAMP))

$(MODLOOP_DIRSTAMP): $(MODLOOP_KERNELSTAMP)
113
	@rm -rf $(addprefix $(MODLOOP_DIR)/lib/modules/*/, source build)
114
	@depmod $(MODLOOP_KERNEL_RELEASE) -b $(MODLOOP_DIR)
115 116 117 118 119
	@touch $(MODLOOP_DIRSTAMP)

$(MODLOOP): $(MODLOOP_DIRSTAMP)
	@echo "==> modloop: building image $(notdir $(MODLOOP))"
	@mkdir -p $(dir $(MODLOOP))
120
	@$(MKCRAMFS) $(MODLOOP_DIR)/lib $(MODLOOP)
121

122
clean-modloop:
123
	@rm -rf $(MODLOOP_DIR) $(MODLOOP_DIRSTAMP) $(MODLOOP_PKGSTAMP) $(MODLOOP)
124

125 126 127 128
#
# Initramfs rules
#

129 130 131 132
# isolinux cannot handle - in filenames
#INITFS_NAME	:= initramfs-$(MODLOOP_KERNEL_RELEASE)
INITFS_NAME	:= $(KERNEL_FLAVOR).gz
INITFS		:= $(ISO_DIR)/boot/$(INITFS_NAME)
133 134

INITFS_DIR	:= $(DESTDIR)/initfs
135 136
INITFS_TMP	:= $(DESTDIR)/tmp.initfs
INITFS_DIRSTAMP := $(DESTDIR)/stamp.initfs
Natanael Copa's avatar
Natanael Copa committed
137
INITFS_FEATURES	:= ata base bootchart cdrom cramfs ext3 floppy raid scsi usb
138
INITFS_PKGS	:= $(MODLOOP_PKGS) alpine-base acct
139

140
initfs: $(INITFS)
141 142 143 144 145 146 147 148 149

$(INITFS_DIRSTAMP):
	@rm -rf $(INITFS_DIR) $(INITFS_TMP)
	@mkdir -p $(INITFS_DIR) $(INITFS_TMP)
	@for i in `apk fetch $(APK_OPTS) --simulate -R $(INITFS_PKGS) 2>&1\
			| sed 's:^Downloading ::; s:-[0-9].*::' | sort | uniq`; do \
		apk fetch $(APK_OPTS) --stdout $$i \
			| tar -C $(INITFS_DIR) -zx || exit 1; \
	done
150
	@cp -r /etc/apk/keys $(INITFS_DIR)/etc/apk/ || true
151 152 153
	@touch $@

#$(INITFS):	$(shell mkinitfs -F "$(INITFS_FEATURES)" -l $(KERNEL))
154
$(INITFS): $(INITFS_DIRSTAMP) $(MODLOOP_DIRSTAMP)
155
	@mkinitfs -F "$(INITFS_FEATURES)" -t $(INITFS_TMP) \
156
		-b $(INITFS_DIR) -o $@ $(MODLOOP_KERNEL_RELEASE)
157 158 159

clean-initfs:
	@rm -rf $(INITFS) $(INITFS_DIRSTAMP) $(INITFS_DIR)
160

161 162 163 164 165 166 167 168 169
#
# Vserver template rules
#
VSTEMPLATE	:= $(ISO_DIR)/vs-template.tar.bz2
VSTEMPLATE_DIR 	:= $(DESTDIR)/vs-template

vstemplate: $(VSTEMPLATE)
	@echo "==> vstemplate: built $(VSTEMPLATE)"

170
#must be run as root or in fakeroot
171
$(VSTEMPLATE):
172 173 174
	@rm -rf "$(VSTEMPLATE_DIR)"
	@mkdir -p "$(VSTEMPLATE_DIR)"
	@apk add $(APK_OPTS) --initdb --root $(VSTEMPLATE_DIR) \
175
		alpine-base
176
	@cd $(VSTEMPLATE_DIR) && tar -jcf $@ *
177

178 179 180 181
#
# ISO rules
#

182 183
ISOLINUX_DIR	:= boot/isolinux
ISOLINUX	:= $(ISO_DIR)/$(ISOLINUX_DIR)
184 185
ISOLINUX_BIN	:= $(ISOLINUX)/isolinux.bin
ISOLINUX_CFG	:= $(ISOLINUX)/isolinux.cfg
186
SYSLINUX_CFG	:= $(ISO_DIR)/syslinux.cfg
187

188
$(ISOLINUX_BIN):
189 190
	@echo "==> iso: install isolinux"
	@mkdir -p $(dir $(ISOLINUX_BIN))
191 192 193
	@if ! apk fetch $(APK_REPO) --stdout syslinux | tar -O -zx usr/share/syslinux/isolinux.bin > $@; then \
		rm -f $@ && exit 1;\
	fi
194 195 196 197 198 199

$(ISOLINUX_CFG):
	@echo "==> iso: configure isolinux"
	@mkdir -p $(dir $(ISOLINUX_BIN))
	@echo "timeout 20" >$(ISOLINUX_CFG)
	@echo "prompt 1" >>$(ISOLINUX_CFG)
200 201
	@echo "default $(KERNEL_NAME)" >>$(ISOLINUX_CFG)
	@echo "label $(KERNEL_NAME)" >>$(ISOLINUX_CFG)
202
	@echo "	kernel /boot/$(KERNEL_NAME)" >>$(ISOLINUX_CFG)
203
	@echo "	append initrd=/boot/$(INITFS_NAME) alpine_dev=cdrom:iso9660 modules=loop,cramfs,sd-mod,usb-storage,floppy quiet" >>$(ISOLINUX_CFG)
204

205
$(SYSLINUX_CFG): $(MODLOOP_DIRSTAMP)
206 207 208 209 210 211
	@echo "==> iso: configure syslinux"
	@echo "timeout 20" >$@
	@echo "prompt 1" >>$@
	@echo "default $(KERNEL_NAME)" >>$@
	@echo "label $(KERNEL_NAME)" >>$@
	@echo "	kernel /boot/$(KERNEL_NAME)" >>$@
212
	@echo "	append initrd=/boot/$(INITFS_NAME) alpine_dev=usbdisk:vfat modules=loop,cramfs,sd-mod,usb-storage quiet" >>$@
213

214 215 216
clean-syslinux:
	@rm -f $(SYSLINUX_CFG) $(ISOLINUX_CFG) $(ISOLINUX_BIN)

217
ISO_KERNEL	:= $(ISO_DIR)/boot/$(KERNEL_NAME)
218
ISO_REPOS_DIRSTAMP := $(DESTDIR)/stamp.isorepos
219
ISOFS_DIRSTAMP	:= $(DESTDIR)/stamp.isofs
220

221
$(ISO_REPOS_DIRSTAMP): $(ISO_PKGDIR)/APKINDEX.tar.gz
222
	@touch $(ISO_PKGDIR)/.boot_repository
223
	@rm -f $(ISO_PKGDIR)/.SIGN.*
224 225
	@touch $@

226
$(ISO_PKGDIR)/APKINDEX.tar.gz: $(APK_FILES)
227
	@echo "==> iso: generating repository index"
228 229
	@apk index --description "$(ALPINE_NAME) $(ALPINE_RELEASE)" \
		-o $@ $(ISO_PKGDIR)/*.apk
230
	@abuild-sign $@
231

232
$(ISO_KERNEL): $(MODLOOP_DIRSTAMP)
233 234
	@echo "==> iso: install kernel $(KERNEL)"
	@mkdir -p $(dir $(ISO_KERNEL))
235 236
	@apk fetch $(APK_OPTS) --stdout $(KERNEL_PKGNAME) \
		| tar -C $(ISO_DIR) -xz boot
237 238
	@rm -f $(ISO_DIR)/boot/$(KERNEL_NAME)
	@ln -s vmlinuz-$(MODLOOP_KERNEL_RELEASE) $@
239
	@rm -rf $(ISO_DIR)/.[A-Z]* $(ISO_DIR)/.[a-z]* $(ISO_DIR)/lib
240

241
$(ISOFS_DIRSTAMP): $(MODLOOP) $(INITFS) $(ISOLINUX_CFG) $(ISOLINUX_BIN) $(ISO_KERNEL) $(ISO_REPOS_DIRSTAMP) $(SYSLINUX_CFG)
242 243
	@echo "$(ALPINE_NAME)-$(ALPINE_RELEASE) $(BUILD_DATE)" \
		> $(ISO_DIR)/.alpine-release
244 245 246
	@touch $@

$(ISO): $(ISOFS_DIRSTAMP)
247 248
	@echo "==> iso: building $(notdir $(ISO))"
	@genisoimage -o $(ISO) -l -J -R \
249 250
		-b $(ISOLINUX_DIR)/isolinux.bin \
		-c $(ISOLINUX_DIR)/boot.cat	\
251 252 253 254
		-no-emul-boot		\
		-boot-load-size 4	\
		-boot-info-table	\
		-quiet			\
255
		-follow-links		\
256
		$(ISO_OPTS)		\
257
		$(ISO_DIR)
258
	@ln -fs $@ $(ISO_LINK)
259

260
isofs: $(ISOFS_DIRSTAMP)
261
iso: $(ISO)
262 263 264 265

#
# SHA1 sum of ISO
#
266
ISO_SHA1	:= $(ISO).sha1
267

268
$(ISO_SHA1):	$(ISO)
269 270 271
	@echo "==> Generating sha1 sum"
	@sha1sum $(ISO) > $@ || rm -f $@

272 273 274 275
#
# USB image
#
USBIMG 		:= $(ALPINE_NAME)-$(ALPINE_RELEASE)-$(ALPINE_ARCH).img
276 277
USBIMG_FREE	?= 8192
USBIMG_SIZE 	= $(shell echo $$(( `du -s $(ISO_DIR) | awk '{print $$1}'` + $(USBIMG_FREE) )) )
278 279 280
MBRPATH 	:= /usr/share/syslinux/mbr.bin

$(USBIMG): $(ISOFS_DIRSTAMP)
281
	@echo "==> Generating $@"
282
	@mformat -C -v 'ALPINE' -c 16 -h 64 -n 32 -i $(USBIMG) \
283
		-t $$(($(USBIMG_SIZE) / 1000)) ::
284
	@syslinux $(USBIMG)
285 286 287
	@mcopy -i $(USBIMG) $(ISO_DIR)/* $(ISO_DIR)/.[a-z]* ::
	@mcopy -i $(USBIMG) /dev/zero ::/zero 2>/dev/null || true
	@mdel -i $(USBIMG) ::/zero
288 289 290 291 292 293

USBIMG_SHA1	:= $(USBIMG).sha1
$(USBIMG_SHA1):	$(USBIMG)
	@echo "==> Generating sha1 sum"
	@sha1sum $(USBIMG) > $@ || rm -f $@

294 295 296
$(ALPINE_NAME).img:	$(USBIMG)
	@ln -sf $(USBIMG) $@

297 298
img:	$(ALPINE_NAME).img

299 300 301 302
sha1: $(ISO_SHA1) $(USBIMG_SHA1)

release: $(ISO_SHA1)