blob: 734cf9bd178a138e4d3d2838a53cf71963db1998 [file] [log] [blame]
Cyril Hrubis2492ce22013-08-08 14:24:38 +02001/*
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01002 * Copyright (c) 2013-2016 Cyril Hrubis <chrubis@suse.cz>
Cyril Hrubis2492ce22013-08-08 14:24:38 +02003 *
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01004 * 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.
Cyril Hrubis2492ce22013-08-08 14:24:38 +02008 *
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01009 * This program is distributed in the hope that it will be useful,
Cyril Hrubis2492ce22013-08-08 14:24:38 +020010 * 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
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010015 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Cyril Hrubis2492ce22013-08-08 14:24:38 +020016 */
17
18#include "test.h"
Cyril Hrubise6c24c12014-06-19 12:46:45 +020019#include "ltp_priv.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010020#include "tst_mkfs.h"
Cyril Hrubis2492ce22013-08-08 14:24:38 +020021
Cyril Hrubise6c24c12014-06-19 12:46:45 +020022#define OPTS_MAX 32
Xiaoguang Wang35266852014-03-26 17:23:45 +080023
Cyril Hrubis2492ce22013-08-08 14:24:38 +020024void tst_mkfs(void (cleanup_fn)(void), const char *dev,
Zorro Langd5e17882016-03-15 11:37:48 +080025 const char *fs_type, const char *const fs_opts[],
26 const char *extra_opt)
Cyril Hrubis2492ce22013-08-08 14:24:38 +020027{
Cyril Hrubis77c81132016-04-26 12:38:18 +020028 int i, pos = 1, ret;
29 char mkfs[64];
30 const char *argv[OPTS_MAX] = {mkfs};
Xiaoguang Wang35266852014-03-26 17:23:45 +080031 char fs_opts_str[1024] = "";
Cyril Hrubis2492ce22013-08-08 14:24:38 +020032
Stanislav Kholmanskikh045920b2016-03-14 19:24:15 +030033 if (!dev)
34 tst_brkm(TBROK, cleanup_fn, "No device specified");
35
Cyril Hrubis858a5762013-08-22 11:02:09 +020036 if (!fs_type)
37 tst_brkm(TBROK, cleanup_fn, "No fs_type specified");
38
Cyril Hrubis77c81132016-04-26 12:38:18 +020039 snprintf(mkfs, sizeof(mkfs), "mkfs.%s", fs_type);
40
Xiaoguang Wang35266852014-03-26 17:23:45 +080041 if (fs_opts) {
42 for (i = 0; fs_opts[i]; i++) {
43 argv[pos++] = fs_opts[i];
Cyril Hrubisb3c4fbe2013-08-28 14:41:57 +020044
Xiaoguang Wang35266852014-03-26 17:23:45 +080045 if (pos + 2 > OPTS_MAX) {
46 tst_brkm(TBROK, cleanup_fn,
47 "Too much mkfs options");
48 }
Cyril Hrubisb3c4fbe2013-08-28 14:41:57 +020049
Xiaoguang Wang35266852014-03-26 17:23:45 +080050 if (i)
51 strcat(fs_opts_str, " ");
52 strcat(fs_opts_str, fs_opts[i]);
53 }
54 }
55
56 argv[pos++] = dev;
Zorro Langd5e17882016-03-15 11:37:48 +080057
58 if (extra_opt) {
59 argv[pos++] = extra_opt;
60
61 if (pos + 1 > OPTS_MAX) {
62 tst_brkm(TBROK, cleanup_fn,
63 "Too much mkfs options");
64 }
65 }
66
Xiaoguang Wang35266852014-03-26 17:23:45 +080067 argv[pos] = NULL;
68
Zorro Langd5e17882016-03-15 11:37:48 +080069 tst_resm(TINFO, "Formatting %s with %s opts='%s' extra opts='%s'",
70 dev, fs_type, fs_opts_str, extra_opt ? extra_opt : "");
Cyril Hrubis77c81132016-04-26 12:38:18 +020071 ret = tst_run_cmd(cleanup_fn, argv, "/dev/null", NULL, 1);
72
73 switch (ret) {
74 case 0:
75 break;
76 case 255:
77 tst_brkm(TCONF, cleanup_fn,
78 "%s not found in $PATH", mkfs);
79 default:
80 tst_brkm(TBROK, cleanup_fn,
81 "%s failed with %i", mkfs, ret);
82 }
Cyril Hrubis2492ce22013-08-08 14:24:38 +020083}
Cyril Hrubise6c24c12014-06-19 12:46:45 +020084
85const char *tst_dev_fs_type(void)
86{
87 const char *fs_type;
88
89 fs_type = getenv("LTP_DEV_FS_TYPE");
90
91 if (fs_type)
92 return fs_type;
93
94 return DEFAULT_FS_TYPE;
95}
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010096
97void safe_mkfs(const int lineno, const char *fname, const char *dev,
98 const char *fs_type, const char *const fs_opts[],
99 const char *extra_opt)
100{
101 /* ignore for now, will fix once all tst_mkfs() users are converted */
102 (void)lineno;
103 (void)fname;
104
105 tst_mkfs(NULL, dev, fs_type, fs_opts, extra_opt);
106}