tune2fs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * tune2fs.c - Change the file system parameters on an ext2 file system
  4. *
  5. * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
  6. * Laboratoire MASI, Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
  10. *
  11. * %Begin-Header%
  12. * This file may be redistributed under the terms of the GNU Public
  13. * License.
  14. * %End-Header%
  15. */
  16. /*
  17. * History:
  18. * 93/06/01 - Creation
  19. * 93/10/31 - Added the -c option to change the maximal mount counts
  20. * 93/12/14 - Added -l flag to list contents of superblock
  21. * M.J.E. Mol (marcel@duteca.et.tudelft.nl)
  22. * F.W. ten Wolde (franky@duteca.et.tudelft.nl)
  23. * 93/12/29 - Added the -e option to change errors behavior
  24. * 94/02/27 - Ported to use the ext2fs library
  25. * 94/03/06 - Added the checks interval from Uwe Ohse (uwe@tirka.gun.de)
  26. */
  27. #include <sys/types.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. #include <getopt.h>
  35. #include "e2fsbb.h"
  36. #include "ext2fs/ext2_fs.h"
  37. #include "ext2fs/ext2fs.h"
  38. #include "uuid/uuid.h"
  39. #include "e2p/e2p.h"
  40. #include "ext2fs/kernel-jbd.h"
  41. #include "util.h"
  42. #include "blkid/blkid.h"
  43. #include "busybox.h"
  44. static char * device_name = NULL;
  45. static char * new_label, *new_last_mounted, *new_UUID;
  46. static char * io_options;
  47. static int c_flag, C_flag, e_flag, f_flag, g_flag, i_flag, l_flag, L_flag;
  48. static int m_flag, M_flag, r_flag, s_flag = -1, u_flag, U_flag, T_flag;
  49. static time_t last_check_time;
  50. static int print_label;
  51. static int max_mount_count, mount_count, mount_flags;
  52. static unsigned long interval, reserved_blocks;
  53. static unsigned reserved_ratio;
  54. static unsigned long resgid, resuid;
  55. static unsigned short errors;
  56. static int open_flag;
  57. static char *features_cmd;
  58. static char *mntopts_cmd;
  59. static int journal_size, journal_flags;
  60. static char *journal_device = NULL;
  61. static const char *please_fsck = "Please run e2fsck on the filesystem\n";
  62. static __u32 ok_features[3] = {
  63. EXT3_FEATURE_COMPAT_HAS_JOURNAL | EXT2_FEATURE_COMPAT_DIR_INDEX,
  64. EXT2_FEATURE_INCOMPAT_FILETYPE,
  65. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER
  66. };
  67. /*
  68. * Remove an external journal from the filesystem
  69. */
  70. static void remove_journal_device(ext2_filsys fs)
  71. {
  72. char *journal_path;
  73. ext2_filsys jfs;
  74. char buf[1024];
  75. journal_superblock_t *jsb;
  76. int i, nr_users;
  77. errcode_t retval;
  78. int commit_remove_journal = 0;
  79. io_manager io_ptr;
  80. if (f_flag)
  81. commit_remove_journal = 1; /* force removal even if error */
  82. uuid_unparse(fs->super->s_journal_uuid, buf);
  83. journal_path = blkid_get_devname(NULL, "UUID", buf);
  84. if (!journal_path) {
  85. journal_path =
  86. ext2fs_find_block_device(fs->super->s_journal_dev);
  87. if (!journal_path)
  88. return;
  89. }
  90. io_ptr = unix_io_manager;
  91. retval = ext2fs_open(journal_path, EXT2_FLAG_RW|
  92. EXT2_FLAG_JOURNAL_DEV_OK, 0,
  93. fs->blocksize, io_ptr, &jfs);
  94. if (retval) {
  95. bb_error_msg("Failed to open external journal");
  96. goto no_valid_journal;
  97. }
  98. if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  99. bb_error_msg("%s is not a journal device", journal_path);
  100. goto no_valid_journal;
  101. }
  102. /* Get the journal superblock */
  103. if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) {
  104. bb_error_msg("Failed to read journal superblock");
  105. goto no_valid_journal;
  106. }
  107. jsb = (journal_superblock_t *) buf;
  108. if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
  109. (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) {
  110. bb_error_msg("Journal superblock not found!");
  111. goto no_valid_journal;
  112. }
  113. /* Find the filesystem UUID */
  114. nr_users = ntohl(jsb->s_nr_users);
  115. for (i=0; i < nr_users; i++) {
  116. if (memcmp(fs->super->s_uuid,
  117. &jsb->s_users[i*16], 16) == 0)
  118. break;
  119. }
  120. if (i >= nr_users) {
  121. bb_error_msg("Filesystem's UUID not found on journal device");
  122. commit_remove_journal = 1;
  123. goto no_valid_journal;
  124. }
  125. nr_users--;
  126. for (i=0; i < nr_users; i++)
  127. memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16);
  128. jsb->s_nr_users = htonl(nr_users);
  129. /* Write back the journal superblock */
  130. if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) {
  131. bb_error_msg("Failed to write journal superblock");
  132. goto no_valid_journal;
  133. }
  134. commit_remove_journal = 1;
  135. no_valid_journal:
  136. if (commit_remove_journal == 0)
  137. bb_error_msg_and_die("Journal NOT removed");
  138. fs->super->s_journal_dev = 0;
  139. uuid_clear(fs->super->s_journal_uuid);
  140. ext2fs_mark_super_dirty(fs);
  141. puts("Journal removed");
  142. free(journal_path);
  143. }
  144. /* Helper function for remove_journal_inode */
  145. static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
  146. int blockcnt EXT2FS_ATTR((unused)),
  147. void *private EXT2FS_ATTR((unused)))
  148. {
  149. blk_t block;
  150. int group;
  151. block = *blocknr;
  152. ext2fs_unmark_block_bitmap(fs->block_map,block);
  153. group = ext2fs_group_of_blk(fs, block);
  154. fs->group_desc[group].bg_free_blocks_count++;
  155. fs->super->s_free_blocks_count++;
  156. return 0;
  157. }
  158. /*
  159. * Remove the journal inode from the filesystem
  160. */
  161. static void remove_journal_inode(ext2_filsys fs)
  162. {
  163. struct ext2_inode inode;
  164. errcode_t retval;
  165. ino_t ino = fs->super->s_journal_inum;
  166. char *msg = "to read";
  167. char *s = "journal inode";
  168. retval = ext2fs_read_inode(fs, ino, &inode);
  169. if (retval)
  170. goto REMOVE_JOURNAL_INODE_ERROR;
  171. if (ino == EXT2_JOURNAL_INO) {
  172. retval = ext2fs_read_bitmaps(fs);
  173. if (retval) {
  174. msg = "to read bitmaps";
  175. s = "";
  176. goto REMOVE_JOURNAL_INODE_ERROR;
  177. }
  178. retval = ext2fs_block_iterate(fs, ino, 0, NULL,
  179. release_blocks_proc, NULL);
  180. if (retval) {
  181. msg = "clearing";
  182. goto REMOVE_JOURNAL_INODE_ERROR;
  183. }
  184. memset(&inode, 0, sizeof(inode));
  185. ext2fs_mark_bb_dirty(fs);
  186. fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
  187. } else
  188. inode.i_flags &= ~EXT2_IMMUTABLE_FL;
  189. retval = ext2fs_write_inode(fs, ino, &inode);
  190. if (retval) {
  191. msg = "writing";
  192. REMOVE_JOURNAL_INODE_ERROR:
  193. bb_error_msg_and_die("Failed %s %s", msg, s);
  194. }
  195. fs->super->s_journal_inum = 0;
  196. ext2fs_mark_super_dirty(fs);
  197. }
  198. /*
  199. * Update the default mount options
  200. */
  201. static void update_mntopts(ext2_filsys fs, char *mntopts)
  202. {
  203. struct ext2_super_block *sb= fs->super;
  204. if (e2p_edit_mntopts(mntopts, &sb->s_default_mount_opts, ~0))
  205. bb_error_msg_and_die("Invalid mount option set: %s", mntopts);
  206. ext2fs_mark_super_dirty(fs);
  207. }
  208. /*
  209. * Update the feature set as provided by the user.
  210. */
  211. static void update_feature_set(ext2_filsys fs, char *features)
  212. {
  213. int sparse, old_sparse, filetype, old_filetype;
  214. int journal, old_journal, dxdir, old_dxdir;
  215. struct ext2_super_block *sb= fs->super;
  216. __u32 old_compat, old_incompat, old_ro_compat;
  217. old_compat = sb->s_feature_compat;
  218. old_ro_compat = sb->s_feature_ro_compat;
  219. old_incompat = sb->s_feature_incompat;
  220. old_sparse = sb->s_feature_ro_compat &
  221. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  222. old_filetype = sb->s_feature_incompat &
  223. EXT2_FEATURE_INCOMPAT_FILETYPE;
  224. old_journal = sb->s_feature_compat &
  225. EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  226. old_dxdir = sb->s_feature_compat &
  227. EXT2_FEATURE_COMPAT_DIR_INDEX;
  228. if (e2p_edit_feature(features, &sb->s_feature_compat, ok_features))
  229. bb_error_msg_and_die("Invalid filesystem option set: %s", features);
  230. sparse = sb->s_feature_ro_compat &
  231. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  232. filetype = sb->s_feature_incompat &
  233. EXT2_FEATURE_INCOMPAT_FILETYPE;
  234. journal = sb->s_feature_compat &
  235. EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  236. dxdir = sb->s_feature_compat &
  237. EXT2_FEATURE_COMPAT_DIR_INDEX;
  238. if (old_journal && !journal) {
  239. if ((mount_flags & EXT2_MF_MOUNTED) &&
  240. !(mount_flags & EXT2_MF_READONLY)) {
  241. bb_error_msg_and_die(
  242. "The has_journal flag may only be "
  243. "cleared when the filesystem is\n"
  244. "unmounted or mounted "
  245. "read-only");
  246. }
  247. if (sb->s_feature_incompat &
  248. EXT3_FEATURE_INCOMPAT_RECOVER) {
  249. bb_error_msg_and_die(
  250. "The needs_recovery flag is set. "
  251. "%s before clearing the has_journal flag.",
  252. please_fsck);
  253. }
  254. if (sb->s_journal_inum) {
  255. remove_journal_inode(fs);
  256. }
  257. if (sb->s_journal_dev) {
  258. remove_journal_device(fs);
  259. }
  260. }
  261. if (journal && !old_journal) {
  262. /*
  263. * If adding a journal flag, let the create journal
  264. * code below handle creating setting the flag and
  265. * creating the journal. We supply a default size if
  266. * necessary.
  267. */
  268. if (!journal_size)
  269. journal_size = -1;
  270. sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  271. }
  272. if (dxdir && !old_dxdir) {
  273. if (!sb->s_def_hash_version)
  274. sb->s_def_hash_version = EXT2_HASH_TEA;
  275. if (uuid_is_null((unsigned char *) sb->s_hash_seed))
  276. uuid_generate((unsigned char *) sb->s_hash_seed);
  277. }
  278. if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
  279. (sb->s_feature_compat || sb->s_feature_ro_compat ||
  280. sb->s_feature_incompat))
  281. ext2fs_update_dynamic_rev(fs);
  282. if ((sparse != old_sparse) ||
  283. (filetype != old_filetype)) {
  284. sb->s_state &= ~EXT2_VALID_FS;
  285. printf("\n%s\n", please_fsck);
  286. }
  287. if ((old_compat != sb->s_feature_compat) ||
  288. (old_ro_compat != sb->s_feature_ro_compat) ||
  289. (old_incompat != sb->s_feature_incompat))
  290. ext2fs_mark_super_dirty(fs);
  291. }
  292. /*
  293. * Add a journal to the filesystem.
  294. */
  295. static void add_journal(ext2_filsys fs)
  296. {
  297. if (fs->super->s_feature_compat &
  298. EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
  299. bb_error_msg_and_die("The filesystem already has a journal");
  300. }
  301. if (journal_device) {
  302. make_journal_device(journal_device, fs, 0, 0);
  303. } else if (journal_size) {
  304. make_journal_blocks(fs, journal_size, journal_flags, 0);
  305. /*
  306. * If the filesystem wasn't mounted, we need to force
  307. * the block group descriptors out.
  308. */
  309. if ((mount_flags & EXT2_MF_MOUNTED) == 0)
  310. fs->flags &= ~EXT2_FLAG_SUPER_ONLY;
  311. }
  312. print_check_message(fs);
  313. return;
  314. }
  315. /*
  316. * Busybox stuff
  317. */
  318. static char * x_blkid_get_devname(const char *token)
  319. {
  320. char * dev_name;
  321. if (!(dev_name = blkid_get_devname(NULL, token, NULL)))
  322. bb_error_msg_and_die("Unable to resolve '%s'", token);
  323. return dev_name;
  324. }
  325. #ifdef CONFIG_E2LABEL
  326. static void parse_e2label_options(int argc, char ** argv)
  327. {
  328. if ((argc < 2) || (argc > 3))
  329. bb_show_usage();
  330. io_options = strchr(argv[1], '?');
  331. if (io_options)
  332. *io_options++ = 0;
  333. device_name = x_blkid_get_devname(argv[1]);
  334. if (argc == 3) {
  335. open_flag = EXT2_FLAG_RW | EXT2_FLAG_JOURNAL_DEV_OK;
  336. L_flag = 1;
  337. new_label = argv[2];
  338. } else
  339. print_label++;
  340. }
  341. #else
  342. #define parse_e2label_options(x,y)
  343. #endif
  344. static time_t parse_time(char *str)
  345. {
  346. struct tm ts;
  347. if (strcmp(str, "now") == 0) {
  348. return time(0);
  349. }
  350. memset(&ts, 0, sizeof(ts));
  351. #ifdef HAVE_STRPTIME
  352. strptime(str, "%Y%m%d%H%M%S", &ts);
  353. #else
  354. sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
  355. &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
  356. ts.tm_year -= 1900;
  357. ts.tm_mon -= 1;
  358. if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
  359. ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
  360. ts.tm_min > 59 || ts.tm_sec > 61)
  361. ts.tm_mday = 0;
  362. #endif
  363. if (ts.tm_mday == 0) {
  364. bb_error_msg_and_die("Cannot parse date/time specifier: %s", str);
  365. }
  366. return mktime(&ts);
  367. }
  368. static void parse_tune2fs_options(int argc, char **argv)
  369. {
  370. int c;
  371. char * tmp;
  372. printf("tune2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
  373. while ((c = getopt(argc, argv, "c:e:fg:i:jlm:o:r:s:u:C:J:L:M:O:T:U:")) != EOF)
  374. switch (c)
  375. {
  376. case 'c':
  377. max_mount_count = xatou_range(optarg, 0, 16000);
  378. if (max_mount_count == 0)
  379. max_mount_count = -1;
  380. c_flag = 1;
  381. open_flag = EXT2_FLAG_RW;
  382. break;
  383. case 'C':
  384. mount_count = xatou_range(optarg, 0, 16000);
  385. C_flag = 1;
  386. open_flag = EXT2_FLAG_RW;
  387. break;
  388. case 'e':
  389. if (strcmp (optarg, "continue") == 0)
  390. errors = EXT2_ERRORS_CONTINUE;
  391. else if (strcmp (optarg, "remount-ro") == 0)
  392. errors = EXT2_ERRORS_RO;
  393. else if (strcmp (optarg, "panic") == 0)
  394. errors = EXT2_ERRORS_PANIC;
  395. else {
  396. bb_error_msg_and_die("bad error behavior - %s", optarg);
  397. }
  398. e_flag = 1;
  399. open_flag = EXT2_FLAG_RW;
  400. break;
  401. case 'f': /* Force */
  402. f_flag = 1;
  403. break;
  404. case 'g':
  405. resgid = bb_strtoul(optarg, NULL, 10);
  406. if (errno)
  407. resgid = xgroup2gid(optarg);
  408. g_flag = 1;
  409. open_flag = EXT2_FLAG_RW;
  410. break;
  411. case 'i':
  412. interval = strtoul(optarg, &tmp, 0);
  413. switch (*tmp) {
  414. case 's':
  415. tmp++;
  416. break;
  417. case '\0':
  418. case 'd':
  419. case 'D': /* days */
  420. interval *= 86400;
  421. if (*tmp != '\0')
  422. tmp++;
  423. break;
  424. case 'm':
  425. case 'M': /* months! */
  426. interval *= 86400 * 30;
  427. tmp++;
  428. break;
  429. case 'w':
  430. case 'W': /* weeks */
  431. interval *= 86400 * 7;
  432. tmp++;
  433. break;
  434. }
  435. if (*tmp || interval > (365 * 86400)) {
  436. bb_error_msg_and_die("bad interval - %s", optarg);
  437. }
  438. i_flag = 1;
  439. open_flag = EXT2_FLAG_RW;
  440. break;
  441. case 'j':
  442. if (!journal_size)
  443. journal_size = -1;
  444. open_flag = EXT2_FLAG_RW;
  445. break;
  446. case 'J':
  447. parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
  448. open_flag = EXT2_FLAG_RW;
  449. break;
  450. case 'l':
  451. l_flag = 1;
  452. break;
  453. case 'L':
  454. new_label = optarg;
  455. L_flag = 1;
  456. open_flag = EXT2_FLAG_RW |
  457. EXT2_FLAG_JOURNAL_DEV_OK;
  458. break;
  459. case 'm':
  460. reserved_ratio = xatou_range(optarg, 0, 50);
  461. m_flag = 1;
  462. open_flag = EXT2_FLAG_RW;
  463. break;
  464. case 'M':
  465. new_last_mounted = optarg;
  466. M_flag = 1;
  467. open_flag = EXT2_FLAG_RW;
  468. break;
  469. case 'o':
  470. if (mntopts_cmd) {
  471. bb_error_msg_and_die("-o may only be specified once");
  472. }
  473. mntopts_cmd = optarg;
  474. open_flag = EXT2_FLAG_RW;
  475. break;
  476. case 'O':
  477. if (features_cmd) {
  478. bb_error_msg_and_die("-O may only be specified once");
  479. }
  480. features_cmd = optarg;
  481. open_flag = EXT2_FLAG_RW;
  482. break;
  483. case 'r':
  484. reserved_blocks = xatoul(optarg);
  485. r_flag = 1;
  486. open_flag = EXT2_FLAG_RW;
  487. break;
  488. case 's':
  489. s_flag = atoi(optarg);
  490. open_flag = EXT2_FLAG_RW;
  491. break;
  492. case 'T':
  493. T_flag = 1;
  494. last_check_time = parse_time(optarg);
  495. open_flag = EXT2_FLAG_RW;
  496. break;
  497. case 'u':
  498. resuid = bb_strtoul(optarg, NULL, 10);
  499. if (errno)
  500. resuid = xuname2uid(optarg);
  501. u_flag = 1;
  502. open_flag = EXT2_FLAG_RW;
  503. break;
  504. case 'U':
  505. new_UUID = optarg;
  506. U_flag = 1;
  507. open_flag = EXT2_FLAG_RW |
  508. EXT2_FLAG_JOURNAL_DEV_OK;
  509. break;
  510. default:
  511. bb_show_usage();
  512. }
  513. if (optind < argc - 1 || optind == argc)
  514. bb_show_usage();
  515. if (!open_flag && !l_flag)
  516. bb_show_usage();
  517. io_options = strchr(argv[optind], '?');
  518. if (io_options)
  519. *io_options++ = 0;
  520. device_name = x_blkid_get_devname(argv[optind]);
  521. }
  522. #ifdef CONFIG_FINDFS
  523. static ATTRIBUTE_NORETURN void do_findfs(int argc, char **argv)
  524. {
  525. if ((argc != 2) ||
  526. (strncmp(argv[1], "LABEL=", 6) && strncmp(argv[1], "UUID=", 5)))
  527. bb_show_usage();
  528. device_name = x_blkid_get_devname(argv[1]);
  529. puts(device_name);
  530. exit(0);
  531. }
  532. #else
  533. #define do_findfs(x, y)
  534. #endif
  535. static void tune2fs_clean_up(void)
  536. {
  537. if (ENABLE_FEATURE_CLEAN_UP && device_name) free(device_name);
  538. if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
  539. }
  540. int tune2fs_main(int argc, char **argv)
  541. {
  542. errcode_t retval;
  543. ext2_filsys fs;
  544. struct ext2_super_block *sb;
  545. io_manager io_ptr;
  546. if (ENABLE_FEATURE_CLEAN_UP)
  547. atexit(tune2fs_clean_up);
  548. if (ENABLE_FINDFS && (applet_name[0] == 'f')) /* findfs */
  549. do_findfs(argc, argv); /* no return */
  550. else if (ENABLE_E2LABEL && (applet_name[0] == 'e')) /* e2label */
  551. parse_e2label_options(argc, argv);
  552. else
  553. parse_tune2fs_options(argc, argv); /* tune2fs */
  554. io_ptr = unix_io_manager;
  555. retval = ext2fs_open2(device_name, io_options, open_flag,
  556. 0, 0, io_ptr, &fs);
  557. if (retval)
  558. bb_error_msg_and_die("No valid superblock on %s", device_name);
  559. sb = fs->super;
  560. if (print_label) {
  561. /* For e2label emulation */
  562. printf("%.*s\n", (int) sizeof(sb->s_volume_name),
  563. sb->s_volume_name);
  564. return 0;
  565. }
  566. retval = ext2fs_check_if_mounted(device_name, &mount_flags);
  567. if (retval)
  568. bb_error_msg_and_die("cannot determine if %s is mounted", device_name);
  569. /* Normally we only need to write out the superblock */
  570. fs->flags |= EXT2_FLAG_SUPER_ONLY;
  571. if (c_flag) {
  572. sb->s_max_mnt_count = max_mount_count;
  573. ext2fs_mark_super_dirty(fs);
  574. printf("Setting maximal mount count to %d\n", max_mount_count);
  575. }
  576. if (C_flag) {
  577. sb->s_mnt_count = mount_count;
  578. ext2fs_mark_super_dirty(fs);
  579. printf("Setting current mount count to %d\n", mount_count);
  580. }
  581. if (e_flag) {
  582. sb->s_errors = errors;
  583. ext2fs_mark_super_dirty(fs);
  584. printf("Setting error behavior to %d\n", errors);
  585. }
  586. if (g_flag) {
  587. sb->s_def_resgid = resgid;
  588. ext2fs_mark_super_dirty(fs);
  589. printf("Setting reserved blocks gid to %lu\n", resgid);
  590. }
  591. if (i_flag) {
  592. sb->s_checkinterval = interval;
  593. ext2fs_mark_super_dirty(fs);
  594. printf("Setting interval between check %lu seconds\n", interval);
  595. }
  596. if (m_flag) {
  597. sb->s_r_blocks_count = (sb->s_blocks_count / 100)
  598. * reserved_ratio;
  599. ext2fs_mark_super_dirty(fs);
  600. printf("Setting reserved blocks percentage to %u (%u blocks)\n",
  601. reserved_ratio, sb->s_r_blocks_count);
  602. }
  603. if (r_flag) {
  604. if (reserved_blocks >= sb->s_blocks_count/2)
  605. bb_error_msg_and_die("reserved blocks count is too big (%lu)", reserved_blocks);
  606. sb->s_r_blocks_count = reserved_blocks;
  607. ext2fs_mark_super_dirty(fs);
  608. printf("Setting reserved blocks count to %lu\n", reserved_blocks);
  609. }
  610. if (s_flag == 1) {
  611. if (sb->s_feature_ro_compat &
  612. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)
  613. bb_error_msg("\nThe filesystem already has sparse superblocks");
  614. else {
  615. sb->s_feature_ro_compat |=
  616. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  617. sb->s_state &= ~EXT2_VALID_FS;
  618. ext2fs_mark_super_dirty(fs);
  619. printf("\nSparse superblock flag set. %s", please_fsck);
  620. }
  621. }
  622. if (s_flag == 0) {
  623. if (!(sb->s_feature_ro_compat &
  624. EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER))
  625. bb_error_msg("\nThe filesystem already has sparse superblocks disabled");
  626. else {
  627. sb->s_feature_ro_compat &=
  628. ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
  629. sb->s_state &= ~EXT2_VALID_FS;
  630. fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
  631. ext2fs_mark_super_dirty(fs);
  632. printf("\nSparse superblock flag cleared. %s", please_fsck);
  633. }
  634. }
  635. if (T_flag) {
  636. sb->s_lastcheck = last_check_time;
  637. ext2fs_mark_super_dirty(fs);
  638. printf("Setting time filesystem last checked to %s\n",
  639. ctime(&last_check_time));
  640. }
  641. if (u_flag) {
  642. sb->s_def_resuid = resuid;
  643. ext2fs_mark_super_dirty(fs);
  644. printf("Setting reserved blocks uid to %lu\n", resuid);
  645. }
  646. if (L_flag) {
  647. if (strlen(new_label) > sizeof(sb->s_volume_name))
  648. bb_error_msg("Warning: label too long, truncating");
  649. memset(sb->s_volume_name, 0, sizeof(sb->s_volume_name));
  650. safe_strncpy(sb->s_volume_name, new_label,
  651. sizeof(sb->s_volume_name));
  652. ext2fs_mark_super_dirty(fs);
  653. }
  654. if (M_flag) {
  655. memset(sb->s_last_mounted, 0, sizeof(sb->s_last_mounted));
  656. safe_strncpy(sb->s_last_mounted, new_last_mounted,
  657. sizeof(sb->s_last_mounted));
  658. ext2fs_mark_super_dirty(fs);
  659. }
  660. if (mntopts_cmd)
  661. update_mntopts(fs, mntopts_cmd);
  662. if (features_cmd)
  663. update_feature_set(fs, features_cmd);
  664. if (journal_size || journal_device)
  665. add_journal(fs);
  666. if (U_flag) {
  667. if ((strcasecmp(new_UUID, "null") == 0) ||
  668. (strcasecmp(new_UUID, "clear") == 0)) {
  669. uuid_clear(sb->s_uuid);
  670. } else if (strcasecmp(new_UUID, "time") == 0) {
  671. uuid_generate_time(sb->s_uuid);
  672. } else if (strcasecmp(new_UUID, "random") == 0) {
  673. uuid_generate(sb->s_uuid);
  674. } else if (uuid_parse(new_UUID, sb->s_uuid)) {
  675. bb_error_msg_and_die("Invalid UUID format");
  676. }
  677. ext2fs_mark_super_dirty(fs);
  678. }
  679. if (l_flag)
  680. list_super (sb);
  681. return (ext2fs_close (fs) ? 1 : 0);
  682. }