• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2016 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "test.h"
19 #include "ltp_priv.h"
20 #include "tst_mkfs.h"
21 #include "tst_device.h"
22 
23 #define OPTS_MAX 32
24 
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[])25 void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
26 	       const char *dev, const char *fs_type,
27 	       const char *const fs_opts[], const char *const extra_opts[])
28 {
29 	int i, pos = 1, ret;
30 	char mkfs[64];
31 	const char *argv[OPTS_MAX] = {mkfs};
32 	char fs_opts_str[1024] = "";
33 	char extra_opts_str[1024] = "";
34 
35 	if (!dev) {
36 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
37 			"No device specified");
38 		return;
39 	}
40 
41 	if (!fs_type) {
42 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
43 			"No fs_type specified");
44 		return;
45 	}
46 
47 	if (!strcmp(fs_type, "tmpfs")) {
48 		tst_resm_(file, lineno, TINFO,
49 		          "Skipping mkfs for TMPFS filesystem");
50 		return;
51 	}
52 
53 	snprintf(mkfs, sizeof(mkfs), "mkfs.%s", fs_type);
54 
55 	if (fs_opts) {
56 		for (i = 0; fs_opts[i]; i++) {
57 			argv[pos++] = fs_opts[i];
58 
59 			if (pos + 2 > OPTS_MAX) {
60 				tst_brkm_(file, lineno, TBROK, cleanup_fn,
61 					"Too many mkfs options");
62 				return;
63 			}
64 
65 			if (i)
66 				strcat(fs_opts_str, " ");
67 			strcat(fs_opts_str, fs_opts[i]);
68 		}
69 	}
70 
71 	argv[pos++] = dev;
72 
73 	if (extra_opts) {
74 		for (i = 0; extra_opts[i]; i++) {
75 			argv[pos++] = extra_opts[i];
76 
77 			if (pos + 1 > OPTS_MAX) {
78 				tst_brkm_(file, lineno, TBROK, cleanup_fn,
79 					"Too many mkfs options");
80 				return;
81 			}
82 
83 			if (i)
84 				strcat(extra_opts_str, " ");
85 			strcat(extra_opts_str, extra_opts[i]);
86 		}
87 	}
88 
89 	argv[pos] = NULL;
90 
91 	if (tst_clear_device(dev)) {
92 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
93 			"tst_clear_device() failed");
94 	}
95 
96 	tst_resm_(file, lineno, TINFO,
97 		"Formatting %s with %s opts='%s' extra opts='%s'",
98 		dev, fs_type, fs_opts_str, extra_opts_str);
99 	ret = tst_cmd(cleanup_fn, argv, "/dev/null", NULL, TST_CMD_PASS_RETVAL |
100 				  TST_CMD_TCONF_ON_MISSING);
101 
102 	switch (ret) {
103 	case 0:
104 	break;
105 	case 255:
106 		tst_brkm_(file, lineno, TCONF, cleanup_fn,
107 			"%s not found in $PATH", mkfs);
108 	break;
109 	default:
110 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
111 			"%s failed with exit code %i", mkfs, ret);
112 	}
113 }
114 
tst_dev_fs_type(void)115 const char *tst_dev_fs_type(void)
116 {
117 	const char *fs_type;
118 
119 	fs_type = getenv("LTP_DEV_FS_TYPE");
120 
121 	if (fs_type)
122 		return fs_type;
123 
124 	return DEFAULT_FS_TYPE;
125 }
126