From f9b7913992bacc296b395de5d348aa2152ec2a28 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Mon, 5 Jun 2017 20:18:03 -0700 Subject: [PATCH] initial commit --- Dockerfile | 8 ++++++++ README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ build | 23 +++++++++++++++++++++++ start | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100755 build create mode 100755 start diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b0ef418 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM ubuntu:16.10 +MAINTAINER Dalton Hubble +ARG CL_CHANNEL +ARG CL_VERSION +COPY scripts /scripts +RUN /scripts/build +EXPOSE 2222 +ENTRYPOINT ["/scripts/start"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1a552b1 --- /dev/null +++ b/README.md @@ -0,0 +1,63 @@ +# coreos-kvm [![Docker Repository on Quay](https://quay.io/repository/dghubble/coreos-kvm/status?token=27944968-4bc2-40c0-af8b-6cea576ee5be "Docker Repository on Quay")](https://quay.io/repository/dghubble/coreos-kvm) + +`coreos-kvm` is a container images which runs a QEMU/KVM CoreOS VM. + +## Usage + +Run the container image as a privileged container. Provide Ignition or Cloud-Config user-data to provision the guest instance. + + # config + sudo docker run --name kvm --privileged -e CONFIG_FILE=/userdata/config.yaml -v $PWD/examples/config.yaml:/userdata/config.yaml quay.io/dghubble/coreos-kvm + + # ignition + sudo docker run --name kvm --privileged -e IGNITION_CONFIG_FILE=/userdata/ignition.json -v $PWD/examples/ignition.json:/userdata/ignition.json quay.io/dghubble/coreos-kvm + + # cloud-config + sudo docker run --name kvm --privileged -e CLOUD_CONFIG_FILE=/userdata/cloud.yaml -v $PWD/examples/cloud.yaml:/userdata/cloud.yaml quay.io/dghubble/coreos-kvm + +SSH into the container IP on port 2222. + + sudo docker network inspect bridge + ssh -p 2222 core@172.17.0.2 + +The container runs a local port (2222) forward to the CoreOS VM (22). + +### Environment Variables + +| env variable | example | +|----------------------|-------------------------| +| CONFIG_FILE | /userdata/config.yaml | +| IGNITION_CONFIG_FILE | /userdata/ignition.json | +| CLOUD_CONFIG_FILE | /userdata/cloud.yaml | +| VM_NAME | coreos-kvm | +| VM_MEMORY | 4G | +| VM_DISK_SIZE | 12G | +| HOSTFWD | hostfwd=tcp::2222-:22 | + +## Development + +To develop the container image, build it locally. + +### Container Image + +Build the container image. + + make docker-image + +### Run + +Run the container image as a privileged container. Provide Ignition or Cloud-Config user-data to provision the guest instance. + + # config + sudo docker run --name kvm --privileged -e CONFIG_FILE=/userdata/config.yaml -v $PWD/examples/config.yaml:/userdata/config.yaml dghubble/coreos-kvm + + # ignition + sudo docker run --name kvm --privileged -e IGNITION_CONFIG_FILE=/userdata/ignition.json -v $PWD/examples/ignition.json:/userdata/ignition.json dghubble/coreos-kvm + + # cloud-config + sudo docker run --name kvm --privileged -e CLOUD_CONFIG_FILE=/userdata/cloud.yaml -v $PWD/examples/cloud.yaml:/userdata/cloud.yaml dghubble/coreos-kvm + +Remove the container from another terminal. + + sudo docker stop kvm + sudo docker rm kvm diff --git a/build b/build new file mode 100755 index 0000000..26aa0f7 --- /dev/null +++ b/build @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -exu + +# dependencies +apt-get update +apt-get install -y \ + curl \ + bzip2 \ + openssh-client \ + qemu-kvm + +URL="http://${CL_CHANNEL}.release.core-os.net/amd64-usr/${CL_VERSION}/coreos_production_qemu_image.img.bz2" +echo "Downloading Container Linux $CL_CHANNEL $CL_VERSION $URL" +curl -O $URL +bzip2 -d coreos_production_qemu_image.img.bz2 + +curl -L https://github.com/coreos/container-linux-config-transpiler/releases/download/v0.3.0/ct-v0.3.0-x86_64-unknown-linux-gnu -o ct +chmod +x ct + +apt-get remove -y curl bzip2 +apt-get autoremove -y +apt-get clean +rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \ No newline at end of file diff --git a/start b/start new file mode 100755 index 0000000..0800bfc --- /dev/null +++ b/start @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +set -eox pipefail + +VM_NAME="${VM_NAME:-coreos-kvm}" +VM_MEMORY="${VM_MEMORY:-4G}" +VM_DISK_SIZE="${VM_DISK_SIZE:-12G}" +# hostfwd=[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport +HOSTFWD="${HOSTFWD:-hostfwd=tcp::2222-:22}" + +if [ -n "${CLOUD_CONFIG_FILE}" ]; then + echo "Setting cloud config file: ${CLOUD_CONFIG_FILE}" + mkdir -p "${PWD}/openstack/latest" + cp "${CLOUD_CONFIG_FILE}" "${PWD}/openstack/latest/user_data" + cat "${PWD}/openstack/latest/user_data" + set -- \ + -fsdev local,id=conf,security_model=none,readonly,path="${PWD}" \ + -device virtio-9p-pci,fsdev=conf,mount_tag=config-2 "$@" +fi + +if [ -n "${IGNITION_CONFIG_FILE}" ]; then + echo "Setting ignition config file: ${IGNITION_CONFIG_FILE}" + cat "${IGNITION_CONFIG_FILE}" + set -- -fw_cfg name=opt/com.coreos/config,file="${IGNITION_CONFIG_FILE}" "$@" +fi + +if [ -n "$CONFIG_FILE" ]; then + echo "Transpiling config file: ${CONFIG_FILE}" + cat "${CONFIG_FILE}" + ./ct -in-file $CONFIG_FILE -out-file ${PWD}/ignition.json + echo "Setting ignition config file ${PWD}/ignition.json" + cat "${PWD}/ignition.json" + set -- -fw_cfg name=opt/com.coreos/config,file="${PWD}/ignition.json" "$@" +fi + +qemu-img resize ./coreos_production_qemu_image.img "${VM_DISK_SIZE}" + +# https://alpha.release.core-os.net/amd64-usr/current/coreos_production_qemu.sh +qemu-system-x86_64 \ + -name "${VM_NAME}" \ + -enable-kvm \ + -cpu host \ + -m "${VM_MEMORY}" \ + -net nic,model=virtio \ + -net user,"${HOSTFWD}",hostname="${VM_NAME}" \ + -drive if=virtio,file=./coreos_production_qemu_image.img \ + -nographic \ + -machine accel=kvm \ + -smp 2 \ + "$@"