Skip to content

Instantly share code, notes, and snippets.

@ohaiibuzzle
Last active May 14, 2026 02:43
Show Gist options
  • Select an option

  • Save ohaiibuzzle/0c39a5f633a9a3bb7e02e08b0fd081ed to your computer and use it in GitHub Desktop.

Select an option

Save ohaiibuzzle/0c39a5f633a9a3bb7e02e08b0fd081ed to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
ALPINE_VERSION_MAJOR="3.23"
ALPINE_VERSION="${ALPINE_VERSION_MAJOR}.4"
ALPINE_ARCH="x86_64"
ALPINE_ROOTFS="https://dl-cdn.alpinelinux.org/alpine/v${ALPINE_VERSION_MAJOR}/releases/${ALPINE_ARCH}/alpine-minirootfs-${ALPINE_VERSION}-${ALPINE_ARCH}.tar.gz"
# Download the Alpine Linux root filesystem
# make damn sure the tools we need are available
if ! command -v curl &> /dev/null || ! command -v tar &> /dev/null; then
echo "Error: curl and tar are required but not installed."
exit 1
fi
curl -L -o alpine-rootfs.tar.gz $ALPINE_ROOTFS
# Create a directory for the root filesystem
mkdir -p /x
# Extract the root filesystem
tar -xzf alpine-rootfs.tar.gz -C /x
# Set up resolv.conf for DNS resolution
echo "nameserver 8.8.8.8" > /x/etc/resolv.conf
# Inject our /etc/network/interfaces for DHCP
cat <<EOF > /x/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
# Sketchy: NUKE the existing fs
find / \( ! -path '/dev/*' -and ! -path '/proc/*' -and ! -path '/sys/*' -and ! -path '/x/*' \) -delete || true
# Move the new root filesystem to /
/x/lib/ld-musl-x86_64.so.1 /x/bin/busybox cp -a /x/* /
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
rm -rf /x alpine-rootfs.tar.gz
apk update
apk add --no-cache alpine-base openssh-server linux-virt grub-efi efibootmgr
# Set up SSH server
echo PermitRootLogin yes >> /etc/ssh/sshd_config
echo 'GRUB_CMDLINE_LINUX_DEFAULT="rootfstype=ext4"' >> /etc/default/grub
rc-update add root boot
rc-update add networking default
rc-update add sshd default
rc-update add mdev sysinit
rc-update add devfs sysinit
rc-update add hostname boot
# Install the bootloader
grub-install --bootloader-id=Alpine
grub-mkconfig -o /boot/grub/grub.cfg
# sh so user can do stuff
echo 'Please "passwd root" right now to set a password for the new root user.'
sh
# done
sync
reboot -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment