alpine.mk 7.04 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
SUDO		= sudo

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

18 19 20
APK_OPTS	:= $(addprefix --repo ,$(APK_REPOS))

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

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

27 28
KERNEL_FLAVOR	?= grsec
KERNEL_PKGNAME	?= linux-$(KERNEL_FLAVOR)
29
KERNEL_NAME	:= $(KERNEL_FLAVOR)
30
KERNEL_APK	:= $(call get_apk,$(KERNEL_PKGNAME))
31

32 33 34 35
XTABLES_ADDONS_APK:= $(call get_apk,xtables-addons-$(KERNEL_FLAVOR))
DAHDI_LINUX_APK	:= $(call get_apk,dahdi-linux-$(KERNEL_FLAVOR))
ISCSITARGET_APK	:= $(call get_apk,iscsitarget-$(KERNEL_FLAVOR))
MOD_APKS	:= $(KERNEL_APK) $(XTABLES_ADDONS_APK) $(DAHDI_LINUX_APK) \
36
		   $(ISCSITARGET_APK)
37

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

ALPINEBASELAYOUT_APK := $(call find_apk,alpine-baselayout)
41 42 43 44
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)
45

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

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

51
all: isofs
52 53 54 55

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

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

81

82 83
$(APK_FILES):
	@mkdir -p "$(dir $@)";\
84 85 86 87
	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`
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)

Natanael Copa's avatar
Natanael Copa committed
98
$(MODLOOP_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):
153 154
	@echo "==> iso: install isolinux"
	@mkdir -p $(dir $(ISOLINUX_BIN))
155 156 157
	@if ! apk fetch $(APK_REPO) --stdout syslinux | tar -O -zx usr/share/syslinux/isolinux.bin > $@; then \
		rm -f $@ && exit 1;\
	fi
158 159 160 161 162 163

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

169 170 171 172 173 174 175
$(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)" >>$@
176
	@echo "	append initrd=/boot/$(KERNEL_NAME).gz alpine_dev=usbdisk:vfat modules=loop,cramfs,sd-mod,usb-storage quiet" >>$@
177

178
ISO_KERNEL	:= $(ISO_DIR)/boot/$(KERNEL_NAME)
179
ISO_REPOS_DIRSTAMP := $(DESTDIR)/stamp.isorepos
180
ISOFS_DIRSTAMP	:= $(DESTDIR)/stamp.isofs
181

182
$(ISO_REPOS_DIRSTAMP): $(ISO_PKGDIR)/APK_INDEX.gz
183 184
	@touch $@

185 186
$(ISO_PKGDIR)/APK_INDEX.gz: $(APK_FILES)
	@echo "==> iso: generating repository index"
187
	@apk index $(ISO_PKGDIR)/*.apk | gzip > $@
188 189 190 191

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

196 197 198 199
$(ISOFS_DIRSTAMP): $(MODLOOP) $(INITFS) $(ISOLINUX_CFG) $(ISOLINUX_BIN) $(ISO_KERNEL) $(ISO_REPOS_DIRSTAMP) $(SYSLINUX_CFG)
	@touch $@

$(ISO): $(ISOFS_DIRSTAMP)
200
	@echo "==> iso: building $(notdir $(ISO))"
201 202
	@echo "$(ALPINE_NAME)-$(ALPINE_RELEASE) $(BUILD_DATE)" \
		> $(ISO_DIR)/.alpine-release
203 204 205 206 207 208 209
	@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			\
210
		-follow-links		\
211
		$(ISO_OPTS)		\
212
		$(ISO_DIR)
213
	@ln -fs $@ $(ISO_LINK)
214

215
isofs: $(ISOFS_DIRSTAMP)
216
iso: $(ISO)
217 218 219 220

#
# SHA1 sum of ISO
#
221
ISO_SHA1	:= $(ISO).sha1
222

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

227 228 229 230
#
# USB image
#
USBIMG 		:= $(ALPINE_NAME)-$(ALPINE_RELEASE)-$(ALPINE_ARCH).img
231 232
USBIMG_FREE	?= 8192
USBIMG_SIZE 	= $(shell echo $$(( `du -s $(ISO_DIR) | awk '{print $$1}'` + $(USBIMG_FREE) )) )
233 234 235
MBRPATH 	:= /usr/share/syslinux/mbr.bin

$(USBIMG): $(ISOFS_DIRSTAMP)
236 237
	@echo "==> Generating $@"
	@dd if=/dev/zero of=$(USBIMG) bs=1024 count=$(USBIMG_SIZE)
238 239
	@mkfs.vfat $(USBIMG) >/dev/null
	@syslinux $(USBIMG)
240
	@MTOOLS_SKIP_CHECK=1 mcopy -i $(USBIMG) $(ISO_DIR)/* $(ISO_DIR)/.[a-z]* ::
241 242 243 244 245 246

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

247 248 249
$(ALPINE_NAME).img:	$(USBIMG)
	@ln -sf $(USBIMG) $@

250 251
img:	$(ALPINE_NAME).img

252
sha1 release: $(ISO_SHA1) $(USBIMG_SHA1)