xref: /aosp_15_r20/external/ltp/testcases/commands/df/df01.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2015 Fujitsu Ltd.
4# Copyright (c) 2018-2023 Petr Vorel <[email protected]>
5# Author: Zhang Jin <[email protected]>
6#
7# Test df command with some basic options.
8
9TST_ALL_FILESYSTEMS=1
10TST_MOUNT_DEVICE=1
11TST_CNT=12
12TST_SETUP=setup
13TST_TESTFUNC=test
14TST_NEEDS_ROOT=1
15
16setup()
17{
18	DF_FS_TYPE="$(grep -E "$TST_MNTPOINT ($TST_FS_TYPE|fuseblk)" /proc/mounts | awk 'NR==1{print $3}')"
19}
20
21df_test()
22{
23	local cmd="$1 -P"
24
25	df_verify $cmd
26	if [ $? -ne 0 ]; then
27		return
28	fi
29
30	df_check $cmd
31	if [ $? -ne 0 ]; then
32		tst_res TFAIL "'$cmd' failed, not expected."
33		return
34	fi
35
36	ROD_SILENT dd if=/dev/zero of=$TST_MNTPOINT/testimg bs=1024 count=1024
37
38	df_verify $cmd
39
40	df_check $cmd
41	if [ $? -eq 0 ]; then
42		tst_res TPASS "'$cmd' passed."
43	else
44		tst_res TFAIL "'$cmd' failed."
45	fi
46
47	ROD_SILENT rm -rf $TST_MNTPOINT/testimg
48
49	# force all the background garbage collection to run to completion
50	if [ "$TST_FS_TYPE" = "xfs" ]; then
51		tst_fsfreeze $TST_MNTPOINT
52	fi
53
54	# flush file system buffers, then we can get the actual sizes.
55	sync
56}
57
58df_verify()
59{
60	$@ >output 2>&1
61	if [ $? -ne 0 ]; then
62		grep -q -E "unrecognized option | invalid option" output
63		if [ $? -eq 0 ]; then
64			tst_res TCONF "'$@' not supported."
65			return 32
66		else
67			tst_res TFAIL "'$@' failed."
68			cat output
69			return 1
70		fi
71	fi
72}
73
74df_check()
75{
76	if [ "$(echo $@)" = "df -i -P" ]; then
77		local total=$(stat -f $TST_MNTPOINT --printf=%c)
78		local free=$(stat -f $TST_MNTPOINT --printf=%d)
79		local used=$((total-free))
80	else
81		local total=$(stat -f $TST_MNTPOINT --printf=%b)
82		local free=$(stat -f $TST_MNTPOINT --printf=%f)
83		local used=$((total-free))
84		local bsize=$(stat -f $TST_MNTPOINT --printf=%s)
85		total=$((($total * $bsize + 512)/ 1024))
86		used=$((($used * $bsize + 512) / 1024))
87	fi
88
89	grep $TST_DEVICE output | grep -q "${total}.*${used}"
90	if [ $? -ne 0 ]; then
91		echo "total: ${total}, used: ${used}"
92		echo "df saved output:"
93		cat output
94		echo "df output:"
95		$@
96		return 1
97	fi
98}
99
100test1()
101{
102	df_test "df"
103}
104
105test2()
106{
107	df_test "df -a"
108}
109
110test3()
111{
112	df_test "df -i"
113}
114
115test4()
116{
117	df_test "df -k"
118}
119
120test5()
121{
122	df_test "df -t $DF_FS_TYPE"
123}
124
125test6()
126{
127	df_test "df -T"
128}
129
130test7()
131{
132	df_test "df -v $TST_DEVICE"
133}
134
135test8()
136{
137	df_verify "df -h"
138	if [ $? -eq 0 ]; then
139		tst_res TPASS "'df -h' passed."
140	fi
141}
142
143test9()
144{
145	df_verify "df -H"
146	if [ $? -eq 0 ]; then
147		tst_res TPASS "'df -H' passed."
148	fi
149}
150
151test10()
152{
153	df_verify "df -m"
154	if [ $? -eq 0 ]; then
155		tst_res TPASS "'df -m' passed."
156	fi
157}
158
159test11()
160{
161	df_verify "df --version"
162	if [ $? -eq 0 ]; then
163		tst_res TPASS "'df --version' passed."
164	fi
165}
166
167test12()
168{
169	local fs="$DF_FS_TYPE"
170
171	local cmd="df -x $fs -P"
172
173	df_verify $cmd
174	if [ $? -ne 0 ]; then
175		return
176	fi
177
178	grep $TST_DEVICE output | grep -q $TST_MNTPOINT
179	if [ $? -ne 0 ]; then
180		tst_res TPASS "'$cmd' passed."
181	else
182		tst_res TFAIL "'$cmd' failed."
183	fi
184}
185
186. tst_test.sh
187tst_run
188