fstests: use _scratch_shutdown instead of calling src/godown
[xfstests-dev.git] / tests / generic / 050
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2009 Christoph Hellwig.
4 #
5 # FS QA Test No. 050
6 #
7 # Check out various mount/remount/unmount scenarious on a read-only blockdev.
8 #
9 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16
17 _cleanup()
18 {
19         cd /
20         blockdev --setrw $SCRATCH_DEV
21 }
22 trap "_cleanup; exit \$status" 0 1 2 3 15
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27
28 # real QA test starts here
29 _supported_fs generic
30 _supported_os Linux
31
32 _require_scratch_nocheck
33 _require_scratch_shutdown
34 _require_local_device $SCRATCH_DEV
35 _require_norecovery
36
37 _scratch_mkfs >/dev/null 2>&1
38
39 filter_ro_mount() {
40         local arg=""
41
42         if [ -n "$expect_mount_failure" ]; then
43                 arg="s|mount: $SCRATCH_MNT: permission denied|mount: device write-protected, mounting read-only|g"
44         fi
45         sed -e "$arg" | _filter_ro_mount
46 }
47
48 # Mounting with quota on XFS requires a writable fs, which means
49 # we expect to fail the ro blockdev test with with EPERM.
50 expect_mount_failure=
51 if [ "$FSTYP" = "xfs" ] && echo "$MOUNT_OPTIONS" | grep -q quota ; then
52         expect_mount_failure=1
53 fi
54
55 #
56 # Mark the device read-only
57 #
58 echo "setting device read-only"
59 blockdev --setro $SCRATCH_DEV
60
61 #
62 # Mount it, and make sure we can't write to it, and we can unmount it again
63 #
64 echo "mounting read-only block device:"
65 _try_scratch_mount 2>&1 | filter_ro_mount
66 if [ "${PIPESTATUS[0]}" -eq 0 ]; then
67         echo "touching file on read-only filesystem (should fail)"
68         touch $SCRATCH_MNT/foo 2>&1 | _filter_scratch
69
70         #
71         # Apparently this used to be broken at some point:
72         #       http://oss.sgi.com/bugzilla/show_bug.cgi?id=807
73         #
74         echo "unmounting read-only filesystem"
75         _scratch_unmount 2>&1 | _filter_scratch
76 elif [ -n "${expect_mount_failure}" ]; then
77         # Mount failed, so simulate EROFS instead of scribbling on root fs
78         echo "touching file on read-only filesystem (should fail)"
79         echo "touch: cannot touch 'SCRATCH_MNT/foo': Read-only file system"
80         echo "unmounting read-only filesystem"
81 else
82         echo "Mount failed, though it wasn't supposed to!"
83 fi
84
85 echo "setting device read-write"
86 blockdev --setrw $SCRATCH_DEV
87
88 echo "mounting read-write block device:"
89 _try_scratch_mount 2>&1 | _filter_scratch
90
91 echo "touch files"
92 touch $SCRATCH_MNT/{0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}
93
94 echo "going down:"
95 _scratch_shutdown -f
96
97 echo "unmounting shutdown filesystem:"
98 _scratch_unmount 2>&1 | _filter_scratch
99
100 echo "setting device read-only"
101 blockdev --setro $SCRATCH_DEV
102
103 #
104 # Mounting a filesystem that requires log-recovery fails unless
105 # -o norecovery is used.
106 #
107 echo "mounting filesystem that needs recovery on a read-only device:"
108 _try_scratch_mount 2>&1 | _filter_ro_mount
109
110 echo "unmounting read-only filesystem"
111 _scratch_unmount 2>&1 | _filter_scratch | _filter_ending_dot
112
113 #
114 # This is the way out if the underlying device really is read-only.
115 # Doesn't mean it's a good idea in practice, more a last resort
116 # data recovery hack.
117 #
118 echo "mounting filesystem with -o norecovery on a read-only device:"
119 _try_scratch_mount -o norecovery 2>&1 | filter_ro_mount
120 if [ "${PIPESTATUS[0]}" -eq 0 ]; then
121         echo "unmounting read-only filesystem"
122         _scratch_unmount 2>&1 | _filter_scratch
123 elif [ -n "${expect_mount_failure}" ]; then
124         # Mount failed, simulate correct output
125         echo "unmounting read-only filesystem"
126 else
127         echo "Mount failed, though it wasn't supposed to!"
128 fi
129
130 echo "setting device read-write"
131 blockdev --setrw $SCRATCH_DEV
132
133 #
134 # But log recovery is performed when mount with -o ro as long as
135 # the underlying device is not write protected.
136 #
137 echo "mounting filesystem that needs recovery with -o ro:"
138 _try_scratch_mount -o ro 2>&1 | _filter_scratch
139
140 # success, all done
141 echo "*** done"
142 rm -f $seqres.full
143 status=0