diff --git a/base-containers/kvm/Dockerfile b/base-containers/kvm/Dockerfile index 051f0407f..7e9f33084 100644 --- a/base-containers/kvm/Dockerfile +++ b/base-containers/kvm/Dockerfile @@ -95,7 +95,7 @@ RUN git clone https://github.com/amluto/virtme &&\ WORKDIR / # Get expect script launching the VM -COPY run.expect setup.sh / +COPY run.expect setup.sh telnet_login.sh / ENV KVM_START_PATH=/run.expect # Add telnetd config diff --git a/base-containers/kvm/setup.sh b/base-containers/kvm/setup.sh index 3ab868730..837f72cf9 100755 --- a/base-containers/kvm/setup.sh +++ b/base-containers/kvm/setup.sh @@ -1,11 +1,28 @@ #! /bin/bash -x +# Load env variables STUDENT_DIR=/task/student KVM_DIR="${STUDENT_DIR}/kvm" +SCRIPTS_DIR="${STUDENT_DIR}/scripts" +STUDENT_LOGIN="${SCRIPTS_DIR}/student_login" +# Set kvm group in human-readable way groupdel kvm groupadd -g $(stat -c '%g' /dev/kvm) kvm + +# Create RW dir mounted in the KVM mkdir "${KVM_DIR}" +chown worker:worker /task chown worker:worker "${KVM_DIR}" -cp "${STUDENT_DIR}/scripts/bzImage" /tmp + +# Copy the kernel in a path readable by "worker" within the SSH container +cp "${SCRIPTS_DIR}/bzImage" /tmp + +# Copy student_login file, if any, in a path readable by "worker" within the VM +if [[ -f "${STUDENT_LOGIN}" ]] +then + cp "${STUDENT_LOGIN}" / +fi + +# Launch the KVM as "worker" su - worker -G worker -G kvm -c "virtme-run --cpus 2 --memory 256 --kimg /tmp/bzImage --rwdir=/tmp/student=${KVM_DIR}" diff --git a/base-containers/kvm/telnet b/base-containers/kvm/telnet index 7a2c08a83..9c5e8817a 100644 --- a/base-containers/kvm/telnet +++ b/base-containers/kvm/telnet @@ -5,6 +5,6 @@ socket_type = stream wait = no user = root server = /usr/sbin/telnetd -server_args = -E /bin/bash +server_args = -E /telnet_login.sh disable = no } diff --git a/base-containers/kvm/telnet_login.sh b/base-containers/kvm/telnet_login.sh new file mode 100755 index 000000000..9e9ab81d3 --- /dev/null +++ b/base-containers/kvm/telnet_login.sh @@ -0,0 +1,21 @@ +#! /bin/bash + +FIRST=/tmp/.first +STUDENT_LOGIN="/student_login" + +if [[ ! -f "${FIRST}" ]] +then + # On first login within the KVM + touch "${FIRST}" + + if [[ -f "${STUDENT_LOGIN}" ]] + then + # If the task specifies a given setup to launch (e.g. a mininet script), run it + ./"${STUDENT_LOGIN}" + else + # Else, simply spawn a shell in the KVM + /bin/bash + fi +else + /bin/bash +fi