summaryrefslogtreecommitdiffstats
path: root/tests/ts/losetup/losetup-loop
blob: 037e5c7c6d33ba5e7f3fbb800aee70c660e81142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/bash

#
# Copyright (C) 2016 Stanislav Brabec <sbrabec@suse.cz>
#
# This file is part of util-linux.
#
# This file 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 2 of the License, or
# (at your option) any later version.
#
# This file 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.
#

TS_TOPDIR="${0%/*}/../.."
TS_DESC="losetup-loop"

. $TS_TOPDIR/functions.sh
ts_init "$*"

ts_check_test_command "$TS_CMD_LOSETUP"
ts_check_test_command "$TS_CMD_LSBLK"

ts_skip_nonroot
ts_check_losetup

. "$TS_SELF/losetup_functions.sh"

#
# Backing file is a block device
#
ts_scsi_debug_init dev_size_mb=11
BACKFILE="$TS_DEVICE"

# All tests are separated by "udevadm settle" because loop device exists some time after
# "losetup -d". This device confuses some tests. And find-race-condition, tests,
# whether re-use of this device works.

ts_init_subtest "find-race-condition"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
$TS_CMD_LOSETUP -d "$LODEV"
# The loop device may or may not exist here because no "udevadm settle".
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
udevadm settle
dd if=/dev/zero of="$LODEV" count=1 bs=1 >/dev/null 2>&1
$TS_CMD_LOSETUP --list | grep -q "$LODEV\b"
ts_log $?
udevadm settle
$TS_CMD_LOSETUP -d "$LODEV" >/dev/null 2>&1
ts_log "Success"
ts_finalize_subtest

udevadm settle

ts_init_subtest "find-re-use"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
udevadm settle
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d "$LODEVR"
	fi
fi
$TS_CMD_LOSETUP -d "$LODEV"
ts_log "Success"
ts_finalize_subtest

udevadm settle

ts_init_subtest "find-no-re-use"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=0 --sizelimit=1MiB $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=1MiB --sizelimit=1MiB $BACKFILE )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
udevadm settle
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d "$LODEVR"
	fi
fi
$TS_CMD_LOSETUP -d "$LODEV"
ts_log "Success"
ts_finalize_subtest

udevadm settle

ts_init_subtest "find-conflict"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=1MiB --sizelimit=2MiB $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=2MiB --sizelimit=2MiB $BACKFILE >/dev/null 2>&1 )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
udevadm settle
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d "$LODEVR"
	fi
fi
$TS_CMD_LOSETUP -d "$LODEV"
ts_log "Success"
ts_finalize_subtest

udevadm settle

ts_init_subtest "find-conflict-no-sizelimit"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=1MiB $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=2MiB --sizelimit=2MiB $BACKFILE >/dev/null 2>&1 )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
udevadm settle
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d "$LODEVR"
	fi
fi
$TS_CMD_LOSETUP -d "$LODEV"
ts_log "Success"
ts_finalize_subtest

udevadm settle

ts_init_subtest "find-conflict-readonly"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --read-only $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=2MiB --sizelimit=2MiB $BACKFILE >/dev/null 2>&1 )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
udevadm settle
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d "$LODEVR"
	fi
fi
$TS_CMD_LOSETUP -d "$LODEV"
$TS_CMD_LOSETUP -d "$LODEVR" >/dev/null 2>&1
ts_log "Success"
ts_finalize_subtest

udevadm settle

ts_init_subtest "find-ok-no-sizelimit"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=1MiB --sizelimit=1MiB $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=2MiB $BACKFILE )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
udevadm settle
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d "$LODEVR"
	fi
fi
$TS_CMD_LOSETUP -d "$LODEV"
ts_log "Success"
ts_finalize_subtest

udevadm settle

ts_init_subtest "plain-conflict"
if [ "$TS_PARALLEL" = "yes" ]; then
	# There is a race in $LODEV is usage
	ts_skip_subtest "no-reentrant"
else
	LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
	if [ -z "$LODEV" ]; then
		ts_log "Failed to create loop device"
	fi
	LODEVR=$( $TS_CMD_LOSETUP --find )
	$TS_CMD_LOSETUP --nooverlap "$LODEVR" $BACKFILE >/dev/null 2>&1
	ts_log $?
	udevadm settle
	$TS_CMD_LOSETUP -d "$LODEV"
	$TS_CMD_LOSETUP -d "$LODEVR" >/dev/null 2>&1
	ts_log "Success"
	ts_finalize_subtest
fi

udevadm settle

ts_init_subtest "plain-readonly"
if [ "$TS_PARALLEL" = "yes" ]; then
	# There is a race in $LODEV is usage
	ts_skip_subtest "no-reentrant"
else
	LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --read-only $BACKFILE )
	if [ -z "$LODEV" ]; then
		ts_log "Failed to create loop device"
	fi
	LODEVR=$( $TS_CMD_LOSETUP --find )
	$TS_CMD_LOSETUP --nooverlap "$LODEVR" $BACKFILE >/dev/null 2>&1
	ts_log $?
	udevadm settle
	$TS_CMD_LOSETUP -d "$LODEV"
	$TS_CMD_LOSETUP -d "$LODEVR" >/dev/null 2>&1
	ts_log "Success"
	ts_finalize_subtest
fi

udevadm settle

ts_log "Success"
ts_finalize