Home | History | Annotate | Download | only in lib
      1 /*
      2  * Copyright (c) 2013-2016 Cyril Hrubis <chrubis (at) 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 
     22 #define OPTS_MAX 32
     23 
     24 void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
     25 	       const char *dev, const char *fs_type,
     26 	       const char *const fs_opts[], const char *extra_opt)
     27 {
     28 	int i, pos = 1, ret;
     29 	char mkfs[64];
     30 	const char *argv[OPTS_MAX] = {mkfs};
     31 	char fs_opts_str[1024] = "";
     32 
     33 	if (!dev) {
     34 		tst_brkm(TBROK, cleanup_fn,
     35 			 "%s:%d: No device specified", file, lineno);
     36 		return;
     37 	}
     38 
     39 	if (!fs_type) {
     40 		tst_brkm(TBROK, cleanup_fn,
     41 			 "%s:%d: No fs_type specified", file, lineno);
     42 		return;
     43 	}
     44 
     45 	snprintf(mkfs, sizeof(mkfs), "mkfs.%s", fs_type);
     46 
     47 	if (fs_opts) {
     48 		for (i = 0; fs_opts[i]; i++) {
     49 			argv[pos++] = fs_opts[i];
     50 
     51 			if (pos + 2 > OPTS_MAX) {
     52 				tst_brkm(TBROK, cleanup_fn,
     53 				         "%s:%d: Too much mkfs options",
     54 					 file, lineno);
     55 				return;
     56 			}
     57 
     58 			if (i)
     59 				strcat(fs_opts_str, " ");
     60 			strcat(fs_opts_str, fs_opts[i]);
     61 		}
     62 	}
     63 
     64 	argv[pos++] = dev;
     65 
     66 	if (extra_opt) {
     67 		argv[pos++] = extra_opt;
     68 
     69 		if (pos + 1 > OPTS_MAX) {
     70 			tst_brkm(TBROK, cleanup_fn,
     71 			         "%s:%d: Too much mkfs options", file, lineno);
     72 			return;
     73 		}
     74 	}
     75 
     76 	argv[pos] = NULL;
     77 
     78 	tst_resm(TINFO, "Formatting %s with %s opts='%s' extra opts='%s'",
     79 	         dev, fs_type, fs_opts_str, extra_opt ? extra_opt : "");
     80 	ret = tst_run_cmd(cleanup_fn, argv, "/dev/null", NULL, 1);
     81 
     82 	switch (ret) {
     83 	case 0:
     84 	break;
     85 	case 255:
     86 		tst_brkm(TCONF, cleanup_fn,
     87 			 "%s:%d: %s not found in $PATH", file, lineno, mkfs);
     88 	default:
     89 		tst_brkm(TBROK, cleanup_fn,
     90 			 "%s:%d: %s failed with %i", mkfs, ret, file, lineno);
     91 	}
     92 }
     93 
     94 const char *tst_dev_fs_type(void)
     95 {
     96 	const char *fs_type;
     97 
     98 	fs_type = getenv("LTP_DEV_FS_TYPE");
     99 
    100 	if (fs_type)
    101 		return fs_type;
    102 
    103 	return DEFAULT_FS_TYPE;
    104 }
    105