[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 = "";
|
---|
[aab85d90] | 58 |
|
---|
[6ba36a0] | 59 | cfg.version = ext4_def_fs_version;
|
---|
[2175178] | 60 |
|
---|
[aab85d90] | 61 | if (argc < 2) {
|
---|
| 62 | printf(NAME ": Error, argument missing.\n");
|
---|
| 63 | syntax_print();
|
---|
| 64 | return 1;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | --argc;
|
---|
| 68 | ++argv;
|
---|
| 69 |
|
---|
[6abdef37] | 70 | while (*argv && *argv[0] == '-') {
|
---|
[aab85d90] | 71 | if (str_cmp(*argv, "--size") == 0) {
|
---|
| 72 | --argc;
|
---|
| 73 | ++argv;
|
---|
| 74 | if (*argv == NULL) {
|
---|
| 75 | printf(NAME ": Error, argument missing.\n");
|
---|
| 76 | syntax_print();
|
---|
| 77 | return 1;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | nblocks = strtol(*argv, &endptr, 10);
|
---|
| 81 | if (*endptr != '\0') {
|
---|
| 82 | printf(NAME ": Error, invalid argument.\n");
|
---|
| 83 | syntax_print();
|
---|
| 84 | return 1;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | --argc;
|
---|
| 88 | ++argv;
|
---|
[6abdef37] | 89 | continue;
|
---|
[aab85d90] | 90 | }
|
---|
| 91 |
|
---|
[2175178] | 92 | if (str_cmp(*argv, "--type") == 0) {
|
---|
| 93 | --argc;
|
---|
| 94 | ++argv;
|
---|
| 95 | if (*argv == NULL) {
|
---|
| 96 | printf(NAME ": Error, argument missing.\n");
|
---|
| 97 | syntax_print();
|
---|
| 98 | return 1;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[6ba36a0] | 101 | rc = ext4_version_parse(*argv, &cfg.version);
|
---|
[2175178] | 102 | if (rc != EOK) {
|
---|
| 103 | printf(NAME ": Error, invalid argument.\n");
|
---|
| 104 | syntax_print();
|
---|
| 105 | return 1;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | --argc;
|
---|
| 109 | ++argv;
|
---|
[6abdef37] | 110 | continue;
|
---|
[2175178] | 111 | }
|
---|
| 112 |
|
---|
[aab85d90] | 113 | if (str_cmp(*argv, "--label") == 0) {
|
---|
| 114 | --argc;
|
---|
| 115 | ++argv;
|
---|
| 116 | if (*argv == NULL) {
|
---|
| 117 | printf(NAME ": Error, argument missing.\n");
|
---|
| 118 | syntax_print();
|
---|
| 119 | return 1;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | label = *argv;
|
---|
| 123 |
|
---|
| 124 | --argc;
|
---|
| 125 | ++argv;
|
---|
[6abdef37] | 126 | continue;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | if (str_cmp(*argv, "--help") == 0) {
|
---|
| 130 | syntax_print();
|
---|
| 131 | return 0;
|
---|
[aab85d90] | 132 | }
|
---|
| 133 |
|
---|
| 134 | if (str_cmp(*argv, "-") == 0) {
|
---|
| 135 | --argc;
|
---|
| 136 | ++argv;
|
---|
| 137 | break;
|
---|
[6abdef37] | 138 | } else {
|
---|
| 139 | printf(NAME ": Invalid argument: %s\n", *argv);
|
---|
| 140 | syntax_print();
|
---|
| 141 | return 1;
|
---|
[aab85d90] | 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (argc != 1) {
|
---|
| 146 | printf(NAME ": Error, unexpected argument.\n");
|
---|
| 147 | syntax_print();
|
---|
| 148 | return 1;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | dev_path = *argv;
|
---|
| 152 | printf("Device: %s\n", dev_path);
|
---|
| 153 |
|
---|
| 154 | rc = loc_service_get_id(dev_path, &service_id, 0);
|
---|
| 155 | if (rc != EOK) {
|
---|
| 156 | printf(NAME ": Error resolving device `%s'.\n", dev_path);
|
---|
| 157 | return 2;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[6ba36a0] | 160 | cfg.volume_name = label;
|
---|
[4f38ad77] | 161 | cfg.bsize = 4096;
|
---|
[aab85d90] | 162 | (void) nblocks;
|
---|
| 163 |
|
---|
[6ba36a0] | 164 | rc = ext4_filesystem_create(&cfg, service_id);
|
---|
[aab85d90] | 165 | if (rc != EOK) {
|
---|
| 166 | printf(NAME ": Error initializing file system.\n");
|
---|
| 167 | return 3;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | printf("Success.\n");
|
---|
| 171 |
|
---|
| 172 | return 0;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | static void syntax_print(void)
|
---|
| 176 | {
|
---|
| 177 | printf("syntax: mkext4 [<options>...] <device_name>\n");
|
---|
| 178 | printf("options:\n"
|
---|
| 179 | "\t--size <sectors> Filesystem size, overrides device size\n"
|
---|
[2175178] | 180 | "\t--label <label> Volume label\n"
|
---|
| 181 | "\t--type <fstype> Filesystem type (ext2, ext2old)\n");
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | static errno_t ext4_version_parse(const char *str, ext4_cfg_ver_t *ver)
|
---|
| 185 | {
|
---|
| 186 | if (str_cmp(str, "ext2old") == 0) {
|
---|
| 187 | *ver = extver_ext2_old;
|
---|
| 188 | return EOK;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | if (str_cmp(str, "ext2") == 0) {
|
---|
| 192 | *ver = extver_ext2;
|
---|
| 193 | return EOK;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | return EINVAL;
|
---|
[aab85d90] | 197 | }
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
| 200 | * @}
|
---|
| 201 | */
|
---|