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

3
4
-include alpine.conf.mk

5
6
7
BUILD_DATE	:= $(shell date +%y%m%d)
ALPINE_RELEASE	?= $(BUILD_DATE)
ALPINE_NAME	?= alpine-test
8
ALPINE_ARCH	:= i386
9
DESTDIR		?= $(shell pwd)/isotmp
10

11
12
REPOS		?= core extra

13
14
SUDO		= sudo

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

20
21
22
find_apk_ver	= $(shell apk search $(1) | sort | uniq)
find_apk_file	= $(addsuffix .apk,$(call find_apk_ver,$(1)))
find_apk	= $(addprefix $(ISO_PKGDIR)/,$(call find_apk_file,$(1)))
23

24
25
KERNEL_FLAVOR	?= grsec
KERNEL_PKGNAME	?= linux-$(KERNEL_FLAVOR)
26
KERNEL_NAME	:= $(KERNEL_FLAVOR)
27
KERNEL_APK	:= $(call find_apk,$(KERNEL_PKGNAME))
28
MODULE_APK	:= $(subst /$(KERNEL_PKGNAME)-,/$(KERNEL_PKGNAME)-mod-,$(KERNEL_APK))
29
30

XTABLES_ADDONS_APK:= $(subst xtables-addons,xtables-addons-$(KERNEL_FLAVOR),$(call find_apk,xtables-addons))
31
DAHDI_LINUX_APK:= $(subst dahdi-linux,dahdi-linux-$(KERNEL_FLAVOR),$(call find_apk,dahdi-linux))
32
33
34
ISCSITARGET_APK:= $(subst iscsitarget,iscsitarget-$(KERNEL_FLAVOR),$(call find_apk,iscsitarget))
MOD_APKS	:= $(MODULE_APK) $(XTABLES_ADDONS_APK) $(DAHDI_LINUX_APK) \
		   $(ISCSITARGET_APK)
35

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

ALPINEBASELAYOUT_APK := $(call find_apk,alpine-baselayout)
UCLIBC_APK	:= $(call find_apk,uclibc)
BUSYBOX_APK	:= $(call find_apk,busybox)
APK_TOOLS_APK	:= $(call find_apk,apk-tools)
42
43
SYSLINUX_APK	:= $(call find_apk,syslinux)
STRACE_APK	:= $(call find_apk,strace)
44
ACCT_APK	:= $(call find_apk,acct)
45

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

APKS		?= '*'
APK_FILES	:= $(call find_apk,$(APKS))
APK_REPO	?= --repo /var/cache/abuild/apks
51

52
all: isofs
53
54
55
56

help:
	@echo "Alpine ISO builder"
	@echo
57
	@echo "Type 'make iso' to build $(ISO)"
58
59
60
61
62
63
64
65
66
67
68
69
70
	@echo
	@echo "I will use the following sources files:"
	@echo " 1. $(notdir $(KERNEL_APK)) (looks like $(KERNEL))"
	@echo " 2. $(notdir $(MODULE_APK))"
	@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
71
72
73
	@echo "ALPINE_NAME:    $(ALPINE_NAME)"
	@echo "ALPINE_RELEASE: $(ALPINE_RELEASE)"
	@echo "KERNEL_FLAVOR:  $(KERNEL_FLAVOR)"
74
	@echo "KERNEL:         $(KERNEL)"
75
	@echo
76

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

82

83
84
85
86
87
$(APK_FILES):
	@mkdir -p "$(dir $@)";\
	p="$(notdir $(basename $@))";\
	apk fetch $(APK_REPO) -R -v -o "$(dir $@)" $${p%-[0-9]*}
#	apk fetch --repo /var/cache/abuild/apks -v -R -o $(ISO_PKGDIR) \
88

89
90
91
#
# Modloop
#
92
MODLOOP		:= $(ISO_DIR)/boot/$(KERNEL_NAME).cmg
93
94
95
MODLOOP_DIR	:= $(DESTDIR)/modloop
MODLOOP_DIRSTAMP := $(DESTDIR)/stamp.modloop

96
97
modloop: $(MODLOOP)

98
$(MODLOOP_DIRSTAMP): $(REPOS_DIRSTAMP) $(MOD_APKS)
99
100
	@rm -rf $(MODLOOP_DIR)
	@mkdir -p $(MODLOOP_DIR)/lib/modules/
101
102
103
104
	@for i in $(MOD_APKS); do \
		echo "==> modloop: prepare modules $$i";\
		tar -C $(MODLOOP_DIR) -xzf "$$i"; \
	done
105
106
107
108
109
110
111
112
113
114
115
116
117
	@rm -rf $(addprefix $(MODLOOP_DIR)/lib/modules/*/, source build)
	@depmod $(KERNEL) -b $(MODLOOP_DIR)
	@touch $(MODLOOP_DIRSTAMP)

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

#
# Initramfs rules
#

118
INITFS		:= $(ISO_DIR)/boot/$(KERNEL_NAME).gz
119
120

INITFS_DIR	:= $(DESTDIR)/initfs
121
INITFS_FEATURES	:= ata base bootchart cdrom cramfs ext3 ide floppy raid scsi usb
122

123
initfs: $(INITFS)
124
125
126
$(INITFS):	$(shell mkinitfs -F "$(INITFS_FEATURES)" -l $(KERNEL))
	@mkinitfs -F "$(INITFS_FEATURES)" \
		-t $(INITFS_DIR) -o $@ $(KERNEL)
127

128
129
130
131
132
133
134
135
136
137
138
139
#
# Vserver template rules
#
VSTEMPLATE	:= $(ISO_DIR)/vs-template.tar.bz2
VSTEMPLATE_DIR 	:= $(DESTDIR)/vs-template

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

$(VSTEMPLATE):
	@$(SUDO) rm -rf "$(VSTEMPLATE_DIR)"
	@$(SUDO) mkdir -p "$(VSTEMPLATE_DIR)"
140
	@$(SUDO) apk add --initdb --root $(VSTEMPLATE_DIR) alpine-baselayout openrc busybox
141
142
	@cd $(VSTEMPLATE_DIR) && $(SUDO) tar -jcf $@ *

143
144
145
146
147
148
149
#
# ISO rules
#

ISOLINUX	:= $(ISO_DIR)/isolinux
ISOLINUX_BIN	:= $(ISOLINUX)/isolinux.bin
ISOLINUX_CFG	:= $(ISOLINUX)/isolinux.cfg
150
SYSLINUX_CFG	:= $(ISO_DIR)/syslinux.cfg
151

152
$(ISOLINUX_BIN): $(SYSLINUX_APK)
153
154
	@echo "==> iso: install isolinux"
	@mkdir -p $(dir $(ISOLINUX_BIN))
155
156
157
158
	@for i in $(SYSLINUX_APK); do \
		[ -f "$$i" ] || continue ;\
		tar -O -zxf "$$i" usr/share/syslinux/isolinux.bin > $@ && exit 0;\
	done ; exit 1
159
160
161
162
163
164

$(ISOLINUX_CFG):
	@echo "==> iso: configure isolinux"
	@mkdir -p $(dir $(ISOLINUX_BIN))
	@echo "timeout 20" >$(ISOLINUX_CFG)
	@echo "prompt 1" >>$(ISOLINUX_CFG)
165
166
	@echo "default $(KERNEL_NAME)" >>$(ISOLINUX_CFG)
	@echo "label $(KERNEL_NAME)" >>$(ISOLINUX_CFG)
167
	@echo "	kernel /boot/$(KERNEL_NAME)" >>$(ISOLINUX_CFG)
168
	@echo "	append initrd=/boot/$(KERNEL_NAME).gz alpine_dev=cdrom:iso9660 modules=sd-mod,usb-storage,floppy quiet" >>$(ISOLINUX_CFG)
169

170
171
172
173
174
175
176
$(SYSLINUX_CFG):
	@echo "==> iso: configure syslinux"
	@echo "timeout 20" >$@
	@echo "prompt 1" >>$@
	@echo "default $(KERNEL_NAME)" >>$@
	@echo "label $(KERNEL_NAME)" >>$@
	@echo "	kernel /boot/$(KERNEL_NAME)" >>$@
177
	@echo "	append initrd=/boot/$(KERNEL_NAME).gz alpine_dev=sda1:vfat modules=sd-mod,usb-storage quiet" >>$@
178

179
ISO_KERNEL	:= $(ISO_DIR)/boot/$(KERNEL_NAME)
180
181
182
ISO_REPOS	:= $(addprefix $(ISO_PKGDIR)/,$(REPOS))
ISO_APKINDEX	:= $(addsuffix /APK_INDEX.gz,$(ISO_REPOS))
ISO_REPOS_DIRSTAMP := $(DESTDIR)/stamp.isorepos
183
ISOFS_DIRSTAMP	:= $(DESTDIR)/stamp.isofs
184

185
$(ISO_REPOS_DIRSTAMP): $(ISO_PKGDIR)/APK_INDEX.gz
186
187
	@touch $@

188
189
190
$(ISO_PKGDIR)/APK_INDEX.gz: $(APK_FILES)
	@echo "==> iso: generating repository index"
	@apk index $(APK_FILES) | gzip > $@
191
192
193
194

$(ISO_KERNEL): $(KERNEL_APK)
	@echo "==> iso: install kernel $(KERNEL)"
	@mkdir -p $(dir $(ISO_KERNEL))
195
196
	@tar -C $(ISO_DIR) -xzf $(KERNEL_APK)
	@rm -f $(ISO_DIR)/.[A-Z]*
197
198
	@touch $(ISO_KERNEL)

199
200
201
202
$(ISOFS_DIRSTAMP): $(MODLOOP) $(INITFS) $(ISOLINUX_CFG) $(ISOLINUX_BIN) $(ISO_KERNEL) $(ISO_REPOS_DIRSTAMP) $(SYSLINUX_CFG)
	@touch $@

$(ISO): $(ISOFS_DIRSTAMP)
203
	@echo "==> iso: building $(notdir $(ISO))"
204
205
	@echo "$(ALPINE_NAME)-$(ALPINE_RELEASE) $(BUILD_DATE)" \
		> $(ISO_DIR)/.alpine-release
206
207
208
209
210
211
212
	@genisoimage -o $(ISO) -l -J -R \
		-b isolinux/isolinux.bin \
		-c isolinux/boot.cat	\
		-no-emul-boot		\
		-boot-load-size 4	\
		-boot-info-table	\
		-quiet			\
213
		$(ISO_OPTS)		\
214
		$(ISO_DIR)
215
	@ln -fs $@ $(ISO_LINK)
216

217
isofs: $(ISOFS_DIRSTAMP)
218
iso: $(ISO)
219
220
221
222

#
# SHA1 sum of ISO
#
223
ISO_SHA1	:= $(ISO).sha1
224

225
$(ISO_SHA1):	$(ISO)
226
227
228
	@echo "==> Generating sha1 sum"
	@sha1sum $(ISO) > $@ || rm -f $@

229
230
231
232
#
# USB image
#
USBIMG 		:= $(ALPINE_NAME)-$(ALPINE_RELEASE)-$(ALPINE_ARCH).img
233
USBIMG_SIZE 	:= $(shell echo $$(( `du -s $(ISO_DIR) | awk '{print $$1}'` + 8192 )) )
234
235
236
237
238
239
MBRPATH 	:= /usr/share/syslinux/mbr.bin
# the offset where the frist partition is found
USBIMG_OFFSET	:= 16384

$(USBIMG): $(ISOFS_DIRSTAMP)
	#Creating imagefile
240
	dd if=/dev/zero of=$(USBIMG) bs=1024 count=$(USBIMG_SIZE)
241
	parted -s $(USBIMG) mklabel msdos
242
	parted -s $(USBIMG) mkpartfs primary fat32 0 $$(( $(USBIMG_SIZE) * 1024 / 1000000))
243
	parted -s $(USBIMG) set 1 boot on
244
	dd if=$(MBRPATH) of=$(USBIMG) conv=notrunc
245
246
247
248
249
250
251
252
253
	syslinux -o $(USBIMG_OFFSET) $(USBIMG)
	mcopy -i $(USBIMG)@@$(USBIMG_OFFSET) $(ISO_DIR)/* $(ISO_DIR)/.[a-z]* ::

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

sha1 release: $(ISO_SHA1) $(USBIMG_SHA1)