alpine.mk 7.85 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
MKCRAMFS	= mkcramfs
12 13
SUDO		= sudo

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

19
APK_OPTS	:= $(addprefix --repository ,$(APK_REPOS)) --keys-dir /etc/apk/keys
20 21

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

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

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

33 34 35
KERNEL		:= $(word 3,$(subst -, ,$(notdir $(KERNEL_APK))))-$(word 2,$(subst -, ,$(notdir $(KERNEL_APK))))

ALPINEBASELAYOUT_APK := $(call find_apk,alpine-baselayout)
36 37 38 39
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)
40

41 42 43 44
APKS_FILTER	?= | grep -v -- '-dev$$' | grep -v 'sources'

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

46
all: isofs
47 48 49 50

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

Natanael Copa's avatar
Natanael Copa committed
71 72 73
clean:
	rm -rf $(MODLOOP) $(MODLOOP_DIR) $(MODLOOP_DIRSTAMP) \
		$(INITFS) $(INITFS_DIRSTAMP) $(INITFS_DIR) \
Natanael Copa's avatar
Natanael Copa committed
74
		$(ISO_DIR) $(ISO_REPOS_DIRSTAMP)
75

76

77 78
$(APK_FILES):
	@mkdir -p "$(dir $@)";\
79 80 81 82
	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`
83

84 85 86
#
# Modloop
#
87
MODLOOP		:= $(ISO_DIR)/boot/$(KERNEL_NAME).cmg
88 89
MODLOOP_DIR	:= $(DESTDIR)/modloop
MODLOOP_DIRSTAMP := $(DESTDIR)/stamp.modloop
90
MODLOOP_PKGS	:= $(KERNEL_PKGNAME) $(addsuffix -$(KERNEL_FLAVOR), dahdi-linux iscsitarget xtables-addons)
91

92 93
modloop: $(MODLOOP)

94 95
$(MODLOOP_DIRSTAMP):
	@echo "==> modloop: Unpacking kernel modules";
96 97
	@rm -rf $(MODLOOP_DIR)
	@mkdir -p $(MODLOOP_DIR)/lib/modules/
98 99 100
	@for i in $(MODLOOP_PKGS); do \
		apk fetch $(APK_OPTS) --stdout $$i \
			| tar -C $(MODLOOP_DIR) -xz; \
101
	done
102 103 104 105 106 107 108
	@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))
109
	@$(MKCRAMFS) $(MODLOOP_DIR)/lib $(MODLOOP)
110

111 112 113
clean-modloop:
	@rm -rf $(MODLOOP_DIR) $(MODLOOP_DIRSTAMP) $(MODLOOP)

114 115 116 117
#
# Initramfs rules
#

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

INITFS_DIR	:= $(DESTDIR)/initfs
121 122
INITFS_TMP	:= $(DESTDIR)/tmp.initfs
INITFS_DIRSTAMP := $(DESTDIR)/stamp.initfs
123
INITFS_FEATURES	:= ata base bootchart cdrom cramfs ext3 ide floppy raid scsi usb
124
INITFS_PKGS	:= $(MODLOOP_PKGS) alpine-base acct
125

126
initfs: $(INITFS)
127 128 129 130 131 132 133 134 135

$(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
136
	@cp -r /etc/apk/keys $(INITFS_DIR)/etc/apk/ || true
137 138 139 140 141 142 143 144 145
	@touch $@

#$(INITFS):	$(shell mkinitfs -F "$(INITFS_FEATURES)" -l $(KERNEL))
$(INITFS): $(INITFS_DIRSTAMP)
	@mkinitfs -F "$(INITFS_FEATURES)" -t $(INITFS_TMP) \
		-b $(INITFS_DIR) -o $@ $(KERNEL)

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

147 148 149 150 151 152 153 154 155 156 157 158
#
# 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)"
159 160
	@$(SUDO) apk add $(APK_OPTS) --initdb --root $(VSTEMPLATE_DIR) \
		alpine-base
161 162
	@cd $(VSTEMPLATE_DIR) && $(SUDO) tar -jcf $@ *

163 164 165 166 167 168 169
#
# ISO rules
#

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

172
$(ISOLINUX_BIN):
173 174
	@echo "==> iso: install isolinux"
	@mkdir -p $(dir $(ISOLINUX_BIN))
175 176 177
	@if ! apk fetch $(APK_REPO) --stdout syslinux | tar -O -zx usr/share/syslinux/isolinux.bin > $@; then \
		rm -f $@ && exit 1;\
	fi
178 179 180 181 182 183

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

189 190 191 192 193 194 195
$(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)" >>$@
196
	@echo "	append initrd=/boot/$(KERNEL_NAME).gz alpine_dev=usbdisk:vfat modules=loop,cramfs,sd-mod,usb-storage quiet" >>$@
197

198
ISO_KERNEL	:= $(ISO_DIR)/boot/$(KERNEL_NAME)
199
ISO_REPOS_DIRSTAMP := $(DESTDIR)/stamp.isorepos
200
ISOFS_DIRSTAMP	:= $(DESTDIR)/stamp.isofs
201

202
$(ISO_REPOS_DIRSTAMP): $(ISO_PKGDIR)/APKINDEX.tar.gz
203
	@touch $(ISO_PKGDIR)/.boot_repository
204 205
	@touch $@

206
$(ISO_PKGDIR)/APKINDEX.tar.gz: $(APK_FILES)
207
	@echo "==> iso: generating repository index"
208 209
	@apk index --description "$(ALPINE_NAME) $(ALPINE_RELEASE)" \
		-o $@ $(ISO_PKGDIR)/*.apk
210
	@abuild-sign $@
211

212
$(ISO_KERNEL): 
213 214
	@echo "==> iso: install kernel $(KERNEL)"
	@mkdir -p $(dir $(ISO_KERNEL))
215 216
	@apk fetch $(APK_OPTS) --stdout $(KERNEL_PKGNAME) \
		| tar -C $(ISO_DIR) -xz boot
217
	@rm -rf $(ISO_DIR)/.[A-Z]* $(ISO_DIR)/.[a-z]* $(ISO_DIR)/lib
218

219
$(ISOFS_DIRSTAMP): $(MODLOOP) $(INITFS) $(ISOLINUX_CFG) $(ISOLINUX_BIN) $(ISO_KERNEL) $(ISO_REPOS_DIRSTAMP) $(SYSLINUX_CFG)
220 221
	@echo "$(ALPINE_NAME)-$(ALPINE_RELEASE) $(BUILD_DATE)" \
		> $(ISO_DIR)/.alpine-release
222 223 224
	@touch $@

$(ISO): $(ISOFS_DIRSTAMP)
225 226 227 228 229 230 231 232
	@echo "==> iso: building $(notdir $(ISO))"
	@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			\
233
		-follow-links		\
234
		$(ISO_OPTS)		\
235
		$(ISO_DIR)
236
	@ln -fs $@ $(ISO_LINK)
237

238
isofs: $(ISOFS_DIRSTAMP)
239
iso: $(ISO)
240 241 242 243

#
# SHA1 sum of ISO
#
244
ISO_SHA1	:= $(ISO).sha1
245

246
$(ISO_SHA1):	$(ISO)
247 248 249
	@echo "==> Generating sha1 sum"
	@sha1sum $(ISO) > $@ || rm -f $@

250 251 252 253
#
# USB image
#
USBIMG 		:= $(ALPINE_NAME)-$(ALPINE_RELEASE)-$(ALPINE_ARCH).img
254 255
USBIMG_FREE	?= 8192
USBIMG_SIZE 	= $(shell echo $$(( `du -s $(ISO_DIR) | awk '{print $$1}'` + $(USBIMG_FREE) )) )
256 257 258
MBRPATH 	:= /usr/share/syslinux/mbr.bin

$(USBIMG): $(ISOFS_DIRSTAMP)
259
	@echo "==> Generating $@"
260
	@mformat -C -v 'ALPINE' -c 16 -h 64 -n 32 -i $(USBIMG) \
261
		-t $$(($(USBIMG_SIZE) / 1000)) ::
262
	@syslinux $(USBIMG)
263 264 265
	@mcopy -i $(USBIMG) $(ISO_DIR)/* $(ISO_DIR)/.[a-z]* ::
	@mcopy -i $(USBIMG) /dev/zero ::/zero 2>/dev/null || true
	@mdel -i $(USBIMG) ::/zero
266 267 268 269 270 271

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

272 273 274
$(ALPINE_NAME).img:	$(USBIMG)
	@ln -sf $(USBIMG) $@

275 276
img:	$(ALPINE_NAME).img

277 278 279 280
sha1: $(ISO_SHA1) $(USBIMG_SHA1)

release: $(ISO_SHA1)