summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/local_fs.c
blob: 137ff0a398a1871425d08066afe92c3101d2c05c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Mini $local_fs boot implementation for busybox.
 *
 * Copyright (C) 2004 by David Seikel won_fang@yahoo.com.au
 *
 * Originally based on code from Trinux.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 */

#include <string.h>
#include <unistd.h>

#include "busybox.h"
#include "lib_init_d.h"


static int start(struct init_d_handle_s *, int);
static int status(struct init_d_handle_s *, int);
static int stop(struct init_d_handle_s *, int);


static init_d_handle_t my_commands = {
	&start,
	&stop,
	NULL,
	NULL,
	&no_reload,
	NULL,
	&status,
	NULL,
	"local_fs",
	NULL,
	NULL,
	NULL,
	INIT_D_BEGIN
		INIT_D_PROV "$local_fs"
		INIT_D_DSTART "1 2 3 4 5"
		INIT_D_DSTOP "0 6"
		INIT_D_SDESC "Mount all local file systems."
		INIT_D_DESC "Mount all local file systems."
		INIT_D_CONT
		"Including any left over partitions not otherwise mounted." INIT_D_END
};


static const char *i386_sys_types[] = {
//  " 0Empty",
//  " 1fat",
//  " 4fat",
//  " 6fat",
//  " bvfat",
	" cvfat",
//  " evfat",
//  " fvfat",
	"80minix",
	"81minix",
	"82swap",
	"83ext2",
	"85Linux extended",
	0
};


int local_fs_main(int argc, char **argv)
{
	return do_init_from_main(argc, argv, &my_commands);
}


static int start(struct init_d_handle_s *init_d, int just_checking)
{
	int i;
	char *CMDLINE = 0;
	char *ROOT = 0;
	char *CDEV = 0;
	char *FDEV = 0;
	char *token;
	char *strtok_temp;

	sleep(5);			// delay it for testing purposes
// Should fsck all non root partitions first.
	doit(0, "/bin/mount -a");
	bb_xasprintf(&CMDLINE, "%s", doit(REDIR, "cat /proc/cmdline"));
	for (token = strtok_r(CMDLINE, " \t\n\r", &strtok_temp); token != NULL;
		 token = strtok_r(NULL, " \t\n\r", &strtok_temp)) {
		if (strncmp(token, "ROOT=", 5) == 0)
			ROOT = &token[5];
	}

	bb_xasprintf(&CDEV, "%s",
				 doit(REDIR,
					  "dmesg | grep D-ROM | grep hd | cut -d: -f1 | sort | uniq"));

	bb_xasprintf(&FDEV, "%s %s",
				 doit(REDIR,
					  "dmesg | grep -v LDM | grep \"^ [sh]d[a-f]\" | cut -d':' -f2 | sort | uniq"),
				 (CDEV != 0) ? CDEV : "");
	token = strtok_r(FDEV, " \r\n", &strtok_temp);
	for (i = 0; token != NULL; i++) {
		if (strlen(token) > 2)
			make_disk(token);
		token = strtok_r(NULL, " \r\n", &strtok_temp);
	}

	bb_xasprintf(&FDEV, "%s",
				 doit(REDIR,
					  "fdisk -l | grep \"^/dev/\" | cut -b6-10,52-55"));
	token = strtok_r(FDEV, "\r\n", &strtok_temp);
	for (i = 0; token != NULL; i++) {
		int j;
		int found = 0;
		char *type = &token[6];

		token[4] = '\0';
		for (j = 0; i386_sys_types[j] != 0; j++) {
			if (strncmp(i386_sys_types[j], type, 2) == 0) {
				char *DEV = 0;
				char *MOUNT = 0;
				char *TYPE = 0;

				bb_xasprintf(&DEV, "/dev/%s", token);
				bb_xasprintf(&MOUNT, "/media/%s", token);
				bb_xasprintf(&TYPE, "%s", &i386_sys_types[j][2]);
				found = 1;
				if (strncmp(&i386_sys_types[j][2], "swap", 4) == 0) {
					doit(QUIET, "mkswap %s", DEV);
					doit(QUIET, "swapon %s", DEV);
				} else {
// Should not mount it if it is already mounted.
					if ((ROOT == 0) || strcmp(ROOT, DEV) != 0) {	/* Don't try to remount ROOT, */
						make_disk(token);
						quick_mount(TYPE, DEV, MOUNT, "-o ro");
//          quick_mount("auto", DEV, MOUNT, "-o ro");
					}
				}

				break;
			}
		}
//  if (found == 0)
//      bb_printf(" %s - unknown\n", token);

		token = strtok_r(NULL, "\r\n", &strtok_temp);
	}

	free(FDEV);
	free(CDEV);
	free(ROOT);
	free(CMDLINE);

	return INIT_D_OK;
}


static int status(struct init_d_handle_s *init_d, int quiet)
{
	return print_status(init_d, quiet, INIT_D_STATUS_OK);
}


static int stop(struct init_d_handle_s *init_d, int just_checking)
{
// Should only umount things we mounted in the first place.
// Same for swap
	doit(0, "/sbin/swapoff -a");
	doit(0, "/bin/umount -a -r");
	return INIT_D_OK;
}