#!/bin/sh set -e if test "$(id -u)" -ne 0 then echo 'This must be run as root.' exit 1 fi if [ -z "$1" -o -z "$2" ] then echo "Usage: $0 " echo "Hints:" echo " must be an absolute path" echo " is arm64 for aarch64 (e.g.) Raspberry Pi 3+4" exit 1 fi PACKAGES_DIRECTORY="$1" DIR="$(pwd)" ARCH="$2" TMP="$(mktemp -d)" VERSION="$3" cd "$TMP" trap "rm -rf '$TMP'" EXIT mkdir -p rootfs/var/lib/pkg touch rootfs/var/lib/pkg/db DATE="$(date +"%Y-%m-%d")" for i in "$PACKAGES_DIRECTORY"/*.pkg.tar.* do echo "Adding $(basename "$i")" pkgadd -r rootfs "$i" done # Insert hostname templates sed -i -e '/HOSTNAME=/cHOSTNAME=LXC_NAME' rootfs/etc/rc.conf sed -i -e 's;localhost.*;& LXC_NAME;' rootfs/etc/hosts # Fix default networking cat > rootfs/etc/rc.d/net << 'EOF' #!/bin/sh # # /etc/rc.d/net: start/stop network interface # case $1 in start) /sbin/dhcpcd -4 eth0 ;; stop) /sbin/dhcpcd -x eth0 ;; restart) $0 stop $0 start ;; *) echo "Usage: $0 [start|stop|restart]" ;; esac EOF # Remove noclear from agettys # Replace linux with xterm on agettys # Remove serial console # Remove existing powerfail entries # Add an entry to allow LXC/LXD to shutdown the container sed -i \ -e 's;--noclear ;;' \ -e 's;linux;xterm;' \ -e '/s1:2:/d' \ -e '/:powerfail:/d' \ -e '/ctrlaltdel/cpf::powerfail:/sbin/telinit 0' \ rootfs/etc/inittab # Disable klogd in containers as it doesn't work sed -i -e '/KLOG/d' -e 's;and klog ;;' rootfs/etc/rc.d/sysklogd # Export a default LANG sed -i -e '/LESS=/iexport LANG="C"' rootfs/etc/profile # Disable startup functionality that doesn't work in a container sed -i \ -e '/# Start udev/,/^ *$/d' \ -e '/# Create device-mapper device nodes and scan for LVM volume groups/,/^ *$/d' \ -e '/# Mount root read-only/,/^ *$/d' \ -e '/-f \/forcefsck/,/^ *$/d' \ -e '/# Check filesystems/,/^ *$/d' \ -e '/# Mount local filesystems/,/^ *$/d' \ -e '/# Activate swap/,/^ *$/d' \ -e '/hwclock/d' \ -e '/# Load console font/,/^ *$/d' \ -e '/# Load console keymap/,/^ *$/d' \ -e '/# Screen blanks after 15 minutes idle time/,/^ *$/d' \ -e '/# Run module initialization script/,/^ *$/d' \ rootfs/etc/rc # Disable shutdown functionality that doesn't work in a container sed -i \ -e '/# Set linefeed mode to avoid staircase effect/,/^ *$/d' \ -e '/# Save system clock/,/^ *$/d' \ -e '/# Turn off swap/,/^ *$/d' \ -e '/# Unmount file systems/,/^ *$/d' \ -e '/# Remount root filesystem read-only/,/^ *$/d' \ rootfs/etc/rc.shutdown # Disable certain functionality in single user login sed -i \ -e '/# Start udev/,/^ *$/d' \ rootfs/etc/rc.single # Remove variables that are not used in containers sed -i -e '/FONT=/d' -e '/KEYMAP=/d' rootfs/etc/rc.conf # Create the LXC metadata tarball cat > config << EOF lxc.include = LXC_TEMPLATE_CONFIG/common.conf lxc.arch = $ARCH lxc.tty.max = 6 EOF cat > excludes << 'EOF' dev/* EOF cat > config-user << EOF lxc.include = LXC_TEMPLATE_CONFIG/common.conf lxc.include = LXC_TEMPLATE_CONFIG/userns.conf lxc.arch = $ARCH lxc.tty.max = 6 EOF cat > excludes-user << 'EOF' dev/* EOF cat > templates << 'EOF' /etc/rc.conf /etc/hosts EOF cat > create-message << EOF You just created a CRUX $VERSION $ARCH ($DATE) container. EOF tar -c -f - config excludes config-user excludes-user templates create-message | xz -9 > "$DIR/crux-$VERSION-$ARCH-$DATE-lxc.tar.xz" rm -rf config config-user create-message excludes-user templates # Create the LXD metadata tarball mkdir -p templates sed -e 's;LXC_NAME;{{ container.name }};' rootfs/etc/rc.conf > templates/rc.conf.tpl sed -e 's;LXC_NAME;{{ container.name }};' rootfs/etc/hosts > templates/hosts.tpl sed -e '/c1:2:/,/^ *$/c{% for i in config_get("user.ttys","")|make_list %}c{{ i }}:2:respawn:/sbin/agetty 38400 tty{{ i }} xterm\ {% endfor %}' rootfs/etc/inittab > templates/inittab.tpl cat > metadata.yaml << EOF architecture: $ARCH creation_date: $(date +%s) properties: architecture: $ARCH description: CRUX $VERSION $ARCH ($DATE) name: crux-$VERSION-$ARCH-$DATE os: crux release: "$VERSION" serial: "$DATE" templates: /etc/rc.conf: when: - create - copy create_only: false template: rc.conf.tpl /etc/hosts: when: - create - copy create_only: false template: hosts.tpl /etc/inittab: when: - create - copy create_only: false template: inittab.tpl EOF tar -c -f - templates/ metadata.yaml | xz -9 > "$DIR/crux-$VERSION-$ARCH-$DATE-lxd.tar.xz" rm -rf templates/ metadata.yaml # Create the root tarball (cd rootfs; tar -c -f - *) | xz -9 > "$DIR/crux-$VERSION-$ARCH-$DATE-rootfs.tar.xz" rm -rf rootfs