blob: 6b72d0e0c9122ff529e2ea9287080151cd849715 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger38a33f92005-05-09 22:13:22 +00002/*
3 * util.c --- helper functions used by tune2fs and mke2fs
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
Mike Frysinger38a33f92005-05-09 22:13:22 +00005 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 by Theodore Ts'o.
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Public
9 * License.
10 * %End-Header%
11 */
12
13#include <stdio.h>
14#include <string.h>
15#include <errno.h>
16#include <linux/major.h>
17#include <sys/stat.h>
18
19#include "e2fsbb.h"
20#include "e2p/e2p.h"
21#include "ext2fs/ext2_fs.h"
22#include "ext2fs/ext2fs.h"
23#include "blkid/blkid.h"
24#include "util.h"
25
26void proceed_question(void)
27{
28 fputs("Proceed anyway? (y,n) ", stdout);
29 if (bb_ask_confirmation() == 0)
30 exit(1);
31}
32
Mike Frysinger7f782da2005-10-02 08:10:31 +000033void check_plausibility(const char *device, int force)
Mike Frysinger38a33f92005-05-09 22:13:22 +000034{
35 int val;
36#ifdef CONFIG_LFS
37 struct stat64 s;
38 val = stat64(device, &s);
39#else
40 struct stat s;
41 val = stat(device, &s);
42#endif
Mike Frysinger7f782da2005-10-02 08:10:31 +000043 if (force)
44 return;
Mike Frysinger38a33f92005-05-09 22:13:22 +000045 if(val == -1)
46 bb_perror_msg_and_die("Could not stat %s", device);
47 if (!S_ISBLK(s.st_mode)) {
48 printf("%s is not a block special device.\n", device);
49 proceed_question();
50 return;
51 }
52
53#ifdef HAVE_LINUX_MAJOR_H
54#ifndef MAJOR
55#define MAJOR(dev) ((dev)>>8)
56#define MINOR(dev) ((dev) & 0xff)
57#endif
58#ifndef SCSI_BLK_MAJOR
59#ifdef SCSI_DISK0_MAJOR
60#ifdef SCSI_DISK8_MAJOR
61#define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
62 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
63 ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
64#else
65#define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
66 ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
67#endif /* defined(SCSI_DISK8_MAJOR) */
68#define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
69#else
70#define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
71#endif /* defined(SCSI_DISK0_MAJOR) */
72#endif /* defined(SCSI_BLK_MAJOR) */
73 if (((MAJOR(s.st_rdev) == HD_MAJOR &&
74 MINOR(s.st_rdev)%64 == 0) ||
75 (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) &&
76 MINOR(s.st_rdev)%16 == 0))) {
77 printf("%s is entire device, not just one partition!\n", device);
78 proceed_question();
79 }
80#endif
81}
82
83void check_mount(const char *device, int force, const char *type)
84{
85 errcode_t retval;
86 int mount_flags;
87
88 retval = ext2fs_check_if_mounted(device, &mount_flags);
89 if (retval) {
90 bb_error_msg("Could not determine if %s is mounted", device);
91 return;
92 }
Mike Frysinger874af852006-03-08 07:03:27 +000093 if (mount_flags & EXT2_MF_MOUNTED) {
94 bb_error_msg("%s is mounted !", device);
95force_check:
96 if (force)
97 bb_error_msg("badblocks forced anyways");
98 else
99 bb_error_msg_and_die("it's not safe to run badblocks!");
100 }
Mike Frysinger38a33f92005-05-09 22:13:22 +0000101
Mike Frysinger874af852006-03-08 07:03:27 +0000102 if (mount_flags & EXT2_MF_BUSY) {
103 bb_error_msg("%s is apparently in use by the system", device);
104 goto force_check;
105 }
106
Mike Frysinger38a33f92005-05-09 22:13:22 +0000107}
108
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000109void parse_journal_opts(char **journal_device, int *journal_flags,
110 int *journal_size, const char *opts)
Mike Frysinger38a33f92005-05-09 22:13:22 +0000111{
112 char *buf, *token, *next, *p, *arg;
113 int journal_usage = 0;
Mike Frysinger38a33f92005-05-09 22:13:22 +0000114 buf = bb_xstrdup(opts);
Mike Frysinger38a33f92005-05-09 22:13:22 +0000115 for (token = buf; token && *token; token = next) {
116 p = strchr(token, ',');
117 next = 0;
118 if (p) {
119 *p = 0;
120 next = p+1;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000121 }
Mike Frysinger38a33f92005-05-09 22:13:22 +0000122 arg = strchr(token, '=');
123 if (arg) {
124 *arg = 0;
125 arg++;
126 }
127 if (strcmp(token, "device") == 0) {
128 *journal_device = blkid_get_devname(NULL, arg, NULL);
129 if (!journal_device) {
130 journal_usage++;
131 continue;
132 }
133 } else if (strcmp(token, "size") == 0) {
134 if (!arg) {
135 journal_usage++;
136 continue;
137 }
138 (*journal_size) = strtoul(arg, &p, 0);
139 if (*p)
140 journal_usage++;
141 } else if (strcmp(token, "v1_superblock") == 0) {
142 (*journal_flags) |= EXT2_MKJOURNAL_V1_SUPER;
143 continue;
144 } else
145 journal_usage++;
146 }
147 if (journal_usage)
148 bb_error_msg_and_die(
149 "\nBad journal options specified.\n\n"
150 "Journal options are separated by commas, "
151 "and may take an argument which\n"
152 "\tis set off by an equals ('=') sign.\n\n"
153 "Valid journal options are:\n"
154 "\tsize=<journal size in megabytes>\n"
155 "\tdevice=<journal device>\n\n"
156 "The journal size must be between "
157 "1024 and 102400 filesystem blocks.\n\n");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000158}
Mike Frysinger38a33f92005-05-09 22:13:22 +0000159
160/*
161 * Determine the number of journal blocks to use, either via
162 * user-specified # of megabytes, or via some intelligently selected
163 * defaults.
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000164 *
Mike Frysinger38a33f92005-05-09 22:13:22 +0000165 * Find a reasonable journal file size (in blocks) given the number of blocks
166 * in the filesystem. For very small filesystems, it is not reasonable to
167 * have a journal that fills more than half of the filesystem.
168 */
169int figure_journal_size(int size, ext2_filsys fs)
170{
171 blk_t j_blocks;
172
173 if (fs->super->s_blocks_count < 2048) {
174 bb_error_msg("Filesystem too small for a journal");
175 return 0;
176 }
177
178 if (size >= 0) {
179 j_blocks = size * 1024 / (fs->blocksize / 1024);
180 if (j_blocks < 1024 || j_blocks > 102400)
181 bb_error_msg_and_die("\nThe requested journal "
182 "size is %d blocks;\n it must be "
183 "between 1024 and 102400 blocks; Aborting",
184 j_blocks);
185 if (j_blocks > fs->super->s_free_blocks_count)
186 bb_error_msg_and_die("Journal size too big for filesystem");
187 return j_blocks;
188 }
189
190 if (fs->super->s_blocks_count < 32768)
191 j_blocks = 1024;
Mike Frysinger874af852006-03-08 07:03:27 +0000192 else if (fs->super->s_blocks_count < 256*1024)
Mike Frysinger38a33f92005-05-09 22:13:22 +0000193 j_blocks = 4096;
Mike Frysinger874af852006-03-08 07:03:27 +0000194 else if (fs->super->s_blocks_count < 512*1024)
Mike Frysinger38a33f92005-05-09 22:13:22 +0000195 j_blocks = 8192;
Mike Frysinger874af852006-03-08 07:03:27 +0000196 else if (fs->super->s_blocks_count < 1024*1024)
197 j_blocks = 16384;
198 else
199 j_blocks = 32768;
Mike Frysinger38a33f92005-05-09 22:13:22 +0000200
201 return j_blocks;
202}
203
204void print_check_message(ext2_filsys fs)
205{
206 printf("This filesystem will be automatically "
207 "checked every %d mounts or\n"
208 "%g days, whichever comes first. "
209 "Use tune2fs -c or -i to override.\n",
210 fs->super->s_max_mnt_count,
211 (double)fs->super->s_checkinterval / (3600 * 24));
212}
Mike Frysinger7f782da2005-10-02 08:10:31 +0000213
214void make_journal_device(char *journal_device, ext2_filsys fs, int quiet, int force)
215{
216 errcode_t retval;
217 ext2_filsys jfs;
218 io_manager io_ptr;
219
220 check_plausibility(journal_device, force);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000221 check_mount(journal_device, force, "journal");
222 io_ptr = unix_io_manager;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000223 retval = ext2fs_open(journal_device, EXT2_FLAG_RW|
224 EXT2_FLAG_JOURNAL_DEV_OK, 0,
225 fs->blocksize, io_ptr, &jfs);
226 if (retval)
227 bb_error_msg_and_die("Could not journal device %s", journal_device);
228 if(!quiet)
229 printf("Adding journal to device %s: ", journal_device);
230 fflush(stdout);
231 retval = ext2fs_add_journal_device(fs, jfs);
232 if(retval)
233 bb_error_msg_and_die("\nFailed to add journal to device %s", journal_device);
234 if(!quiet)
235 puts("done");
236 ext2fs_close(jfs);
237}
238
239void make_journal_blocks(ext2_filsys fs, int journal_size, int journal_flags, int quiet)
240{
241 unsigned long journal_blocks;
242 errcode_t retval;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000243
Mike Frysinger7f782da2005-10-02 08:10:31 +0000244 journal_blocks = figure_journal_size(journal_size, fs);
245 if (!journal_blocks) {
246 fs->super->s_feature_compat &=
247 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
248 return;
249 }
250 if(!quiet)
251 printf("Creating journal (%ld blocks): ", journal_blocks);
252 fflush(stdout);
253 retval = ext2fs_add_journal_inode(fs, journal_blocks,
254 journal_flags);
255 if(retval)
256 bb_error_msg_and_die("Could not create journal");
257 if(!quiet)
258 puts("done");
259}
"Vladimir N. Oleynik"d20cfbd2005-10-12 16:22:19 +0000260
"Vladimir N. Oleynik"350865e2005-11-26 11:01:23 +0000261char *e2fs_set_sbin_path(void)
"Vladimir N. Oleynik"d20cfbd2005-10-12 16:22:19 +0000262{
263 char *oldpath = getenv("PATH");
264 /* Update our PATH to include /sbin */
265#define PATH_SET "/sbin"
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000266 if (oldpath)
"Vladimir N. Oleynik"350865e2005-11-26 11:01:23 +0000267 oldpath = bb_xasprintf("%s:%s", PATH_SET, oldpath);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000268 else
"Vladimir N. Oleynik"350865e2005-11-26 11:01:23 +0000269 oldpath = PATH_SET;
270 putenv (oldpath);
271 return oldpath;
"Vladimir N. Oleynik"d20cfbd2005-10-12 16:22:19 +0000272}