Changeset e192339 in mainline


Ignore:
Timestamp:
2024-09-05T21:58:12Z (8 months ago)
Author:
Miroslav Cimerman <mc@…>
Children:
095a989
Parents:
44ea48e
Message:

hrctl: add option for creating array from config file

Location:
uspace/app/hrctl
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/hrctl/hrctl.c

    r44ea48e re192339  
    3737#include <getopt.h>
    3838#include <hr.h>
     39#include <sif.h>
    3940#include <stdlib.h>
    4041#include <stdio.h>
     
    4243#include <str_error.h>
    4344
     45#define HRCTL_SAMPLE_CONFIG_PATH "/cfg/sample_hr_config.sif"
     46
    4447static void usage(void);
     48static errno_t fill_config_devs(int, char **, int, hr_config_t *);
     49static errno_t load_config(const char *, hr_config_t *);
    4550
    4651static const char usage_str[] =
     
    4853    "\n"
    4954    "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"
     55    "  -h, --help              display this help and exit\n"
     56    "  -C, --config-file=path  create an array from file,\n"
     57    "                          sample file at: " HRCTL_SAMPLE_CONFIG_PATH "\n"
     58    "  -s, --status            display status of active arrays\n"
     59    "  -a, --assemble=NAME     assemble an existing array\n"
     60    "  -c, --create=NAME       create new array\n"
     61    "  -n                      non-zero number of devices\n"
     62    "  -l, --level=LEVEL       set the RAID level,\n"
     63    "                          valid values: 0, 1, 5, linear\n"
     64    "  -0                      striping\n"
     65    "  -1                      mirroring\n"
     66    "  -5                      distributed parity\n"
     67    "  -L                      linear concatenation\n"
    6168    "\n"
    6269    "Example usage:\n"
     
    7481        { "create", required_argument, 0, 'c' },
    7582        { "level", required_argument, 0, 'l' },
     83        { "config-file", required_argument, 0, 'C' },
    7684        { 0, 0, 0, 0 }
    7785};
     
    97105
    98106        return EOK;
     107}
     108
     109static errno_t load_config(const char *path, hr_config_t *cfg)
     110{
     111        errno_t rc;
     112        size_t i;
     113        sif_doc_t *doc = NULL;
     114        sif_node_t *narrays;
     115        sif_node_t *rnode;
     116        sif_node_t *narray;
     117        sif_node_t *nextent;
     118        const char *ntype;
     119        const char *devname;
     120        const char *level_str;
     121        const char *dev_no_str;
     122        const char *extent_devname;
     123
     124        rc = sif_load(path, &doc);
     125        if (rc != EOK)
     126                goto error;
     127
     128        rnode = sif_get_root(doc);
     129
     130        narrays = sif_node_first_child(rnode);
     131        ntype = sif_node_get_type(narrays);
     132        if (str_cmp(ntype, "arrays") != 0) {
     133                rc = EIO;
     134                goto error;
     135        }
     136
     137        narray = sif_node_first_child(narrays);
     138        ntype = sif_node_get_type(narray);
     139        if (str_cmp(ntype, "array") != 0) {
     140                rc = EIO;
     141                goto error;
     142        }
     143
     144        devname = sif_node_get_attr(narray, "devname");
     145        if (devname == NULL) {
     146                rc = EIO;
     147                goto error;
     148        }
     149        str_cpy(cfg->devname, 32, devname);
     150
     151        level_str = sif_node_get_attr(narray, "level");
     152        if (level_str == NULL) {
     153                rc = EIO;
     154                goto error;
     155        }
     156        cfg->level = strtol(level_str, NULL, 10);
     157
     158        dev_no_str = sif_node_get_attr(narray, "n");
     159        if (dev_no_str == NULL) {
     160                rc = EIO;
     161                goto error;
     162        }
     163        cfg->dev_no = strtol(dev_no_str, NULL, 10);
     164
     165        nextent = sif_node_first_child(narray);
     166        for (i = 0; i < cfg->dev_no; i++) {
     167                if (nextent == NULL) {
     168                        rc = EINVAL;
     169                        goto error;
     170                }
     171
     172                ntype = sif_node_get_type(nextent);
     173                if (str_cmp(ntype, "extent") != 0) {
     174                        rc = EIO;
     175                        goto error;
     176                }
     177
     178                extent_devname = sif_node_get_attr(nextent, "devname");
     179                if (extent_devname == NULL) {
     180                        rc = EIO;
     181                        goto error;
     182                }
     183
     184                rc = loc_service_get_id(extent_devname, &cfg->devs[i], 0);
     185                if (rc != EOK) {
     186                        printf("hrctl: error resolving device \"%s\"\n",
     187                            extent_devname);
     188                        return EINVAL;
     189                }
     190
     191                nextent = sif_node_next_child(nextent);
     192        }
     193
     194error:
     195        if (doc != NULL)
     196                sif_delete(doc);
     197        return rc;
    99198}
    100199
     
    125224
    126225        while (c != -1) {
    127                 c = getopt_long(argc, argv, "hsc:a:l:015Ln:",
     226                c = getopt_long(argc, argv, "hsC:c:a:l:015Ln:",
    128227                    long_options, NULL);
    129228                switch (c) {
     
    133232                case 's':
    134233                        printf("hrctl: status not implemented yet\n");
    135                         return 0;
     234                        return 1;
    136235                case 'a':
    137236                        if (str_size(optarg) > 31) {
     
    142241                        assemble = true;
    143242                        break;
     243                case 'C':
     244                        /* only support 1 array inside config for now XXX */
     245                        rc = load_config(optarg, cfg);
     246                        if (rc != EOK) {
     247                                printf("hrctl: failed to load config\n");
     248                                return 1;
     249                        }
     250                        create = true;
     251                        goto skip;
    144252                case 'c':
    145253                        if (str_size(optarg) > 31) {
     
    193301        }
    194302
     303skip:
    195304        if ((create && assemble) ||
    196305            (!create && !assemble) ||
  • uspace/app/hrctl/meson.build

    r44ea48e re192339  
    2727#
    2828
    29 deps = [ 'device' ]
     29deps = [ 'device', 'sif' ]
    3030src = files('hrctl.c')
     31
     32installed_data += { 'name': 'sample_hr_config.sif', 'dir': '/cfg' }
Note: See TracChangeset for help on using the changeset viewer.