ubirmvol.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19. * An utility to remove UBI volumes.
  20. *
  21. * Authors: Artem Bityutskiy <dedekind@infradead.org>
  22. * Frank Haverkamp <haver@vnet.ibm.com>
  23. */
  24. #define PROGRAM_NAME "ubirmvol"
  25. #include <stdio.h>
  26. #include <stdint.h>
  27. #include <getopt.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <libubi.h>
  31. #include "common.h"
  32. /* The variables below are set by command line arguments */
  33. struct args {
  34. int vol_id;
  35. const char *node;
  36. const char *name;
  37. };
  38. static struct args args = {
  39. .vol_id = -1,
  40. };
  41. static const char doc[] = PROGRAM_NAME " version " VERSION
  42. " - a tool to remove UBI volumes.";
  43. static const char optionsstr[] =
  44. "-n, --vol_id=<volume id> volume ID to remove\n"
  45. "-N, --name=<volume name> volume name to remove\n"
  46. "-h, -?, --help print help message\n"
  47. "-V, --version print program version";
  48. static const char usage[] =
  49. "Usage: " PROGRAM_NAME " <UBI device node file name> [-n <volume id>] [--vol_id=<volume id>]\n\n"
  50. " [-N <volume name>] [--name=<volume name>] [-h] [--help]\n\n"
  51. "Example: " PROGRAM_NAME " /dev/ubi0 -n 1 - remove UBI volume 1 from UBI device corresponding\n"
  52. " to /dev/ubi0\n"
  53. " " PROGRAM_NAME " /dev/ubi0 -N my_vol - remove UBI named \"my_vol\" from UBI device\n"
  54. " corresponding to /dev/ubi0";
  55. static const struct option long_options[] = {
  56. { .name = "vol_id", .has_arg = 1, .flag = NULL, .val = 'n' },
  57. { .name = "name", .has_arg = 1, .flag = NULL, .val = 'N' },
  58. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  59. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  60. { NULL, 0, NULL, 0},
  61. };
  62. static int param_sanity_check(void)
  63. {
  64. if (args.vol_id == -1 && !args.name) {
  65. errmsg("please, specify either volume ID or volume name");
  66. return -1;
  67. }
  68. if (args.vol_id != -1 && args.name) {
  69. errmsg("please, specify either volume ID or volume name, not both");
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. static int parse_opt(int argc, char * const argv[])
  75. {
  76. while (1) {
  77. int key, error = 0;
  78. key = getopt_long(argc, argv, "n:N:h?V", long_options, NULL);
  79. if (key == -1)
  80. break;
  81. switch (key) {
  82. case 'n':
  83. args.vol_id = simple_strtoul(optarg, &error);
  84. if (error || args.vol_id < 0) {
  85. errmsg("bad volume ID: " "\"%s\"", optarg);
  86. return -1;
  87. }
  88. break;
  89. case 'N':
  90. args.name = optarg;
  91. break;
  92. case 'h':
  93. printf("%s\n\n", doc);
  94. printf("%s\n\n", usage);
  95. printf("%s\n", optionsstr);
  96. exit(EXIT_SUCCESS);
  97. case '?':
  98. printf("%s\n\n", doc);
  99. printf("%s\n\n", usage);
  100. printf("%s\n", optionsstr);
  101. return -1;
  102. case 'V':
  103. common_print_version();
  104. exit(EXIT_SUCCESS);
  105. case ':':
  106. errmsg("parameter is missing");
  107. return -1;
  108. default:
  109. fprintf(stderr, "Use -h for help\n");
  110. return -1;
  111. }
  112. }
  113. if (optind == argc) {
  114. errmsg("UBI device name was not specified (use -h for help)");
  115. return -1;
  116. } else if (optind != argc - 1) {
  117. errmsg("more then one UBI device specified (use -h for help)");
  118. return -1;
  119. }
  120. args.node = argv[optind];
  121. if (param_sanity_check())
  122. return -1;
  123. return 0;
  124. }
  125. int main(int argc, char * const argv[])
  126. {
  127. int err;
  128. libubi_t libubi;
  129. err = parse_opt(argc, argv);
  130. if (err)
  131. return -1;
  132. libubi = libubi_open();
  133. if (!libubi) {
  134. if (errno == 0)
  135. return errmsg("UBI is not present in the system");
  136. return sys_errmsg("cannot open libubi");
  137. }
  138. err = ubi_probe_node(libubi, args.node);
  139. if (err == 2) {
  140. errmsg("\"%s\" is an UBI volume node, not an UBI device node",
  141. args.node);
  142. goto out_libubi;
  143. } else if (err < 0) {
  144. if (errno == ENODEV)
  145. errmsg("\"%s\" is not an UBI device node", args.node);
  146. else
  147. sys_errmsg("error while probing \"%s\"", args.node);
  148. goto out_libubi;
  149. }
  150. if (args.name) {
  151. struct ubi_dev_info dev_info;
  152. struct ubi_vol_info vol_info;
  153. err = ubi_get_dev_info(libubi, args.node, &dev_info);
  154. if (err) {
  155. sys_errmsg("cannot get information about UBI device \"%s\"",
  156. args.node);
  157. goto out_libubi;
  158. }
  159. err = ubi_get_vol_info1_nm(libubi, dev_info.dev_num,
  160. args.name, &vol_info);
  161. if (err) {
  162. sys_errmsg("cannot find UBI volume \"%s\"", args.name);
  163. goto out_libubi;
  164. }
  165. args.vol_id = vol_info.vol_id;
  166. }
  167. err = ubi_rmvol(libubi, args.node, args.vol_id);
  168. if (err) {
  169. sys_errmsg("cannot UBI remove volume");
  170. goto out_libubi;
  171. }
  172. libubi_close(libubi);
  173. return 0;
  174. out_libubi:
  175. libubi_close(libubi);
  176. return -1;
  177. }