~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to .gitlab-ci/crosvm-runner.sh

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
 
3
 
set -e
4
 
 
5
 
#
6
 
# Helper to generate CIDs for virtio-vsock based communication with processes
7
 
# running inside crosvm guests.
8
 
#
9
 
# A CID is a 32-bit Context Identifier to be assigned to a crosvm instance
10
 
# and must be unique across the host system. For this purpose, let's take
11
 
# the least significant 25 bits from CI_JOB_ID as a base and generate a 7-bit
12
 
# prefix number to handle up to 128 concurrent crosvm instances per job runner.
13
 
#
14
 
# As a result, the following variables are set:
15
 
#  - VSOCK_CID: the crosvm unique CID to be passed as a run argument
16
 
#
17
 
#  - VSOCK_STDOUT, VSOCK_STDERR: the port numbers the guest should accept
18
 
#    vsock connections on in order to transfer output messages
19
 
#
20
 
#  - VSOCK_TEMP_DIR: the temporary directory path used to pass additional
21
 
#    context data towards the guest
22
 
#
23
 
set_vsock_context() {
24
 
    [ -n "${CI_JOB_ID}" ] || {
25
 
        echo "Missing or unset CI_JOB_ID env variable" >&2
26
 
        exit 1
27
 
    }
28
 
 
29
 
    local dir_prefix="/tmp-vsock."
30
 
    local cid_prefix=0
31
 
    unset VSOCK_TEMP_DIR
32
 
 
33
 
    while [ ${cid_prefix} -lt 128 ]; do
34
 
        VSOCK_TEMP_DIR=${dir_prefix}${cid_prefix}
35
 
        mkdir "${VSOCK_TEMP_DIR}" >/dev/null 2>&1 && break || unset VSOCK_TEMP_DIR
36
 
        cid_prefix=$((cid_prefix + 1))
37
 
    done
38
 
 
39
 
    [ -n "${VSOCK_TEMP_DIR}" ] || return 1
40
 
 
41
 
    VSOCK_CID=$(((CI_JOB_ID & 0x1ffffff) | ((cid_prefix & 0x7f) << 25)))
42
 
    VSOCK_STDOUT=5001
43
 
    VSOCK_STDERR=5002
44
 
 
45
 
    return 0
46
 
}
47
 
 
48
 
# The dEQP binary needs to run from the directory it's in
49
 
if [ -n "${1##*.sh}" ] && [ -z "${1##*"deqp"*}" ]; then
50
 
    DEQP_BIN_DIR=$(dirname "$1")
51
 
    export DEQP_BIN_DIR
52
 
fi
53
 
 
54
 
set_vsock_context || { echo "Could not generate crosvm vsock CID" >&2; exit 1; }
55
 
 
56
 
# Ensure cleanup on script exit
57
 
trap 'exit ${exit_code}' INT TERM
58
 
trap 'exit_code=$?; [ -z "${CROSVM_PID}${SOCAT_PIDS}" ] || kill ${CROSVM_PID} ${SOCAT_PIDS} >/dev/null 2>&1 || true; rm -rf ${VSOCK_TEMP_DIR}' EXIT
59
 
 
60
 
# Securely pass the current variables to the crosvm environment
61
 
echo "Variables passed through:"
62
 
SCRIPT_DIR=$(readlink -en "${0%/*}")
63
 
${SCRIPT_DIR}/common/generate-env.sh | tee ${VSOCK_TEMP_DIR}/crosvm-env.sh
64
 
 
65
 
# Set the crosvm-script as the arguments of the current script
66
 
echo "$@" > ${VSOCK_TEMP_DIR}/crosvm-script.sh
67
 
 
68
 
# Setup networking
69
 
/usr/sbin/iptables-legacy -w -t nat -A POSTROUTING -o eth0 -j MASQUERADE
70
 
echo 1 > /proc/sys/net/ipv4/ip_forward
71
 
 
72
 
# Start background processes to receive output from guest
73
 
socat -u vsock-connect:${VSOCK_CID}:${VSOCK_STDERR},retry=200,interval=0.1 stderr &
74
 
SOCAT_PIDS=$!
75
 
socat -u vsock-connect:${VSOCK_CID}:${VSOCK_STDOUT},retry=200,interval=0.1 stdout &
76
 
SOCAT_PIDS="${SOCAT_PIDS} $!"
77
 
 
78
 
# Prepare to start crosvm
79
 
unset DISPLAY
80
 
unset XDG_RUNTIME_DIR
81
 
 
82
 
CROSVM_KERN_ARGS="quiet console=null root=my_root rw rootfstype=virtiofs ip=192.168.30.2::192.168.30.1:255.255.255.0:crosvm:eth0"
83
 
CROSVM_KERN_ARGS="${CROSVM_KERN_ARGS} init=${SCRIPT_DIR}/crosvm-init.sh -- ${VSOCK_STDOUT} ${VSOCK_STDERR} ${VSOCK_TEMP_DIR}"
84
 
 
85
 
[ "${CROSVM_GALLIUM_DRIVER}" = "llvmpipe" ] && \
86
 
    CROSVM_LIBGL_ALWAYS_SOFTWARE=true || CROSVM_LIBGL_ALWAYS_SOFTWARE=false
87
 
 
88
 
set +e -x
89
 
 
90
 
# We aren't testing the host driver here, so we don't need to validate NIR on the host
91
 
NIR_DEBUG="novalidate" \
92
 
LIBGL_ALWAYS_SOFTWARE=${CROSVM_LIBGL_ALWAYS_SOFTWARE} \
93
 
GALLIUM_DRIVER=${CROSVM_GALLIUM_DRIVER} \
94
 
crosvm run \
95
 
    --gpu "${CROSVM_GPU_ARGS}" -m 4096 -c 2 --disable-sandbox \
96
 
    --shared-dir /:my_root:type=fs:writeback=true:timeout=60:cache=always \
97
 
    --host_ip "192.168.30.1" --netmask "255.255.255.0" --mac "AA:BB:CC:00:00:12" \
98
 
    --cid ${VSOCK_CID} -p "${CROSVM_KERN_ARGS}" \
99
 
    /lava-files/${KERNEL_IMAGE_NAME:-bzImage} > ${VSOCK_TEMP_DIR}/crosvm 2>&1 &
100
 
 
101
 
# Wait for crosvm process to terminate
102
 
CROSVM_PID=$!
103
 
wait ${CROSVM_PID}
104
 
CROSVM_RET=$?
105
 
unset CROSVM_PID
106
 
 
107
 
[ ${CROSVM_RET} -eq 0 ] && {
108
 
    # socat background processes terminate gracefully on remote peers exit
109
 
    wait
110
 
    unset SOCAT_PIDS
111
 
    # The actual return code is the crosvm guest script's exit code
112
 
    CROSVM_RET=$(cat ${VSOCK_TEMP_DIR}/exit_code 2>/dev/null)
113
 
    # Force error when the guest script's exit code is not available
114
 
    CROSVM_RET=${CROSVM_RET:-1}
115
 
}
116
 
 
117
 
# Show crosvm output on error to help with debugging
118
 
[ ${CROSVM_RET} -eq 0 ] || {
119
 
    set +x
120
 
    echo "Dumping crosvm output.." >&2
121
 
    cat ${VSOCK_TEMP_DIR}/crosvm >&2
122
 
    set -x
123
 
}
124
 
 
125
 
exit ${CROSVM_RET}