source: mainline/uspace/app/mkext4/mkext4.c@ 0777a933

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0777a933 was 6ba36a0, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Setting a custom ext4 volume label.

  • Property mode set to 100644
File size: 4.1 KB
Line 
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
29/** @addtogroup mkext4
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
46static void syntax_print(void);
47static errno_t ext4_version_parse(const char *, ext4_cfg_ver_t *);
48
49int main(int argc, char **argv)
50{
51 errno_t rc;
52 char *dev_path;
53 service_id_t service_id;
54 ext4_cfg_t cfg;
55 char *endptr;
56 aoff64_t nblocks;
57 const char *label = "";
58
59 cfg.version = ext4_def_fs_version;
60
61 if (argc < 2) {
62 printf(NAME ": Error, argument missing.\n");
63 syntax_print();
64 return 1;
65 }
66
67 --argc;
68 ++argv;
69
70 while (*argv[0] == '-') {
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;
89 }
90
91 if (str_cmp(*argv, "--type") == 0) {
92 --argc;
93 ++argv;
94 if (*argv == NULL) {
95 printf(NAME ": Error, argument missing.\n");
96 syntax_print();
97 return 1;
98 }
99
100 rc = ext4_version_parse(*argv, &cfg.version);
101 if (rc != EOK) {
102 printf(NAME ": Error, invalid argument.\n");
103 syntax_print();
104 return 1;
105 }
106
107 --argc;
108 ++argv;
109 }
110
111 if (str_cmp(*argv, "--label") == 0) {
112 --argc;
113 ++argv;
114 if (*argv == NULL) {
115 printf(NAME ": Error, argument missing.\n");
116 syntax_print();
117 return 1;
118 }
119
120 label = *argv;
121
122 --argc;
123 ++argv;
124 }
125
126 if (str_cmp(*argv, "-") == 0) {
127 --argc;
128 ++argv;
129 break;
130 }
131 }
132
133 if (argc != 1) {
134 printf(NAME ": Error, unexpected argument.\n");
135 syntax_print();
136 return 1;
137 }
138
139 dev_path = *argv;
140 printf("Device: %s\n", dev_path);
141
142 rc = loc_service_get_id(dev_path, &service_id, 0);
143 if (rc != EOK) {
144 printf(NAME ": Error resolving device `%s'.\n", dev_path);
145 return 2;
146 }
147
148 cfg.volume_name = label;
149 (void) nblocks;
150
151 rc = ext4_filesystem_create(&cfg, service_id);
152 if (rc != EOK) {
153 printf(NAME ": Error initializing file system.\n");
154 return 3;
155 }
156
157 printf("Success.\n");
158
159 return 0;
160}
161
162static void syntax_print(void)
163{
164 printf("syntax: mkext4 [<options>...] <device_name>\n");
165 printf("options:\n"
166 "\t--size <sectors> Filesystem size, overrides device size\n"
167 "\t--label <label> Volume label\n"
168 "\t--type <fstype> Filesystem type (ext2, ext2old)\n");
169}
170
171static errno_t ext4_version_parse(const char *str, ext4_cfg_ver_t *ver)
172{
173 if (str_cmp(str, "ext2old") == 0) {
174 *ver = extver_ext2_old;
175 return EOK;
176 }
177
178 if (str_cmp(str, "ext2") == 0) {
179 *ver = extver_ext2;
180 return EOK;
181 }
182
183 return EINVAL;
184}
185
186/**
187 * @}
188 */
Note: See TracBrowser for help on using the repository browser.