From 2091e3431ab5bc0b23194145435221ccdb8295d5 Mon Sep 17 00:00:00 2001 From: Adnan RIHAN Date: Fri, 30 Sep 2022 14:27:46 +0200 Subject: [PATCH] Alpine Linux (Virt) packer script --- packer/alpine-linux-virt/.gitignore | 2 + packer/alpine-linux-virt/README.md | 22 ++++++++ packer/alpine-linux-virt/alpine.json | 35 ++++++++++++ packer/alpine-linux-virt/files/alpine.conf | 24 ++++++++ packer/alpine-linux-virt/files/install.sh | 66 ++++++++++++++++++++++ packer/alpine-linux-virt/macos.json | 3 + 6 files changed, 152 insertions(+) create mode 100644 packer/alpine-linux-virt/.gitignore create mode 100644 packer/alpine-linux-virt/README.md create mode 100644 packer/alpine-linux-virt/alpine.json create mode 100644 packer/alpine-linux-virt/files/alpine.conf create mode 100644 packer/alpine-linux-virt/files/install.sh create mode 100644 packer/alpine-linux-virt/macos.json diff --git a/packer/alpine-linux-virt/.gitignore b/packer/alpine-linux-virt/.gitignore new file mode 100644 index 0000000..23cbc22 --- /dev/null +++ b/packer/alpine-linux-virt/.gitignore @@ -0,0 +1,2 @@ +output-qemu +packer_cache diff --git a/packer/alpine-linux-virt/README.md b/packer/alpine-linux-virt/README.md new file mode 100644 index 0000000..f2a4868 --- /dev/null +++ b/packer/alpine-linux-virt/README.md @@ -0,0 +1,22 @@ +# Packer for Alpine (Virt) GNS3 appliance + +This is the bare version of `Alpine Linux` installed from iso, no extra package added. + +Build in 1m12s on `macOS Monterey`. + +### Linux (untested) + +```bash +packer build alpine.json +``` + +> :information_source: Uses `tcg` QEMU accelerator. + + +### macOS (tested) + +```bash +packer build -var-file macos.json alpine.json +``` + +> :information_source: Uses `hvf` QEMU accelerator. Actually much much faster than the default one, `packer` will fail on `macOS` without `hvf` (timing issue). diff --git a/packer/alpine-linux-virt/alpine.json b/packer/alpine-linux-virt/alpine.json new file mode 100644 index 0000000..92e486a --- /dev/null +++ b/packer/alpine-linux-virt/alpine.json @@ -0,0 +1,35 @@ +{ + "variables": { + "accelerator": "tcg" + }, + "builders": [ + { + "communicator": "none", + "type": "qemu", + "accelerator": "{{ user `accelerator` }}", + "use_default_display": true, + "iso_url": "https://dl-cdn.alpinelinux.org/alpine/v3.16/releases/x86_64/alpine-virt-3.16.2-x86_64.iso", + "iso_checksum": "6c7cb998ec2c8925d5a1239410a4d224b771203f916a18f8015f31169dd767a2", + "http_directory": "./files", + "vm_name": "alpine-virt-3.16.img", + "disk_size": "92M", + "format": "raw", + "qemu_img_args": { + "create": ["-o", "preallocation=off"] + }, + "disk_interface": "virtio", + "disk_discard": "unmap", + "boot_wait": "15s", + "boot_key_interval": "20ms", + "boot_keygroup_interval": "100ms", + "boot_command": [ + "root", + "ifconfig lo up", + "ifconfig eth0 up && udhcpc -i eth0", + "wget -q http://{{ .HTTPIP }}:{{ .HTTPPort }}/alpine.conf && setup-alpine -e -f alpine.conf", + "wget -q http://{{ .HTTPIP }}:{{ .HTTPPort }}/install.sh && chmod +x install.sh && ./install.sh", + "poweroff" + ] + } + ] +} diff --git a/packer/alpine-linux-virt/files/alpine.conf b/packer/alpine-linux-virt/files/alpine.conf new file mode 100644 index 0000000..0fc153c --- /dev/null +++ b/packer/alpine-linux-virt/files/alpine.conf @@ -0,0 +1,24 @@ +KEYMAPOPTS=none +HOSTNAMEOPTS=alpine +DEVDOPTS=mdev +INTERFACESOPTS="auto lo +iface lo inet loopback + +auto eth0 +iface eth0 inet manual +# DHCP +#iface eth0 inet dhcp +# Static +#iface eth0 inet static +# address IP_ADDRESS/MASK +# gateway GW_IP_ADDRESS +# dns-nameserver DNS_IP_ADDRESS +" +TIMEZONEOPTS=UTC +PROXYOPTS=none +APKREPOSOPTS=-1 +SSHDOPTS=none +NTPOPTS=none +DISKOPTS=none +#LBUOPTS=none +APKCACHEOPTS=none diff --git a/packer/alpine-linux-virt/files/install.sh b/packer/alpine-linux-virt/files/install.sh new file mode 100644 index 0000000..19a9d02 --- /dev/null +++ b/packer/alpine-linux-virt/files/install.sh @@ -0,0 +1,66 @@ +#!/bin/ash + +DRIVE="/dev/vda" + +# Format drive, from sector 2048 to end, bootable +fdisk $DRIVE << EOS +o +n +p +1 +2048 + +a +1 +w +EOS + +if [[ $? != 0 ]]; then + echo "Error while partitioning device" >&2 + exit 1 +fi + +# Format partition without "64bit" and "has_journal" flags +apk add e2fsprogs && mkfs.ext4 -O \^64bit,\^has_journal ${DRIVE}1 && mount ${DRIVE}1 /mnt -t ext4 && mkdir /mnt/boot && apk del e2fsprogs +if [[ $? != 0 ]]; then + echo "Error somewhere while making filesystem or mounting partition" >&2 + exit 1 +fi + +# Actually install Alpine Linux in mounted directory +BOOT_SIZE=0 setup-disk -k virt -s 0 /mnt +if [[ $? != 0 ]]; then + echo "Error while installing Alpine" >&2 + exit 1 +fi + +# Install the MBR +dd if=/mnt/usr/share/syslinux/mbr.bin of=/dev/vda +if [[ $? != 0 ]]; then + echo "Error while fixing kernel path" >&2 + exit 1 +fi + +# Fix kernel and initrd path, enable console on ttyS0 +sed -i.bkp 's_LINUX vmlinuz_LINUX /boot/vmlinuz_; s_INITRD initram_INITRD /boot/initram_; /APPEND/s/$/ console=ttyS0,115200/' /mnt/boot/extlinux.conf +if [[ $? != 0 ]]; then + echo "Error while fixing kernel path" >&2 + exit 1 +fi + +# Enable autologin on ttyS0 +sed -i.bkp 's/^ttyS0/#ttyS0/g' /mnt/etc/inittab && echo "ttyS0::respawn:/bin/login -f root" >> /mnt/etc/inittab +if [[ $? != 0 ]]; then + echo "Error while enabling autologin on ttyS0" >&2 + exit 1 +fi + +# Write 0 +dd if=/dev/zero bs=1M of=/mnt/zero ; rm -f /mnt/zero + +# fsck +umount ${DRIVE}1 && apk add e2fsprogs && fsck.ext4 ${DRIVE}1 +if [[ $? != 0 ]]; then + echo "Couldn't fsck partition" >&2 + exit 1 +fi diff --git a/packer/alpine-linux-virt/macos.json b/packer/alpine-linux-virt/macos.json new file mode 100644 index 0000000..9940f66 --- /dev/null +++ b/packer/alpine-linux-virt/macos.json @@ -0,0 +1,3 @@ +{ + "accelerator": "hvf" +}