Commit 576541e3 authored by Timo Teräs's avatar Timo Teräs
Browse files

make.alpine: makefile to build alpine iso

tries to get correct dependencies for modloop, initramfs and iso
so minimum amount of work is required for rebuild.
parent 0b2f41e9
#!/bin/ash
VERSION=1.9.1-pre0
NEWROOT=/newroot
SINGLEMODE=no
# basic environment
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
# basic mounts
mount -t proc -o noexec,nosuid,nodev proc /proc
mount -t sysfs -o noexec,nosuid,nodev sysfs /sys
# some helpers
ebegin() {
echo -n " * $*: "
}
eend() {
local msg
if [ "$1" = 0 ] || [ $# -lt 1 ] ; then
echo "ok."
else
shift
echo "failed. $*"
echo "Emergency recovery shell started."
exec /bin/busybox sh
fi
}
scan_drivers() {
if [ "$AUTODETECT" != no ] ; then
find /sys -name modalias | xargs sort | while read a ; do
modprobe $a 2>/dev/null \
|| echo $a >> /tmp/hwdrivers.failed
done
fi
}
# gotta start from somewhere :)
echo "Starting Alpine $VERSION"
# read the kernel options
for i in `cat /proc/cmdline` ; do
case $i in
s|single|1)
SINGLEMODE=yes ;;
modules=*)
MODULES="`echo ${i#modules=} | tr ',' ' '`";;
noautodetect)
AUTODETECT=no;;
*=*) eval KOPT_$i ;;
*) eval KOPT_$i=yes ;;
esac
done
ALPINE_DEV=${KOPT_alpine_dev%%:*}
ALPINE_MNT=/media/$ALPINE_DEV
# hide kernel messages
dmesg -n 1
# setup /dev
ebegin "Starting mdev"
mount -t tmpfs -o exec,nosuid,mode=0755 mdev /dev
mknod -m 666 /dev/null c 1 3
ln -s sr0 /dev/cdrom
echo "/sbin/mdev" > /proc/sys/kernel/hotplug
mdev -s
RC=$?
[ -d /dev/pts ] || mkdir -m 755 /dev/pts
[ -c /dev/ptmx ] || mknod -m 666 /dev/ptmx c 5 2
mount -t devpts -o gid=5,mode=0620,noexec,nosuid devpts /dev/pts
[ -d /dev/shm ] || mkdir /dev/shm
mount -t tmpfs -o nodev,nosuid,noexec shm /dev/shm
eend $RC
# load available drivers to get access to modloop media
ebegin "Loading boot drivers"
[ "$MODULES" ] && modprobe $MODULES 2> /dev/null
scan_drivers
scan_drivers
eend 0
# locate boot media and mount it
ebegin "Mounting boot media"
mount $ALPINE_MNT >/dev/null 2>&1
eend $?
ebegin "Mounting loopback device for kernel modules"
modprobe loop
mount -o loop,ro -t cramfs /media/$ALPINE_DEV/$(dirname ${KOPT_BOOT_IMAGE:-""})/modloop.cmg /.modloop
rc=$?
if [ "$rc" = 0 ]; then
rm -rf /lib/modules
ln -sf /.modloop/modules /lib
fi
if [ -d $ALPINE_MNT/firmware ]; then
mkdir -p /lib/firmware
cp -a $ALPINE_MNT/firmware/* /lib/firmware/
fi
eend $?
# early console?
if [ "$SINGLEMODE" = "yes" ]; then
echo "Entering single mode. Type 'exit' to continue booting."
sh
fi
# more drivers
ebegin "Loading hardware drivers"
scan_drivers
eend 0
# install new root
ebegin "Installing packets to root filesystem"
mount -t tmpfs -o size=50M tmpfs $NEWROOT
apk create --root /newroot
apk add --root /newroot --repository /media/cdrom/apks
eend $?
# switch over to new root
cat /proc/mounts | while read DEV DIR TYPE OPTS ; do
if [ "$DIR" != "/" -a "$DIR" != "$NEWROOT" -a -d "$DIR" ]; then
mkdir -p $NEWROOT/$DIR
mount -o move $DIR $NEWROOT/$DIR
fi
done
sync
exec /bin/busybox switch_root $NEWROOT /sbin/init $KOPT_init_args || exec /bin/busybox sh
#!/usr/bin/make -f
ISO ?= alpine-test.iso
DESTDIR ?= $(shell pwd)/isotmp
APKDIRS ?= ../aports/core/*/
ISO_DIR := $(DESTDIR)/isofs
find_apk = $(firstword $(wildcard $(addprefix $(APKDIRS),$(1)-[0-9]*.apk)))
KERNEL_APK := $(call find_apk,linux-grsec)
MODULE_APK := $(wildcard $(subst /linux-grsec-,/linux-grsec-mod-,$(KERNEL_APK)))
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)
SOURCE_APKS := $(wildcard $(APKDIRS)/*apk)
APK_BIN := $(shell which apk)
ifneq ($(words $(KERNEL_APK) $(MODULE_APK) $(ALPINEBASELAYOUT_APK) $(UCLIBC_APK) $(BUSYBOX_APK) $(APK_TOOLS_APK)),6)
$(error I did not find all APKs which I need.)
endif
all: $(ISO)
help:
@echo "Alpine ISO builder"
@echo
@echo "Type 'make' to build $(ISO)"
@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
#clean:
# rm -rf $(DESTDIR)
#
# Modloop
#
MODLOOP := $(ISO_DIR)/boot/modloop.cmg
MODLOOP_DIR := $(DESTDIR)/modloop
MODLOOP_DIRSTAMP := $(DESTDIR)/stamp.modloop
$(MODLOOP_DIRSTAMP): $(MODULE_APK)
@echo "==> modloop: prepare modules $(notdir $(MODULE_APK))"
@rm -rf $(MODLOOP_DIR)
@mkdir -p $(MODLOOP_DIR)/lib/modules/
@tar -C $(MODLOOP_DIR) -xzf $(MODULE_APK)
@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
#
INITFS := $(ISO_DIR)/boot/initramfs.gz
INITFS_DIRSTAMP := $(DESTDIR)/stamp.initfs
INITFS_DIR := $(DESTDIR)/initfs
INITFS_MODDIR := $(INITFS_DIR)/lib/modules/$(KERNEL)
INITFS_MODDIRSTAMP := $(DESTDIR)/stamp.initfs.modules
INITFS_APKS := $(UCLIBC_APK) $(BUSYBOX_APK)
INITFS_RAWBASEFILES := etc/mdev.conf etc/passwd etc/group etc/fstab
INITFS_BASEFILES := $(addprefix $(INITFS_DIR)/, $(INITFS_RAWBASEFILES))
$(INITFS_DIRSTAMP): $(INITFS_APKS)
@echo "==> initramfs: prepare baselayout"
@rm -rf $(INITFS_DIR)
@mkdir -p $(addprefix $(INITFS_DIR)/, \
dev proc sys sbin bin .modloop lib/modules \
media/cdrom media/floppy media/usb \
newroot/proc newroot/sys)
@for apk in $(INITFS_APKS) ; do \
tar -C $(INITFS_DIR) -xzf $$apk ; \
done
@rm -f "$(INITFS_DIR)/.PKGINFO"
@mknod $(INITFS_DIR)/dev/null c 1 3
@touch $(INITFS_DIRSTAMP)
$(INITFS_BASEFILES): $(INITFS_DIRSTAMP) $(ALPINEBASELAYOUT_APK)
@echo "==> initramfs: $(notdir $(ALPINEBASELAYOUT_APK))"
@tar -C $(INITFS_DIR) -xzf $(ALPINEBASELAYOUT_APK) $(INITFS_RAWBASEFILES)
@touch $(INITFS_BASEFILES)
$(INITFS_DIR)/init: initramfs-init $(INITFS_DIRSTAMP)
@echo "==> initramfs: init script"
@cp initramfs-init "$(INITFS_DIR)/init"
ifeq ($(APK_BIN),)
$(INITFS_DIR)/sbin/apk: $(APK_TOOLS_APK) $(INITFS_DIRSTAMP)
@echo "==> initramfs: $(notdir $(APK_TOOLS_APK))"
@tar -C $(INITFS_DIR) -xzf $(APK_TOOLS_APK) sbin/apk
else
$(INITFS_DIR)/sbin/apk: $(APK_BIN) $(INITFS_DIRSTAMP)
@echo "==> initramfs: copy $(APK_BIN) from buildroot"
@cp $(APK_BIN) "$(INITFS_DIR)/sbin"
endif
$(INITFS_MODDIRSTAMP): $(INITFS_DIRSTAMP) $(INITFS_MODFILES) $(MODLOOP_DIRSTAMP)
@echo "==> initramfs: $(notdir $(MODULE_APK))"
@rm -rf $(INITFS_DIR)/lib/modules
@mkdir -p $(addprefix $(INITFS_MODDIR)/kernel/,drivers fs)
@for i in acpi ata block ide scsi cdrom usb message hid; do \
cp -flLpR $(MODLOOP_DIR)/lib/modules/*/kernel/drivers/$$i \
$(INITFS_MODDIR)/kernel/drivers/ ; \
done
@for i in isofs vfat nls ext2 cramfs '*.ko'; do \
cp -flLpR $(MODLOOP_DIR)/lib/modules/*/kernel/fs/$$i \
$(INITFS_MODDIR)/kernel/fs/ ; \
done
@cp -flLpR $(MODLOOP_DIR)/lib/modules/*/kernel/lib \
$(INITFS_MODDIR)/kernel/
@depmod $(KERNEL) -b $(INITFS_DIR)
@touch $(INITFS_MODDIRSTAMP)
$(INITFS): $(INITFS_DIRSTAMP) $(INITFS_DIR)/init $(INITFS_DIR)/sbin/apk $(INITFS_MODDIRSTAMP) $(INITFS_BASEFILES)
@echo "==> initramfs: creating $(notdir $(INITFS))"
@(cd $(INITFS_DIR) && find . | cpio -o -H newc | gzip -9) > $(INITFS)
#
# ISO rules
#
ISOLINUX := $(ISO_DIR)/isolinux
ISOLINUX_BIN := $(ISOLINUX)/isolinux.bin
ISOLINUX_CFG := $(ISOLINUX)/isolinux.cfg
$(ISOLINUX_BIN): /usr/share/syslinux/isolinux.bin
@echo "==> iso: install isolinux"
@mkdir -p $(dir $(ISOLINUX_BIN))
@cp /usr/share/syslinux/isolinux.bin $(ISOLINUX_BIN)
$(ISOLINUX_CFG):
@echo "==> iso: configure isolinux"
@mkdir -p $(dir $(ISOLINUX_BIN))
@echo "timeout 20" >$(ISOLINUX_CFG)
@echo "prompt 1" >>$(ISOLINUX_CFG)
@echo "default linux" >>$(ISOLINUX_CFG)
@echo "label linux" >>$(ISOLINUX_CFG)
@echo " kernel /boot/vmlinuz" >>$(ISOLINUX_CFG)
@echo " append initrd=/boot/initramfs.gz alpine_dev=cdrom quiet" >>$(ISOLINUX_CFG)
ISO_KERNEL := $(ISO_DIR)/boot/vmlinuz
ISO_APKS := $(ISO_DIR)/apks
ISO_APKINDEX := $(ISO_APKS)/APK_INDEX.gz
$(ISO_APKS): $(SOURCE_APKS)
@echo "==> iso: prepare APK repository"
@rm -rf $(ISO_APKS)
@mkdir -p $(ISO_APKS)
@for a in $(SOURCE_APKS) ; do \
ln -f $$a $(ISO_APKS) 2>/dev/null || cp $$a $(ISO_APKS) ; \
done
@apk index $(SOURCE_APKS) | gzip -9 > $(ISO_APKINDEX)
$(ISO_KERNEL): $(KERNEL_APK)
@echo "==> iso: install kernel $(KERNEL)"
@mkdir -p $(dir $(ISO_KERNEL))
@tar -C $(ISO_DIR) -xzf $(KERNEL_APK) boot/vmlinuz boot/System.map
@touch $(ISO_KERNEL)
$(ISO): $(MODLOOP) $(INITFS) $(ISOLINUX_CFG) $(ISOLINUX_BIN) $(ISO_KERNEL) $(ISO_APKS)
@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 \
$(ISO_DIR)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment