How to create alpine linux cloud image

Currently (27 April 2022) alpine linux does not provide cloud images for Openstack. Here is how to make it.

Step-by-step guide

  1. The main thing is needed to build a simple openstack alpine image is to install cloud-init apk package!
  2. Optionally qemu-guest-agent for better integration with KVM and e2fsprogs-extra for resizing file system
  3. are installed, as well as sudo and acpi
  4. Download the latest alpine-virt iso wget https://dl-cdn.alpinelinux.org/alpine/v3.15/releases/x86_64/alpine-virt-3.15.4-x86_64.iso
  5. Create an empty 512MB image qemu-img create -f qcow2 alpine-virt-3.15.4-x86_64.qcow 512M
  6. Boot alpine from the iso file using QEMU (or anything else like VirtualBox or VMWare, in which case you may need to create an appropriate image as raw may not work) /usr/bin/qemu-system-x86_64 -drive file=alpine-virt-3.15.4-x86_64.qcow,if=virtio,cache=writeback,discard=ignore,format=qcow2 -drive file=alpine-virt-3.15.4-x86_64.iso,media=cdrom -boot once=d -vnc 0.0.0.0:15 -name alpine -machine type=pc,accel=tcg -netdev bridge,id=user.0,br=virbr0 -m 512M -device virtio-net,netdev=user.0
  7. Connect to alpine VM with a VNC client to port 0.0.0.0:5915 if using QEMU.
  8. login with root (no password)
  9. run “setup-alpine” answering “us us” for keyboard, “alpine” for hostname, “dhcp” for eth0, “sda”,
  10. “sys” for disk drive, enable “openssh”, use “1” mirror for apk, reboot.
  11. If volumes are required, then for the disk drive choose “none” and run setup-disk but growpart won’t work in this case
  12. setup-disk -m sys -L
  13. Or with the answer file:
 setup-alpine -c answers
 sed -i -e 's/alpine-test/alpine/g' answers
 sed -i -e '/^PROXYOPTS/cPROXYOPTS="none"' answers
 sed -i -e '/^APKREPOSOPTS/s/-r/-1/' answers
 sed -i -e '/hostname/d' answers
 sed -i -e 's/openntpd/chrony/' answers
 #sed -i -e '/^DISKOPTS/cDISKOPTS="-m sys -L /dev/vda"' answers
 sed -i -e '/^DISKOPTS/cDISKOPTS="-m sys /dev/sda"' answers
 sed -i -e 's/^LBUOPTS/#LBUOPTS/' answers
 sed -i -e 's/^APKCACHEOPTS/#APKCACHEOPTS/' answers
 sed -i -e '/^DNSOPTS/cDSNOPTS="-m ${DOMAIN_NAME} 8.8.8.8"' answers
 setup-alpine -f answers
 reboot
  1. Then:
 . /etc/os-release
 VERSION=`echo ${VERSION_ID}|cut -f1-2 -d"."`
  1. Enable community apk repo sed -i -e “/\/v${VERSION}\/community$s^#” /etc/apk/repositories
  2. Upgrade All Packages in OneShot apk upgrade --update-cache --available
  3. Install sudo, cloud-init, acpi, qemu-guest-agent and e2fsprogs-extra apk add sudo cloud-init acpi qemu-guest-agent e2fsprogs-extra
  4. Add cloud-init and qemu-guest-agent to startup setup-cloud-init rc-update add qemu-guest-agent
  5. prevent cloud-init config overwriting /etc/network/interfaces
 cat << EOF > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
 network:
   config: disabled
 EOF
   
 # add udhcpc staticroutes option to create a route for 169.254.169.254 Openstack datasource address 
 cat << EOF > /etc/network/interfaces
 auto lo
 iface lo inet loopback
 auto eth0
 iface eth0 inet dhcp
         udhcpc_opts -O staticroutes
 EOF
  1. allow ssh-rsa algorithm cat << EOF >> /etc/ssh/sshd_config PubkeyAuthentication yes PubkeyAcceptedAlgorithms ssh-rsa,rsa-sha2-256,rsa-sha2-512 EOF
  2. Configure Openstack datasource and unlock alpine account setting lock_passwd to false and adding passwd line! vi /etc/cloud/cloud.cfg datasource: OpenStack: timeout: 5 retries: 3
  3. metadata_urls: [“http://169.254.169.254”]
  4. max_wait: -1
  5. apply_network_config: True system_info: distro: alpine default_user: lock_passwd: false passwd: $6$e.MLvnlNXAjBVtGV$LoeBb3B0c9fZ6TSAZvKnv0lLLJa61blgpJJUGyy5KIjRRdXK6xH8am7TckNEMW8bgd8IpcBC7BnebTO9PbYi00
  6. Update /etc/inittab to allow serial console on ttyS1 (serial1), serial0 is used for logging by kvm cat << EOF >> /etc/inittab ttyS1::respawn:/sbin/getty -L 0 ttyS1 vt100 EOF
  7. Optionally update MOTD cat << EOF > /etc/motd Welcome to $PRETTY_NAME ($VERSION_ID)! Home URL: $HOME_URL EOF
  8. Poweroff poweroff
  9. Upload the alpine openstack image to glance openstack image create --public --file alpine-virt-3.15.4-x86_64.qcow --property hw_scsi_model=virtio-scsi --property hw_disk_bus=scsi --property hw_qemu_guest_agent=yes --property os_require_quiesce=yes alpine-virt-3.15.4-x86_64