[e96047c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 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 fdisk
|
---|
| 30 | * @brief Disk management tool
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[6c4eedf] | 37 | #include <cap.h>
|
---|
[e96047c] | 38 | #include <errno.h>
|
---|
| 39 | #include <nchoice.h>
|
---|
| 40 | #include <stdbool.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include <fdisk.h>
|
---|
| 44 |
|
---|
[9c2c7d2] | 45 | #define NO_LABEL_CAPTION "(No name)"
|
---|
| 46 |
|
---|
[e96047c] | 47 | static bool quit = false;
|
---|
[22fb7ab] | 48 | static fdisk_t *fdisk;
|
---|
[e96047c] | 49 |
|
---|
| 50 | /** Device menu actions */
|
---|
| 51 | typedef enum {
|
---|
| 52 | /** Create label */
|
---|
| 53 | devac_create_label,
|
---|
| 54 | /** Delete label */
|
---|
| 55 | devac_delete_label,
|
---|
[ea0ff6b] | 56 | /** Erase disk */
|
---|
| 57 | devac_erase_disk,
|
---|
[b7a4d06] | 58 | /** Create (primary) partition */
|
---|
| 59 | devac_create_pri_part,
|
---|
| 60 | /** Create extended partition */
|
---|
| 61 | devac_create_ext_part,
|
---|
| 62 | /** Create logical partition */
|
---|
| 63 | devac_create_log_part,
|
---|
[e96047c] | 64 | /** Delete partition */
|
---|
| 65 | devac_delete_part,
|
---|
| 66 | /** Exit */
|
---|
| 67 | devac_exit
|
---|
| 68 | } devac_t;
|
---|
| 69 |
|
---|
[2dab624] | 70 | static int fdsk_pcnt_fs_format(vol_part_cnt_t pcnt, vol_fstype_t fstype,
|
---|
| 71 | char **rstr)
|
---|
| 72 | {
|
---|
| 73 | int rc;
|
---|
| 74 | char *s;
|
---|
| 75 |
|
---|
| 76 | switch (pcnt) {
|
---|
| 77 | case vpc_empty:
|
---|
| 78 | s = str_dup("Empty");
|
---|
| 79 | if (s == NULL)
|
---|
| 80 | return ENOMEM;
|
---|
| 81 | break;
|
---|
| 82 | case vpc_fs:
|
---|
| 83 | rc = fdisk_fstype_format(fstype, &s);
|
---|
| 84 | if (rc != EOK)
|
---|
| 85 | return ENOMEM;
|
---|
| 86 | break;
|
---|
| 87 | case vpc_unknown:
|
---|
| 88 | s = str_dup("Unknown");
|
---|
| 89 | if (s == NULL)
|
---|
| 90 | return ENOMEM;
|
---|
| 91 | break;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | *rstr = s;
|
---|
| 95 | return EOK;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[ef9dac04] | 98 | /** Confirm user selection. */
|
---|
| 99 | static int fdsk_confirm(const char *msg, bool *rconfirm)
|
---|
| 100 | {
|
---|
| 101 | tinput_t *tinput = NULL;
|
---|
| 102 | char *answer;
|
---|
| 103 | int rc;
|
---|
| 104 |
|
---|
| 105 | tinput = tinput_new();
|
---|
| 106 | if (tinput == NULL) {
|
---|
| 107 | rc = ENOMEM;
|
---|
| 108 | goto error;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | rc = tinput_set_prompt(tinput, "y/n> ");
|
---|
| 112 | if (rc != EOK)
|
---|
| 113 | goto error;
|
---|
| 114 |
|
---|
| 115 | while (true) {
|
---|
| 116 | printf("%s\n", msg);
|
---|
| 117 |
|
---|
| 118 | rc = tinput_read(tinput, &answer);
|
---|
| 119 | if (rc == ENOENT) {
|
---|
| 120 | *rconfirm = false;
|
---|
| 121 | free(answer);
|
---|
| 122 | break;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | if (rc != EOK)
|
---|
| 126 | goto error;
|
---|
| 127 |
|
---|
| 128 | if (str_cmp(answer, "y") == 0) {
|
---|
| 129 | *rconfirm = true;
|
---|
| 130 | free(answer);
|
---|
| 131 | break;
|
---|
| 132 | } else if (str_cmp(answer, "n") == 0) {
|
---|
| 133 | *rconfirm = false;
|
---|
| 134 | free(answer);
|
---|
| 135 | break;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | tinput_destroy(tinput);
|
---|
| 140 | return EOK;
|
---|
| 141 | error:
|
---|
| 142 | if (tinput != NULL)
|
---|
| 143 | tinput_destroy(tinput);
|
---|
| 144 | return rc;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[e96047c] | 147 | /** Device selection */
|
---|
| 148 | static int fdsk_dev_sel_choice(service_id_t *rsvcid)
|
---|
| 149 | {
|
---|
| 150 | fdisk_dev_list_t *devlist = NULL;
|
---|
| 151 | fdisk_dev_info_t *info;
|
---|
| 152 | nchoice_t *choice = NULL;
|
---|
| 153 | char *svcname = NULL;
|
---|
[6c4eedf] | 154 | cap_spec_t cap;
|
---|
[8227d63] | 155 | fdisk_dev_info_t *sdev;
|
---|
[e96047c] | 156 | char *scap = NULL;
|
---|
| 157 | char *dtext = NULL;
|
---|
| 158 | service_id_t svcid;
|
---|
| 159 | void *sel;
|
---|
[8227d63] | 160 | int ndevs;
|
---|
[e96047c] | 161 | int rc;
|
---|
| 162 |
|
---|
| 163 | rc = nchoice_create(&choice);
|
---|
| 164 | if (rc != EOK) {
|
---|
| 165 | assert(rc == ENOMEM);
|
---|
| 166 | printf("Out of memory.\n");
|
---|
| 167 | goto error;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | rc = nchoice_set_prompt(choice, "Select device");
|
---|
| 171 | if (rc != EOK) {
|
---|
| 172 | assert(rc == ENOMEM);
|
---|
| 173 | printf("Out of memory.\n");
|
---|
| 174 | goto error;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[22fb7ab] | 177 | rc = fdisk_dev_list_get(fdisk, &devlist);
|
---|
[e96047c] | 178 | if (rc != EOK) {
|
---|
| 179 | printf("Error getting device list.\n");
|
---|
| 180 | goto error;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | info = fdisk_dev_first(devlist);
|
---|
[8227d63] | 184 | ndevs = 0;
|
---|
[e96047c] | 185 | while (info != NULL) {
|
---|
[8227d63] | 186 | rc = fdisk_dev_info_get_svcname(info, &svcname);
|
---|
[e96047c] | 187 | if (rc != EOK) {
|
---|
[4627314] | 188 | fdisk_dev_info_get_svcid(info, &svcid);
|
---|
| 189 | printf("Error getting device service name "
|
---|
| 190 | "(service ID %zu).\n", svcid);
|
---|
| 191 | info = fdisk_dev_next(info);
|
---|
| 192 | continue;
|
---|
[e96047c] | 193 | }
|
---|
| 194 |
|
---|
[8227d63] | 195 | rc = fdisk_dev_info_capacity(info, &cap);
|
---|
[e96047c] | 196 | if (rc != EOK) {
|
---|
[4627314] | 197 | printf("Error getting device capacity "
|
---|
| 198 | "(device %s).\n", svcname);
|
---|
| 199 | info = fdisk_dev_next(info);
|
---|
| 200 | continue;
|
---|
[e96047c] | 201 | }
|
---|
| 202 |
|
---|
[6c4eedf] | 203 | cap_simplify(&cap);
|
---|
[9854a8f] | 204 |
|
---|
[6c4eedf] | 205 | rc = cap_format(&cap, &scap);
|
---|
[e96047c] | 206 | if (rc != EOK) {
|
---|
| 207 | assert(rc == ENOMEM);
|
---|
| 208 | printf("Out of memory.\n");
|
---|
| 209 | goto error;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | rc = asprintf(&dtext, "%s (%s)", svcname, scap);
|
---|
| 213 | if (rc < 0) {
|
---|
| 214 | rc = ENOMEM;
|
---|
| 215 | printf("Out of memory.\n");
|
---|
| 216 | goto error;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | free(svcname);
|
---|
| 220 | svcname = NULL;
|
---|
| 221 | free(scap);
|
---|
| 222 | scap = NULL;
|
---|
| 223 |
|
---|
[68b5dd11] | 224 | rc = nchoice_add(choice, dtext, info, 0);
|
---|
[e96047c] | 225 | if (rc != EOK) {
|
---|
| 226 | assert(rc == ENOMEM);
|
---|
| 227 | printf("Out of memory.\n");
|
---|
| 228 | goto error;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[4627314] | 231 | ++ndevs;
|
---|
| 232 |
|
---|
[e96047c] | 233 | free(dtext);
|
---|
| 234 | dtext = NULL;
|
---|
| 235 |
|
---|
| 236 | info = fdisk_dev_next(info);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[8227d63] | 239 | if (ndevs == 0) {
|
---|
| 240 | printf("No disk devices found.\n");
|
---|
| 241 | rc = ENOENT;
|
---|
| 242 | goto error;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[68b5dd11] | 245 | rc = nchoice_add(choice, "Exit", NULL, 0);
|
---|
[8227d63] | 246 | if (rc != EOK) {
|
---|
| 247 | assert(rc == ENOMEM);
|
---|
| 248 | printf("Out of memory.\n");
|
---|
| 249 | goto error;
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[e96047c] | 252 | rc = nchoice_get(choice, &sel);
|
---|
| 253 | if (rc != EOK) {
|
---|
| 254 | printf("Error getting user selection.\n");
|
---|
| 255 | return rc;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[8227d63] | 258 | sdev = (fdisk_dev_info_t *)sel;
|
---|
| 259 | if (sdev != NULL) {
|
---|
| 260 | fdisk_dev_info_get_svcid(sdev, &svcid);
|
---|
| 261 | } else {
|
---|
| 262 | svcid = 0;
|
---|
| 263 | }
|
---|
[e96047c] | 264 |
|
---|
[8227d63] | 265 | fdisk_dev_list_free(devlist);
|
---|
[e96047c] | 266 |
|
---|
| 267 | nchoice_destroy(choice);
|
---|
| 268 | *rsvcid = svcid;
|
---|
| 269 | return EOK;
|
---|
| 270 | error:
|
---|
| 271 | if (devlist != NULL)
|
---|
| 272 | fdisk_dev_list_free(devlist);
|
---|
| 273 | if (choice != NULL)
|
---|
| 274 | nchoice_destroy(choice);
|
---|
| 275 | free(dtext);
|
---|
| 276 | free(svcname);
|
---|
| 277 | free(scap);
|
---|
| 278 | return rc;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | static int fdsk_create_label(fdisk_dev_t *dev)
|
---|
| 282 | {
|
---|
| 283 | nchoice_t *choice = NULL;
|
---|
| 284 | void *sel;
|
---|
[8227d63] | 285 | char *sltype = NULL;
|
---|
| 286 | int i;
|
---|
[e96047c] | 287 | int rc;
|
---|
| 288 |
|
---|
| 289 | rc = nchoice_create(&choice);
|
---|
| 290 | if (rc != EOK) {
|
---|
| 291 | assert(rc == ENOMEM);
|
---|
| 292 | printf("Out of memory.\n");
|
---|
| 293 | goto error;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | rc = nchoice_set_prompt(choice, "Select label type");
|
---|
| 297 | if (rc != EOK) {
|
---|
| 298 | assert(rc == ENOMEM);
|
---|
| 299 | printf("Out of memory.\n");
|
---|
| 300 | goto error;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[372df8f] | 303 | for (i = LT_FIRST; i < LT_LIMIT; i++) {
|
---|
[8227d63] | 304 | rc = fdisk_ltype_format(i, &sltype);
|
---|
| 305 | if (rc != EOK)
|
---|
| 306 | goto error;
|
---|
| 307 |
|
---|
[68b5dd11] | 308 | rc = nchoice_add(choice, sltype, (void *)(uintptr_t)i,
|
---|
| 309 | i == LT_DEFAULT);
|
---|
[8227d63] | 310 | if (rc != EOK) {
|
---|
| 311 | assert(rc == ENOMEM);
|
---|
| 312 | printf("Out of memory.\n");
|
---|
| 313 | goto error;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | free(sltype);
|
---|
| 317 | sltype = NULL;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | rc = nchoice_get(choice, &sel);
|
---|
| 321 | if (rc != EOK) {
|
---|
| 322 | printf("Error getting user selection.\n");
|
---|
| 323 | goto error;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[22fb7ab] | 326 | rc = fdisk_label_create(dev, (label_type_t)sel);
|
---|
[8227d63] | 327 | if (rc != EOK) {
|
---|
| 328 | printf("Error creating label.\n");
|
---|
| 329 | goto error;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | nchoice_destroy(choice);
|
---|
| 333 | return EOK;
|
---|
| 334 | error:
|
---|
| 335 | free(sltype);
|
---|
| 336 | if (choice != NULL)
|
---|
| 337 | nchoice_destroy(choice);
|
---|
| 338 | return rc;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | static int fdsk_delete_label(fdisk_dev_t *dev)
|
---|
| 342 | {
|
---|
[ef9dac04] | 343 | bool confirm;
|
---|
[8227d63] | 344 | int rc;
|
---|
| 345 |
|
---|
[ef9dac04] | 346 | rc = fdsk_confirm("Warning. Any data on disk will be lost. "
|
---|
| 347 | "Really delete label?", &confirm);
|
---|
| 348 | if (rc != EOK) {
|
---|
| 349 | printf("Error getting user confirmation.\n");
|
---|
| 350 | return rc;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | if (!confirm)
|
---|
| 354 | return EOK;
|
---|
| 355 |
|
---|
[8227d63] | 356 | rc = fdisk_label_destroy(dev);
|
---|
| 357 | if (rc != EOK) {
|
---|
| 358 | printf("Error deleting label.\n");
|
---|
| 359 | return rc;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | return EOK;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[ea0ff6b] | 365 | static int fdsk_erase_disk(fdisk_dev_t *dev)
|
---|
| 366 | {
|
---|
[ef9dac04] | 367 | bool confirm;
|
---|
[ea0ff6b] | 368 | int rc;
|
---|
| 369 |
|
---|
[ef9dac04] | 370 | rc = fdsk_confirm("Warning. Any data on disk will be lost. "
|
---|
| 371 | "Really erase disk?", &confirm);
|
---|
| 372 | if (rc != EOK) {
|
---|
| 373 | printf("Error getting user confirmation.\n");
|
---|
| 374 | return rc;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
| 377 | if (!confirm)
|
---|
| 378 | return EOK;
|
---|
| 379 |
|
---|
[ea0ff6b] | 380 | rc = fdisk_dev_erase(dev);
|
---|
| 381 | if (rc != EOK) {
|
---|
| 382 | printf("Error erasing disk.\n");
|
---|
| 383 | return rc;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | return EOK;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[4b6635a7] | 389 | static int fdsk_select_fstype(vol_fstype_t *fstype)
|
---|
[8227d63] | 390 | {
|
---|
| 391 | nchoice_t *choice = NULL;
|
---|
| 392 | void *sel;
|
---|
| 393 | char *sfstype;
|
---|
| 394 | int i;
|
---|
| 395 | int rc;
|
---|
| 396 |
|
---|
| 397 | rc = nchoice_create(&choice);
|
---|
[e96047c] | 398 | if (rc != EOK) {
|
---|
| 399 | assert(rc == ENOMEM);
|
---|
| 400 | printf("Out of memory.\n");
|
---|
| 401 | goto error;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[8227d63] | 404 | rc = nchoice_set_prompt(choice, "Select file system type");
|
---|
[e96047c] | 405 | if (rc != EOK) {
|
---|
| 406 | assert(rc == ENOMEM);
|
---|
| 407 | printf("Out of memory.\n");
|
---|
| 408 | goto error;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
[4b6635a7] | 411 | for (i = 0; i < VOL_FSTYPE_LIMIT; i++) {
|
---|
[8227d63] | 412 | rc = fdisk_fstype_format(i, &sfstype);
|
---|
| 413 | if (rc != EOK)
|
---|
| 414 | goto error;
|
---|
| 415 |
|
---|
[68b5dd11] | 416 | rc = nchoice_add(choice, sfstype, (void *)(uintptr_t)i,
|
---|
| 417 | i == VOL_FSTYPE_DEFAULT);
|
---|
[8227d63] | 418 | if (rc != EOK) {
|
---|
| 419 | assert(rc == ENOMEM);
|
---|
| 420 | printf("Out of memory.\n");
|
---|
| 421 | goto error;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | free(sfstype);
|
---|
| 425 | sfstype = NULL;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[e96047c] | 428 | rc = nchoice_get(choice, &sel);
|
---|
| 429 | if (rc != EOK) {
|
---|
| 430 | printf("Error getting user selection.\n");
|
---|
| 431 | goto error;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
[8227d63] | 434 | nchoice_destroy(choice);
|
---|
[4b6635a7] | 435 | *fstype = (vol_fstype_t)sel;
|
---|
[8227d63] | 436 | return EOK;
|
---|
| 437 | error:
|
---|
| 438 | free(sfstype);
|
---|
| 439 | if (choice != NULL)
|
---|
| 440 | nchoice_destroy(choice);
|
---|
| 441 | return rc;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[b7a4d06] | 444 | static int fdsk_create_part(fdisk_dev_t *dev, label_pkind_t pkind)
|
---|
[8227d63] | 445 | {
|
---|
| 446 | int rc;
|
---|
| 447 | fdisk_part_spec_t pspec;
|
---|
[6c4eedf] | 448 | cap_spec_t cap;
|
---|
| 449 | cap_spec_t mcap;
|
---|
[9c2c7d2] | 450 | vol_label_supp_t vlsupp;
|
---|
[4b6635a7] | 451 | vol_fstype_t fstype = 0;
|
---|
[8227d63] | 452 | tinput_t *tinput = NULL;
|
---|
[b598460a] | 453 | fdisk_spc_t spc;
|
---|
[8227d63] | 454 | char *scap;
|
---|
[b598460a] | 455 | char *smcap = NULL;
|
---|
[9c2c7d2] | 456 | char *label = NULL;
|
---|
[b598460a] | 457 |
|
---|
| 458 | if (pkind == lpk_logical)
|
---|
| 459 | spc = spc_log;
|
---|
| 460 | else
|
---|
| 461 | spc = spc_pri;
|
---|
| 462 |
|
---|
| 463 | rc = fdisk_part_get_max_avail(dev, spc, &mcap);
|
---|
| 464 | if (rc != EOK) {
|
---|
| 465 | rc = EIO;
|
---|
| 466 | goto error;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[6c4eedf] | 469 | cap_simplify(&mcap);
|
---|
[03661d19] | 470 |
|
---|
[6c4eedf] | 471 | rc = cap_format(&mcap, &smcap);
|
---|
[b598460a] | 472 | if (rc != EOK) {
|
---|
| 473 | rc = ENOMEM;
|
---|
| 474 | goto error;
|
---|
| 475 | }
|
---|
[8227d63] | 476 |
|
---|
| 477 | tinput = tinput_new();
|
---|
| 478 | if (tinput == NULL) {
|
---|
| 479 | rc = ENOMEM;
|
---|
| 480 | goto error;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | rc = tinput_set_prompt(tinput, "?> ");
|
---|
| 484 | if (rc != EOK)
|
---|
| 485 | goto error;
|
---|
| 486 |
|
---|
| 487 | while (true) {
|
---|
| 488 | printf("Enter capacity of new partition.\n");
|
---|
[b598460a] | 489 | rc = tinput_read_i(tinput, smcap, &scap);
|
---|
[8227d63] | 490 | if (rc != EOK)
|
---|
| 491 | goto error;
|
---|
| 492 |
|
---|
[6c4eedf] | 493 | rc = cap_parse(scap, &cap);
|
---|
[8227d63] | 494 | if (rc == EOK)
|
---|
| 495 | break;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | tinput_destroy(tinput);
|
---|
[78d50bd] | 499 | tinput = NULL;
|
---|
[b598460a] | 500 | free(smcap);
|
---|
| 501 | smcap = NULL;
|
---|
[8227d63] | 502 |
|
---|
[b7a4d06] | 503 | if (pkind != lpk_extended) {
|
---|
| 504 | rc = fdsk_select_fstype(&fstype);
|
---|
| 505 | if (rc != EOK)
|
---|
| 506 | goto error;
|
---|
| 507 | }
|
---|
[8227d63] | 508 |
|
---|
[9c2c7d2] | 509 | fdisk_get_vollabel_support(dev, fstype, &vlsupp);
|
---|
| 510 | if (vlsupp.supported) {
|
---|
| 511 | tinput = tinput_new();
|
---|
| 512 | if (tinput == NULL) {
|
---|
| 513 | rc = ENOMEM;
|
---|
| 514 | goto error;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | rc = tinput_set_prompt(tinput, "?> ");
|
---|
| 518 | if (rc != EOK)
|
---|
| 519 | goto error;
|
---|
| 520 |
|
---|
| 521 | /* Ask for volume label */
|
---|
| 522 | printf("Enter volume label for new partition.\n");
|
---|
[f3504c1] | 523 | rc = tinput_read_i(tinput, "New volume", &label);
|
---|
[9c2c7d2] | 524 | if (rc != EOK)
|
---|
| 525 | goto error;
|
---|
| 526 |
|
---|
| 527 | tinput_destroy(tinput);
|
---|
| 528 | tinput = NULL;
|
---|
| 529 | }
|
---|
| 530 |
|
---|
[8227d63] | 531 | fdisk_pspec_init(&pspec);
|
---|
| 532 | pspec.capacity = cap;
|
---|
[b7a4d06] | 533 | pspec.pkind = pkind;
|
---|
[8227d63] | 534 | pspec.fstype = fstype;
|
---|
[9c2c7d2] | 535 | pspec.label = label;
|
---|
[8227d63] | 536 |
|
---|
| 537 | rc = fdisk_part_create(dev, &pspec, NULL);
|
---|
[e96047c] | 538 | if (rc != EOK) {
|
---|
[8227d63] | 539 | printf("Error creating partition.\n");
|
---|
[e96047c] | 540 | goto error;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[9c2c7d2] | 543 | free(label);
|
---|
[8227d63] | 544 | return EOK;
|
---|
| 545 | error:
|
---|
[b598460a] | 546 | free(smcap);
|
---|
[9c2c7d2] | 547 | free(label);
|
---|
[8227d63] | 548 | if (tinput != NULL)
|
---|
| 549 | tinput_destroy(tinput);
|
---|
| 550 | return rc;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | static int fdsk_delete_part(fdisk_dev_t *dev)
|
---|
| 554 | {
|
---|
| 555 | nchoice_t *choice = NULL;
|
---|
| 556 | fdisk_part_t *part;
|
---|
| 557 | fdisk_part_info_t pinfo;
|
---|
| 558 | char *scap = NULL;
|
---|
[21f1543] | 559 | char *spkind = NULL;
|
---|
[8227d63] | 560 | char *sfstype = NULL;
|
---|
| 561 | char *sdesc = NULL;
|
---|
[9c2c7d2] | 562 | const char *label;
|
---|
[ef9dac04] | 563 | bool confirm;
|
---|
[8227d63] | 564 | void *sel;
|
---|
| 565 | int rc;
|
---|
| 566 |
|
---|
| 567 | rc = nchoice_create(&choice);
|
---|
| 568 | if (rc != EOK) {
|
---|
| 569 | assert(rc == ENOMEM);
|
---|
| 570 | printf("Out of memory.\n");
|
---|
| 571 | goto error;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | rc = nchoice_set_prompt(choice, "Select partition to delete");
|
---|
| 575 | if (rc != EOK) {
|
---|
| 576 | assert(rc == ENOMEM);
|
---|
| 577 | printf("Out of memory.\n");
|
---|
| 578 | goto error;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | part = fdisk_part_first(dev);
|
---|
| 582 | while (part != NULL) {
|
---|
| 583 | rc = fdisk_part_get_info(part, &pinfo);
|
---|
| 584 | if (rc != EOK) {
|
---|
| 585 | printf("Error getting partition information.\n");
|
---|
| 586 | goto error;
|
---|
| 587 | }
|
---|
| 588 |
|
---|
[6c4eedf] | 589 | cap_simplify(&pinfo.capacity);
|
---|
[9854a8f] | 590 |
|
---|
[6c4eedf] | 591 | rc = cap_format(&pinfo.capacity, &scap);
|
---|
[8227d63] | 592 | if (rc != EOK) {
|
---|
| 593 | printf("Out of memory.\n");
|
---|
| 594 | goto error;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[21f1543] | 597 | rc = fdisk_pkind_format(pinfo.pkind, &spkind);
|
---|
[8227d63] | 598 | if (rc != EOK) {
|
---|
[21f1543] | 599 | printf("\nOut of memory.\n");
|
---|
[8227d63] | 600 | goto error;
|
---|
| 601 | }
|
---|
| 602 |
|
---|
[21f1543] | 603 | if (pinfo.pkind != lpk_extended) {
|
---|
[2dab624] | 604 | rc = fdsk_pcnt_fs_format(pinfo.pcnt, pinfo.fstype, &sfstype);
|
---|
[21f1543] | 605 | if (rc != EOK) {
|
---|
| 606 | printf("Out of memory.\n");
|
---|
| 607 | goto error;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[9c2c7d2] | 610 | if (str_size(pinfo.label) > 0)
|
---|
| 611 | label = pinfo.label;
|
---|
| 612 | else
|
---|
| 613 | label = "(No name)";
|
---|
| 614 |
|
---|
| 615 | rc = asprintf(&sdesc, "%s %s, %s, %s", label,
|
---|
| 616 | scap, spkind, sfstype);
|
---|
[21f1543] | 617 | if (rc < 0) {
|
---|
| 618 | rc = ENOMEM;
|
---|
| 619 | goto error;
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | } else {
|
---|
| 623 | rc = asprintf(&sdesc, "%s, %s", scap, spkind);
|
---|
| 624 | if (rc < 0) {
|
---|
| 625 | rc = ENOMEM;
|
---|
| 626 | goto error;
|
---|
| 627 | }
|
---|
[8227d63] | 628 | }
|
---|
| 629 |
|
---|
[68b5dd11] | 630 | rc = nchoice_add(choice, sdesc, (void *)part, 0);
|
---|
[8227d63] | 631 | if (rc != EOK) {
|
---|
| 632 | assert(rc == ENOMEM);
|
---|
| 633 | printf("Out of memory.\n");
|
---|
| 634 | goto error;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | free(scap);
|
---|
| 638 | scap = NULL;
|
---|
[21f1543] | 639 | free(spkind);
|
---|
| 640 | spkind = NULL;
|
---|
[8227d63] | 641 | free(sfstype);
|
---|
| 642 | sfstype = NULL;
|
---|
| 643 | free(sdesc);
|
---|
| 644 | sdesc = NULL;
|
---|
| 645 |
|
---|
| 646 | part = fdisk_part_next(part);
|
---|
| 647 | }
|
---|
| 648 |
|
---|
[21f1543] | 649 | rc = nchoice_add(choice, "Cancel", NULL, 0);
|
---|
| 650 | if (rc != EOK) {
|
---|
| 651 | assert(rc == ENOMEM);
|
---|
| 652 | printf("Out of memory.\n");
|
---|
| 653 | goto error;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[8227d63] | 656 | rc = nchoice_get(choice, &sel);
|
---|
[ef9dac04] | 657 | if (rc == ENOENT)
|
---|
| 658 | return EOK;
|
---|
[8227d63] | 659 | if (rc != EOK) {
|
---|
| 660 | printf("Error getting user selection.\n");
|
---|
| 661 | goto error;
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 |
|
---|
[e96047c] | 665 | nchoice_destroy(choice);
|
---|
[ef9dac04] | 666 | choice = NULL;
|
---|
| 667 |
|
---|
| 668 | if (sel == NULL)
|
---|
| 669 | return EOK;
|
---|
| 670 |
|
---|
| 671 | rc = fdsk_confirm("Warning. Any data in partition will be lost. "
|
---|
| 672 | "Really delete partition?", &confirm);
|
---|
| 673 | if (rc != EOK) {
|
---|
| 674 | printf("Error getting user confirmation.\n");
|
---|
| 675 | goto error;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | if (!confirm)
|
---|
| 679 | return EOK;
|
---|
| 680 |
|
---|
| 681 | rc = fdisk_part_destroy((fdisk_part_t *)sel);
|
---|
| 682 | if (rc != EOK) {
|
---|
| 683 | printf("Error deleting partition.\n");
|
---|
| 684 | return rc;
|
---|
| 685 | }
|
---|
| 686 |
|
---|
[e96047c] | 687 | return EOK;
|
---|
| 688 | error:
|
---|
[8227d63] | 689 | free(scap);
|
---|
[21f1543] | 690 | free(spkind);
|
---|
[8227d63] | 691 | free(sfstype);
|
---|
| 692 | free(sdesc);
|
---|
| 693 |
|
---|
[e96047c] | 694 | if (choice != NULL)
|
---|
| 695 | nchoice_destroy(choice);
|
---|
| 696 | return rc;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | /** Device menu */
|
---|
| 700 | static int fdsk_dev_menu(fdisk_dev_t *dev)
|
---|
| 701 | {
|
---|
| 702 | nchoice_t *choice = NULL;
|
---|
| 703 | fdisk_label_info_t linfo;
|
---|
[8227d63] | 704 | fdisk_part_t *part;
|
---|
| 705 | fdisk_part_info_t pinfo;
|
---|
[6c4eedf] | 706 | cap_spec_t cap;
|
---|
| 707 | cap_spec_t mcap;
|
---|
[ea0ff6b] | 708 | fdisk_dev_flags_t dflags;
|
---|
[e96047c] | 709 | char *sltype = NULL;
|
---|
[8227d63] | 710 | char *sdcap = NULL;
|
---|
| 711 | char *scap = NULL;
|
---|
[852664b9] | 712 | char *smcap = NULL;
|
---|
[8227d63] | 713 | char *sfstype = NULL;
|
---|
| 714 | char *svcname = NULL;
|
---|
[b7a4d06] | 715 | char *spkind;
|
---|
[d858a660] | 716 | const char *label;
|
---|
[e96047c] | 717 | int rc;
|
---|
[8227d63] | 718 | int npart;
|
---|
[e96047c] | 719 | void *sel;
|
---|
| 720 |
|
---|
| 721 | rc = nchoice_create(&choice);
|
---|
| 722 | if (rc != EOK) {
|
---|
| 723 | assert(rc == ENOMEM);
|
---|
| 724 | printf("Out of memory.\n");
|
---|
| 725 | goto error;
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | rc = nchoice_set_prompt(choice, "Select action");
|
---|
| 729 | if (rc != EOK) {
|
---|
| 730 | assert(rc == ENOMEM);
|
---|
| 731 | printf("Out of memory.\n");
|
---|
| 732 | goto error;
|
---|
| 733 | }
|
---|
| 734 |
|
---|
[8227d63] | 735 | rc = fdisk_dev_capacity(dev, &cap);
|
---|
| 736 | if (rc != EOK) {
|
---|
| 737 | printf("Error getting device capacity.\n");
|
---|
| 738 | goto error;
|
---|
| 739 | }
|
---|
| 740 |
|
---|
[6c4eedf] | 741 | cap_simplify(&cap);
|
---|
[9854a8f] | 742 |
|
---|
[6c4eedf] | 743 | rc = cap_format(&cap, &sdcap);
|
---|
[8227d63] | 744 | if (rc != EOK) {
|
---|
| 745 | printf("Out of memory.\n");
|
---|
| 746 | goto error;
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | rc = fdisk_dev_get_svcname(dev, &svcname);
|
---|
| 750 | if (rc != EOK) {
|
---|
| 751 | printf("Error getting device service name.\n");
|
---|
| 752 | goto error;
|
---|
| 753 | }
|
---|
| 754 |
|
---|
[ea0ff6b] | 755 | fdisk_dev_get_flags(dev, &dflags);
|
---|
| 756 |
|
---|
[d858a660] | 757 | printf("Device: %s (%s)\n", svcname, sdcap);
|
---|
[22fb7ab] | 758 | free(sdcap);
|
---|
| 759 | sdcap = NULL;
|
---|
| 760 |
|
---|
[e96047c] | 761 | rc = fdisk_label_get_info(dev, &linfo);
|
---|
| 762 | if (rc != EOK) {
|
---|
| 763 | printf("Error getting label information.\n");
|
---|
| 764 | goto error;
|
---|
| 765 | }
|
---|
| 766 |
|
---|
[0ecfc62] | 767 | switch (linfo.ltype) {
|
---|
| 768 | case lt_none:
|
---|
| 769 | printf("Disk contains no label.\n");
|
---|
[22fb7ab] | 770 | break;
|
---|
[0ecfc62] | 771 | default:
|
---|
[22fb7ab] | 772 | rc = fdisk_ltype_format(linfo.ltype, &sltype);
|
---|
| 773 | if (rc != EOK) {
|
---|
| 774 | assert(rc == ENOMEM);
|
---|
| 775 | printf("Out of memory.\n");
|
---|
| 776 | goto error;
|
---|
| 777 | }
|
---|
[e96047c] | 778 |
|
---|
[22fb7ab] | 779 | printf("Label type: %s\n", sltype);
|
---|
| 780 | free(sltype);
|
---|
| 781 | sltype = NULL;
|
---|
| 782 | break;
|
---|
| 783 | }
|
---|
[8227d63] | 784 |
|
---|
| 785 | part = fdisk_part_first(dev);
|
---|
| 786 | npart = 0;
|
---|
| 787 | while (part != NULL) {
|
---|
| 788 | ++npart;
|
---|
| 789 | rc = fdisk_part_get_info(part, &pinfo);
|
---|
| 790 | if (rc != EOK) {
|
---|
| 791 | printf("Error getting partition information.\n");
|
---|
| 792 | goto error;
|
---|
| 793 | }
|
---|
| 794 |
|
---|
[6c4eedf] | 795 | cap_simplify(&pinfo.capacity);
|
---|
[9854a8f] | 796 |
|
---|
[6c4eedf] | 797 | rc = cap_format(&pinfo.capacity, &scap);
|
---|
[8227d63] | 798 | if (rc != EOK) {
|
---|
| 799 | printf("Out of memory.\n");
|
---|
| 800 | goto error;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[2dab624] | 803 | rc = fdsk_pcnt_fs_format(pinfo.pcnt, pinfo.fstype, &sfstype);
|
---|
[8227d63] | 804 | if (rc != EOK) {
|
---|
| 805 | printf("Out of memory.\n");
|
---|
| 806 | goto error;
|
---|
| 807 | }
|
---|
| 808 |
|
---|
[d858a660] | 809 | if (str_size(pinfo.label) > 0)
|
---|
| 810 | label = pinfo.label;
|
---|
| 811 | else
|
---|
[9c2c7d2] | 812 | label = "(No name)";
|
---|
[d858a660] | 813 |
|
---|
[edebb4a1] | 814 | if (linfo.ltype == lt_none)
|
---|
[d858a660] | 815 | printf("Entire disk: %s %s", label, scap);
|
---|
[edebb4a1] | 816 | else
|
---|
[d858a660] | 817 | printf("Partition %d: %s %s", npart, label, scap);
|
---|
[edebb4a1] | 818 |
|
---|
[b7a4d06] | 819 | if ((linfo.flags & lf_ext_supp) != 0) {
|
---|
| 820 | rc = fdisk_pkind_format(pinfo.pkind, &spkind);
|
---|
| 821 | if (rc != EOK) {
|
---|
| 822 | printf("\nOut of memory.\n");
|
---|
| 823 | goto error;
|
---|
| 824 | }
|
---|
| 825 |
|
---|
| 826 | printf(", %s", spkind);
|
---|
| 827 | free(spkind);
|
---|
| 828 | }
|
---|
| 829 |
|
---|
[4b6635a7] | 830 | if (pinfo.pkind != lpk_extended) {
|
---|
[2dab624] | 831 | printf(", %s", sfstype);
|
---|
[4b6635a7] | 832 | }
|
---|
| 833 |
|
---|
[b7a4d06] | 834 | printf("\n");
|
---|
| 835 |
|
---|
[8227d63] | 836 | free(scap);
|
---|
| 837 | scap = NULL;
|
---|
| 838 | free(sfstype);
|
---|
| 839 | sfstype = NULL;
|
---|
| 840 |
|
---|
| 841 | part = fdisk_part_next(part);
|
---|
| 842 | }
|
---|
[e96047c] | 843 |
|
---|
[852664b9] | 844 | /* Display available space */
|
---|
| 845 | if ((linfo.flags & lf_can_create_pri) != 0) {
|
---|
| 846 | rc = fdisk_part_get_max_avail(dev, spc_pri, &mcap);
|
---|
| 847 | if (rc != EOK) {
|
---|
| 848 | rc = EIO;
|
---|
| 849 | goto error;
|
---|
| 850 | }
|
---|
| 851 |
|
---|
[6c4eedf] | 852 | cap_simplify(&mcap);
|
---|
[9854a8f] | 853 |
|
---|
[6c4eedf] | 854 | rc = cap_format(&mcap, &smcap);
|
---|
[852664b9] | 855 | if (rc != EOK) {
|
---|
| 856 | rc = ENOMEM;
|
---|
| 857 | goto error;
|
---|
| 858 | }
|
---|
| 859 |
|
---|
| 860 | if ((linfo.flags & lf_ext_supp) != 0)
|
---|
| 861 | printf("Maximum free primary block: %s\n", smcap);
|
---|
| 862 | else
|
---|
| 863 | printf("Maximum free block: %s\n", smcap);
|
---|
| 864 |
|
---|
| 865 | free(smcap);
|
---|
| 866 | smcap = NULL;
|
---|
| 867 |
|
---|
| 868 | rc = fdisk_part_get_tot_avail(dev, spc_pri, &mcap);
|
---|
| 869 | if (rc != EOK) {
|
---|
| 870 | rc = EIO;
|
---|
| 871 | goto error;
|
---|
| 872 | }
|
---|
| 873 |
|
---|
[6c4eedf] | 874 | cap_simplify(&mcap);
|
---|
[9854a8f] | 875 |
|
---|
[6c4eedf] | 876 | rc = cap_format(&mcap, &smcap);
|
---|
[852664b9] | 877 | if (rc != EOK) {
|
---|
| 878 | rc = ENOMEM;
|
---|
| 879 | goto error;
|
---|
| 880 | }
|
---|
| 881 |
|
---|
| 882 | if ((linfo.flags & lf_ext_supp) != 0)
|
---|
| 883 | printf("Total free primary space: %s\n", smcap);
|
---|
| 884 | else
|
---|
| 885 | printf("Total free space: %s\n", smcap);
|
---|
| 886 |
|
---|
| 887 | free(smcap);
|
---|
| 888 | smcap = NULL;
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | /* Display available space */
|
---|
| 892 | if ((linfo.flags & lf_can_create_log) != 0) {
|
---|
| 893 | rc = fdisk_part_get_max_avail(dev, spc_log, &mcap);
|
---|
| 894 | if (rc != EOK) {
|
---|
| 895 | rc = EIO;
|
---|
| 896 | goto error;
|
---|
| 897 | }
|
---|
| 898 |
|
---|
[6c4eedf] | 899 | cap_simplify(&mcap);
|
---|
[9854a8f] | 900 |
|
---|
[6c4eedf] | 901 | rc = cap_format(&mcap, &smcap);
|
---|
[852664b9] | 902 | if (rc != EOK) {
|
---|
| 903 | rc = ENOMEM;
|
---|
| 904 | goto error;
|
---|
| 905 | }
|
---|
| 906 |
|
---|
| 907 | printf("Maximum free logical block: %s\n", smcap);
|
---|
| 908 | free(smcap);
|
---|
| 909 | smcap = NULL;
|
---|
| 910 |
|
---|
| 911 | rc = fdisk_part_get_tot_avail(dev, spc_log, &mcap);
|
---|
| 912 | if (rc != EOK) {
|
---|
| 913 | rc = EIO;
|
---|
| 914 | goto error;
|
---|
| 915 | }
|
---|
| 916 |
|
---|
[6c4eedf] | 917 | cap_simplify(&mcap);
|
---|
[9854a8f] | 918 |
|
---|
[6c4eedf] | 919 | rc = cap_format(&mcap, &smcap);
|
---|
[852664b9] | 920 | if (rc != EOK) {
|
---|
| 921 | rc = ENOMEM;
|
---|
| 922 | goto error;
|
---|
| 923 | }
|
---|
| 924 |
|
---|
| 925 | printf("Total free logical space: %s\n", smcap);
|
---|
| 926 | free(smcap);
|
---|
| 927 | smcap = NULL;
|
---|
| 928 | }
|
---|
| 929 |
|
---|
[e96047c] | 930 | rc = nchoice_set_prompt(choice, "Select action");
|
---|
| 931 | if (rc != EOK) {
|
---|
| 932 | assert(rc == ENOMEM);
|
---|
| 933 | printf("Out of memory.\n");
|
---|
| 934 | goto error;
|
---|
| 935 | }
|
---|
| 936 |
|
---|
[0ecfc62] | 937 | if ((linfo.flags & lf_ext_supp) != 0) {
|
---|
| 938 | if ((linfo.flags & lf_can_create_pri) != 0) {
|
---|
| 939 | rc = nchoice_add(choice, "Create primary "
|
---|
| 940 | "partition",
|
---|
[68b5dd11] | 941 | (void *)devac_create_pri_part, 0);
|
---|
[0ecfc62] | 942 | if (rc != EOK) {
|
---|
| 943 | assert(rc == ENOMEM);
|
---|
| 944 | printf("Out of memory.\n");
|
---|
| 945 | goto error;
|
---|
[b7a4d06] | 946 | }
|
---|
[0ecfc62] | 947 | }
|
---|
[b7a4d06] | 948 |
|
---|
[0ecfc62] | 949 | if ((linfo.flags & lf_can_create_ext) != 0) {
|
---|
| 950 | rc = nchoice_add(choice, "Create extended "
|
---|
| 951 | "partition",
|
---|
[68b5dd11] | 952 | (void *)devac_create_ext_part, 0);
|
---|
[0ecfc62] | 953 | if (rc != EOK) {
|
---|
| 954 | assert(rc == ENOMEM);
|
---|
| 955 | printf("Out of memory.\n");
|
---|
| 956 | goto error;
|
---|
[b7a4d06] | 957 | }
|
---|
[0ecfc62] | 958 | }
|
---|
[b7a4d06] | 959 |
|
---|
[0ecfc62] | 960 | if ((linfo.flags & lf_can_create_log) != 0) {
|
---|
| 961 | rc = nchoice_add(choice, "Create logical "
|
---|
| 962 | "partition",
|
---|
[68b5dd11] | 963 | (void *)devac_create_log_part, 0);
|
---|
[0ecfc62] | 964 | if (rc != EOK) {
|
---|
| 965 | assert(rc == ENOMEM);
|
---|
| 966 | printf("Out of memory.\n");
|
---|
| 967 | goto error;
|
---|
[b7a4d06] | 968 | }
|
---|
[0ecfc62] | 969 | }
|
---|
| 970 | } else { /* (linfo.flags & lf_ext_supp) == 0 */
|
---|
| 971 | if ((linfo.flags & lf_can_create_pri) != 0) {
|
---|
| 972 | rc = nchoice_add(choice, "Create partition",
|
---|
[68b5dd11] | 973 | (void *)devac_create_pri_part, 0);
|
---|
[0ecfc62] | 974 | if (rc != EOK) {
|
---|
| 975 | assert(rc == ENOMEM);
|
---|
[b7a4d06] | 976 | printf("Out of memory.\n");
|
---|
| 977 | goto error;
|
---|
| 978 | }
|
---|
[8227d63] | 979 | }
|
---|
| 980 | }
|
---|
| 981 |
|
---|
[edebb4a1] | 982 | if ((linfo.flags & lf_can_delete_part) != 0) {
|
---|
[8227d63] | 983 | rc = nchoice_add(choice, "Delete partition",
|
---|
[68b5dd11] | 984 | (void *)devac_delete_part, 0);
|
---|
[8227d63] | 985 | if (rc != EOK) {
|
---|
| 986 | assert(rc == ENOMEM);
|
---|
| 987 | printf("Out of memory.\n");
|
---|
| 988 | goto error;
|
---|
| 989 | }
|
---|
| 990 | }
|
---|
| 991 |
|
---|
[ea0ff6b] | 992 | if ((dflags & fdf_can_create_label) != 0) {
|
---|
[e96047c] | 993 | rc = nchoice_add(choice, "Create label",
|
---|
[68b5dd11] | 994 | (void *)devac_create_label, 0);
|
---|
[e96047c] | 995 | if (rc != EOK) {
|
---|
| 996 | assert(rc == ENOMEM);
|
---|
| 997 | printf("Out of memory.\n");
|
---|
| 998 | goto error;
|
---|
| 999 | }
|
---|
[ea0ff6b] | 1000 | }
|
---|
| 1001 |
|
---|
| 1002 | if ((dflags & fdf_can_delete_label) != 0) {
|
---|
[e96047c] | 1003 | rc = nchoice_add(choice, "Delete label",
|
---|
[68b5dd11] | 1004 | (void *)devac_delete_label, 0);
|
---|
[e96047c] | 1005 | if (rc != EOK) {
|
---|
| 1006 | assert(rc == ENOMEM);
|
---|
| 1007 | printf("Out of memory.\n");
|
---|
| 1008 | goto error;
|
---|
| 1009 | }
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
[ea0ff6b] | 1012 | if ((dflags & fdf_can_erase_dev) != 0) {
|
---|
| 1013 | rc = nchoice_add(choice, "Erase disk",
|
---|
[68b5dd11] | 1014 | (void *)devac_erase_disk, 0);
|
---|
[ea0ff6b] | 1015 | if (rc != EOK) {
|
---|
| 1016 | assert(rc == ENOMEM);
|
---|
| 1017 | printf("Out of memory.\n");
|
---|
| 1018 | goto error;
|
---|
| 1019 | }
|
---|
| 1020 | }
|
---|
| 1021 |
|
---|
[68b5dd11] | 1022 | rc = nchoice_add(choice, "Exit", (void *)devac_exit, 0);
|
---|
[e96047c] | 1023 | if (rc != EOK) {
|
---|
| 1024 | assert(rc == ENOMEM);
|
---|
| 1025 | printf("Out of memory.\n");
|
---|
| 1026 | goto error;
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
| 1029 | rc = nchoice_get(choice, &sel);
|
---|
| 1030 | if (rc != EOK) {
|
---|
| 1031 | printf("Error getting user selection.\n");
|
---|
| 1032 | return rc;
|
---|
| 1033 | }
|
---|
| 1034 |
|
---|
| 1035 | switch ((devac_t)sel) {
|
---|
| 1036 | case devac_create_label:
|
---|
[22fb7ab] | 1037 | (void) fdsk_create_label(dev);
|
---|
[e96047c] | 1038 | break;
|
---|
| 1039 | case devac_delete_label:
|
---|
[22fb7ab] | 1040 | (void) fdsk_delete_label(dev);
|
---|
[e96047c] | 1041 | break;
|
---|
[ea0ff6b] | 1042 | case devac_erase_disk:
|
---|
| 1043 | (void) fdsk_erase_disk(dev);
|
---|
| 1044 | break;
|
---|
[b7a4d06] | 1045 | case devac_create_pri_part:
|
---|
| 1046 | (void) fdsk_create_part(dev, lpk_primary);
|
---|
| 1047 | break;
|
---|
| 1048 | case devac_create_ext_part:
|
---|
| 1049 | (void) fdsk_create_part(dev, lpk_extended);
|
---|
| 1050 | break;
|
---|
| 1051 | case devac_create_log_part:
|
---|
| 1052 | (void) fdsk_create_part(dev, lpk_logical);
|
---|
[e96047c] | 1053 | break;
|
---|
| 1054 | case devac_delete_part:
|
---|
[22fb7ab] | 1055 | (void) fdsk_delete_part(dev);
|
---|
[e96047c] | 1056 | break;
|
---|
| 1057 | case devac_exit:
|
---|
| 1058 | quit = true;
|
---|
| 1059 | break;
|
---|
| 1060 | }
|
---|
| 1061 |
|
---|
| 1062 | nchoice_destroy(choice);
|
---|
| 1063 | return EOK;
|
---|
| 1064 | error:
|
---|
[8227d63] | 1065 | free(sdcap);
|
---|
| 1066 | free(scap);
|
---|
[852664b9] | 1067 | free(smcap);
|
---|
[8227d63] | 1068 | free(sfstype);
|
---|
| 1069 | free(svcname);
|
---|
[e96047c] | 1070 | if (choice != NULL)
|
---|
| 1071 | nchoice_destroy(choice);
|
---|
| 1072 | return rc;
|
---|
| 1073 | }
|
---|
| 1074 |
|
---|
| 1075 | int main(int argc, char *argv[])
|
---|
| 1076 | {
|
---|
| 1077 | service_id_t svcid;
|
---|
| 1078 | fdisk_dev_t *dev;
|
---|
| 1079 | int rc;
|
---|
| 1080 |
|
---|
[22fb7ab] | 1081 | rc = fdisk_create(&fdisk);
|
---|
| 1082 | if (rc != EOK) {
|
---|
| 1083 | printf("Error initializing Fdisk.\n");
|
---|
| 1084 | return 1;
|
---|
| 1085 | }
|
---|
| 1086 |
|
---|
[e96047c] | 1087 | rc = fdsk_dev_sel_choice(&svcid);
|
---|
| 1088 | if (rc != EOK)
|
---|
| 1089 | return 1;
|
---|
| 1090 |
|
---|
[8227d63] | 1091 | if (svcid == 0)
|
---|
| 1092 | return 0;
|
---|
| 1093 |
|
---|
[22fb7ab] | 1094 | rc = fdisk_dev_open(fdisk, svcid, &dev);
|
---|
[e96047c] | 1095 | if (rc != EOK) {
|
---|
| 1096 | printf("Error opening device.\n");
|
---|
| 1097 | return 1;
|
---|
| 1098 | }
|
---|
| 1099 |
|
---|
| 1100 | while (!quit) {
|
---|
| 1101 | rc = fdsk_dev_menu(dev);
|
---|
| 1102 | if (rc != EOK) {
|
---|
| 1103 | fdisk_dev_close(dev);
|
---|
| 1104 | return 1;
|
---|
| 1105 | }
|
---|
| 1106 | }
|
---|
| 1107 |
|
---|
| 1108 | fdisk_dev_close(dev);
|
---|
[22fb7ab] | 1109 | fdisk_destroy(fdisk);
|
---|
[e96047c] | 1110 |
|
---|
| 1111 | return 0;
|
---|
| 1112 | }
|
---|
| 1113 |
|
---|
| 1114 |
|
---|
| 1115 | /** @}
|
---|
| 1116 | */
|
---|