--- /dev/null
+#!/bin/bash
+#
+# Test read-only state with BLKROSET and BLKROGET ioctls
+#
+# Copyright (C) 2017 David Disseldorp
+#
+# 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/>.
+
+DESCRIPTION="check read-only state"
+TIMED=1
+
+requires() {
+ _have_program xfs_io
+}
+
+test_device() {
+ echo "Running ${TEST_NAME}"
+
+ blockdev --getro "$TEST_DEV"
+ xfs_io -c "pwrite -w -S 0xaa -b 2M 0 2M" -d "$TEST_DEV" >>"$FULL"
+ dd if="$TEST_DEV" bs=2M count=1 2>>"$FULL" | hexdump
+ blockdev --setro "$TEST_DEV"
+ blockdev --setro "$TEST_DEV"
+ blockdev --getro "$TEST_DEV"
+ # writes should fail while device is read-only
+ xfs_io -c "pwrite -w -S 0xbb -b 2M 0 2M" -d "$TEST_DEV" >>"$FULL"
+ dd if="$TEST_DEV" bs=2M count=1 2>>"$FULL" | hexdump
+ blockdev --setrw "$TEST_DEV"
+ blockdev --getro "$TEST_DEV"
+ xfs_io -c "pwrite -w -S 0xcc -b 2M 0 2M" -d "$TEST_DEV" >>"$FULL"
+ dd if="$TEST_DEV" bs=2M count=1 2>>"$FULL" | hexdump
+
+ echo "Test complete"
+}