3
0

tune2fs.c 21 KB

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