source: mainline/uspace/app/hrctl/hrctl.c@ 94d84a0

Last change on this file since 94d84a0 was 94d84a0, checked in by Miroslav Cimerman <mc@…>, 11 months ago

hr: initial trivial mirroring implementation

  • Property mode set to 100644
File size: 5.6 KB
Line 
1/*
2 * Copyright (c) 2024 Miroslav Cimerman
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 hrctl
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <errno.h>
37#include <getopt.h>
38#include <hr.h>
39#include <stdlib.h>
40#include <stdio.h>
41#include <str.h>
42#include <str_error.h>
43
44static void usage(void);
45
46static const char usage_str[] =
47 "Usage: hrctl [OPTION]... -n <dev_no> <devices>...\n"
48 "\n"
49 "Options:\n"
50 " -h, --help display this help and exit\n"
51 " -s, --status display status of active arrays\n"
52 " -a, --assemble=NAME assemble an existing array\n"
53 " -c, --create=NAME create new array\n"
54 " -n non-zero number of devices\n"
55 " -l, --level=LEVEL set the RAID level,\n"
56 " valid values: 0, 1, 5, linear\n"
57 " -0 striping\n"
58 " -1 mirroring\n"
59 " -5 distributed parity\n"
60 " -L linear concatenation\n"
61 "\n"
62 "Example usage:\n"
63 " hrctl --create /hr0 -0 -n 2 devices/\\hw\\0 devices/\\hw\\1\n"
64 " - creates new mirroring RAID device named /hr0 consisting\n"
65 " of 2 drives\n"
66 " hrctl --assemble /hr0 -n 2 devices/\\hw\\0 devices/\\hw\\1\n"
67 " - assembles RAID device named /hr0 consisting of 2 drives,\n"
68 " that were previously in an array\n";
69
70static struct option const long_options[] = {
71 { "help", no_argument, 0, 'h' },
72 { "status", no_argument, 0, 's' },
73 { "assemble", required_argument, 0, 'a' },
74 { "create", required_argument, 0, 'c' },
75 { "level", required_argument, 0, 'l' },
76 { 0, 0, 0, 0 }
77};
78
79static void usage(void)
80{
81 printf("%s", usage_str);
82}
83
84static errno_t get_dev_svc_ids(int argc, char **argv, int optind, int dev_no,
85 service_id_t **rdevs)
86{
87 errno_t rc;
88 int i;
89 size_t k;
90 service_id_t *devs;
91
92 devs = calloc(dev_no, sizeof(service_id_t));
93 if (devs == NULL)
94 return ENOMEM;
95
96 for (i = optind, k = 0; i < argc; i++, k++) {
97 rc = loc_service_get_id(argv[i], &devs[k], 0);
98 if (rc != EOK) {
99 printf("hrctl: error resolving device \"%s\"\n", argv[i]);
100 free(devs);
101 return EINVAL;
102 }
103 }
104
105 *rdevs = devs;
106
107 return EOK;
108}
109
110int main(int argc, char **argv)
111{
112 errno_t rc;
113 int retval, c, dev_no;
114 bool create, assemble;
115 char *name = NULL;
116 service_id_t *devs = NULL;
117 hr_t *hr;
118 hr_config_t hr_config;
119 hr_level_t level;
120
121 retval = 0;
122 level = hr_l_empty;
123 dev_no = 0;
124 create = assemble = false;
125
126 if (argc < 2) {
127 goto bad;
128 }
129
130 c = 0;
131 optreset = 1;
132 optind = 0;
133
134 while (c != -1) {
135 c = getopt_long(argc, argv, "hsc:a:l:015Ln:",
136 long_options, NULL);
137 switch (c) {
138 case 'h':
139 usage();
140 return 0;
141 case 's':
142 printf("hrctl: status not implemented yet\n");
143 return 0;
144 case 'a':
145 name = optarg;
146 assemble = true;
147 break;
148 case 'c':
149 name = optarg;
150 create = true;
151 break;
152 case 'l':
153 if (level != hr_l_empty)
154 goto bad;
155 if (str_cmp(optarg, "linear") == 0)
156 level = hr_l_linear;
157 else
158 level = strtol(optarg, NULL, 10);
159 break;
160 case '0':
161 if (level != hr_l_empty)
162 goto bad;
163 level = hr_l_0;
164 break;
165 case '1':
166 if (level != hr_l_empty)
167 goto bad;
168 level = hr_l_1;
169 break;
170 case '5':
171 if (level != hr_l_empty)
172 goto bad;
173 level = hr_l_5;
174 break;
175 case 'L':
176 if (level != hr_l_empty)
177 goto bad;
178 level = hr_l_linear;
179 break;
180 case 'n':
181 dev_no = strtol(optarg, NULL, 10);
182 if (dev_no + optind != argc)
183 goto bad;
184 rc = get_dev_svc_ids(argc, argv, optind, dev_no, &devs);
185 if (rc != EOK)
186 return 1;
187 break;
188 }
189 }
190
191 if ((create && assemble) ||
192 (!create && !assemble) ||
193 (create && level == hr_l_empty) ||
194 (assemble && level != hr_l_empty) ||
195 (dev_no == 0)) {
196 free(devs);
197 goto bad;
198 }
199
200 hr_config.level = level;
201 hr_config.dev_no = dev_no;
202 hr_config.name = name;
203 hr_config.devs = devs;
204
205 rc = hr_sess_init(&hr);
206 if (rc != EOK) {
207 printf("hrctl: hr_sess_init() rc: %s\n", str_error(rc));
208 retval = 1;
209 goto end;
210 }
211
212 if (create) {
213 rc = hr_create(hr, &hr_config);
214 printf("hrctl: hr_create() rc: %s\n", str_error(rc));
215 } else if (assemble) {
216 printf("hrctl: assemble not implemented yet\n");
217 }
218
219end:
220 free(devs);
221 hr_sess_destroy(hr);
222 return retval;
223bad:
224 printf("hrctl: bad usage, try hrctl --help\n");
225 return 1;
226}
227
228/** @}
229 */
Note: See TracBrowser for help on using the repository browser.