[db9c889] | 1 | /*
|
---|
[4285f384] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[db9c889] | 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 vol
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Volume administration (interface to volsrv).
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <io/table.h>
|
---|
| 37 | #include <loc.h>
|
---|
[4285f384] | 38 | #include <stdbool.h>
|
---|
[db9c889] | 39 | #include <stdio.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
| 41 | #include <str.h>
|
---|
| 42 | #include <vfs/vfs.h>
|
---|
| 43 | #include <vol.h>
|
---|
| 44 |
|
---|
| 45 | #define NAME "vol"
|
---|
| 46 |
|
---|
| 47 | typedef enum {
|
---|
| 48 | vcmd_eject,
|
---|
[f0f8787] | 49 | vcmd_insert,
|
---|
[b82985e] | 50 | vcmd_insert_by_path,
|
---|
[db9c889] | 51 | vcmd_help,
|
---|
| 52 | vcmd_list,
|
---|
[63c1dd5] | 53 | vcmd_cfglist,
|
---|
[db9c889] | 54 | } vol_cmd_t;
|
---|
| 55 |
|
---|
[4285f384] | 56 | static errno_t vol_cmd_eject(const char *volspec, bool physical)
|
---|
[72c72d4] | 57 | {
|
---|
| 58 | vol_t *vol = NULL;
|
---|
| 59 | service_id_t part_id;
|
---|
| 60 | errno_t rc;
|
---|
| 61 |
|
---|
| 62 | rc = vol_create(&vol);
|
---|
| 63 | if (rc != EOK) {
|
---|
| 64 | printf("Error contacting volume service.\n");
|
---|
| 65 | goto out;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[629b480] | 68 | rc = vol_part_by_mp(vol, volspec, &part_id);
|
---|
[72c72d4] | 69 | if (rc != EOK) {
|
---|
| 70 | printf("Error looking up volume '%s'.\n", volspec);
|
---|
| 71 | goto out;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[4285f384] | 74 | rc = vol_part_eject(vol, part_id, physical ? vef_physical :
|
---|
| 75 | vef_none);
|
---|
[72c72d4] | 76 | if (rc != EOK) {
|
---|
| 77 | printf("Error ejecting volume.\n");
|
---|
| 78 | goto out;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | rc = EOK;
|
---|
| 82 | out:
|
---|
| 83 | vol_destroy(vol);
|
---|
| 84 | return rc;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[f0f8787] | 87 | static errno_t vol_cmd_insert(const char *volspec)
|
---|
| 88 | {
|
---|
| 89 | vol_t *vol = NULL;
|
---|
| 90 | service_id_t svc_id;
|
---|
| 91 | errno_t rc;
|
---|
| 92 |
|
---|
| 93 | rc = loc_service_get_id(volspec, &svc_id, 0);
|
---|
| 94 | if (rc != EOK) {
|
---|
| 95 | printf("Error looking up service '%s'.\n", volspec);
|
---|
| 96 | goto out;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | rc = vol_create(&vol);
|
---|
| 100 | if (rc != EOK) {
|
---|
| 101 | printf("Error contacting volume service.\n");
|
---|
| 102 | goto out;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | rc = vol_part_insert(vol, svc_id);
|
---|
| 106 | if (rc != EOK) {
|
---|
| 107 | printf("Error inserting volume.\n");
|
---|
| 108 | goto out;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | rc = EOK;
|
---|
| 112 | out:
|
---|
| 113 | vol_destroy(vol);
|
---|
| 114 | return rc;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[b82985e] | 117 | static errno_t vol_cmd_insert_by_path(const char *volspec)
|
---|
| 118 | {
|
---|
| 119 | vol_t *vol = NULL;
|
---|
| 120 | errno_t rc;
|
---|
| 121 |
|
---|
| 122 | rc = vol_create(&vol);
|
---|
| 123 | if (rc != EOK) {
|
---|
| 124 | printf("Error contacting volume service.\n");
|
---|
| 125 | goto out;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | rc = vol_part_insert_by_path(vol, volspec);
|
---|
| 129 | if (rc != EOK) {
|
---|
| 130 | printf("Error inserting volume.\n");
|
---|
| 131 | goto out;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | rc = EOK;
|
---|
| 135 | out:
|
---|
| 136 | vol_destroy(vol);
|
---|
| 137 | return rc;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[72c72d4] | 140 | static errno_t vol_cmd_list(void)
|
---|
[db9c889] | 141 | {
|
---|
| 142 | vol_t *vol = NULL;
|
---|
| 143 | vol_part_info_t vinfo;
|
---|
| 144 | service_id_t *part_ids = NULL;
|
---|
| 145 | char *svc_name;
|
---|
| 146 | char *sfstype;
|
---|
| 147 | size_t nparts;
|
---|
| 148 | size_t i;
|
---|
| 149 | table_t *table = NULL;
|
---|
[72c72d4] | 150 | errno_t rc;
|
---|
[db9c889] | 151 |
|
---|
| 152 | rc = vol_create(&vol);
|
---|
| 153 | if (rc != EOK) {
|
---|
| 154 | printf("Error contacting volume service.\n");
|
---|
| 155 | goto out;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | rc = vol_get_parts(vol, &part_ids, &nparts);
|
---|
| 159 | if (rc != EOK) {
|
---|
| 160 | printf("Error getting list of volumes.\n");
|
---|
| 161 | goto out;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | rc = table_create(&table);
|
---|
| 165 | if (rc != EOK) {
|
---|
| 166 | printf("Out of memory.\n");
|
---|
| 167 | goto out;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | table_header_row(table);
|
---|
| 171 | table_printf(table, "Volume Name\t" "Resource\t" "Content\t" "Auto\t"
|
---|
| 172 | "Mounted at\n");
|
---|
| 173 |
|
---|
| 174 | for (i = 0; i < nparts; i++) {
|
---|
| 175 | rc = vol_part_info(vol, part_ids[i], &vinfo);
|
---|
| 176 | if (rc != EOK) {
|
---|
| 177 | printf("Error getting volume information.\n");
|
---|
| 178 | return EIO;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | rc = loc_service_get_name(part_ids[i], &svc_name);
|
---|
| 182 | if (rc != EOK) {
|
---|
| 183 | printf("Error getting service name.\n");
|
---|
| 184 | return EIO;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | rc = vol_pcnt_fs_format(vinfo.pcnt, vinfo.fstype, &sfstype);
|
---|
| 188 | if (rc != EOK) {
|
---|
| 189 | printf("Out of memory.\n");
|
---|
| 190 | free(svc_name);
|
---|
| 191 | return ENOMEM;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[1a9174e] | 194 | table_printf(table, "%s\t" "%s\t" "%s\t" "%s\t" "%s\n",
|
---|
| 195 | vinfo.label, svc_name, sfstype,
|
---|
| 196 | vinfo.cur_mp_auto ? "Yes" : "", vinfo.cur_mp);
|
---|
[db9c889] | 197 |
|
---|
| 198 | free(svc_name);
|
---|
| 199 | free(sfstype);
|
---|
| 200 | svc_name = NULL;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | rc = table_print_out(table, stdout);
|
---|
| 204 | if (rc != EOK)
|
---|
| 205 | printf("Error printing table.\n");
|
---|
| 206 | out:
|
---|
| 207 | table_destroy(table);
|
---|
| 208 | vol_destroy(vol);
|
---|
| 209 | free(part_ids);
|
---|
| 210 |
|
---|
| 211 | return rc;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[63c1dd5] | 214 | /** List volume configuration entries.
|
---|
| 215 | *
|
---|
| 216 | * @return EOK on success or an error code
|
---|
| 217 | */
|
---|
| 218 | static errno_t vol_cmd_cfglist(void)
|
---|
| 219 | {
|
---|
| 220 | vol_t *vol = NULL;
|
---|
| 221 | vol_info_t vinfo;
|
---|
| 222 | volume_id_t *volume_ids = NULL;
|
---|
| 223 | size_t nvols;
|
---|
| 224 | size_t i;
|
---|
| 225 | table_t *table = NULL;
|
---|
| 226 | errno_t rc;
|
---|
| 227 |
|
---|
| 228 | rc = vol_create(&vol);
|
---|
| 229 | if (rc != EOK) {
|
---|
| 230 | printf("Error contacting volume service.\n");
|
---|
| 231 | goto out;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | rc = vol_get_volumes(vol, &volume_ids, &nvols);
|
---|
| 235 | if (rc != EOK) {
|
---|
| 236 | printf("Error getting list of volumes.\n");
|
---|
| 237 | goto out;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | rc = table_create(&table);
|
---|
| 241 | if (rc != EOK) {
|
---|
| 242 | printf("Out of memory.\n");
|
---|
| 243 | goto out;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | table_header_row(table);
|
---|
| 247 | table_printf(table, "Volume Name\t" "Path\n");
|
---|
| 248 |
|
---|
| 249 | for (i = 0; i < nvols; i++) {
|
---|
| 250 | rc = vol_info(vol, volume_ids[i], &vinfo);
|
---|
| 251 | if (rc != EOK) {
|
---|
| 252 | printf("Error getting volume information.\n");
|
---|
| 253 | return EIO;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | table_printf(table, "%s\t" "%s\n", vinfo.label, vinfo.path);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | rc = table_print_out(table, stdout);
|
---|
| 260 | if (rc != EOK)
|
---|
| 261 | printf("Error printing table.\n");
|
---|
| 262 | out:
|
---|
| 263 | table_destroy(table);
|
---|
| 264 | vol_destroy(vol);
|
---|
| 265 | free(volume_ids);
|
---|
| 266 |
|
---|
| 267 | return rc;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[db9c889] | 270 | static void print_syntax(void)
|
---|
| 271 | {
|
---|
| 272 | printf("Syntax:\n");
|
---|
[4285f384] | 273 | printf(" %s List present volumes\n", NAME);
|
---|
| 274 | printf(" %s -c List volume configuration entries\n", NAME);
|
---|
| 275 | printf(" %s -h Print help\n", NAME);
|
---|
| 276 | printf(" %s eject [-s] <mp> Eject volume mounted in a directory\n", NAME);
|
---|
| 277 | printf(" -s to eject physically\n");
|
---|
| 278 | printf(" %s insert <svc> Insert volume based on service identifier\n", NAME);
|
---|
| 279 | printf(" %s insert -p <mp> Insert volume based on filesystem path\n", NAME);
|
---|
[db9c889] | 280 | }
|
---|
| 281 |
|
---|
| 282 | int main(int argc, char *argv[])
|
---|
| 283 | {
|
---|
| 284 | char *cmd;
|
---|
[f0f8787] | 285 | char *volspec;
|
---|
[db9c889] | 286 | vol_cmd_t vcmd;
|
---|
[4285f384] | 287 | bool physical = false;
|
---|
[db9c889] | 288 | int i;
|
---|
[2d56e2e] | 289 | errno_t rc = EINVAL;
|
---|
[db9c889] | 290 |
|
---|
| 291 | if (argc < 2) {
|
---|
| 292 | vcmd = vcmd_list;
|
---|
| 293 | i = 1;
|
---|
| 294 | } else {
|
---|
| 295 | cmd = argv[1];
|
---|
| 296 | i = 2;
|
---|
| 297 | if (str_cmp(cmd, "-h") == 0) {
|
---|
| 298 | vcmd = vcmd_help;
|
---|
[63c1dd5] | 299 | } else if (str_cmp(cmd, "-c") == 0) {
|
---|
| 300 | vcmd = vcmd_cfglist;
|
---|
[db9c889] | 301 | } else if (str_cmp(cmd, "eject") == 0) {
|
---|
| 302 | vcmd = vcmd_eject;
|
---|
[4285f384] | 303 | if (str_cmp(argv[i], "-s") == 0) {
|
---|
| 304 | physical = true;
|
---|
| 305 | ++i;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[db9c889] | 308 | if (argc <= i) {
|
---|
| 309 | printf("Parameter missing.\n");
|
---|
| 310 | goto syntax_error;
|
---|
| 311 | }
|
---|
| 312 | volspec = argv[i++];
|
---|
[f0f8787] | 313 | } else if (str_cmp(cmd, "insert") == 0) {
|
---|
[b82985e] | 314 | if (str_cmp(argv[i], "-p") == 0) {
|
---|
| 315 | vcmd = vcmd_insert_by_path;
|
---|
| 316 | ++i;
|
---|
| 317 | } else {
|
---|
| 318 | vcmd = vcmd_insert;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[f0f8787] | 321 | if (argc <= i) {
|
---|
| 322 | printf("Parameter missing.\n");
|
---|
| 323 | goto syntax_error;
|
---|
| 324 | }
|
---|
| 325 | volspec = argv[i++];
|
---|
[db9c889] | 326 | } else {
|
---|
| 327 | printf("Invalid sub-command '%s'.\n", cmd);
|
---|
| 328 | goto syntax_error;
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | if (argc > i) {
|
---|
| 333 | printf("Unexpected argument '%s'.\n", argv[i]);
|
---|
| 334 | goto syntax_error;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | switch (vcmd) {
|
---|
| 338 | case vcmd_eject:
|
---|
[4285f384] | 339 | rc = vol_cmd_eject(volspec, physical);
|
---|
[db9c889] | 340 | break;
|
---|
[f0f8787] | 341 | case vcmd_insert:
|
---|
| 342 | rc = vol_cmd_insert(volspec);
|
---|
| 343 | break;
|
---|
[b82985e] | 344 | case vcmd_insert_by_path:
|
---|
| 345 | rc = vol_cmd_insert_by_path(volspec);
|
---|
| 346 | break;
|
---|
[db9c889] | 347 | case vcmd_help:
|
---|
| 348 | print_syntax();
|
---|
| 349 | rc = EOK;
|
---|
| 350 | break;
|
---|
| 351 | case vcmd_list:
|
---|
| 352 | rc = vol_cmd_list();
|
---|
| 353 | break;
|
---|
[63c1dd5] | 354 | case vcmd_cfglist:
|
---|
| 355 | rc = vol_cmd_cfglist();
|
---|
| 356 | break;
|
---|
[db9c889] | 357 | }
|
---|
| 358 |
|
---|
| 359 | if (rc != EOK)
|
---|
| 360 | return 1;
|
---|
| 361 |
|
---|
| 362 | return 0;
|
---|
| 363 |
|
---|
| 364 | syntax_error:
|
---|
| 365 | printf("Use %s -h to get help.\n", NAME);
|
---|
| 366 | return 1;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | /** @}
|
---|
| 370 | */
|
---|