prepare-vm (1019B)
1 #!/bin/sh 2 set -eE 3 4 trap 'cleanup' ERR 5 6 # Initializes an Alpine Linux vm for testing 7 IMAGE_URL="https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-virt-3.20.1-x86_64.iso" 8 IMAGE=${IMAGE_URL##*/} 9 10 ### 11 12 former_pwd=$(pwd) 13 cd -P -- "$(dirname "$0")" || return 14 15 download_image() { 16 echo "downloading alpine image" 17 18 [ ! -f "$IMAGE" ] && curl -o "$IMAGE" $IMAGE_URL 19 [ ! -f "$IMAGE.sha256" ] && curl -o "$IMAGE.sha256" "$IMAGE_URL.sha256" 20 21 sha256sum -c "$IMAGE.sha256" 22 } 23 24 create_vm() { 25 echo "initializing alpine vm" 26 27 [ ! -f "alpine.qcow2" ] && qemu-img create -f qcow2 alpine.qcow2 8G 28 29 mkdir ./tmp.share 30 31 cp ./init ./answers ./tmp.share 32 33 ./controller $IMAGE 34 35 if [ -f "alpine.qcow2" ]; then 36 echo "generated alpine.qcow2" 37 mv ./alpine.qcow2 ./tmp.share/initramfs ./tmp.share/vmlinuz-virt "$former_pwd" 38 fi 39 } 40 41 cleanup() { 42 echo "cleaning up" 43 44 rm -fv "$IMAGE" "$IMAGE.sha256" ./alpine.qcow2 45 46 if [ -d ./tmp.share ]; then 47 rm -rfv ./tmp.share 48 fi 49 } 50 51 download_image 52 create_vm 53 cleanup