]> git.proxmox.com Git - mirror_qemu.git/blob - tests/qemu-iotests/common.nbd
block: test block-stream with a base node that is used by block-commit
[mirror_qemu.git] / tests / qemu-iotests / common.nbd
1 #!/usr/bin/env 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
22 nbd_unix_socket="${TEST_DIR}/qemu-nbd.sock"
23 nbd_tcp_addr="127.0.0.1"
24 nbd_pid_file="${TEST_DIR}/qemu-nbd.pid"
25
26 nbd_server_stop()
27 {
28 local NBD_PID
29 if [ -f "$nbd_pid_file" ]; then
30 read NBD_PID < "$nbd_pid_file"
31 rm -f "$nbd_pid_file"
32 if [ -n "$NBD_PID" ]; then
33 kill "$NBD_PID"
34 fi
35 fi
36 rm -f "$nbd_unix_socket"
37 }
38
39 nbd_server_wait_for_unix_socket()
40 {
41 pid=$1
42
43 for ((i = 0; i < 300; i++))
44 do
45 if [ -r "$nbd_unix_socket" ]; then
46 return
47 fi
48 kill -s 0 $pid 2>/dev/null
49 if test $? != 0
50 then
51 echo "qemu-nbd unexpectedly quit"
52 exit 1
53 fi
54 sleep 0.1
55 done
56 echo "Failed in check of unix socket created by qemu-nbd"
57 exit 1
58 }
59
60 nbd_server_start_unix_socket()
61 {
62 nbd_server_stop
63 $QEMU_NBD -v -t -k "$nbd_unix_socket" "$@" &
64 nbd_server_wait_for_unix_socket $!
65 }
66
67 nbd_server_set_tcp_port()
68 {
69 (ss --help) >/dev/null 2>&1 || _notrun "ss utility not found, skipping test"
70
71 for ((port = 10809; port <= 10909; port++))
72 do
73 if ! ss -tln | grep -sqE ":$port\b"; then
74 nbd_tcp_port=$port
75 return
76 fi
77 done
78
79 echo "Cannot find free TCP port for nbd in range 10809-10909"
80 exit 1
81 }
82
83 nbd_server_wait_for_tcp_socket()
84 {
85 pid=$1
86
87 for ((i = 0; i < 300; i++))
88 do
89 if ss -tln | grep -sqE ":$nbd_tcp_port\b"; then
90 return
91 fi
92 kill -s 0 $pid 2>/dev/null
93 if test $? != 0
94 then
95 echo "qemu-nbd unexpectedly quit"
96 exit 1
97 fi
98 sleep 0.1
99 done
100 echo "Failed in check of TCP socket created by qemu-nbd"
101 exit 1
102 }
103
104 nbd_server_start_tcp_socket()
105 {
106 nbd_server_stop
107 $QEMU_NBD -v -t -b $nbd_tcp_addr -p $nbd_tcp_port "$@" &
108 nbd_server_wait_for_tcp_socket $!
109 }