summaryrefslogtreecommitdiffstats
path: root/loader2/nfsinstall.c
blob: 1598e362f44b69358d503a1fd8da4c00ae6d12b5 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * nfsinstall.c - code to set up nfs installs
 *
 * Erik Troan <ewt@redhat.com>
 * Matt Wilson <msw@redhat.com>
 * Michael Fulbright <msf@redhat.com>
 * Jeremy Katz <katzj@redhat.com>
 *
 * Copyright 1997 - 2002 Red Hat, Inc.
 *
 * This software may be freely redistributed under the terms of the GNU
 * General Public License.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <fcntl.h>
#include <newt.h>
#include <popt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "loader.h"
#include "lang.h"
#include "loadermisc.h"
#include "kickstart.h"
#include "log.h"
#include "method.h"
#include "nfsinstall.h"
#include "net.h"

#include "../isys/imount.h"

int nfsGetSetup(char ** hostptr, char ** dirptr) {
    struct newtWinEntry entries[3];
    char * buf;
    char * newServer = *hostptr ? strdup(*hostptr) : NULL;
    char * newDir = *dirptr ? strdup(*dirptr) : NULL;
    int rc;

    entries[0].text = _("NFS server name:");
    entries[0].value = &newServer;
    entries[0].flags = NEWT_FLAG_SCROLL;
    entries[1].text = _("Red Hat directory:");
    entries[1].value = &newDir;
    entries[1].flags = NEWT_FLAG_SCROLL;
    entries[2].text = NULL;
    entries[2].value = NULL;
    buf = sdupprintf(_(netServerPrompt), "NFS", getProductName());
    rc = newtWinEntries(_("NFS Setup"), buf, 60, 5, 15,
                        24, entries, _("OK"), _("Back"), NULL);
    free(buf);

    if (rc == 2) {
        if (newServer) free(newServer);
        if (newDir) free(newDir);
        return LOADER_BACK;
    }

    if (*hostptr) free(*hostptr);
    if (*dirptr) free(*dirptr);
    *hostptr = newServer;
    *dirptr = newDir;

    return 0;
}



char * mountNfsImage(struct installMethod * method,
                     char * location, struct knownDevices * kd,
                     struct loaderData_s * loaderData,
                     moduleInfoSet modInfo, moduleList modLoaded,
                     moduleDeps * modDepsPtr, int flags) {
    static struct networkDeviceConfig netDev;
    char * devName = NULL;
    char * host = NULL;
    char * directory = NULL;
    char * fullPath = NULL;
    char * path;
    char * url = NULL;

    enum { NFS_STAGE_IFACE, NFS_STAGE_IP, NFS_STAGE_NFS, 
           NFS_STAGE_MOUNT, NFS_STAGE_DONE } stage = NFS_STAGE_IFACE;

    int rc;
    int dir = 1;

    initLoopback();

    memset(&netDev, 0, sizeof(netDev));
    netDev.isDynamic = 1;

    /* populate netDev based on any kickstart data */
    setupNetworkDeviceConfig(&netDev, loaderData, flags);

    /* JKFIXME: ASSERT -- we have a network device when we get here */
    while (stage != NFS_STAGE_DONE) {
        switch (stage) {
        case NFS_STAGE_IFACE:
            logMessage("going to pick interface");
            rc = chooseNetworkInterface(kd, loaderData, flags);
            if ((rc == LOADER_BACK) || (rc == LOADER_ERROR) ||
                ((dir == -1) && (rc == LOADER_NOOP))) return NULL;

            stage = NFS_STAGE_IP;
            dir = 1;
            logMessage("using interface %s", loaderData->netDev);
            devName = loaderData->netDev;
            break;
            
        case NFS_STAGE_IP:
            logMessage("going to do getNetConfig");
            rc = readNetConfig(devName, &netDev, flags);
            if (rc) {
                stage = NFS_STAGE_IFACE;
                dir = -1;
                break;
            }
            stage = NFS_STAGE_NFS;
            break;
            
        case NFS_STAGE_NFS:
            logMessage("going to do nfsGetSetup");
            if (loaderData->method &&
                !strncmp(loaderData->method, "nfs", 3) &&
                loaderData->methodData) {
                host = ((struct nfsInstallData *)loaderData->methodData)->host;
                directory = ((struct nfsInstallData *)loaderData->methodData)->directory;

                logMessage("host is %s, dir is %s", host, directory);

                if (!host || !directory) {
                    logMessage("missing host or directory specification");
                    free(loaderData->method);
                    loaderData->method = NULL;
                    break;
                }
            } else if (nfsGetSetup(&host, &directory) == LOADER_BACK) {
                stage = NFS_STAGE_IP;
                dir = -1;
                break;
            }
             
            stage = NFS_STAGE_MOUNT;
            dir = 1;
            break;

        case NFS_STAGE_MOUNT: {
            int foundinvalid = 0;
            char * buf;

            fullPath = alloca(strlen(host) + strlen(directory) + 2);
            sprintf(fullPath, "%s:%s", host, directory);

            logMessage("mounting nfs path %s", fullPath);

            if (FL_TESTING(flags)) {
                stage = NFS_STAGE_DONE;
                dir = 1;
                break;
            }

            stage = NFS_STAGE_NFS;

            if (!doPwMount(fullPath, "/mnt/source", "nfs", 1, 0, NULL, NULL)) {
                logMessage("mounted %s on /mnt/source", fullPath);
                if (!access("/mnt/source/RedHat/base/stage2.img", R_OK)) {
                    logMessage("can access stage2.img");
                    rc = mountStage2("/mnt/source/RedHat/base/stage2.img");
                    logMessage("after mountStage2, rc is %d", rc);
                    if (rc) {
                        umount("/mnt/source");
                        if (rc == -1) { 
                            foundinvalid = 1; 
                            logMessage("not the right one"); 
                        }
                    } else {
                        stage = NFS_STAGE_DONE;
                        url = "nfs://mnt/source/.";
                        break;
                    }
                }
                if ((path = validIsoImages("/mnt/source"))) {
		    logMessage("Path to valid iso is %s", path);
                    copyUpdatesImg("/mnt/source/updates.img");

                    if (mountLoopback(path, "/mnt/source2", "loop1")) 
                        logMessage("failed to mount iso %s loopback", path);
                    else {
                        rc = mountStage2("/mnt/source2/RedHat/base/stage2.img");
                        if (rc) {
                            umountLoopback("/mnt/source2", "loop1");
                            if (rc == -1)
				foundinvalid = 1;
                        } else {
                            queryIsoMediaCheck(path, flags);

                            stage = NFS_STAGE_DONE;
                            url = "nfsiso:/mnt/source";
                            break;
                        }
                    }
                }

                if (foundinvalid) 
                    buf = sdupprintf(_("The %s installation tree in that "
                                       "directory does not seem to match "
                                       "your boot media."), getProductName());
                else
                    buf = sdupprintf(_("That directory does not seem to "
                                       "contain a %s installation tree."),
                                     getProductName());
                newtWinMessage(_("Error"), _("OK"), buf);
                break;
            } else {
                newtWinMessage(_("Error"), _("OK"),
                               _("That directory could not be mounted from "
                                 "the server."));
                if (loaderData->method) {
                    free(loaderData->method);
                    loaderData->method = NULL;
                }
                break;
            }
        }

        case NFS_STAGE_DONE:
            break;
        }
    }

    writeNetInfo("/tmp/netinfo", &netDev, kd);
    free(host);
    free(directory);

    return url;
}


void setKickstartNfs(struct loaderData_s * loaderData, int argc,
                     char ** argv, int * flagsPtr) {
    char * host, * dir;
    poptContext optCon;
    int rc;
    struct poptOption ksNfsOptions[] = {
        { "server", '\0', POPT_ARG_STRING, &host, 0 },
        { "dir", '\0', POPT_ARG_STRING, &dir, 0 },
        { 0, 0, 0, 0, 0 }
    };

    logMessage("kickstartFromNfs");
    optCon = poptGetContext(NULL, argc, (const char **) argv, ksNfsOptions, 0);
    if ((rc = poptGetNextOpt(optCon)) < -1) {
        newtWinMessage(_("Kickstart Error"), _("OK"),
                       _("Bad argument to NFS kickstart method "
                         "command %s: %s"),
                       poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
                       poptStrerror(rc));
        return;
    }

    loaderData->method = strdup("nfs");
    loaderData->methodData = calloc(sizeof(struct nfsInstallData *), 1);
    if (host)
        ((struct nfsInstallData *)loaderData->methodData)->host = host;
    if (dir)
        ((struct nfsInstallData *)loaderData->methodData)->directory = dir;

    logMessage("results of nfs, host is %s, dir is %s", host, dir);
}


int kickstartFromNfs(char * url, struct knownDevices * kd,
                     struct loaderData_s * loaderData, int flags) {
    char * host = NULL, *path = NULL, * file = NULL;
    int failed = 0;
    struct networkDeviceConfig netCfg;

    logMessage("going to get ks from nfs");

    if (kickstartNetworkUp(kd, loaderData, &netCfg, flags)) {
        logMessage("unable to bring up network");
        return 1;
    }

    /* if they just did 'linux ks', they want us to figure it out from
     * the dhcp/bootp information
     */
    if (url == NULL) {
        if (!(netCfg.dev.set & PUMP_INTFINFO_HAS_NEXTSERVER)) {
            logMessage("no bootserver was found");
            return 1;
        }
         
        if (!(netCfg.dev.set & PUMP_INTFINFO_HAS_BOOTFILE)) {
            url = sdupprintf("%s:%s", inet_ntoa(netCfg.dev.nextServer),
                             "/kickstart/");
            logMessage("bootp: no bootfile received");
        } else {
            url = sdupprintf("%s:%s", inet_ntoa(netCfg.dev.nextServer),
                             netCfg.dev.bootFile);
        }
    } 
      
    logMessage("url is %s", url);

    getHostandPath(url, &host, &path, inet_ntoa(netCfg.dev.ip));

    /* nfs has to be a little bit different... split off the last part as
     * the file and then concatenate host + dir path */
    file = strrchr(path, '/');
    if (!file) {
        file = path;
    } else {
        *file++ ='\0';
        host = sdupprintf("%s/%s", host, path);
    }

    logMessage("ks location: nfs://%s/%s", host, file);

    if (!doPwMount(host, "/tmp/ks", "nfs", 1, 0, NULL, NULL)) {
        char * buf;

        buf = alloca(strlen(file) + 10);
        sprintf(buf, "/tmp/ks/%s", file);
        if (copyFile(buf, "/tmp/ks.cfg")) {
            logMessage("failed to copy ks.cfg to /tmp/ks.cfg");
            failed = 1;
        }
        
    } else {
        logMessage("failed to mount nfs source");
        failed = 1;
    }

    umount("/tmp/ks");
    unlink("/tmp/ks");

    return failed;
}