ubirsvol.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 resize UBI volumes.
  20. *
  21. * Authors: Artem Bityutskiy <dedekind@infradead.org>
  22. * Frank Haverkamp <haver@vnet.ibm.com>
  23. */
  24. #define PROGRAM_NAME "ubirsvol"
  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. long long bytes;
  38. int lebs;
  39. };
  40. static struct args args = {
  41. .vol_id = -1,
  42. .bytes = -1,
  43. .lebs = -1,
  44. };
  45. static const char doc[] = PROGRAM_NAME " version " VERSION
  46. " - a tool to resize UBI volumes.";
  47. static const char optionsstr[] =
  48. "-n, --vol_id=<volume id> volume ID to resize\n"
  49. "-N, --name=<volume name> volume name to resize\n"
  50. "-s, --size=<bytes> volume size volume size in bytes, kilobytes (KiB)\n"
  51. " or megabytes (MiB)\n"
  52. "-S, --lebs=<LEBs count> alternative way to give volume size in logical\n"
  53. " eraseblocks\n"
  54. "-h, -?, --help print help message\n"
  55. "-V, --version print program version";
  56. static const char usage[] =
  57. "Usage: " PROGRAM_NAME " <UBI device node file name> [-n <volume id>] [--vol_id=<volume id>]\n\n"
  58. " [-N <volume name>] [--name=<volume name>] [-s <bytes>] [-S <LEBs>] [-h] [--help]\n\n"
  59. "Example: " PROGRAM_NAME " /dev/ubi0 -n 1 -s 1MiB resize UBI volume 1 to 1 MiB on\n"
  60. " UBI device corresponding to /dev/ubi0\n"
  61. " " PROGRAM_NAME " /dev/ubi0 -N my_vol -s 1MiB - resize UBI volume named \"my_vol\" to 1 MiB\n"
  62. " on UBI device corresponding to /dev/ubi0";
  63. static const struct option long_options[] = {
  64. { .name = "vol_id", .has_arg = 1, .flag = NULL, .val = 'n' },
  65. { .name = "name", .has_arg = 1, .flag = NULL, .val = 'N' },
  66. { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' },
  67. { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' },
  68. { .name = "size", .has_arg = 1, .flag = NULL, .val = 's' },
  69. { .name = "lebs", .has_arg = 1, .flag = NULL, .val = 'S' },
  70. { NULL, 0, NULL, 0},
  71. };
  72. static int param_sanity_check(void)
  73. {
  74. if (args.vol_id == -1 && !args.name) {
  75. errmsg("please, specify either volume ID or volume name");
  76. return -1;
  77. }
  78. if (args.vol_id != -1 && args.name) {
  79. errmsg("please, specify either volume ID or volume name, not both");
  80. return -1;
  81. }
  82. if (args.bytes == -1 && args.lebs == -1)
  83. return errmsg("volume size was not specified (use -h for help)");
  84. if (args.bytes != -1 && args.lebs != -1)
  85. return errmsg("size specified with more then one option");
  86. return 0;
  87. }
  88. static int parse_opt(int argc, char * const argv[])
  89. {
  90. while (1) {
  91. int key, error = 0;
  92. key = getopt_long(argc, argv, "s:S:n:N:h?V", long_options, NULL);
  93. if (key == -1)
  94. break;
  95. switch (key) {
  96. case 's':
  97. args.bytes = util_get_bytes(optarg);
  98. if (args.bytes <= 0)
  99. return errmsg("bad volume size: \"%s\"", optarg);
  100. break;
  101. case 'S':
  102. args.lebs = simple_strtoull(optarg, &error);
  103. if (error || args.lebs <= 0)
  104. return errmsg("bad LEB count: \"%s\"", optarg);
  105. break;
  106. case 'n':
  107. args.vol_id = simple_strtoul(optarg, &error);
  108. if (error || args.vol_id < 0) {
  109. errmsg("bad volume ID: " "\"%s\"", optarg);
  110. return -1;
  111. }
  112. break;
  113. case 'N':
  114. args.name = optarg;
  115. break;
  116. case 'h':
  117. printf("%s\n\n", doc);
  118. printf("%s\n\n", usage);
  119. printf("%s\n", optionsstr);
  120. exit(EXIT_SUCCESS);
  121. case '?':
  122. printf("%s\n\n", doc);
  123. printf("%s\n\n", usage);
  124. printf("%s\n", optionsstr);
  125. return -1;
  126. case 'V':
  127. common_print_version();
  128. exit(EXIT_SUCCESS);
  129. case ':':
  130. errmsg("parameter is missing");
  131. return -1;
  132. default:
  133. fprintf(stderr, "Use -h for help\n");
  134. return -1;
  135. }
  136. }
  137. if (optind == argc) {
  138. errmsg("UBI device name was not specified (use -h for help)");
  139. return -1;
  140. } else if (optind != argc - 1) {
  141. errmsg("more then one UBI device specified (use -h for help)");
  142. return -1;
  143. }
  144. args.node = argv[optind];
  145. if (param_sanity_check())
  146. return -1;
  147. return 0;
  148. }
  149. int main(int argc, char * const argv[])
  150. {
  151. int err;
  152. libubi_t libubi;
  153. struct ubi_dev_info dev_info;
  154. struct ubi_vol_info vol_info;
  155. err = parse_opt(argc, argv);
  156. if (err)
  157. return -1;
  158. libubi = libubi_open();
  159. if (!libubi)
  160. return sys_errmsg("cannot open libubi");
  161. err = ubi_probe_node(libubi, args.node);
  162. if (err == 2) {
  163. errmsg("\"%s\" is an UBI volume node, not an UBI device node",
  164. args.node);
  165. goto out_libubi;
  166. } else if (err < 0) {
  167. if (errno == ENODEV)
  168. errmsg("\"%s\" is not an UBI device node", args.node);
  169. else
  170. sys_errmsg("error while probing \"%s\"", args.node);
  171. }
  172. err = ubi_get_dev_info(libubi, args.node, &dev_info);
  173. if (err) {
  174. sys_errmsg("cannot get information about UBI device \"%s\"",
  175. args.node);
  176. goto out_libubi;
  177. }
  178. if (args.name) {
  179. err = ubi_get_vol_info1_nm(libubi, dev_info.dev_num,
  180. args.name, &vol_info);
  181. if (err) {
  182. sys_errmsg("cannot find UBI volume \"%s\"", args.name);
  183. goto out_libubi;
  184. }
  185. args.vol_id = vol_info.vol_id;
  186. } else {
  187. err = ubi_get_vol_info1(libubi, dev_info.dev_num,
  188. args.vol_id, &vol_info);
  189. if (err) {
  190. sys_errmsg("cannot find UBI volume ID %d", args.vol_id);
  191. goto out_libubi;
  192. }
  193. }
  194. if (args.lebs != -1)
  195. args.bytes = vol_info.leb_size * args.lebs;
  196. err = ubi_rsvol(libubi, args.node, args.vol_id, args.bytes);
  197. if (err) {
  198. sys_errmsg("cannot UBI resize volume");
  199. goto out_libubi;
  200. }
  201. libubi_close(libubi);
  202. return 0;
  203. out_libubi:
  204. libubi_close(libubi);
  205. return -1;
  206. }