Skip to content

Instantly share code, notes, and snippets.

@kruton
Last active January 15, 2025 21:36
Show Gist options
  • Save kruton/73a3231acaab4318d86b2ac13a609712 to your computer and use it in GitHub Desktop.
Save kruton/73a3231acaab4318d86b2ac13a609712 to your computer and use it in GitHub Desktop.
Simulating Alpine Linux provisioning

Simulating how Alpine Linux boots at VPS provider

Steps to create the guest

  1. Get a pristine image of the Alpine Linux 3.19 disk with cloud-init support.

    wget https://dl-cdn.alpinelinux.org/alpine/v3.19/releases/x86_64/alpine-virt-3.19.6-x86_64.iso
  2. Add your SSH keys to user-data. You can find your SSH keys with:

    ssh-add -L

    Copy them into the lines of ssh-rsa ... in user-data below.

  3. Create your own user-data file. Example is in another file in this gist. Also create a meta-data file:

    echo "hostname: my-test-infra" > meta-data
  4. Run the libvirt installer

    virt-install --connect qemu:///session \
      --osinfo alpinelinux3.19 \
      --name my-test-infra \
      --network user \
      --location alpine-virt-3.19.6-x86_64.iso,kernel=boot/vmlinuz-virt,initrd=boot/initramfs-virt \
      --extra-args console=ttyS0 \
      --graphics none \
      --console pty,target_type=serial \
      --cloud-init meta-data=meta-data,user-data=user-data \
      --noreboot

To access the guest from your host

  1. Start the guest:

    virsh --connect qemu:///session start my-test-infra
  2. Run a QEMU monitor command to forward the port.

    virsh --connect qemu:///session qemu-monitor-command my-test-infra --hmp --cmd "hostfwd_add tcp::2222-:22"
  3. SSH to the VM without saving the keys (you will keep rebuilding this host to test, right?)

    ssh localhost -l root -p 2222 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no

Get rid of the guest

  1. Destroy the running instance
    virsh --connect qemu:///session destroy my-test-infra
  2. Undefine the guest
    virsh --connect qemu:///session undefine my-test-infra --storage vda
#alpine-config
ssh_authorized_keys:
- ssh-rsa AAA...
- ecdsa-sha2-nistp256 AAA..
apk:
repositories:
- base_url: https://dl-cdn.alpinelinux.org/alpine
repos:
- main
- community
packages:
- python3
runcmd:
- rm /etc/runlevels/*/tiny-cloud*
- mkdir /root/.ssh
- cp /home/alpine/.ssh/authorized_keys /root/.ssh
- chmod 700 /root/.ssh
- chmod 600 /root/.ssh/authorized_keys
- lbu include /root/.ssh /home/alpine/.ssh
- ERASE_DISKS=/dev/vda setup-disk -m sys /dev/vda
- mount /dev/vda3 /mnt
- mount /dev/vda1 /mnt/boot
- sed -i 's/quiet/console=ttyS0/g' /mnt/etc/update-extlinux.conf
- sed -i 's/^serial_port=/serial_port=0/g' /mnt/etc/update-extlinux.conf
- chroot /mnt update-extlinux
- chroot /mnt rc-update add networking
- poweroff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment