xref: /qemu/tests/qemu-iotests/common.nbd (revision b39b58d5d0da3e7057d7d636641018b0fc25139b)
1#!/bin/bash
2# -*- shell-script-mode -*-
3#
4# Helpers for NBD server related config
5#
6# Copyright (C) 2018 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22nbd_unix_socket="${TEST_DIR}/qemu-nbd.sock"
23nbd_pid_file="${TEST_DIR}/qemu-nbd.pid"
24
25function nbd_server_stop()
26{
27    local NBD_PID
28    if [ -f "$nbd_pid_file" ]; then
29        read NBD_PID < "$nbd_pid_file"
30        rm -f "$nbd_pid_file"
31        if [ -n "$NBD_PID" ]; then
32            kill "$NBD_PID"
33        fi
34    fi
35    rm -f "$nbd_unix_socket"
36}
37
38function nbd_server_wait_for_unix_socket()
39{
40    pid=$1
41
42    for ((i = 0; i < 300; i++))
43    do
44        if [ -r "$nbd_unix_socket" ]; then
45            return
46        fi
47        kill -s 0 $pid 2>/dev/null
48        if test $? != 0
49        then
50            echo "qemu-nbd unexpectedly quit"
51            exit 1
52        fi
53        sleep 0.1
54    done
55    echo "Failed in check of unix socket created by qemu-nbd"
56    exit 1
57}
58
59function nbd_server_start_unix_socket()
60{
61    nbd_server_stop
62    $QEMU_NBD -v -t -k "$nbd_unix_socket" "$@" &
63    nbd_server_wait_for_unix_socket $!
64}
65