5 * Jordan Hubbard. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * verbatim and that no modifications are made prior to this
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/disklabel.h>
35 #include <sys/errno.h>
36 #include <sys/ioctl.h>
37 #include <sys/fcntl.h>
38 #include <sys/param.h>
41 #include <sys/mount.h>
46 static Chunk *chunk_list[MAX_CHUNKS];
48 static int rootdev_is_od;
52 chunk_compare(Chunk *c1, Chunk *c2)
60 else if (!c1->private_data && !c2->private_data)
62 else if (c1->private_data && !c2->private_data)
64 else if (!c1->private_data && c2->private_data)
67 return strcmp(((PartInfo *)(c1->private_data))->mountpoint, ((PartInfo *)(c2->private_data))->mountpoint);
75 for (i = 0; i < nchunks; i++) {
76 for (j = 0; j < nchunks; j++) {
77 if (chunk_compare(chunk_list[j], chunk_list[j + 1]) > 0) {
78 Chunk *tmp = chunk_list[j];
80 chunk_list[j] = chunk_list[j + 1];
81 chunk_list[j + 1] = tmp;
88 check_rootdev(Chunk **list, int n)
94 for (i = 0; i < n; i++) {
96 if (c->type == part && (c->flags & CHUNK_IS_ROOT)
97 && strncmp(c->disk->name, "od", 2) == 0)
109 mount_point(Chunk *c1)
111 if (c1->type == part && c1->subtype == FS_SWAP)
113 else if (c1->type == part || c1->type == fat || c1->type == efi)
114 return ((PartInfo *)c1->private_data)->mountpoint;
121 if (c1->type == fat || c1->type == efi)
123 else if (c1->type == part) {
124 if (c1->subtype != FS_SWAP)
133 fstype_short(Chunk *c1)
135 if (c1->type == part) {
136 if (c1->subtype != FS_SWAP) {
137 if (rootdev_is_od == 0 && strncmp(c1->name, "od", 2) == 0)
145 else if (c1->type == fat) {
146 if (strncmp(c1->name, "od", 2) == 0)
151 else if (c1->type == efi)
160 if (c1->type == part && c1->subtype != FS_SWAP) {
161 if (rootdev_is_od == 0 && strncmp(c1->name, "od", 2) == 0)
163 else if (c1->flags & CHUNK_IS_ROOT)
172 configFstab(dialogMenuItem *self)
180 if (file_readable("/etc/fstab"))
181 return DITEM_SUCCESS;
183 msgConfirm("Attempting to rebuild your /etc/fstab file. Warning: If you had\n"
184 "any CD devices in use before running %s then they may NOT\n"
185 "be found by this run!", ProgName);
188 devs = deviceFind(NULL, DEVICE_TYPE_DISK);
190 msgConfirm("No disks found!");
191 return DITEM_FAILURE;
194 /* Record all the chunks */
196 for (i = 0; devs[i]; i++) {
197 if (!devs[i]->enabled)
199 disk = (Disk *)devs[i]->private;
201 msgFatal("No chunk list found for %s!", disk->name);
202 for (c1 = disk->chunks->part; c1; c1 = c1->next) {
204 if (c1->type == apple) {
206 if (c1->type == freebsd) {
208 for (c2 = c1->part; c2; c2 = c2->next) {
209 if (c2->type == part && (c2->subtype == FS_SWAP || c2->private_data))
210 chunk_list[nchunks++] = c2;
213 else if (((c1->type == fat || c1->type == efi || c1->type == part) &&
214 c1->private_data) || (c1->type == part && c1->subtype == FS_SWAP))
215 chunk_list[nchunks++] = c1;
218 chunk_list[nchunks] = 0;
221 fstab = fopen("/etc/fstab", "w");
223 msgConfirm("Unable to create a new /etc/fstab file! Manual intervention\n"
224 "will be required.");
225 return DITEM_FAILURE;
228 check_rootdev(chunk_list, nchunks);
230 /* Go for the burn */
231 msgDebug("Generating /etc/fstab file\n");
232 fprintf(fstab, "# Device\t\tMountpoint\tFStype\tOptions\t\tDump\tPass#\n");
233 for (i = 0; i < nchunks; i++)
234 fprintf(fstab, "/dev/%s\t\t%s\t\t%s\t%s\t\t%d\t%d\n", name_of(chunk_list[i]), mount_point(chunk_list[i]),
235 fstype(chunk_list[i]), fstype_short(chunk_list[i]), seq_num(chunk_list[i]), seq_num(chunk_list[i]));
240 msgDebug("Wrote out /etc/fstab file\n");
241 return DITEM_SUCCESS;
245 /* Do the work of sucking in a config file.
246 * config is the filename to read in.
247 * lines is a fixed (max) sized array of char*
248 * returns number of lines read. line contents
249 * are malloc'd and must be freed by the caller.
252 readConfig(char *config, char **lines, int max)
258 fp = fopen(config, "r");
263 /* Read in the entire file */
264 for (i = 0; i < max; i++) {
265 if (!fgets(line, sizeof line, fp))
267 lines[nlines++] = strdup(line);
271 msgDebug("readConfig: Read %d lines from %s.\n", nlines, config);
276 #define MAX_LINES 2000 /* Some big number we're not likely to ever reach - I'm being really lazy here, I know */
280 readConfigFile(char *config, int marked)
282 char *lines[MAX_LINES], *cp, *cp2;
285 nlines = readConfig(config, lines, MAX_LINES);
289 for (i = 0; i < nlines; i++) {
290 /* Skip the comments & non-variable settings */
291 if (lines[i][0] == '#' || !(cp = index(lines[i], '='))) {
297 if ((cp2 = index(cp, '"')) || (cp2 = index(cp, '\047'))) {
299 cp2 = index(cp, *cp2);
301 /* If valid quotes, use it */
304 /* If we have a legit value, set it */
306 variable_set2(lines[i], cp, marked);
312 /* Load the environment from rc.conf file(s) */
314 configEnvironmentRC_conf(void)
320 { "/etc/defaults/rc.conf", 0 },
321 { "/etc/rc.conf", 0 },
322 { "/etc/rc.conf.local", 0 },
327 for (i = 0; configs[i].fname; i++) {
328 if (file_readable(configs[i].fname))
329 readConfigFile(configs[i].fname, configs[i].marked);