ungleich-tools/alpine-rebuild-initramfs.sh
2020-10-18 09:47:53 +02:00

114 lines
2.6 KiB
Bash
Executable file

#!/bin/sh
if [ $# -ne 1 ]; then
echo "$0 ssh-keyfile"
echo " ssh-keyfile: ssh keys to add into the image"
exit 1
fi
set -e
set -x
SSH_KEYS=$1; shift
MAJOR_VERSION=3.12
MINOR_VERSION=0
IMAGE=alpine-minirootfs-$MAJOR_VERSION.$MINOR_VERSION-x86_64.tar.gz
RESOLVCONF=/etc/resolv.conf
working_directory=$(pwd -P)
rootfs_tmpdir=$(mktemp -d)
#rootfs_tmpdir=alpine_${MAJOR_VERSION}-${MINOR_VERSION}-rootfs
mkdir -p ${rootfs_tmpdir}
rootfs_url="http://dl-cdn.alpinelinux.org/alpine/v$MAJOR_VERSION/releases/x86_64/$IMAGE"
run_root () {
sudo chroot $rootfs_tmpdir /usr/bin/env \
PATH=/bin:/sbin \
/bin/sh -c "$*"
}
wget -c "$rootfs_url" -O "$IMAGE"
# keep right permissions, use sudo
sudo tar xf $IMAGE -C $rootfs_tmpdir
# Add SSH keys
run_root mkdir -p root/.ssh
sudo cp $SSH_KEYS $rootfs_tmpdir/root/.ssh/authorized_keys
run_root chown root:root root/.ssh/authorized_keys
run_root chmod 0600 root/.ssh/authorized_keys
run_root chmod 0700 root/.ssh
# Import local resolv.conf.
sudo cp "$RESOLVCONF" $rootfs_tmpdir/etc/resolv.conf
# Make sure init is found by the kernel.
run_root ln -sf /sbin/init /init
run_root apk update
run_root apk add linux-lts openrc udev openssh rdnssd
run_root rc-update add udev
run_root rc-update add udev-trigger
run_root rc-update add sshd
run_root rc-update add rdnssd
# Layer atop base rootfs.
# run_root apk add openssh linux-lts openrc udev ndisc6
# run_root rc-update add networking
# run_root rc-update add hostname
# run_root rc-update add rdnssd
sudo tee "$rootfs_tmpdir/etc/network/interfaces" <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet6 manual
pre-up ip link set eth0 up
pre-up echo 1 > /proc/sys/net/ipv6/conf/eth0/accept_ra
EOF
# Generate iniramfs image
(cd $rootfs_tmpdir; sudo find . | sudo cpio -H newc -o | gzip -9 > "$working_directory/alpine-initramfs.gz")
cp "$rootfs_tmpdir/boot/vmlinuz-lts" "$working_directory/alpine-kernel"
echo rm -rf "$rootfs_tmpdir"
exit 0
# Servers have static addresses, disable the standard
# alpine setting of using tempaddr = 2
sudo tee "$rootfs_tmpdir/etc/sysctl.d/99-ipv6.conf" <<EOF
net.ipv6.conf.default.use_tempaddr = 0
net.ipv6.conf.all.use_tempaddr = 0
net.ipv6.conf.all.accept_ra = 1
EOF
sudo tee "$rootfs_tmpdir/etc/hostname" <<EOF
alpine-unconfigured
EOF
echo ipv6 | sudo tee -a "$rootfs_tmpdir/etc/modules"
# Upload to netboot server. - needs to be done outside sudo
echo "Use alpine-initramfs.gz alpine-kernel from $working_directory"!
exit 0
# FIXME: add / install rdnssd / ndisc6 / start it on boot
# ndisc6 is only @testing
# Cleanup.
#rm -r "$rootfs_tmpdir"