summaryrefslogtreecommitdiffstats
path: root/src/kernel/tests/lib/tst_mkfs.c
blob: 38b2e715130f0c077c7393d27886801bca0d1cc3 (plain) (blame)
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
/*
 * Copyright (c) 2013-2016 Cyril Hrubis <chrubis@suse.cz>
 *
 * 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 2 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/>.
 */

#include "test.h"
#include "ltp_priv.h"
#include "tst_mkfs.h"
#include "tst_device.h"

#define OPTS_MAX 32

void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
	       const char *dev, const char *fs_type,
	       const char *const fs_opts[], const char *const extra_opts[])
{
	int i, pos = 1, ret;
	char mkfs[64];
	const char *argv[OPTS_MAX] = {mkfs};
	char fs_opts_str[1024] = "";
	char extra_opts_str[1024] = "";

	if (!dev) {
		tst_brkm(TBROK, cleanup_fn,
			 "%s:%d: No device specified", file, lineno);
		return;
	}

	if (!fs_type) {
		tst_brkm(TBROK, cleanup_fn,
			 "%s:%d: No fs_type specified", file, lineno);
		return;
	}

	snprintf(mkfs, sizeof(mkfs), "mkfs.%s", fs_type);

	if (fs_opts) {
		for (i = 0; fs_opts[i]; i++) {
			argv[pos++] = fs_opts[i];

			if (pos + 2 > OPTS_MAX) {
				tst_brkm(TBROK, cleanup_fn,
				         "%s:%d: Too much mkfs options",
					 file, lineno);
				return;
			}

			if (i)
				strcat(fs_opts_str, " ");
			strcat(fs_opts_str, fs_opts[i]);
		}
	}

	argv[pos++] = dev;

	if (extra_opts) {
		for (i = 0; extra_opts[i]; i++) {
			argv[pos++] = extra_opts[i];

			if (pos + 1 > OPTS_MAX) {
				tst_brkm(TBROK, cleanup_fn,
				         "%s:%d: Too much mkfs options", file, lineno);
				return;
			}

			if (i)
				strcat(extra_opts_str, " ");
			strcat(extra_opts_str, extra_opts[i]);
		}
	}

	argv[pos] = NULL;

	if (tst_clear_device(dev))
		tst_brkm(TBROK, cleanup_fn, "tst_clear_device() failed");

	tst_resm(TINFO, "Formatting %s with %s opts='%s' extra opts='%s'",
	         dev, fs_type, fs_opts_str, extra_opts_str);
	ret = tst_cmd(cleanup_fn, argv, "/dev/null", NULL, TST_CMD_PASS_RETVAL |
				  TST_CMD_TCONF_ON_MISSING);

	switch (ret) {
	case 0:
	break;
	case 255:
		tst_brkm(TCONF, cleanup_fn,
			 "%s:%d: %s not found in $PATH", file, lineno, mkfs);
	break;
	default:
		tst_brkm(TBROK, cleanup_fn,
			 "%s:%d: %s failed with %i", file, lineno, mkfs, ret);
	}
}

const char *tst_dev_fs_type(void)
{
	const char *fs_type;

	fs_type = getenv("LTP_DEV_FS_TYPE");

	if (fs_type)
		return fs_type;

	return DEFAULT_FS_TYPE;
}