#!/usr/bin/env bash set -eo pipefail DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )" ORIG_DISK_IMAGE=$1 function usage { cat< Éxecute l'image disque donnée avec Qemu EOF exit 1 } function find_available_port { read lower_port upper_port < /proc/sys/net/ipv4/ip_local_port_range while :; do for (( port = lower_port ; port <= upper_port ; port++ )); do (echo > /dev/tcp/0.0.0.0/$port) 2>/dev/null >/dev/null if [ $? -ne 0 ]; then echo $port return fi done done } [ -z "$ORIG_DISK_IMAGE" ] && usage DISK_IMAGE_DIR="$DIR/../.run/$(dirname $ORIG_DISK_IMAGE)" ORIG_DISK_IMAGE=$(readlink -f "$ORIG_DISK_IMAGE") DISK_IMAGE_NAME=$(date -r "$ORIG_DISK_IMAGE" | shasum - | cut -f1 -d ' ').img DISK_IMAGE="$DISK_IMAGE_DIR/$DISK_IMAGE_NAME" if [ -f "$DISK_IMAGE" ]; then echo "Using existing qcow2 image '$DISK_IMAGE'..." else mkdir -p "$(dirname $DISK_IMAGE)" rm -f "$DISK_IMAGE_DIR"/*.img cd "$(dirname $DISK_IMAGE)" echo "Generating qcow2 image '$DISK_IMAGE'..." qemu-img create -f qcow2 -b "$ORIG_DISK_IMAGE" "$(basename $DISK_IMAGE)" cd - fi SSH_PORT=$(find_available_port) echo "Starting VM... SSH will be available on '127.0.0.1:${SSH_PORT}'" qemu-system-x86_64 -boot once=d \ -device virtio-net,netdev=user.0 \ -m 1024M -machine type=pc,accel=kvm \ -display sdl \ -hda "${DISK_IMAGE}" \ -netdev user,id=user.0,hostfwd=tcp::${SSH_PORT}-:22