[aab85d90] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[b1834a01] | 29 | /** @addtogroup mkext4
|
---|
[aab85d90] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief Tool for creating new Ext4 file systems.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <ext4/filesystem.h>
|
---|
| 40 | #include <loc.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <str.h>
|
---|
| 43 |
|
---|
| 44 | #define NAME "mkext4"
|
---|
| 45 |
|
---|
| 46 | static void syntax_print(void);
|
---|
[2175178] | 47 | static errno_t ext4_version_parse(const char *, ext4_cfg_ver_t *);
|
---|
[aab85d90] | 48 |
|
---|
| 49 | int main(int argc, char **argv)
|
---|
| 50 | {
|
---|
| 51 | errno_t rc;
|
---|
| 52 | char *dev_path;
|
---|
| 53 | service_id_t service_id;
|
---|
[6ba36a0] | 54 | ext4_cfg_t cfg;
|
---|
[aab85d90] | 55 | char *endptr;
|
---|
| 56 | aoff64_t nblocks;
|
---|
[6ba36a0] | 57 | const char *label = "";
|
---|
[4bb4cf88] | 58 | unsigned int bsize = 4096;
|
---|
[aab85d90] | 59 |
|
---|
[6ba36a0] | 60 | cfg.version = ext4_def_fs_version;
|
---|
[2175178] | 61 |
|
---|
[aab85d90] | 62 | if (argc < 2) {
|
---|
| 63 | printf(NAME ": Error, argument missing.\n");
|
---|
| 64 | syntax_print();
|
---|
| 65 | return 1;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | --argc;
|
---|
| 69 | ++argv;
|
---|
| 70 |
|
---|
[6abdef37] | 71 | while (*argv && *argv[0] == '-') {
|
---|
[aab85d90] | 72 | if (str_cmp(*argv, "--size") == 0) {
|
---|
| 73 | --argc;
|
---|
| 74 | ++argv;
|
---|
| 75 | if (*argv == NULL) {
|
---|
| 76 | printf(NAME ": Error, argument missing.\n");
|
---|
| 77 | syntax_print();
|
---|
| 78 | return 1;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | nblocks = strtol(*argv, &endptr, 10);
|
---|
| 82 | if (*endptr != '\0') {
|
---|
| 83 | printf(NAME ": Error, invalid argument.\n");
|
---|
| 84 | syntax_print();
|
---|
| 85 | return 1;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | --argc;
|
---|
| 89 | ++argv;
|
---|
[6abdef37] | 90 | continue;
|
---|
[aab85d90] | 91 | }
|
---|
| 92 |
|
---|
[91fcbabc] | 93 | if (str_cmp(*argv, "--bsize") == 0) {
|
---|
| 94 | --argc;
|
---|
| 95 | ++argv;
|
---|
| 96 | if (*argv == NULL) {
|
---|
| 97 | printf(NAME ": Error, argument missing.\n");
|
---|
| 98 | syntax_print();
|
---|
| 99 | return 1;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | bsize = strtol(*argv, &endptr, 10);
|
---|
| 103 | if (*endptr != '\0') {
|
---|
| 104 | printf(NAME ": Error, invalid argument.\n");
|
---|
| 105 | syntax_print();
|
---|
| 106 | return 1;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | --argc;
|
---|
| 110 | ++argv;
|
---|
| 111 | continue;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[2175178] | 114 | if (str_cmp(*argv, "--type") == 0) {
|
---|
| 115 | --argc;
|
---|
| 116 | ++argv;
|
---|
| 117 | if (*argv == NULL) {
|
---|
| 118 | printf(NAME ": Error, argument missing.\n");
|
---|
| 119 | syntax_print();
|
---|
| 120 | return 1;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[6ba36a0] | 123 | rc = ext4_version_parse(*argv, &cfg.version);
|
---|
[2175178] | 124 | if (rc != EOK) {
|
---|
| 125 | printf(NAME ": Error, invalid argument.\n");
|
---|
| 126 | syntax_print();
|
---|
| 127 | return 1;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | --argc;
|
---|
| 131 | ++argv;
|
---|
[6abdef37] | 132 | continue;
|
---|
[2175178] | 133 | }
|
---|
| 134 |
|
---|
[aab85d90] | 135 | if (str_cmp(*argv, "--label") == 0) {
|
---|
| 136 | --argc;
|
---|
| 137 | ++argv;
|
---|
| 138 | if (*argv == NULL) {
|
---|
| 139 | printf(NAME ": Error, argument missing.\n");
|
---|
| 140 | syntax_print();
|
---|
| 141 | return 1;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | label = *argv;
|
---|
| 145 |
|
---|
| 146 | --argc;
|
---|
| 147 | ++argv;
|
---|
[6abdef37] | 148 | continue;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | if (str_cmp(*argv, "--help") == 0) {
|
---|
| 152 | syntax_print();
|
---|
| 153 | return 0;
|
---|
[aab85d90] | 154 | }
|
---|
| 155 |
|
---|
| 156 | if (str_cmp(*argv, "-") == 0) {
|
---|
| 157 | --argc;
|
---|
| 158 | ++argv;
|
---|
| 159 | break;
|
---|
[6abdef37] | 160 | } else {
|
---|
| 161 | printf(NAME ": Invalid argument: %s\n", *argv);
|
---|
| 162 | syntax_print();
|
---|
| 163 | return 1;
|
---|
[aab85d90] | 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | if (argc != 1) {
|
---|
| 168 | printf(NAME ": Error, unexpected argument.\n");
|
---|
| 169 | syntax_print();
|
---|
| 170 | return 1;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | dev_path = *argv;
|
---|
| 174 | printf("Device: %s\n", dev_path);
|
---|
| 175 |
|
---|
| 176 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
---|
| 177 | if (rc != EOK) {
|
---|
| 178 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
| 179 | return 2;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[6ba36a0] | 182 | cfg.volume_name = label;
|
---|
[91fcbabc] | 183 | cfg.bsize = bsize;
|
---|
[aab85d90] | 184 | (void) nblocks;
|
---|
| 185 |
|
---|
[6ba36a0] | 186 | rc = ext4_filesystem_create(&cfg, service_id);
|
---|
[aab85d90] | 187 | if (rc != EOK) {
|
---|
| 188 | printf(NAME ": Error initializing file system.\n");
|
---|
| 189 | return 3;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | printf("Success.\n");
|
---|
| 193 |
|
---|
| 194 | return 0;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | static void syntax_print(void)
|
---|
| 198 | {
|
---|
| 199 | printf("syntax: mkext4 [<options>...] <device_name>\n");
|
---|
| 200 | printf("options:\n"
|
---|
| 201 | "\t--size <sectors> Filesystem size, overrides device size\n"
|
---|
[2175178] | 202 | "\t--label <label> Volume label\n"
|
---|
[91fcbabc] | 203 | "\t--type <fstype> Filesystem type (ext2, ext2old)\n"
|
---|
[4bb4cf88] | 204 | "\t--bsize <bytes> Filesystem block size in bytes (default = 4096)\n");
|
---|
[2175178] | 205 | }
|
---|
| 206 |
|
---|
| 207 | static errno_t ext4_version_parse(const char *str, ext4_cfg_ver_t *ver)
|
---|
| 208 | {
|
---|
| 209 | if (str_cmp(str, "ext2old") == 0) {
|
---|
| 210 | *ver = extver_ext2_old;
|
---|
| 211 | return EOK;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | if (str_cmp(str, "ext2") == 0) {
|
---|
| 215 | *ver = extver_ext2;
|
---|
| 216 | return EOK;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | return EINVAL;
|
---|
[aab85d90] | 220 | }
|
---|
| 221 |
|
---|
| 222 | /**
|
---|
| 223 | * @}
|
---|
| 224 | */
|
---|