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