4
0
Fork 0

initial version

This commit is contained in:
Benjamin Böhmke 2019-11-02 14:41:03 +01:00
commit 45bcaa9172
4 changed files with 417 additions and 0 deletions

27
Dockerfile Normal file
View File

@ -0,0 +1,27 @@
FROM alpine:3.10
RUN apk update && \
apk add automake build-base git autoconf confuse-dev linux-headers \
findutils mtools e2fsprogs-extra alpine-sdk dosfstools && \
rm -rf /var/cache/apk/*
RUN git clone https://github.com/pengutronix/genimage.git /tmp/genimage && \
cd /tmp/genimage && \
./autogen.sh && \
./configure CFLAGS='-g -O0' --prefix=/usr && \
make install && \
cd && \
rm -rf /tmp/genimage
ADD ./resources/genext2fs /genext2fs
RUN cd /genext2fs && \
abuild-keygen -a -i -q && \
abuild -F -P /tmp/pkg && \
apk add /tmp/pkg/x86_64/genext2fs-1*.apk && \
rm -rf /tmp/pkg/
ADD ./resources /resources
WORKDIR /work

283
resources/build.sh Executable file
View File

@ -0,0 +1,283 @@
#!/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.img"}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# static config
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
RES_PATH=/resources/
BASE_PACKAGES="alpine-base tzdata parted ifupdown e2fsprogs-extra util-linux coreutils linux-rpi2"
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 <<EOF
${ALPINE_MIRROR}/v${ALPINE_BRANCH}/main
${ALPINE_MIRROR}/v${ALPINE_BRANCH}/community
EOF
# copy apk keys to new root (required for initial apk add run)
mkdir -p ${ROOTFS_PATH}/etc/apk/keys/
cp /usr/share/apk/keys/*.rsa.pub ${ROOTFS_PATH}/etc/apk/keys/
# copy repositories to new root
cp /etc/apk/repositories ${ROOTFS_PATH}/etc/apk/repositories
# initial package installation
apk --root ${ROOTFS_PATH} --update-cache --initdb --arch armhf add $BASE_PACKAGES
# add google DNS to enable network access inside chroot
echo "nameserver 8.8.8.8" > ${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 <<EOF
127.0.0.1 localhost ${HOST_NAME}
::1 localhost ${HOST_NAME}
EOF
cat >${ROOTFS_PATH}/etc/hostname <<EOF
${HOST_NAME}
EOF
# enable local startup files (stored in /etc/local.d/)
chroot_exec rc-update add local default
cat >${ROOTFS_PATH}/etc/conf.d/local <<EOF
rc_verbose=yes
EOF
# prepare network
chroot_exec rc-update add networking default
cat >${ROOTFS_PATH}/etc/network/interfaces <<EOF
# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
# networking should not wait for local -> 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 <<EOF
#!/bin/sh
ifconfig eth0 up
EOF
chmod +x ${ROOTFS_PATH}/etc/local.d/11-up_eth0.start
# make root file system writeable
cat >${ROOTFS_PATH}/etc/local.d/10-remount_root.start <<EOF
#!/bin/sh
mount -o remount,rw /
EOF
chmod +x ${ROOTFS_PATH}/etc/local.d/10-remount_root.start
# add script to resize data partition
cp ${RES_PATH}/resizedata.sh ${ROOTFS_PATH}/etc/local.d/90-resizedata.start
chmod +x ${ROOTFS_PATH}/etc/local.d/90-resizedata.start
# mount data and boot partition (root is already mounted)
cat >${ROOTFS_PATH}/etc/fstab <<EOF
/dev/mmcblk0p1 /boot vfat defaults,ro 0 2
/dev/mmcblk0p3 /data ext4 defaults 0 1
EOF
# custom
chroot_exec apk add dropbear
chroot_exec rc-update add dropbear
rm -rf ${ROOTFS_PATH}/var/cache/apk/*
# TODO /etc/motd
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# 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_x.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_x.elf
# copy linux kernel and overlays
cp ${ROOTFS_PATH}/usr/lib/linux-*-rpi2/*.dtb ${BOOTFS_PATH}/
cp -r ${ROOTFS_PATH}/usr/lib/linux-*-rpi2/overlays ${BOOTFS_PATH}/
cp ${ROOTFS_PATH}/boot/initramfs-rpi2 ${BOOTFS_PATH}/
cp ${ROOTFS_PATH}/boot/vmlinuz-rpi2 ${BOOTFS_PATH}/
# write boot config
cat >${BOOTFS_PATH}/config.txt <<EOF
disable_splash=1
boot_delay=0
gpu_mem=256
gpu_mem_256=64
hdmi_drive=1
hdmi_group=2
hdmi_mode=1
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0
[pi2]
kernel=vmlinuz-rpi2
initramfs initramfs-rpi2
[pi3]
kernel=vmlinuz-rpi2
initramfs initramfs-rpi2
[all]
include usercfg.txt
EOF
cat >${BOOTFS_PATH}/cmdline.txt <<EOF
console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 fsck.repair=yes rw rootwait quiet
EOF
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# create data FS
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
echo ">> Configure data FS"
mkdir -p ${DATAFS_PATH}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# create image
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
echo ">> Create SD card image"
# boot partition
cat >${WORK_PATH}/genimage_boot.cfg <<EOF
image boot.vfat {
name = "boot"
vfat {
}
size = 32M
}
EOF
make_image ${BOOTFS_PATH} ${WORK_PATH}/genimage_boot.cfg
# root partition
cat >${WORK_PATH}/genimage_root.cfg <<EOF
image rootfs.ext4 {
name = "root"
ext4 {
}
size = 150MB
}
EOF
make_image ${ROOTFS_PATH} ${WORK_PATH}/genimage_root.cfg
# data partition
cat >${WORK_PATH}/genimage_data.cfg <<EOF
image datafs.ext4 {
name = "data"
ext4 {
}
size = 20MB
}
EOF
make_image ${DATAFS_PATH} ${WORK_PATH}/genimage_data.cfg
# sd card image
cat >${WORK_PATH}/genimage_sdcard.cfg <<EOF
image sdcard.img {
hdimage {
}
partition boot {
partition-type = 0xC
bootable = "true"
image = "boot.vfat"
}
partition rootfs {
partition-type = 0x83
image = "rootfs.ext4"
}
partition datafs {
partition-type = 0x83
image = "datafs.ext4"
}
}
EOF
make_image ${IMAGE_PATH} ${WORK_PATH}/genimage_sdcard.cfg
# copy final image
cp ${IMAGE_PATH}/sdcard.img ${OUTPUT_PATH}/${IMG_NAME}

View File

@ -0,0 +1,44 @@
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=genext2fs
pkgver=1.4.1
pkgrel=0
pkgdesc="Tool for generating an ext2 filesystem as non-root"
url="http://genext2fs.sourceforge.net/"
arch="all"
license="GPLv2"
depends=""
depends_dev=""
makedepends="$depends_dev"
install=""
subpackages="$pkgname-doc"
source="http://downloads.sourceforge.net/project/genext2fs/genext2fs/$pkgver/genext2fs-$pkgver.tar.gz"
_builddir="$srcdir"/genext2fs-$pkgver
prepare() {
local i
cd "$_builddir"
for i in $source; do
case $i in
*.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;;
esac
done
}
build() {
cd "$_builddir"
./configure \
--build=$CBUILD \
--host=$CHOST \
--prefix=/usr \
--sysconfdir=/etc \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--localstatedir=/var \
|| return 1
make || return 1
}
package() {
cd "$_builddir"
make DESTDIR="$pkgdir" install || return 1
}
md5sums="b7b6361bcce2cedff1ae437fadafe53b genext2fs-1.4.1.tar.gz"
sha256sums="404dbbfa7a86a6c3de8225c8da254d026b17fd288e05cec4df2cc7e1f4feecfc genext2fs-1.4.1.tar.gz"
sha512sums="1b9ec7044014423345ae6b09862ba6903f5b3e0f68fb8bbcf97daf2705471cc1633a9fdbc5e00afe1b191e1af7bed87bde2e538bc7365469218f2a00b062845c genext2fs-1.4.1.tar.gz"

63
resources/resizedata.sh Executable file
View File

@ -0,0 +1,63 @@
#!/bin/sh -e
logger -t "rc.resizedata" "Expanding root partition"
# Detect root partition device
ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
if [ -z "$ROOT_PART" ] ; then
log_warning_msg "unable to detect root partition device"
return 1
fi
# Extract root device name
case "${ROOT_PART}" in
mmcblk0*) ROOT_DEV=mmcblk0 ;;
sda*) ROOT_DEV=sda ;;
esac
# get last partition
LAST_PART_NUM=$(parted /dev/${ROOT_DEV} -ms unit s p | tail -n 1 | cut -f 1 -d:)
LAST_PART="${ROOT_DEV}p${LAST_PART_NUM}"
# unmount if mounted
if grep -qs "/dev/${LAST_PART}" /proc/mounts; then
umount /dev/${LAST_PART}
fi
# Get the starting offset of last partition
PART_START=$(parted /dev/${ROOT_DEV} -ms unit s p | grep "^${LAST_PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g')
if [ -z "$PART_START" ] ; then
logger -t "rc.resizedata" "${ROOT_DEV} unable to get starting sector of the partition"
return 1
fi
# Get the possible last sector for the root partition
PART_LAST=$(fdisk -l /dev/${ROOT_DEV} | grep '^Disk.*sectors' | awk '{ print $7 - 1 }')
if [ -z "$PART_LAST" ] ; then
logger -t "rc.resizedata" "${ROOT_DEV} unable to get last sector of the partition"
return 1
fi
### Since rc.local is run with "sh -e", let's add "|| true" to prevent premature exit
echo "resize partition"
fdisk /dev/${ROOT_DEV} > /dev/null <<EOF2 || true
p
d
$LAST_PART_NUM
n
p
$LAST_PART_NUM
$PART_START
$PART_LAST
p
w
EOF2
# Reload the partition table, resize root filesystem then remove resizing code from this file
partprobe /dev/${ROOT_DEV} &&
resize2fs -p /dev/${LAST_PART} &&
logger -t "rc.resizedata" "Root partition successfully resized."
mount -a
rm -f /etc/local.d/90-resizedata.start