Skip to content

Instantly share code, notes, and snippets.

@risyasin
Created April 17, 2025 08:09
Show Gist options
  • Save risyasin/496204f52f82bda43a44ca8c06430def to your computer and use it in GitHub Desktop.
Save risyasin/496204f52f82bda43a44ca8c06430def to your computer and use it in GitHub Desktop.
Alpine linux on Qemu/KVM as docker playground
#!/bin/sh
setup-timezone -z UTC
cat <<-EOF > /etc/network/interfaces
iface lo inet loopback
iface eth0 inet dhcp
EOF
ln -s networking /etc/init.d/net.lo
ln -s networking /etc/init.d/net.eth0
sed -Ei \
-e 's/^[# ](rc_depend_strict)=.*/\1=NO/' \
-e 's/^[# ](rc_logger)=.*/\1=YES/' \
-e 's/^[# ](unicode)=.*/\1=YES/' \
/etc/rc.conf
rc-update add acpid boot
rc-update add networking boot
rc-update add net.eth0 boot
rc-update add net.lo boot
rc-update add chronyd boot 2>/dev/null
rc-update add crond boot 2>/dev/null
rc-update add sshd boot 2>/dev/null
rc-update add docker boot 2>/dev/null
echo 'alpine' > /etc/hostname
### shamelessly ripped off from https://gist.github.com/arvati/7d698d4472c8b2a6a9995b05f168c15a
cat <<'EOTF' > /etc/periodic/15min/motd
#!/bin/sh
#. /etc/os-release
PRETTY_NAME=`awk -F= '$1=="PRETTY_NAME" { print $2 ;}' /etc/os-release | tr -d '"'`
VERSION_ID=`awk -F= '$1=="VERSION_ID" { print $2 ;}' /etc/os-release`
cat > /etc/motd << EOF
%++++++++++++++++++ SERVER INFO ++++++++++++++++++%
% %
$PRETTY_NAME - $VERSION_ID
Kernel: `uname -r`
Load: `cat /proc/loadavg | awk '{print $1 ", " $2 ", " $3}'`
Memory: `free -m | head -n 2 | tail -n 1 | awk {'print $2'}`M Free: `free -m | head -n 2 | tail -n 1 | awk {'print $4'}`M
Disk: `df -h / | awk '{ a = $2 } END { print a }'` Free: `df -h / | awk '{ a = $4 } END { print a }'`
% %
%++++++++++++++++++++++++++++++++++++++++++++++++++%
EOF
EOTF
chmod a+x /etc/periodic/15min/motd
run-parts --test /etc/periodic/15min
chsh --shell /bin/bash root
cat <<'EOF' > /root/.bash_profile
PS1="\u@\h:\w${_p} # "
export EDITOR=/usr/bin/nano
alias ll='ls -allh'
EOF
mkdir /root/.ssh
cat <<'EOF' > /root/.ssh/authorized_keys
ADD_YOUR_SSH_KEYS
EOF
#!/bin/sh
HOSTNAME=alpine
VCPU_COUNT=4
MEM_SIZE=8192
DISK_SPACE=40
echo "Cleaning up!"
## clean up previous
sudo virsh destroy ${HOSTNAME} 2>/dev/null
sudo virsh undefine ${HOSTNAME} 2>/dev/null
sudo virsh vol-delete --pool default ${HOSTNAME} 2>/dev/null
sudo virsh vol-delete --pool default ${HOSTNAME}.qcow2 2>/dev/null
sudo alpine-make-vm-image --image-format qcow2 --image-size ${DISK_SPACE}G \
--repositories-file repositories \
--packages "$(cat packages)" \
--script-chroot ${HOSTNAME}.qcow2 -- configure.sh
# Get the image size
sudo qemu-img info --output json ${HOSTNAME}.qcow2 | jq -r .[\"virtual-size\"]
# Get the image type
sudo qemu-img info --output json ${HOSTNAME}.qcow2 | jq -r .format
sudo virsh vol-create-as default ${HOSTNAME} ${DISK_SPACE}G --format qcow2
sudo virsh vol-upload --pool default --vol ${HOSTNAME} ./${HOSTNAME}.qcow2
VOL_PATH=`sudo virsh vol-path --pool default --vol ${HOSTNAME}`
## Install
sudo virt-install --name ${HOSTNAME} \
--os-variant alpinelinux3.19 \
--vcpus ${VCPU_COUNT} \
--memory ${MEM_SIZE} \
--disk pool=default,size=${DISK_SPACE},backing_store=${VOL_PATH},backing_format=qcow2,format=qcow2 \
--import \
--graphics vnc \
--network default,model=virtio \
--noautoconsole
## cleanup disk image
rm ${HOSTNAME}.qcow2
alpine-base
bash
ca-certificates
chrony
coreutils
curl
docker
docker-cli-compose
git
grep
htop
iproute2
iputils
nano
openssh
qemu-guest-agent
rsync
shadow
sudo
https://nl.alpinelinux.org/alpine/edge/main
https://nl.alpinelinux.org/alpine/edge/community
@testing http://nl.alpinelinux.org/alpine/edge/testing
@risyasin
Copy link
Author

Depends on proper install of

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment