Skip to content
Snippets Groups Projects
qga_comms.sh 1.50 KiB
#!/usr/bin/env bash

if [ $# -eq 0 ]; then
    echo "Usage: $0 <message>"
    exit 1
fi

user_msg=$1

first_user=$({ echo '{"execute":"guest-get-users"}'; sleep 1; } | socat unix-connect:/tmp/qga.sock - | jq -r '.return[0].user')
echo "First user: $first_user"

file_path="/home/"$first_user"/busted"
echo "Creating file: $file_path"

create_file_cmd=$(jq -n --arg file_path "$file_path" '{"execute":"guest-file-open", "arguments":{"path":$file_path,"mode":"w"}}')
file_handle=$({ echo $create_file_cmd; sleep 1; } | socat unix-connect:/tmp/qga.sock - | jq '.return')

bytes_count=$(echo -n $user_msg | wc -c | awk '{print $1}')
encoded_str=$(echo -n $user_msg | base64 | tr -d '\n')

int_handle=$((file_handle))
int_bytes_count=$((bytes_count))

write_to_file_cmd=$(jq -n --argjson int_handle "$int_handle" \
    --arg encoded_str "$encoded_str" \
    --argjson int_bytes_count "$int_bytes_count" \
    '{"execute":"guest-file-write", "arguments":{"handle":$int_handle,"buf-b64":$encoded_str,"count":$int_bytes_count}}')

echo "Writing to $file_path ($file_handle)..."

{ echo $write_to_file_cmd; sleep 1; } | socat unix-connect:/tmp/qga.sock - > /dev/null

close_file_cmd=$(jq -n --argjson int_handle "$int_handle" \
    '{"execute":"guest-file-close", "arguments":{"handle":$int_handle}}')


echo "Closing $file_path ($file_handle)..."

{ echo $close_file_cmd; sleep 1; } | socat unix-connect:/tmp/qga.sock - > /dev/null

echo "Shutting down VM..."
{ echo '{"execute":"guest-shutdown"}'; sleep 1; } | socat unix-connect:/tmp/qga.sock -