#!/bin/bash set -e # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # User config # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # : ${ALPINE_BRANCH:="3.10"} : ${ALPINE_MIRROR:="http://dl-cdn.alpinelinux.org/alpine"} : ${TIME_ZONE:="Etc/UTC"} : ${HOST_NAME:="alpine"} : ${ROOT_PASSWORD:="alpine"} : ${IMG_NAME:="alpine-${ALPINE_BRANCH}-sdcard"} # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # static config # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # RES_PATH=/resources/ BASE_PACKAGES="alpine-base tzdata parted ifupdown e2fsprogs-extra util-linux coreutils linux-rpi2 uboot-tools" WORK_PATH="/work" OUTPUT_PATH="/output" ROOTFS_PATH="${WORK_PATH}/root_fs" BOOTFS_PATH="${WORK_PATH}/boot_fs" DATAFS_PATH="${WORK_PATH}/data_fs" IMAGE_PATH="${WORK_PATH}/img" # ensure work directory is clean rm -rf ${WORK_PATH}/* # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # functions # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # chroot_exec() { chroot "${ROOTFS_PATH}" "$@" 1>&2 } make_image() { [ -d /tmp/genimage ] && rm -rf /tmp/genimage genimage --rootpath $1 \ --tmppath /tmp/genimage \ --inputpath ${IMAGE_PATH} \ --outputpath ${IMAGE_PATH} \ --config $2 } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # create root FS # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # echo ">> Prepare root FS" # update local repositories to destination ones to ensure the right packages where installed cat >/etc/apk/repositories < ${ROOTFS_PATH}/etc/resolv.conf # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # echo ">> Configure root FS" # set root password chroot_exec passwd << EOF ${ROOT_PASSWORD} ${ROOT_PASSWORD} EOF # Set time zone echo "${TIME_ZONE}" > ${ROOTFS_PATH}/etc/timezone chroot_exec ln -fs /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime # Set host name chroot_exec rc-update add hostname default cat >${ROOTFS_PATH}/etc/hosts <${ROOTFS_PATH}/etc/hostname <${ROOTFS_PATH}/etc/conf.d/local <${ROOTFS_PATH}/etc/network/interfaces < local brings up the interface sed -i '/^\tneed/ s/$/ local/' ${ROOTFS_PATH}/etc/init.d/networking # bring up eth0 on startup cat >${ROOTFS_PATH}/etc/local.d/11-up_eth0.start <${ROOTFS_PATH}/etc/fstab <${ROOTFS_PATH}/etc/fw_env.config <${ROOTFS_PATH}/etc/local.d/99-uboot.start <> Move persistent data to /data" # prepare /data cat >${ROOTFS_PATH}/etc/local.d/20-data_prepare.start <${ROOTFS_PATH}/etc/udhcpc/udhcpc.conf <> Prepare kernel for uboot" # build uImage mkimage -A arm -O linux -T kernel -C none -a 0x00008000 -e 0x00008000 -n "Linux kernel" -d ${ROOTFS_PATH}/boot/vmlinuz-rpi2 ${ROOTFS_PATH}/boot/uImage # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # create boot FS # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # echo ">> Configure boot FS" # download base firmware mkdir -p ${BOOTFS_PATH} wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/bootcode.bin wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/fixup.dat wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/fixup_cd.dat wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/fixup_db.dat wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/fixup_x.dat wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/fixup4.dat wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/fixup4cd.dat wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/fixup4db.dat wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/fixup4x.dat wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/start.elf wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/start_cd.elf wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/start_db.elf wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/start_x.elf wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/start4.elf wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/start4cd.elf wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/start4db.elf wget -P ${BOOTFS_PATH} https://github.com/raspberrypi/firmware/raw/master/boot/start4x.elf # copy linux kernel and overlays to boot cp ${ROOTFS_PATH}/usr/lib/linux-*-rpi2/*.dtb ${BOOTFS_PATH}/ cp -r ${ROOTFS_PATH}/usr/lib/linux-*-rpi2/overlays ${BOOTFS_PATH}/ # copy u-boot cp /uboot/* ${BOOTFS_PATH}/ # generate boot script mkimage -A arm -T script -C none -n "Boot script" -d ${RES_PATH}/boot.cmd ${BOOTFS_PATH}/boot.scr # write boot config cat >${BOOTFS_PATH}/config.txt <${BOOTFS_PATH}/cmdline.txt <> Configure data FS" mkdir -p ${DATAFS_PATH} # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # create image # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # echo ">> Create SD card image" # boot partition cat >${WORK_PATH}/genimage_boot.cfg <${WORK_PATH}/genimage_root.cfg <${WORK_PATH}/genimage_data.cfg <${WORK_PATH}/genimage_sdcard.cfg <> Compress images" # copy final image gzip -c ${IMAGE_PATH}/sdcard.img > ${OUTPUT_PATH}/${IMG_NAME}.img.gz gzip -c ${IMAGE_PATH}/rootfs.ext4 > ${OUTPUT_PATH}/${IMG_NAME}_update.img.gz # create checksums cd ${OUTPUT_PATH}/ sha256sum ${IMG_NAME}.img.gz > ${IMG_NAME}.img.gz.sha256 sha256sum ${IMG_NAME}_update.img.gz > ${IMG_NAME}_update.img.gz.sha256