Skip to content

Commit

Permalink
[env/kvm] Launch task script, if any, on student SSH login
Browse files Browse the repository at this point in the history
  • Loading branch information
nrybowski committed Aug 9, 2023
1 parent 703ba30 commit d726555
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion base-containers/kvm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion base-containers/kvm/setup.sh
Original file line number Diff line number Diff line change
@@ -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}"
2 changes: 1 addition & 1 deletion base-containers/kvm/telnet
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions base-containers/kvm/telnet_login.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d726555

Please sign in to comment.