summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lwirzenius@wikimedia.org>2020-09-18 15:58:48 +0300
committerLars Wirzenius <lwirzenius@wikimedia.org>2020-09-18 15:58:48 +0300
commitbf7a156b8eb2939e603cce3063569db3463bd20b (patch)
tree5a845e68669acc97bb597825dbd9f4e46a5d04b6
parentd624977e3679e13afd8fd7f028e9c68aa360da6a (diff)
downloadick-contractor-bf7a156b8eb2939e603cce3063569db3463bd20b.tar.gz
change to start qemu
-rwxr-xr-xvm-qemu.sh96
1 files changed, 68 insertions, 28 deletions
diff --git a/vm-qemu.sh b/vm-qemu.sh
index 3492eb6..ab39ef1 100755
--- a/vm-qemu.sh
+++ b/vm-qemu.sh
@@ -1,8 +1,8 @@
-#!/bin/sh
+#!/bin/bash
#
# Create a new VM using libvirt on the local host.
-set -eu
+set -eux
verbose()
@@ -17,35 +17,75 @@ die()
exit 1
}
+cloud_init_iso()
+{
+ local hostname
+ local pubkey
+ local iso
+ local tmpdir
-# Check parameters.
+ hostname="$1"
+ pubkey="$2"
-if [ "$#" -lt 2 ]
-then
- die "Usage: $ IMAGE PORT"
-fi
+ iso="$hostname.iso"
+ tmpdir="$(mktemp -d)"
+ cat <<EOF > "$tmpdir/meta-data"
+# Amazon EC2 style metadata
+local-hostname: $hostname
+EOF
+
+ cat <<EOF > "$tmpdir/user-data"
+#cloud-config
+ssh_authorized_keys:
+- $(cat "$pubkey")
+EOF
-# Command line parameters: image file and port number for SSH.
-image="$1"
-port="$2"
-verbose "creating VM from image $image"
-verbose "once it's running, log in from another terminal: ssh -p $port manager@localhost"
+ genisoimage -quiet -output "$iso" -volid cidata -joliet -rock "$tmpdir"
-shift 2
+ rm -r "$tmpdir"
+ echo "$iso"
+}
-# Does the image exist?
-if [ ! -e "$image" ]
-then
- echo "$image does not exist" 1>&2
- exit 1
-fi
-
-# Start VM.
-qemu-system-x86_64 \
- -enable-kvm \
- -m 8192 \
- -drive "file=$image,format=raw,if=virtio" \
- -device virtio-net,netdev=user.0 \
- -netdev "user,id=user.0,hostfwd=tcp::$port-:22"
-
+main() {
+ local image
+ local port
+ local pubkey
+ local iso
+
+ # Check parameters.
+ if [ "$#" -lt 3 ]
+ then
+ die "Usage: $ IMAGE PORT SSH-PUBKEY-FILE"
+ fi
+
+ image="$1"
+ port="$2"
+ pubkey="$3"
+ shift 3
+
+ # Does the image exist?
+ if [ ! -e "$image" ]
+ then
+ echo "$image does not exist" 1>&2
+ exit 1
+ fi
+
+ iso="$(cloud_init_iso contractor "$pubkey")"
+
+ # Start VM.
+ qemu-system-x86_64 \
+ -name contractor \
+ -m 8192 \
+ -cpu host \
+ -smp cpus=6 \
+ -enable-kvm \
+ -drive "file=$image,format=qcow2,cache=none,if=virtio" \
+ -drive "file=$iso,if=ide,media=cdrom" \
+ -device virtio-net,netdev=user.0 \
+ -netdev "user,id=user.0,hostfwd=tcp::$port-:22" \
+ -nographic \
+ "$@"
+}
+
+main "$@"