Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 1 | /* |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2013-2016 Cyril Hrubis <chrubis@suse.cz> |
Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 3 | * |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 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. |
Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 8 | * |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 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 |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include "test.h" |
Cyril Hrubis | e6c24c1 | 2014-06-19 12:46:45 +0200 | [diff] [blame] | 19 | #include "ltp_priv.h" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 20 | #include "tst_mkfs.h" |
Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 21 | |
Cyril Hrubis | e6c24c1 | 2014-06-19 12:46:45 +0200 | [diff] [blame] | 22 | #define OPTS_MAX 32 |
Xiaoguang Wang | 3526685 | 2014-03-26 17:23:45 +0800 | [diff] [blame] | 23 | |
Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 24 | void tst_mkfs(void (cleanup_fn)(void), const char *dev, |
Zorro Lang | d5e1788 | 2016-03-15 11:37:48 +0800 | [diff] [blame] | 25 | const char *fs_type, const char *const fs_opts[], |
| 26 | const char *extra_opt) |
Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 27 | { |
Cyril Hrubis | 77c8113 | 2016-04-26 12:38:18 +0200 | [diff] [blame] | 28 | int i, pos = 1, ret; |
| 29 | char mkfs[64]; |
| 30 | const char *argv[OPTS_MAX] = {mkfs}; |
Xiaoguang Wang | 3526685 | 2014-03-26 17:23:45 +0800 | [diff] [blame] | 31 | char fs_opts_str[1024] = ""; |
Cyril Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 32 | |
Stanislav Kholmanskikh | 045920b | 2016-03-14 19:24:15 +0300 | [diff] [blame] | 33 | if (!dev) |
| 34 | tst_brkm(TBROK, cleanup_fn, "No device specified"); |
| 35 | |
Cyril Hrubis | 858a576 | 2013-08-22 11:02:09 +0200 | [diff] [blame] | 36 | if (!fs_type) |
| 37 | tst_brkm(TBROK, cleanup_fn, "No fs_type specified"); |
| 38 | |
Cyril Hrubis | 77c8113 | 2016-04-26 12:38:18 +0200 | [diff] [blame] | 39 | snprintf(mkfs, sizeof(mkfs), "mkfs.%s", fs_type); |
| 40 | |
Xiaoguang Wang | 3526685 | 2014-03-26 17:23:45 +0800 | [diff] [blame] | 41 | if (fs_opts) { |
| 42 | for (i = 0; fs_opts[i]; i++) { |
| 43 | argv[pos++] = fs_opts[i]; |
Cyril Hrubis | b3c4fbe | 2013-08-28 14:41:57 +0200 | [diff] [blame] | 44 | |
Xiaoguang Wang | 3526685 | 2014-03-26 17:23:45 +0800 | [diff] [blame] | 45 | if (pos + 2 > OPTS_MAX) { |
| 46 | tst_brkm(TBROK, cleanup_fn, |
| 47 | "Too much mkfs options"); |
| 48 | } |
Cyril Hrubis | b3c4fbe | 2013-08-28 14:41:57 +0200 | [diff] [blame] | 49 | |
Xiaoguang Wang | 3526685 | 2014-03-26 17:23:45 +0800 | [diff] [blame] | 50 | if (i) |
| 51 | strcat(fs_opts_str, " "); |
| 52 | strcat(fs_opts_str, fs_opts[i]); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | argv[pos++] = dev; |
Zorro Lang | d5e1788 | 2016-03-15 11:37:48 +0800 | [diff] [blame] | 57 | |
| 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 Wang | 3526685 | 2014-03-26 17:23:45 +0800 | [diff] [blame] | 67 | argv[pos] = NULL; |
| 68 | |
Zorro Lang | d5e1788 | 2016-03-15 11:37:48 +0800 | [diff] [blame] | 69 | tst_resm(TINFO, "Formatting %s with %s opts='%s' extra opts='%s'", |
| 70 | dev, fs_type, fs_opts_str, extra_opt ? extra_opt : ""); |
Cyril Hrubis | 77c8113 | 2016-04-26 12:38:18 +0200 | [diff] [blame] | 71 | 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 Hrubis | 2492ce2 | 2013-08-08 14:24:38 +0200 | [diff] [blame] | 83 | } |
Cyril Hrubis | e6c24c1 | 2014-06-19 12:46:45 +0200 | [diff] [blame] | 84 | |
| 85 | const 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 96 | |
| 97 | void 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 | } |