#!/bin/sh set -e if [ -z "$1" -o -z "$2" ] then echo "Usage: $0 " exit 1 fi for bin in 7z do if ! command -V "$bin" 2>&1 > /dev/null then echo "Missing utility: $bin" exit 1 fi done DIR="$(pwd)" ISO="$(realpath "$1")" ARCH="$2" TMP="$(mktemp -d)" cd "$TMP" trap "rm -rf '$TMP'" EXIT mkdir -p iso rootfs/var/lib/pkg touch rootfs/var/lib/pkg/db (cd iso; 7z x "$ISO") RELEASE="$(cat iso/crux-media)" VERSION="${RELEASE%-*}" DATE="${RELEASE#*-}" for i in iso/crux/core/*.pkg.tar.* do echo "Adding $(basename "$i")" pkgadd -r rootfs "$i" done rm -rf iso # 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 # Disable agettys in containers as they don't work # Enable a login console for LXC/LXD usage # Remove existing powerfail entries # Add an entry to allow LXC/LXD to shutdown the container sed -i \ -e 's;^[^#].*agetty;#&;' \ -e '/s1:2:/acon:2:respawn:/sbin/agetty 38400 console' \ -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 # Create the LXC metadata tarball cat > config << EOF lxc.include = LXC_TEMPLATE_CONFIG/common.conf lxc.arch = $ARCH 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 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 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 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