--- /dev/null
+#!/bin/bash
+#
+# NBD helper functions
+#
+# Copyright (C) 2018 Josef Bacik
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+_have_nbd_netlink_support() {
+ if ! _have_program genl-ctrl-list; then
+ return 1
+ fi
+ if ! genl-ctrl-list | grep -q nbd; then
+ SKIP_REASON="nbd does not support netlink"
+ return 1
+ fi
+ return 0
+}
+
+_have_nbd_netlink() {
+ _have_nbd && _have_nbd_netlink_support
+}
+
+_start_nbd_server() {
+ truncate -s 10G "${TMPDIR}/export"
+ cat > "${TMPDIR}/nbd.conf" << EOF
+[generic]
+[export]
+exportname=${TMPDIR}/export
+EOF
+ nbd-server -p "${TMPDIR}/nbd.pid" -C "${TMPDIR}/nbd.conf"
+}
+
+_stop_nbd_server() {
+ kill -SIGTERM "$(cat "${TMPDIR}/nbd.pid")"
+ rm -f "${TMPDIR}/export"
+}
_have_module loop && _have_program losetup
}
+_have_nbd() {
+ _have_module nbd && _have_program nbd-server && \
+ _have_program nbd-client
+}
+
_have_blktrace() {
# CONFIG_BLK_DEV_IO_TRACE might still be disabled, but this is easier
# to check. We can fix it if someone complains.