source: MondoRescue/branches/stable/mindi-busybox/e2fsprogs/util.c@ 1247

Last change on this file since 1247 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

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