Skip to content

Instantly share code, notes, and snippets.

@nrocco
Last active December 15, 2023 00:41
Show Gist options
  • Save nrocco/072ea98cca82e4721b2289cb75558da1 to your computer and use it in GitHub Desktop.
Save nrocco/072ea98cca82e4721b2289cb75558da1 to your computer and use it in GitHub Desktop.
Alpine Playground with libvirt

First download alpine-make-vm-image from https://github.com/alpinelinux/alpine-make-vm-image

wget https://raw.githubusercontent.com/alpinelinux/alpine-make-vm-image/v0.4.0/alpine-make-vm-image \
    && echo '5fb3270e0d665e51b908e1755b40e9c9156917c0  alpine-make-vm-image' | sha1sum -c \
    || exit 1
mv alpine-make-vm-image /usr/local/bin/
chmod +x /usr/local/bin/alpine-make-vm-image

Create a new alpine qcow2 image

alpine-make-vm-image --image-format qcow2 --image-size 5G \
    --repositories-file repositories \
    --packages "$(cat packages)" \
    --script-chroot alpine-base.qcow2 -- configure.sh

Import that image into libvirt (https://askubuntu.com/questions/299570/how-do-i-import-a-disk-image-into-libvirt)

# Get the image size
qemu-img info --output json alpine-base.qcow2 | jq -r .[\"virtual-size\"]

# Get the image type
qemu-img info --output json alpine-base.qcow2 | jq -r .format

virsh vol-create-as default alpine-base 5368709120 --format qcow2
virsh vol-upload --pool default --vol alpine-base ./alpine-base.qcow2

rm alpine-base.qcow2

Create a new domain based on the above base image (https://jlk.fjfi.cvut.cz/arch/manpages/man/virt-install.1)

virt-install --name alpine \
    --os-variant alpinelinux3.7 \
    --memory 512 \
    --disk pool=default,size=5,backing_store=$(virsh vol-path --pool default --vol alpine-base),backing_format=qcow2,format=qcow2 \
    --import \
    --graphics vnc \
    --network default,model=virtio \
    --noautoconsole
#!/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 default
rc-update add chronyd default
rc-update add crond default
rc-update add sshd default
rc-update add net.eth0 default
rc-update add net.lo boot
rc-update add termencoding boot
rc-update add haveged boot
cat <<EOF > /etc/motd
Welcome to Alpine!
EOF
mkdir -m 700 -p /root/.ssh
curl https://github.com/nrocco.keys | tee /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
chsh --shell /bin/bash root
cat <<'EOF' > /root/.bash_profile
PS1="[\u@\h:\w${_p}] "
export EDITOR=/usr/bin/vim
EOF
bash
bash-completion
bind-tools
binutils
ca-certificates
chrony
coreutils
curl
drill
findutils
grep
haveged
htop
iproute2
iptables
iputils
less
logrotate
netcat-openbsd
nload
nmap
nmap-scripts
openssh
pciutils
procps
shadow
ssmtp
sudo
tcpdump
usbutils
util-linux
vim
https://nl.alpinelinux.org/alpine/edge/main
https://nl.alpinelinux.org/alpine/edge/community
@testing http://nl.alpinelinux.org/alpine/edge/testing
@CodyKochmann
Copy link

CodyKochmann commented Jan 1, 2021

this helped a lot. turned it into a nice little repo for scratch debian hosts on digitalocean

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